Skip to content

Commit 5e3541f

Browse files
committed
analyze_crypted_code: Add recursive processing, inspired by #13
1 parent 2076337 commit 5e3541f

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

analyze_crypted_code.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,42 @@ def get_crypto_info(all_data: bytes, code_obj) -> dict:
9696
}
9797

9898

99-
with open(sys.argv[1], "rb") as fp:
100-
fp.seek(0x20)
101-
data = fp.read()
99+
def process_code_object(code_obj, filedata: bytes, crypted_regions: list[dict]) -> None:
100+
"""
101+
Recursively processes a code object and its nested code objects in constants
102+
in order to extract encryption information.
102103
103-
obj = marshal.load(BytesIO(data))
104+
Args:
105+
code_obj: The code object to process
106+
filedata: Entire contents of the Python module
107+
crypted_regions: List that is appended to
108+
"""
109+
for const in code_obj.co_consts:
110+
if isinstance(const, type((lambda: None).__code__)):
111+
print("Found nested code object: " + str(const))
112+
display_code(const)
113+
if info := get_crypto_info(filedata, const):
114+
crypted_regions.append(info)
104115

105-
display_code(obj)
116+
process_code_object(const, filedata, crypted_regions)
106117

107-
crypted_regions = []
108118

109-
for const in obj.co_consts:
110-
if isinstance(const, type((lambda: None).__code__)):
111-
print("Found " + str(const))
112-
display_code(const)
113-
if info := get_crypto_info(data, const):
114-
crypted_regions.append(info)
119+
def main(filename: str) -> None:
120+
with open(filename, "rb") as fp:
121+
fp.seek(0x20)
122+
data = fp.read()
115123

116-
crypted_regions.append(get_crypto_info(data, obj))
124+
obj = marshal.load(BytesIO(data))
125+
display_code(obj)
117126

118-
json.dump(crypted_regions, open(sys.argv[1] + ".json", "w"))
127+
crypted_regions: list[dict] = []
128+
process_code_object(obj, data, crypted_regions)
129+
crypted_regions.append(get_crypto_info(data, obj))
119130

120-
print(f"Found {len(crypted_regions)} encrypted code objects. {sys.argv[1]}.json saved.")
131+
json.dump(crypted_regions, open(filename + ".json", "w"))
132+
133+
print(f"Found {len(crypted_regions)} encrypted code objects. {filename}.json saved.")
134+
135+
136+
if __name__ == "__main__":
137+
main(sys.argv[1])

0 commit comments

Comments
 (0)