From ba86a129b4ce74930ddff7f3fbb2e9e01077fd19 Mon Sep 17 00:00:00 2001 From: LTurret Date: Fri, 7 Mar 2025 12:35:25 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20Fix=20mapping=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/main.py | 2 +- src/api/mapping.py | 1 + src/api/rules.py | 7 +++---- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/main.py b/src/api/main.py index 2f48e04..f7f0ec1 100644 --- a/src/api/main.py +++ b/src/api/main.py @@ -32,6 +32,6 @@ def main(config: dict[str, Any]): if __name__ == "__main__": - with open(Directory.CONFIG.value, "r") as config: + with open(Directory.CONFIG.value, "rb") as config: config: dict[str, Any] = tomllib.load(config) main(config) diff --git a/src/api/mapping.py b/src/api/mapping.py index 43380a2..d867c7e 100644 --- a/src/api/mapping.py +++ b/src/api/mapping.py @@ -7,6 +7,7 @@ class Directory(Enum): CONFIG = Path("api_config.toml") CRS = Path("include/coreruleset/rules") RULE = Path("rules.conf") + API_FILE = Path("api_records.json") class RuleAppendType(Enum): diff --git a/src/api/rules.py b/src/api/rules.py index 5beff7d..7a8a7fc 100644 --- a/src/api/rules.py +++ b/src/api/rules.py @@ -54,7 +54,6 @@ def __init__(self, source: Path, destination: Path) -> None: self.source: Path = source self.destination: Path = destination self.rules = [] - self.api_file: Path = Path(__file__).parent / "api_records.json" def __rule_append(self, rule_path: Path, reason: Optional[str] = None) -> None: with open(rule_path, "r", encoding="utf-8") as fp: @@ -76,7 +75,7 @@ def __all(self, root: Path, file: Path) -> None: def __mapping(self, root: Path, file: Path) -> None: v_types: list = [] - with open(self.api_file, "r", encoding="utf-8") as report_fp: + with open(Directory.API_FILE.value, "r", encoding="utf-8") as report_fp: vulnerability_report = json.load(report_fp) for key in vulnerability_report.keys(): @@ -115,13 +114,13 @@ def rule_dump(self, overwrite: bool = False) -> None: run(["cp", self.destination, f"{self.destination}.old"]) with open(self.destination, "a", encoding="utf-8") as file: - file.writelines("\n" + {rule for rule in self.rules}) + file.writelines("\n" + rule for rule in self.rules) print("Rules updated successfully.") self.rules = [] def main(): - with open(Directory.CONFIG.value, "r") as config: + with open(Directory.CONFIG.value, "rb") as config: config: dict[str, Any] = tomllib.load(config) rule_manager: RuleUtil = RuleUtil(Directory.CRS.value, Directory.RULE.value) From 90a3b22b175e4b6471d276a9a7963af71470c781 Mon Sep 17 00:00:00 2001 From: LTurret Date: Fri, 7 Mar 2025 12:39:49 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20Dynamic=20rule=20update=20test?= =?UTF-8?q?=20integrated=20with=20Rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/rules.py | 2 +- tests/dynamic_rule_test.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/dynamic_rule_test.rs diff --git a/src/api/rules.py b/src/api/rules.py index 7a8a7fc..ed84974 100644 --- a/src/api/rules.py +++ b/src/api/rules.py @@ -85,7 +85,7 @@ def __mapping(self, root: Path, file: Path) -> None: rule_path: Path = PurePath(root, file) self.__rule_append(rule_path, file) - def extract_rule(self, option: RuleAppendType = RuleAppendType.MAPPING) -> None: + def extract_rule(self, option: RuleAppendType = RuleAppendType.ALL) -> None: """ 擷取 `.conf` 規則檔案,可選擇擷取所有規則或僅擷取符合 API 記錄的規則 diff --git a/tests/dynamic_rule_test.rs b/tests/dynamic_rule_test.rs new file mode 100644 index 0000000..333c947 --- /dev/null +++ b/tests/dynamic_rule_test.rs @@ -0,0 +1,22 @@ +use std::process::Command; + + +#[test] +pub fn api_fetch() { + let mut output = Command::new("xvfb-run") + .arg("-a") + .arg("--server-args=-screen 0 1920x1080x24") + .arg("python3") + .arg("src/api/main.py") + .output() + .expect("Failed to execute xvfb-run"); + + println!("api_fetch Output: {:?}", output); + + let output = Command::new("python3") + .arg("src/api/rules.py") + .output() + .expect("Failed to update rules"); + + println!("rule_update Output: {:?}", output); +} From 012b289c5b7f782e483583aa4b2d805727b271d1 Mon Sep 17 00:00:00 2001 From: LTurret Date: Fri, 7 Mar 2025 12:40:14 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=85=20Ignore=20backup=20rule=20config?= =?UTF-8?q?=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7bc5758..0800012 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ *.html *.png run.sh -rules.conf +rules.conf* Cargo.lock modsecurity cache \ No newline at end of file