forked from GoSecure/php7-opcache-override
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcache_malware_hunt.py
executable file
·390 lines (291 loc) · 10.9 KB
/
opcache_malware_hunt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env python2
# Copyright (c) 2016 GoSecure Inc.
from opcache_disassembler import OPcacheDisassembler
import hashlib
import opcache_parser
import opcache_parser_64
import sys
import os
import subprocess
import shutil
import difflib
import time
hunt_source_files = "hunt_source_files.tmp"
hunt_ini = "hunt.ini"
hunt_opcache = "hunt_opcache"
hunt_report = "hunt_report" + "_" + str(int(time.time()))
def list_opcache_files(path):
""" List every opcache (.php.bin) file found in a given path """
opcache_files = []
# Check if arg[1] is a folder or a file
if os.path.isdir(path):
# Iterate through all the files of the folder
for subdir, dirs, files in os.walk(path):
for file in files:
# Only check .php.bin files
if file.endswith(".php.bin"):
file = os.path.join(subdir, file)
opcache_files += [file]
else:
# Only check .php.bin files
if path.endswith(".php.bin"):
opcache_files += [path]
return opcache_files
def dump_source_file_list(list):
""" Writes a list of files to the hunt_source_files file"""
with open(hunt_source_files, 'w') as f:
for file in list:
f.write(file + "\n")
def setup_env(phpini_path):
""" Setup all the files and folders needed for this tool """
# hunt.ini
with open(phpini_path, "r") as f:
with open(hunt_ini, "w") as h:
for line in f.readlines():
# opcache.file_cache
if "opcache.file_cache=" in line:
line = "opcache.file_cache=" + os.path.join(os.getcwd(), hunt_opcache)
# opcache.enable_cli
if "opcache.enable_cli=" in line:
line = "opcache.enable_cli=1"
# opcache.enable_cli
if "opcache.enable=" in line:
line = "opcache.enable=1"
if "opcache.file_cache_only=" in line:
line = "opcache.file_cache_only=0"
h.write(line)
# cache folder location
os.mkdir(hunt_opcache)
os.chmod(hunt_opcache, 0o777)
# report folder
os.mkdir(hunt_report)
def cleanup():
""" Cleanup all temporary files and folders """
# Remove cache folder
try:
shutil.rmtree(hunt_opcache)
except:
pass
# Remove hunt.ini
try:
os.remove(hunt_ini)
except:
pass
# Remove source files list
try:
os.remove(hunt_source_files)
except:
pass
def compile_source_files():
""" Uses the compile.php script to compile each file in the hunt_source_files file"""
command = "php -c {0} compile.php {1}".format(hunt_ini, hunt_source_files)
subprocess.call(command.split(), shell=False)
def parse_file(file):
""" Parse a file and return a construct object """
return OPcacheParser(file)
def get_literals(parsed_literals):
""" Extract the literals values from a list of construct objects
Arguments:
parsed_literals : A list of construct objects representing literals
"""
literals = []
for f in parsed_literals:
if f.u1.type == 6:
string = f.string.val
if string.startswith("\x00"):
literals += [string[1:-10]] # Trim address from literal
if f.u1.type == 4:
literals += [f.value.w1]
return literals
def get_opcodes(parsed_opcodes):
""" Extract the opcode values from a list of construct objects
Arguments:
parsed_opcodes : A list of construct objects representing opcodes
"""
return [f.opcode for f in parsed_opcodes]
def get_function(parsed_function):
""" Extract a function from a construct object
Arguments:
parsed_function : A construct object representation a function
"""
function_name = parsed_function['function_name']['value']['val']
opcodes = get_opcodes(parsed_function['opcodes'])
literals = get_literals(parsed_function['literals'])
return {function_name : { 'opcodes' : opcodes, 'literals' : literals }}
def get_class(parsed_class):
""" Extract a class from a construct object
Arguments:
parsed_class : A construct object representation a class
"""
if not parsed_class:
return {}
classname = parsed_class['name']['value']['val']
parsed_functions = [f['val']['op_array'] for f in parsed_class['function_table']['buckets']]
functions = []
for function in parsed_functions:
functions += [get_function(function)]
return { classname : {'functions' : functions}}
def compare_parsed_files(file1, file2):
""" Compare two parsed files based on opcodes, literals, classes and functions.
Arguments :
file1 : A construct object representing an OPcache file
file2 : A construct object representing an OPcache file
"""
# Main OP array
opcodes_1 = get_opcodes(file1['script']['main_op_array']['opcodes'])
opcodes_2 = get_opcodes(file2['script']['main_op_array']['opcodes'])
literals_1 = get_literals(file1['script']['main_op_array']['literals'])
literals_2 = get_literals(file2['script']['main_op_array']['literals'])
main_op_array_1 = { 'opcodes' : opcodes_1, 'literals' : literals_1 }
main_op_array_2 = { 'opcodes' : opcodes_2, 'literals' : literals_2 }
# Function OPcodes
functions_1 = {}
functions_2 = {}
# Functions of file 1
functions = [f['val']['op_array'] for f in file1['script']['function_table']['buckets']]
for function in functions:
functions_1.update(get_function(function))
# Functions of file 2
functions = [f['val']['op_array'] for f in file2['script']['function_table']['buckets']]
for function in functions:
functions_2.update(get_function(function))
# Class OPcodes
classes_1 = {}
classes_2 = {}
# Classes of file 1
classes = [f['val']['class'] for f in file1['script']['class_table']['buckets']]
for class_ in classes:
classes_1.update(get_class(class_))
# Classes of file 2
classes = [f['val']['class'] for f in file2['script']['class_table']['buckets']]
for class_ in classes:
classes_2.update(get_class(class_))
# OPcodes
if main_op_array_1 != main_op_array_2:
return False
# Functions
if functions_1 != functions_2:
return False
# Classes
if classes_1 != classes_2:
return False
return True
def create_diff_report(file1, file2, file_name, from_desc, to_desc, is_64_bit):
""" Create a report showing the differences between two files
Arguments :
file1 : path to a file to disassemble
file2 : path to a file to disassemble
report_name : The name to use for the report
from_desc : A description of the 'from' element
to_desc : A description of the 'to' element
"""
# Disassemble each file and split into lines
disassembled_1 = OPcacheDisassembler(is_64_bit).disassemble(file1).split("\n")
disassembled_2 = OPcacheDisassembler(is_64_bit).disassemble(file2).split("\n")
# Differ
html_differ = difflib.HtmlDiff()
# Generate the report and write into a file
file_name = file_name.replace("/", "%2f") + '.html'
hash_name = hashlib.sha1(file_name).hexdigest()
with open(hunt_report + "/" + hash_name + ".html", "w") as f:
content = html_differ.make_file(disassembled_1, disassembled_2, from_desc, to_desc)
f.write(content)
# Return the name of the report
return (file_name, hash_name + ".html")
def create_index(file_names, report_names):
""" Create an index file containing the list of all the reports generated by create_diff_report
Arguments :
report_names : the list of report names
"""
# Header
header = """
<html>
<head>
<title>OPcache Malware Hunt Report</title>
</head>
<body>
<h1>Potentially infected files</h1>
"""
# The list of links towards each report
body = "<ul>"
for index, report_name in enumerate(report_names):
link = report_name.replace("%2f", "%252f")
link_name = file_names[index].replace("%2f", "/")[:-5]
body += "<li><a href='{0}'>{1}</a></li>".format(link, link_name)
body += "</ul>"
# Footer
footer = """
</body>
</html>
"""
with open(hunt_report + "/" + "index.html", "w") as f:
f.write(header + body + footer)
def show_help():
""" Show the help menu"""
print "Usage : {0} [opcache_folder] [-a(86|64)] [system_id] [php.ini] ".format(sys.argv[0])
if __name__ == "__main__":
if len(sys.argv) < 3:
show_help()
exit(0)
# Remove temporary files and folders
cleanup()
# Paths to analyse
opcache_folder = sys.argv[1]
architecture = sys.argv[2]
system_id = sys.argv[3]
phpini_path = sys.argv[4]
# Is 64 bit
is_64_bit = False
if architecture == "-a64":
is_64_bit = True
OPcacheParser = opcache_parser_64.OPcacheParser
elif architecture == "-a32":
is_64_bit = False
OPcacheParser = opcache_parser.OPcacheParser
# Setup a new phpini for compilation
setup_env(phpini_path)
# OPcache file list
opcache_files = list_opcache_files(opcache_folder)
# Get location of source folder
prefix = os.path.commonprefix(opcache_files)
source_folder = prefix.split(system_id, 1)[1]
# Source files list
if len(opcache_files) > 1:
source_files = [source_folder + file.split(source_folder)[-1][:-4] for file in opcache_files ]
else:
source_files = [source_folder[:-4]]
# Dump source files
dump_source_file_list(source_files)
# Compile source files
compile_source_files()
# Compare original cache files with new ones
flagged_files = []
for idx, file in enumerate(opcache_files):
new_cache_file = os.path.join(hunt_opcache, system_id)
new_cache_file += os.path.join(new_cache_file, source_files[idx])
new_cache_file += ".bin"
# Parse files
print "Parsing " + file
original_file = parse_file(file)
print "Parsing " + new_cache_file
new_parsed = parse_file(new_cache_file)
# Compare files
if not compare_parsed_files(original_file, new_parsed):
flagged_files += [(idx, file, new_cache_file)]
if flagged_files:
print ""
print "Potentially infected files : "
reports = []
file_names = []
for idx, file, new_cache_file in flagged_files:
print " - " + file
(file_name, report) = create_diff_report(new_cache_file, file, opcache_files[idx], "Source Code", "Cache", is_64_bit)
reports += [report]
file_names += [file_name]
create_index(file_names, reports)
else:
print "No infected files found."
print ""
print "Generated report in folder " + hunt_report
# Remove temporary files and folders
cleanup()