rat-king-parser 4.2.0__tar.gz → 4.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. {rat_king_parser-4.2.0/src/rat_king_parser.egg-info → rat_king_parser-4.2.2}/PKG-INFO +4 -3
  2. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/README.md +2 -2
  3. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/pyproject.toml +9 -9
  4. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/_version.py +1 -1
  5. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/rat_config_parser.py +10 -8
  6. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/data_utils.py +6 -6
  7. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/decryptors/config_decryptor_random_hardcoded.py +3 -3
  8. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/decryptors/config_decryptor_rijndael.py +7 -4
  9. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/dotnetpe_payload.py +11 -4
  10. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/extern/maco/rkp_maco.py +12 -7
  11. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2/src/rat_king_parser.egg-info}/PKG-INFO +4 -3
  12. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser.egg-info/SOURCES.txt +2 -1
  13. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser.egg-info/requires.txt +1 -0
  14. rat_king_parser-4.2.2/tests/test_known_samples.py +115 -0
  15. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/LICENSE +0 -0
  16. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/setup.cfg +0 -0
  17. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/__init__.py +0 -0
  18. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/__init__.py +0 -0
  19. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/config_parser_exception.py +0 -0
  20. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/__init__.py +0 -0
  21. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/config_item.py +0 -0
  22. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/config_normalization.py +0 -0
  23. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/decryptors/__init__.py +0 -0
  24. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/decryptors/config_decryptor.py +0 -0
  25. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/decryptors/config_decryptor_aes_with_iv.py +0 -0
  26. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/decryptors/config_decryptor_aes_with_iv_pbkdf.py +0 -0
  27. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/decryptors/config_decryptor_decrypt_xor.py +0 -0
  28. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/decryptors/config_decryptor_ecb.py +0 -0
  29. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/decryptors/config_decryptor_plaintext.py +0 -0
  30. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/config_parser/utils/dotnet_constants.py +0 -0
  31. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/extern/__init__.py +0 -0
  32. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/extern/maco/__init__.py +0 -0
  33. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/rkp.py +0 -0
  34. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/yara_utils/__init__.py +0 -0
  35. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/yara_utils/recompile.py +0 -0
  36. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/yara_utils/rules.yar +0 -0
  37. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser/yara_utils/rules.yarc +0 -0
  38. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser.egg-info/dependency_links.txt +0 -0
  39. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser.egg-info/entry_points.txt +0 -0
  40. {rat_king_parser-4.2.0 → rat_king_parser-4.2.2}/src/rat_king_parser.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rat-king-parser
3
- Version: 4.2.0
3
+ Version: 4.2.2
4
4
  Summary: A robust, multiprocessing-capable, multi-family RAT config parser/config extractor for AsyncRAT, DcRAT, VenomRAT, QuasarRAT, XWorm, Xeno RAT, and cloned/derivative RAT families.
5
5
  Author-email: jeFF0Falltrades <8444166+jeFF0Falltrades@users.noreply.github.com>
6
6
  Maintainer-email: jeFF0Falltrades <8444166+jeFF0Falltrades@users.noreply.github.com>
@@ -31,6 +31,7 @@ Requires-Dist: pycryptodomex
31
31
  Requires-Dist: yara-python
32
32
  Provides-Extra: dev
33
33
  Requires-Dist: pre-commit; extra == "dev"
34
+ Requires-Dist: pytest; extra == "dev"
34
35
  Provides-Extra: maco
35
36
  Requires-Dist: maco; extra == "maco"
36
37
  Requires-Dist: validators; extra == "maco"
@@ -137,7 +138,7 @@ pip install "rat_king_parser[maco] @ git+https://github.com/jeFF0Falltrades/rat_
137
138
 
138
139
  ## Example Input/Output
139
140
 
140
- ## Not Preserving Obfuscated Keys
141
+ ### Not Preserving Obfuscated Keys
141
142
  ```bash
142
143
  $ rat-king-parser -n dangerzone/* | jq
143
144
  ```
@@ -601,7 +602,7 @@ $ rat-king-parser -n dangerzone/* | jq
601
602
  }
602
603
  ]
603
604
  ```
604
- ## Preserving Obfuscated Keys
605
+ ### Preserving Obfuscated Keys
605
606
  ```bash
606
607
  $ rat-king-parser -np dangerzone/* | jq
607
608
  ```
@@ -99,7 +99,7 @@ pip install "rat_king_parser[maco] @ git+https://github.com/jeFF0Falltrades/rat_
99
99
 
100
100
  ## Example Input/Output
101
101
 
102
- ## Not Preserving Obfuscated Keys
102
+ ### Not Preserving Obfuscated Keys
103
103
  ```bash
104
104
  $ rat-king-parser -n dangerzone/* | jq
105
105
  ```
@@ -563,7 +563,7 @@ $ rat-king-parser -n dangerzone/* | jq
563
563
  }
564
564
  ]
565
565
  ```
566
- ## Preserving Obfuscated Keys
566
+ ### Preserving Obfuscated Keys
567
567
  ```bash
568
568
  $ rat-king-parser -np dangerzone/* | jq
569
569
  ```
@@ -1,6 +1,6 @@
1
1
  [build-system]
2
2
  build-backend = "setuptools.build_meta"
3
- requires = [ "setuptools" ]
3
+ requires = ["setuptools"]
4
4
 
5
5
  [project]
6
6
  name = "rat-king-parser"
@@ -34,10 +34,10 @@ classifiers = [
34
34
  "Programming Language :: Python :: 3.12",
35
35
  "Programming Language :: Python :: 3.13",
36
36
  ]
37
- dynamic = [ "version" ]
38
- dependencies = [ "dnfile", "pycryptodomex", "yara-python" ]
39
- optional-dependencies.dev = [ "pre-commit" ]
40
- optional-dependencies.maco = [ "maco", "validators" ]
37
+ dynamic = ["version"]
38
+ dependencies = ["dnfile", "pycryptodomex", "yara-python"]
39
+ optional-dependencies.dev = ["pre-commit", "pytest"]
40
+ optional-dependencies.maco = ["maco", "validators"]
41
41
  urls."Bug Reports" = "https://github.com/jeFF0Falltrades/rat_king_parser/issues"
42
42
  urls."Homepage" = "https://github.com/jeFF0Falltrades/rat_king_parser"
43
43
  urls."Say Thanks!" = "https://www.buymeacoffee.com/jeff0falltrades"
@@ -47,15 +47,15 @@ scripts.rat-king-parser = "rat_king_parser:main"
47
47
  version = { attr = "rat_king_parser._version.__version__" }
48
48
 
49
49
  [tool.setuptools.packages.find]
50
- where = [ "src" ]
51
- include = [ "rat_king_parser*" ]
50
+ where = ["src"]
51
+ include = ["rat_king_parser*"]
52
52
  namespaces = false
53
53
 
54
54
  [tool.setuptools.package-data]
55
- "rat_king_parser.yara_utils" = [ "*.yar", "*.yarc" ]
55
+ "rat_king_parser.yara_utils" = ["*.yar", "*.yarc"]
56
56
 
57
57
  [tool.ruff]
58
- lint.extend-select = [ "I" ]
58
+ lint.extend-select = ["I"]
59
59
 
60
60
  [tool.pre-commit]
61
61
  hook-config = "file:.pre-commit-config.yaml"
@@ -23,4 +23,4 @@
23
23
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
24
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
25
  # SOFTWARE.
26
- __version__ = "4.2.0"
26
+ __version__ = "4.2.2"
@@ -92,11 +92,13 @@ class RATConfigParser:
92
92
  # Assigned in _decrypt_and_decode_config()
93
93
  self._decryptor: ConfigDecryptor = None
94
94
  self.report["config"] = self._get_config()
95
- self.report["key"] = (
96
- self._decryptor.key.hex()
97
- if self._decryptor is not None and self._decryptor.key is not None
98
- else "None"
99
- )
95
+ key_hex = "None"
96
+ if self._decryptor is not None and self._decryptor.key is not None:
97
+ if isinstance(self._decryptor.key, bytes):
98
+ key_hex = self._decryptor.key.hex()
99
+ else:
100
+ key_hex = str(self._decryptor.key)
101
+ self.report["key"] = key_hex
100
102
  self.report["salt"] = (
101
103
  self._decryptor.salt.hex()
102
104
  if self._decryptor is not None and self._decryptor.salt is not None
@@ -122,7 +124,7 @@ class RATConfigParser:
122
124
  config_fields_map[k] = field_name
123
125
  item_data[field_name] = v
124
126
  if len(item_data) > 0:
125
- if type(item) is config_item.EncryptedStringConfigItem:
127
+ if isinstance(item, config_item.EncryptedStringConfigItem):
126
128
  # Translate config value RVAs to string values
127
129
  for k in item_data:
128
130
  item_data[k] = self._dnpp.user_string_from_rva(item_data[k])
@@ -159,7 +161,7 @@ class RATConfigParser:
159
161
  if self._decryptor is None:
160
162
  raise ConfigParserException("All decryptors failed")
161
163
 
162
- elif type(item) is config_item.ByteArrayConfigItem:
164
+ elif isinstance(item, config_item.ByteArrayConfigItem):
163
165
  for k in item_data:
164
166
  arr_size, arr_rva = item_data[k]
165
167
  item_data[k] = self._dnpp.byte_array_from_size_and_rva(
@@ -168,7 +170,7 @@ class RATConfigParser:
168
170
 
169
171
  decoded_config.update(item_data)
170
172
  # UrlHost is a marker of a special case until this can be standardized
171
- if len(decoded_config) < min_config_len and "UrlHost" not in item_data:
173
+ if len(decoded_config) < min_config_len and "UrlHost" not in decoded_config:
172
174
  raise ConfigParserException(
173
175
  f"Minimum threshold of config items not met: {len(decoded_config)}/{min_config_len}"
174
176
  )
@@ -31,11 +31,11 @@ from ..config_parser_exception import ConfigParserException
31
31
 
32
32
 
33
33
  # Converts a bytes object to an int object using the specified byte order
34
- def bytes_to_int(bytes: bytes, order: str = "little") -> int:
34
+ def bytes_to_int(data: bytes, order: str = "little") -> int:
35
35
  try:
36
- return int.from_bytes(bytes, byteorder=order)
36
+ return int.from_bytes(data, byteorder=order)
37
37
  except Exception:
38
- raise ConfigParserException(f"Error parsing int from value: {bytes}")
38
+ raise ConfigParserException(f"Error parsing int from value: {data}")
39
39
 
40
40
 
41
41
  # Decodes a bytes object to a Unicode string, using UTF-16LE for byte values
@@ -57,8 +57,8 @@ def decode_bytes(byte_str: bytes | str) -> str:
57
57
 
58
58
 
59
59
  # Converts an int to a bytes object, with the specified length and order
60
- def int_to_bytes(int: int, length: int = 4, order: str = "little") -> bytes:
60
+ def int_to_bytes(value: int, length: int = 4, order: str = "little") -> bytes:
61
61
  try:
62
- return int.to_bytes(length, order)
62
+ return value.to_bytes(length, order)
63
63
  except Exception:
64
- raise ConfigParserException(f"Error parsing bytes from value: {int}")
64
+ raise ConfigParserException(f"Error parsing bytes from value: {value}")
@@ -79,9 +79,9 @@ class ConfigDecryptorRandomHardcoded(ConfigDecryptor):
79
79
  hardcoded_hosts = []
80
80
  for rva in hardcoded_host_rvas:
81
81
  try:
82
- harcoded_host = self._payload.user_string_from_rva(bytes_to_int(rva))
83
- if harcoded_host != ".":
84
- hardcoded_hosts.append(harcoded_host)
82
+ extracted_hosts = self._payload.user_string_from_rva(bytes_to_int(rva))
83
+ if extracted_hosts != ".":
84
+ hardcoded_hosts.append(extracted_hosts)
85
85
  except Exception as e:
86
86
  logger.error(f"Error translating hardcoded host at {hex(rva)}: {e}")
87
87
  continue
@@ -102,7 +102,6 @@ class ConfigDecryptorRijndael(ConfigDecryptor):
102
102
  logger.debug("Decrypting encrypted strings...")
103
103
  if not self.key:
104
104
  try:
105
- #
106
105
  raw_key_field = self._payload.field_name_from_rva(self._key_rva)
107
106
  if raw_key_field in encrypted_strings:
108
107
  key = encrypted_strings[raw_key_field]
@@ -110,11 +109,15 @@ class ConfigDecryptorRijndael(ConfigDecryptor):
110
109
  else:
111
110
  for key_pattern in self._KEY_PATTERNS:
112
111
  key_hit = search(key_pattern, self._payload.data)
112
+ if not key_hit:
113
+ continue
113
114
  key_rva = bytes_to_int(key_hit.groups()[0])
114
115
  raw_key_field = self._payload.field_name_from_rva(key_rva)
115
- key = encrypted_strings[raw_key_field]
116
- self.key = self._derive_key(key)
117
- break
116
+ if raw_key_field in encrypted_strings:
117
+ key = encrypted_strings[raw_key_field]
118
+ self.key = self._derive_key(key)
119
+ break
120
+
118
121
  except Exception as e:
119
122
  raise ConfigParserException(f"Failed to derive AES key: {e}")
120
123
 
@@ -234,6 +234,13 @@ class DotNetPEPayload:
234
234
  """
235
235
  config = {}
236
236
  try:
237
+ ca_map = {}
238
+ for ca in self.dotnetpe.net.mdtables.CustomAttribute.rows:
239
+ idx = ca.Parent.row_index
240
+ if idx not in ca_map:
241
+ ca_map[idx] = []
242
+ ca_map[idx].append(ca)
243
+
237
244
  for td in self.dotnetpe.net.mdtables.TypeDef.rows:
238
245
  if (
239
246
  td.TypeNamespace.value != typespacename
@@ -253,10 +260,10 @@ class DotNetPEPayload:
253
260
  "String_",
254
261
  ):
255
262
  continue
256
- for ca in self.dotnetpe.net.mdtables.CustomAttribute.rows:
257
- if (
258
- ca.Parent.row_index == pd_row_index + 1
259
- ): # CustomAttribute Parent index is 1-based
263
+ # CustomAttribute Parent index is 1-based
264
+ target_index = pd_row_index + 1
265
+ if target_index in ca_map:
266
+ for ca in ca_map[target_index]:
260
267
  # Extract the value from the CustomAttribute blob
261
268
  blob_data = ca.Value.value
262
269
  if blob_data and blob_data != b"\x01\x00\x00\x00":
@@ -102,13 +102,18 @@ class RKPMACO(extractor.Extractor):
102
102
  # Instead, these will show individually as "server_port"
103
103
  # entries in the MACO config
104
104
  case ConfigValueTypes.PORT:
105
- rkp_model.tcp.extend(
106
- [
107
- rkp_model.Connection(server_port=port)
108
- for port in v
109
- if port.isdigit()
110
- ]
111
- )
105
+ if isinstance(v, int):
106
+ # Single port was extracted rather than a list
107
+ rkp_model.tcp.append(rkp_model.Connection(server_port=v))
108
+ else:
109
+ # Assume a list of ports was extracted
110
+ rkp_model.tcp.extend(
111
+ [
112
+ rkp_model.Connection(server_port=int(port))
113
+ for port in v
114
+ if str(port).isdigit()
115
+ ]
116
+ )
112
117
  case ConfigValueTypes.VERSION:
113
118
  rkp_model.version = v
114
119
  continue
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rat-king-parser
3
- Version: 4.2.0
3
+ Version: 4.2.2
4
4
  Summary: A robust, multiprocessing-capable, multi-family RAT config parser/config extractor for AsyncRAT, DcRAT, VenomRAT, QuasarRAT, XWorm, Xeno RAT, and cloned/derivative RAT families.
5
5
  Author-email: jeFF0Falltrades <8444166+jeFF0Falltrades@users.noreply.github.com>
6
6
  Maintainer-email: jeFF0Falltrades <8444166+jeFF0Falltrades@users.noreply.github.com>
@@ -31,6 +31,7 @@ Requires-Dist: pycryptodomex
31
31
  Requires-Dist: yara-python
32
32
  Provides-Extra: dev
33
33
  Requires-Dist: pre-commit; extra == "dev"
34
+ Requires-Dist: pytest; extra == "dev"
34
35
  Provides-Extra: maco
35
36
  Requires-Dist: maco; extra == "maco"
36
37
  Requires-Dist: validators; extra == "maco"
@@ -137,7 +138,7 @@ pip install "rat_king_parser[maco] @ git+https://github.com/jeFF0Falltrades/rat_
137
138
 
138
139
  ## Example Input/Output
139
140
 
140
- ## Not Preserving Obfuscated Keys
141
+ ### Not Preserving Obfuscated Keys
141
142
  ```bash
142
143
  $ rat-king-parser -n dangerzone/* | jq
143
144
  ```
@@ -601,7 +602,7 @@ $ rat-king-parser -n dangerzone/* | jq
601
602
  }
602
603
  ]
603
604
  ```
604
- ## Preserving Obfuscated Keys
605
+ ### Preserving Obfuscated Keys
605
606
  ```bash
606
607
  $ rat-king-parser -np dangerzone/* | jq
607
608
  ```
@@ -34,4 +34,5 @@ src/rat_king_parser/extern/maco/rkp_maco.py
34
34
  src/rat_king_parser/yara_utils/__init__.py
35
35
  src/rat_king_parser/yara_utils/recompile.py
36
36
  src/rat_king_parser/yara_utils/rules.yar
37
- src/rat_king_parser/yara_utils/rules.yarc
37
+ src/rat_king_parser/yara_utils/rules.yarc
38
+ tests/test_known_samples.py
@@ -4,6 +4,7 @@ yara-python
4
4
 
5
5
  [dev]
6
6
  pre-commit
7
+ pytest
7
8
 
8
9
  [maco]
9
10
  maco
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env python3
2
+ #
3
+ # test_known_samples.py
4
+ #
5
+ # Author: jeFF0Falltrades
6
+ #
7
+ # Test runner for CI job against known samples (only available in main repo)
8
+ #
9
+ # MIT License
10
+ #
11
+ # Copyright (c) 2026 Jeff Archer
12
+ #
13
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ # of this software and associated documentation files (the "Software"), to deal
15
+ # in the Software without restriction, including without limitation the rights
16
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ # copies of the Software, and to permit persons to whom the Software is
18
+ # furnished to do so, subject to the following conditions:
19
+ #
20
+ # The above copyright notice and this permission notice shall be included in all
21
+ # copies or substantial portions of the Software.
22
+ #
23
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ # SOFTWARE.
30
+ import json
31
+ import subprocess
32
+ from pathlib import Path
33
+
34
+ import pytest
35
+
36
+ SAMPLES_DIR = Path("tests/samples")
37
+ EXPECTED_DIR = Path("tests/expected")
38
+
39
+ # Fields that are allowed to exist in parser output
40
+ ALLOWED_FIELDS = {
41
+ "sha256",
42
+ "yara_possible_family",
43
+ "key",
44
+ "salt",
45
+ "config",
46
+ "file_path",
47
+ }
48
+
49
+
50
+ def normalize_output(data: dict) -> dict:
51
+ """
52
+ Strip non-deterministic or environment-specific fields
53
+ """
54
+ return {
55
+ "sha256": data.get("sha256"),
56
+ "yara_possible_family": data.get("yara_possible_family"),
57
+ "key": data.get("key"),
58
+ "salt": data.get("salt"),
59
+ "config": data.get("config"),
60
+ }
61
+
62
+
63
+ @pytest.mark.skipif(
64
+ not SAMPLES_DIR.exists(),
65
+ reason="Malware samples not available (is this a fork PR?)",
66
+ )
67
+ @pytest.mark.parametrize(
68
+ "expected_file",
69
+ sorted(EXPECTED_DIR.glob("*.json")),
70
+ ids=lambda p: p.stem,
71
+ )
72
+ def test_parser_against_known_samples(expected_file):
73
+ with expected_file.open("r", encoding="utf-8") as f:
74
+ expected = json.load(f)
75
+
76
+ assert isinstance(expected, dict), "Expected output must be a JSON object"
77
+ assert "sha256" in expected, "Expected output missing sha256"
78
+
79
+ sha = expected["sha256"]
80
+
81
+ sample_path = next((p for p in SAMPLES_DIR.iterdir() if p.is_file() and p.name.startswith(sha)), None)
82
+
83
+ assert sample_path is not None, f"No sample file found for SHA {sha}"
84
+
85
+ proc = subprocess.run(
86
+ ["rat-king-parser", str(sample_path)],
87
+ capture_output=True,
88
+ text=True,
89
+ check=True,
90
+ )
91
+
92
+ try:
93
+ parsed = json.loads(proc.stdout)
94
+ except json.JSONDecodeError:
95
+ pytest.fail(
96
+ "Parser did not emit valid JSON.\n"
97
+ f"STDOUT:\n{proc.stdout}\n\nSTDERR:\n{proc.stderr}"
98
+ )
99
+
100
+ assert isinstance(parsed, list), "Parser output must be a JSON array"
101
+ assert len(parsed) > 0, "Parser output array is empty"
102
+
103
+ parsed = parsed[0]
104
+
105
+ assert isinstance(parsed, dict), "Parser result must be a JSON object"
106
+ assert parsed.get("sha256") == sha, "SHA256 mismatch in parser output"
107
+ assert isinstance(parsed.get("config"), dict), "config must be a dictionary"
108
+
109
+ unexpected_fields = set(parsed.keys()) - ALLOWED_FIELDS
110
+ assert not unexpected_fields, f"Unexpected output fields: {unexpected_fields}"
111
+
112
+ normalized_actual = normalize_output(parsed)
113
+ normalized_expected = normalize_output(expected)
114
+
115
+ assert normalized_actual == normalized_expected
File without changes