rat-king-parser 3.1.3__py3-none-any.whl
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.
- rat_king_parser/__init__.py +28 -0
- rat_king_parser/_version.py +26 -0
- rat_king_parser/config_parser/__init__.py +28 -0
- rat_king_parser/config_parser/config_parser_exception.py +31 -0
- rat_king_parser/config_parser/rat_config_parser.py +251 -0
- rat_king_parser/config_parser/utils/__init__.py +25 -0
- rat_king_parser/config_parser/utils/config_item.py +151 -0
- rat_king_parser/config_parser/utils/data_utils.py +64 -0
- rat_king_parser/config_parser/utils/decryptors/__init__.py +50 -0
- rat_king_parser/config_parser/utils/decryptors/config_decryptor.py +55 -0
- rat_king_parser/config_parser/utils/decryptors/config_decryptor_aes_cbc.py +319 -0
- rat_king_parser/config_parser/utils/decryptors/config_decryptor_aes_ecb.py +156 -0
- rat_king_parser/config_parser/utils/decryptors/config_decryptor_decrypt_xor.py +128 -0
- rat_king_parser/config_parser/utils/decryptors/config_decryptor_plaintext.py +128 -0
- rat_king_parser/config_parser/utils/decryptors/config_decryptor_random_hardcoded.py +108 -0
- rat_king_parser/config_parser/utils/dotnet_constants.py +86 -0
- rat_king_parser/config_parser/utils/dotnetpe_payload.py +207 -0
- rat_king_parser/extern/__init__.py +0 -0
- rat_king_parser/extern/maco/__init__.py +0 -0
- rat_king_parser/extern/maco/rkp_maco.py +216 -0
- rat_king_parser/rkp.py +122 -0
- rat_king_parser/yara_utils/__init__.py +28 -0
- rat_king_parser/yara_utils/recompile.py +52 -0
- rat_king_parser/yara_utils/rules.yar +123 -0
- rat_king_parser/yara_utils/rules.yarc +0 -0
- rat_king_parser-3.1.3.dist-info/LICENSE +7 -0
- rat_king_parser-3.1.3.dist-info/METADATA +504 -0
- rat_king_parser-3.1.3.dist-info/RECORD +31 -0
- rat_king_parser-3.1.3.dist-info/WHEEL +5 -0
- rat_king_parser-3.1.3.dist-info/entry_points.txt +2 -0
- rat_king_parser-3.1.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# __init__.py
|
|
4
|
+
#
|
|
5
|
+
# Author: jeFF0Falltrades
|
|
6
|
+
#
|
|
7
|
+
# Copyright (c) 2024 Jeff Archer
|
|
8
|
+
#
|
|
9
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
# in the Software without restriction, including without limitation the rights
|
|
12
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
# furnished to do so, subject to the following conditions:
|
|
15
|
+
#
|
|
16
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
# copies or substantial portions of the Software.
|
|
18
|
+
#
|
|
19
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
# SOFTWARE.
|
|
26
|
+
from .rkp import main
|
|
27
|
+
|
|
28
|
+
__all__ = ["main"]
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# _version.py
|
|
4
|
+
#
|
|
5
|
+
# Author: jeFF0Falltrades
|
|
6
|
+
#
|
|
7
|
+
# Copyright (c) 2024 Jeff Archer
|
|
8
|
+
#
|
|
9
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
# in the Software without restriction, including without limitation the rights
|
|
12
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
# furnished to do so, subject to the following conditions:
|
|
15
|
+
#
|
|
16
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
# copies or substantial portions of the Software.
|
|
18
|
+
#
|
|
19
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
# SOFTWARE.
|
|
26
|
+
__version__ = "3.1.3"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# __init__.py
|
|
4
|
+
#
|
|
5
|
+
# Author: jeFF0Falltrades
|
|
6
|
+
#
|
|
7
|
+
# Copyright (c) 2024 Jeff Archer
|
|
8
|
+
#
|
|
9
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
# in the Software without restriction, including without limitation the rights
|
|
12
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
# furnished to do so, subject to the following conditions:
|
|
15
|
+
#
|
|
16
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
# copies or substantial portions of the Software.
|
|
18
|
+
#
|
|
19
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
# SOFTWARE.
|
|
26
|
+
from .rat_config_parser import RATConfigParser
|
|
27
|
+
|
|
28
|
+
__all__ = [RATConfigParser]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# config_parser_exception.py
|
|
4
|
+
#
|
|
5
|
+
# Author: jeFF0Falltrades
|
|
6
|
+
#
|
|
7
|
+
# A simple custom Exception class for use with configuration parsing actions
|
|
8
|
+
#
|
|
9
|
+
# MIT License
|
|
10
|
+
#
|
|
11
|
+
# Copyright (c) 2024 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
|
+
class ConfigParserException(Exception):
|
|
31
|
+
pass
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# rat_config_parser.py
|
|
4
|
+
#
|
|
5
|
+
# Author: jeFF0Falltrades
|
|
6
|
+
#
|
|
7
|
+
# Provides the primary functionality for parsing configurations from RATs
|
|
8
|
+
#
|
|
9
|
+
# MIT License
|
|
10
|
+
#
|
|
11
|
+
# Copyright (c) 2024 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
|
+
from logging import getLogger
|
|
31
|
+
from os.path import isfile
|
|
32
|
+
from re import DOTALL, compile, search
|
|
33
|
+
from typing import Any, Tuple
|
|
34
|
+
|
|
35
|
+
from yara import Rules
|
|
36
|
+
|
|
37
|
+
from .config_parser_exception import ConfigParserException
|
|
38
|
+
from .utils import config_item
|
|
39
|
+
from .utils.decryptors import (
|
|
40
|
+
SUPPORTED_DECRYPTORS,
|
|
41
|
+
ConfigDecryptor,
|
|
42
|
+
IncompatibleDecryptorException,
|
|
43
|
+
)
|
|
44
|
+
from .utils.dotnetpe_payload import DotNetPEPayload
|
|
45
|
+
|
|
46
|
+
logger = getLogger(__name__)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class RATConfigParser:
|
|
50
|
+
# Min and max number of items in a potential config section
|
|
51
|
+
_MIN_CONFIG_LEN_FLOOR = 5
|
|
52
|
+
_MIN_CONFIG_LEN_CEILING = 9
|
|
53
|
+
|
|
54
|
+
# Pattern to find the VerifyHash() method
|
|
55
|
+
_PATTERN_VERIFY_HASH = compile(
|
|
56
|
+
rb"\x7e.{3}\x04(?:\x6f.{3}\x0a){2}\x74.{3}\x01", DOTALL
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
def __init__(self, file_path: str, yara_rule: Rules = None) -> None:
|
|
60
|
+
self.report = {
|
|
61
|
+
"file_path": file_path,
|
|
62
|
+
"sha256": "",
|
|
63
|
+
"yara_possible_family": "",
|
|
64
|
+
"key": "",
|
|
65
|
+
"salt": "",
|
|
66
|
+
"config": {},
|
|
67
|
+
}
|
|
68
|
+
try:
|
|
69
|
+
if not isfile(file_path):
|
|
70
|
+
raise ConfigParserException("File not found")
|
|
71
|
+
# Filled in _decrypt_and_decode_config()
|
|
72
|
+
self._incompatible_decryptors: list[int] = []
|
|
73
|
+
try:
|
|
74
|
+
self._dnpp = DotNetPEPayload(file_path, yara_rule)
|
|
75
|
+
except Exception as e:
|
|
76
|
+
raise e
|
|
77
|
+
self.report["sha256"] = self._dnpp.sha256
|
|
78
|
+
self.report["yara_possible_family"] = self._dnpp.yara_match
|
|
79
|
+
|
|
80
|
+
# Assigned in _decrypt_and_decode_config()
|
|
81
|
+
self._decryptor: ConfigDecryptor = None
|
|
82
|
+
self.report["config"] = self._get_config()
|
|
83
|
+
self.report["key"] = (
|
|
84
|
+
self._decryptor.key.hex()
|
|
85
|
+
if self._decryptor is not None and self._decryptor.key is not None
|
|
86
|
+
else "None"
|
|
87
|
+
)
|
|
88
|
+
self.report["salt"] = (
|
|
89
|
+
self._decryptor.salt.hex()
|
|
90
|
+
if self._decryptor is not None and self._decryptor.salt is not None
|
|
91
|
+
else "None"
|
|
92
|
+
)
|
|
93
|
+
except Exception as e:
|
|
94
|
+
self.report["config"] = f"Exception encountered for {file_path}: {e}"
|
|
95
|
+
|
|
96
|
+
# Decrypts/decodes values from an encrypted config and returns the
|
|
97
|
+
# decrypted/decoded config
|
|
98
|
+
def _decrypt_and_decode_config(
|
|
99
|
+
self, encrypted_config: bytes, min_config_len: int
|
|
100
|
+
) -> dict[str, Any]:
|
|
101
|
+
decoded_config = {}
|
|
102
|
+
|
|
103
|
+
for item_class in config_item.SUPPORTED_CONFIG_ITEMS:
|
|
104
|
+
item = item_class()
|
|
105
|
+
# Translate config Field RVAs to Field names
|
|
106
|
+
item_data = {
|
|
107
|
+
self._dnpp.field_name_from_rva(k): v
|
|
108
|
+
for k, v in item.parse_from(encrypted_config).items()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if len(item_data) > 0:
|
|
112
|
+
if type(item) is config_item.EncryptedStringConfigItem:
|
|
113
|
+
# Translate config value RVAs to string values
|
|
114
|
+
for k in item_data:
|
|
115
|
+
item_data[k] = self._dnpp.user_string_from_rva(item_data[k])
|
|
116
|
+
|
|
117
|
+
# Attempt to decrypt encrypted values
|
|
118
|
+
for decryptor in SUPPORTED_DECRYPTORS:
|
|
119
|
+
if decryptor in self._incompatible_decryptors:
|
|
120
|
+
continue
|
|
121
|
+
|
|
122
|
+
if self._decryptor is None:
|
|
123
|
+
# Try to instantiate the selected decryptor
|
|
124
|
+
# Add to incompatible list and move on upon failure
|
|
125
|
+
try:
|
|
126
|
+
self._decryptor = decryptor(self._dnpp)
|
|
127
|
+
except IncompatibleDecryptorException as ide:
|
|
128
|
+
logger.debug(
|
|
129
|
+
f"Decryptor incompatible {decryptor} : {ide}"
|
|
130
|
+
)
|
|
131
|
+
self._incompatible_decryptors.append(decryptor)
|
|
132
|
+
continue
|
|
133
|
+
try:
|
|
134
|
+
# Try to decrypt the encrypted strings
|
|
135
|
+
# Continue to next compatible decryptor on failure
|
|
136
|
+
item_data = self._decryptor.decrypt_encrypted_strings(
|
|
137
|
+
item_data
|
|
138
|
+
)
|
|
139
|
+
break
|
|
140
|
+
except Exception as e:
|
|
141
|
+
logger.debug(
|
|
142
|
+
f"Decryption failed with decryptor {decryptor} : {e}"
|
|
143
|
+
)
|
|
144
|
+
self._decryptor = None
|
|
145
|
+
|
|
146
|
+
if self._decryptor is None:
|
|
147
|
+
raise ConfigParserException("All decryptors failed")
|
|
148
|
+
|
|
149
|
+
elif type(item) is config_item.ByteArrayConfigItem:
|
|
150
|
+
for k in item_data:
|
|
151
|
+
arr_size, arr_rva = item_data[k]
|
|
152
|
+
item_data[k] = self._dnpp.byte_array_from_size_and_rva(
|
|
153
|
+
arr_size, arr_rva
|
|
154
|
+
).hex()
|
|
155
|
+
|
|
156
|
+
decoded_config.update(item_data)
|
|
157
|
+
|
|
158
|
+
if len(decoded_config) < min_config_len:
|
|
159
|
+
raise ConfigParserException(
|
|
160
|
+
f"Minimum threshold of config items not met: {len(decoded_config)}/{min_config_len}"
|
|
161
|
+
)
|
|
162
|
+
return decoded_config
|
|
163
|
+
|
|
164
|
+
# Searches for the RAT configuration section, using the VerifyHash() marker
|
|
165
|
+
# or brute-force, returning the decrypted config on success
|
|
166
|
+
def _get_config(self) -> dict[str, Any]:
|
|
167
|
+
logger.debug("Extracting config...")
|
|
168
|
+
try:
|
|
169
|
+
config_start, decrypted_config = self._get_config_verify_hash_method()
|
|
170
|
+
except Exception:
|
|
171
|
+
logger.debug("VerifyHash() method failed; Attempting .cctor brute force...")
|
|
172
|
+
# If the VerifyHash() method does not work, move to brute-forcing
|
|
173
|
+
# static constructors
|
|
174
|
+
try:
|
|
175
|
+
config_start, decrypted_config = self._get_config_cctor_brute_force()
|
|
176
|
+
except Exception as e:
|
|
177
|
+
raise ConfigParserException(f"Could not identify config: {e}")
|
|
178
|
+
logger.debug(f"Config found at RVA {hex(config_start)}...")
|
|
179
|
+
return decrypted_config
|
|
180
|
+
|
|
181
|
+
# Attempts to retrieve the config via brute-force, looking through every
|
|
182
|
+
# static constructor (.cctor) and attempting to decode/decrypt a valid
|
|
183
|
+
# config from that constructor, returning the config RVA and decrypted
|
|
184
|
+
# config on success
|
|
185
|
+
def _get_config_cctor_brute_force(self) -> Tuple[int, dict[str, Any]]:
|
|
186
|
+
candidates = self._dnpp.methods_from_name(".cctor")
|
|
187
|
+
if len(candidates) == 0:
|
|
188
|
+
raise ConfigParserException("No .cctor method could be found")
|
|
189
|
+
|
|
190
|
+
# For each .cctor method, map its RVA and body (in raw bytes)
|
|
191
|
+
candidate_cctor_data = {
|
|
192
|
+
method.rva: self._dnpp.method_body_from_method(method)
|
|
193
|
+
for method in candidates
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
config_start, decrypted_config = None, None
|
|
197
|
+
# Start at our ceiling value for number of config items
|
|
198
|
+
min_config_len = self._MIN_CONFIG_LEN_CEILING
|
|
199
|
+
|
|
200
|
+
while decrypted_config is None and min_config_len >= self._MIN_CONFIG_LEN_FLOOR:
|
|
201
|
+
for method_rva, method_body in candidate_cctor_data.items():
|
|
202
|
+
logger.debug(
|
|
203
|
+
f"Attempting brute force at .cctor method at {hex(method_rva)}"
|
|
204
|
+
)
|
|
205
|
+
try:
|
|
206
|
+
config_start, decrypted_config = (
|
|
207
|
+
method_rva,
|
|
208
|
+
self._decrypt_and_decode_config(method_body, min_config_len),
|
|
209
|
+
)
|
|
210
|
+
break
|
|
211
|
+
except Exception as e:
|
|
212
|
+
logger.debug(
|
|
213
|
+
f"Brute force failed for method at {hex(method_rva)}: {e}"
|
|
214
|
+
)
|
|
215
|
+
continue
|
|
216
|
+
# Reduce the minimum config length until we reach our floor
|
|
217
|
+
min_config_len -= 1
|
|
218
|
+
|
|
219
|
+
if decrypted_config is None:
|
|
220
|
+
raise ConfigParserException(
|
|
221
|
+
"No valid configuration could be parsed from any .cctor methods"
|
|
222
|
+
)
|
|
223
|
+
return config_start, decrypted_config
|
|
224
|
+
|
|
225
|
+
# Attempts to retrieve the config via looking for a config section preceded
|
|
226
|
+
# by the VerifyHash() method typically found in a Settings module,
|
|
227
|
+
# returning the config RVA and decrypted config on success
|
|
228
|
+
def _get_config_verify_hash_method(self) -> Tuple[int, dict[str, Any]]:
|
|
229
|
+
# Identify the VerifyHash() Method code
|
|
230
|
+
verify_hash_hit = search(self._PATTERN_VERIFY_HASH, self._dnpp.data)
|
|
231
|
+
if verify_hash_hit is None:
|
|
232
|
+
raise ConfigParserException("Could not identify VerifyHash() marker")
|
|
233
|
+
|
|
234
|
+
# Reverse the hit to find the VerifyHash() method, then grab the
|
|
235
|
+
# subsequent function
|
|
236
|
+
config_method = self._dnpp.method_from_instruction_offset(
|
|
237
|
+
verify_hash_hit.start(), 1
|
|
238
|
+
)
|
|
239
|
+
encrypted_config = self._dnpp.method_body_from_method(config_method)
|
|
240
|
+
min_config_len = self._MIN_CONFIG_LEN_CEILING
|
|
241
|
+
while True:
|
|
242
|
+
try:
|
|
243
|
+
decrypted_config = self._decrypt_and_decode_config(
|
|
244
|
+
encrypted_config, min_config_len
|
|
245
|
+
)
|
|
246
|
+
return config_method.rva, decrypted_config
|
|
247
|
+
except Exception as e:
|
|
248
|
+
# Reduce the minimum config length until we reach our floor
|
|
249
|
+
if min_config_len < self._MIN_CONFIG_LEN_FLOOR:
|
|
250
|
+
raise e
|
|
251
|
+
min_config_len -= 1
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# __init__.py
|
|
4
|
+
#
|
|
5
|
+
# Author: jeFF0Falltrades
|
|
6
|
+
#
|
|
7
|
+
# Copyright (c) 2024 Jeff Archer
|
|
8
|
+
#
|
|
9
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
# in the Software without restriction, including without limitation the rights
|
|
12
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
# furnished to do so, subject to the following conditions:
|
|
15
|
+
#
|
|
16
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
# copies or substantial portions of the Software.
|
|
18
|
+
#
|
|
19
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
# SOFTWARE.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# config_item.py
|
|
4
|
+
#
|
|
5
|
+
# Author: jeFF0Falltrades
|
|
6
|
+
#
|
|
7
|
+
# Provides a utility class for parsing field names and values of various types
|
|
8
|
+
# from raw RAT config data
|
|
9
|
+
#
|
|
10
|
+
# MIT License
|
|
11
|
+
#
|
|
12
|
+
# Copyright (c) 2024 Jeff Archer
|
|
13
|
+
#
|
|
14
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
15
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
16
|
+
# in the Software without restriction, including without limitation the rights
|
|
17
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
18
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
19
|
+
# furnished to do so, subject to the following conditions:
|
|
20
|
+
#
|
|
21
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
22
|
+
# copies or substantial portions of the Software.
|
|
23
|
+
#
|
|
24
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
25
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
26
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
27
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
28
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
29
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
30
|
+
# SOFTWARE.
|
|
31
|
+
from abc import ABC, abstractmethod
|
|
32
|
+
from logging import getLogger
|
|
33
|
+
from re import DOTALL, compile, findall
|
|
34
|
+
from typing import Any, Tuple
|
|
35
|
+
|
|
36
|
+
from .data_utils import bytes_to_int
|
|
37
|
+
from .dotnet_constants import OPCODE_LDC_I4_0, SpecialFolder
|
|
38
|
+
|
|
39
|
+
logger = getLogger(__name__)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# Provides an abstract class for config items
|
|
43
|
+
class ConfigItem(ABC):
|
|
44
|
+
def __init__(self, label: str, pattern: bytes) -> None:
|
|
45
|
+
self._label = label
|
|
46
|
+
self._pattern = compile(pattern, flags=DOTALL)
|
|
47
|
+
|
|
48
|
+
# Should be overridden by children to provide a meaningful value
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def _derive_item_value(self) -> Any:
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
# Derives config Field RVAs and values from data using the specified
|
|
54
|
+
# ConfigItem's pattern
|
|
55
|
+
def parse_from(self, data: bytes) -> dict[int, Any]:
|
|
56
|
+
logger.debug(f"Parsing {self._label} values from data...")
|
|
57
|
+
fields = {}
|
|
58
|
+
raw_data = findall(self._pattern, data)
|
|
59
|
+
found_items = 0
|
|
60
|
+
for obj, bytes_rva in raw_data:
|
|
61
|
+
try:
|
|
62
|
+
field_value = self._derive_item_value(obj)
|
|
63
|
+
field_rva = bytes_to_int(bytes_rva)
|
|
64
|
+
except Exception:
|
|
65
|
+
logger.debug(f"Could not parse value from {obj} at {hex(bytes_rva)}")
|
|
66
|
+
continue
|
|
67
|
+
if field_rva not in fields:
|
|
68
|
+
fields[field_rva] = field_value
|
|
69
|
+
found_items += 1
|
|
70
|
+
else:
|
|
71
|
+
logger.debug(
|
|
72
|
+
f"Overlapping Field RVAs detected in config at {hex(field_rva)}"
|
|
73
|
+
)
|
|
74
|
+
logger.debug(f"Parsed {found_items} {self._label} values")
|
|
75
|
+
return fields
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class BoolConfigItem(ConfigItem):
|
|
79
|
+
def __init__(self) -> None:
|
|
80
|
+
super().__init__("boolean", b"(\x16|\x17)\x80(.{3}\x04)")
|
|
81
|
+
|
|
82
|
+
# Boolean values are derived by examing if the opcode is "ldc.i4.0" (False)
|
|
83
|
+
# or "ldc.i4.1" (True)
|
|
84
|
+
def _derive_item_value(self, opcode: bytes) -> bool:
|
|
85
|
+
return bool(bytes_to_int(opcode) - bytes_to_int(OPCODE_LDC_I4_0))
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class ByteArrayConfigItem(ConfigItem):
|
|
89
|
+
def __init__(self) -> None:
|
|
90
|
+
super().__init__(
|
|
91
|
+
"byte array",
|
|
92
|
+
rb"\x1f(.\x8d.{3}\x01\x25\xd0.{3}\x04)\x28.{3}\x0a\x80(.{3}\x04)",
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
# Byte array size and RVA are returned, as these are needed to
|
|
96
|
+
# extract the value of the bytes from the payload
|
|
97
|
+
def _derive_item_value(self, byte_data: bytes) -> Tuple[int, int]:
|
|
98
|
+
arr_size = byte_data[0]
|
|
99
|
+
arr_rva = bytes_to_int(byte_data[-4:])
|
|
100
|
+
return (arr_size, arr_rva)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class IntConfigItem(ConfigItem):
|
|
104
|
+
def __init__(self) -> None:
|
|
105
|
+
super().__init__("int", b"(\x20.{4}|[\x18-\x1e])\x80(.{3}\x04)")
|
|
106
|
+
|
|
107
|
+
def _derive_item_value(self, int_bytes: bytes) -> int:
|
|
108
|
+
# If single byte, must be value 2-8, represented by opcodes 0x18-0x1e
|
|
109
|
+
# Subtract 0x16 to get the int value, e.g.:
|
|
110
|
+
# ldc.i4.8 == 0x1e - 0x16 == 8
|
|
111
|
+
if len(int_bytes) == 1:
|
|
112
|
+
return bytes_to_int(int_bytes) - 0x16
|
|
113
|
+
# Else, look for which int was loaded by "ldc.i4"
|
|
114
|
+
return bytes_to_int(int_bytes[1:])
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class NullConfigItem(ConfigItem):
|
|
118
|
+
def __init__(self) -> None:
|
|
119
|
+
super().__init__("null", b"(\x14\x80)(.{3}\x04)")
|
|
120
|
+
|
|
121
|
+
# If "ldnull" is being used, simply return "null"
|
|
122
|
+
def _derive_item_value(self, _: bytes) -> str:
|
|
123
|
+
return "null"
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class SpecialFolderConfigItem(ConfigItem):
|
|
127
|
+
def __init__(self) -> None:
|
|
128
|
+
super().__init__("special folder", b"\x1f(.)\x80(.{3}\x04)")
|
|
129
|
+
|
|
130
|
+
# Translates SpecialFolder ID to name
|
|
131
|
+
def _derive_item_value(self, folder_id: bytes) -> str:
|
|
132
|
+
return SpecialFolder(bytes_to_int(folder_id)).name
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class EncryptedStringConfigItem(ConfigItem):
|
|
136
|
+
def __init__(self) -> None:
|
|
137
|
+
super().__init__("encrypted string", b"\x72(.{3}\x70)\x80(.{3}\x04)")
|
|
138
|
+
|
|
139
|
+
# Returns the encrypted string's RVA
|
|
140
|
+
def _derive_item_value(self, enc_str_rva: bytes) -> int:
|
|
141
|
+
return bytes_to_int(enc_str_rva)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
SUPPORTED_CONFIG_ITEMS = [
|
|
145
|
+
BoolConfigItem,
|
|
146
|
+
ByteArrayConfigItem,
|
|
147
|
+
IntConfigItem,
|
|
148
|
+
NullConfigItem,
|
|
149
|
+
SpecialFolderConfigItem,
|
|
150
|
+
EncryptedStringConfigItem,
|
|
151
|
+
]
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# data_utils.py
|
|
4
|
+
#
|
|
5
|
+
# Author: jeFF0Falltrades
|
|
6
|
+
#
|
|
7
|
+
# Provides various utility functions for working with binary data
|
|
8
|
+
#
|
|
9
|
+
# MIT License
|
|
10
|
+
#
|
|
11
|
+
# Copyright (c) 2024 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
|
+
from ..config_parser_exception import ConfigParserException
|
|
31
|
+
|
|
32
|
+
|
|
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:
|
|
35
|
+
try:
|
|
36
|
+
return int.from_bytes(bytes, byteorder=order)
|
|
37
|
+
except Exception:
|
|
38
|
+
raise ConfigParserException(f"Error parsing int from value: {bytes}")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# Decodes a bytes object to a Unicode string, using UTF-16LE for byte values
|
|
42
|
+
# with null bytes still embedded in them, and UTF-8 for all other values
|
|
43
|
+
def decode_bytes(byte_str: bytes | str) -> str:
|
|
44
|
+
if isinstance(byte_str, str):
|
|
45
|
+
return byte_str.strip()
|
|
46
|
+
result = None
|
|
47
|
+
try:
|
|
48
|
+
if b"\x00" in byte_str:
|
|
49
|
+
result = byte_str.decode("utf-16le")
|
|
50
|
+
else:
|
|
51
|
+
result = byte_str.decode("utf-8")
|
|
52
|
+
except Exception:
|
|
53
|
+
raise ConfigParserException(
|
|
54
|
+
f"Error decoding bytes object to Unicode: {byte_str}"
|
|
55
|
+
)
|
|
56
|
+
return result
|
|
57
|
+
|
|
58
|
+
|
|
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:
|
|
61
|
+
try:
|
|
62
|
+
return int.to_bytes(length, order)
|
|
63
|
+
except Exception:
|
|
64
|
+
raise ConfigParserException(f"Error parsing bytes from value: {int}")
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# __init__.py
|
|
4
|
+
#
|
|
5
|
+
# Author: jeFF0Falltrades
|
|
6
|
+
#
|
|
7
|
+
# Copyright (c) 2024 Jeff Archer
|
|
8
|
+
#
|
|
9
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
# in the Software without restriction, including without limitation the rights
|
|
12
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
# furnished to do so, subject to the following conditions:
|
|
15
|
+
#
|
|
16
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
# copies or substantial portions of the Software.
|
|
18
|
+
#
|
|
19
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
# SOFTWARE.
|
|
26
|
+
from .config_decryptor import ConfigDecryptor, IncompatibleDecryptorException
|
|
27
|
+
from .config_decryptor_aes_cbc import ConfigDecryptorAESCBC
|
|
28
|
+
from .config_decryptor_aes_ecb import ConfigDecryptorAESECB
|
|
29
|
+
from .config_decryptor_decrypt_xor import ConfigDecryptorDecryptXOR
|
|
30
|
+
from .config_decryptor_plaintext import ConfigDecryptorPlaintext
|
|
31
|
+
from .config_decryptor_random_hardcoded import ConfigDecryptorRandomHardcoded
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
ConfigDecryptor,
|
|
35
|
+
IncompatibleDecryptorException,
|
|
36
|
+
ConfigDecryptorAESCBC,
|
|
37
|
+
ConfigDecryptorAESECB,
|
|
38
|
+
ConfigDecryptorDecryptXOR,
|
|
39
|
+
ConfigDecryptorRandomHardcoded,
|
|
40
|
+
ConfigDecryptorPlaintext,
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
# ConfigDecryptorPlaintext should always be the last fallthrough case
|
|
44
|
+
SUPPORTED_DECRYPTORS = [
|
|
45
|
+
ConfigDecryptorAESCBC,
|
|
46
|
+
ConfigDecryptorAESECB,
|
|
47
|
+
ConfigDecryptorDecryptXOR,
|
|
48
|
+
ConfigDecryptorRandomHardcoded,
|
|
49
|
+
ConfigDecryptorPlaintext,
|
|
50
|
+
]
|