summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xphp-malware-finder/generate_whitelist.py22
1 files changed, 8 insertions, 14 deletions
diff --git a/php-malware-finder/generate_whitelist.py b/php-malware-finder/generate_whitelist.py
index a8ed8f8..af6be27 100755
--- a/php-malware-finder/generate_whitelist.py
+++ b/php-malware-finder/generate_whitelist.py
@@ -11,11 +11,6 @@ except ImportError:
11 print('Please install python-yara') 11 print('Please install python-yara')
12 sys.exit(1) 12 sys.exit(1)
13 13
14print("/!\\ THIS SCRIPT IS BROKEN AND SHOULD NOT BE USED /!\\")
15print("IF YOU WANT ANYWAY, EDIT IT TO REMOVE THIS WARNING")
16sys.exit(0)
17
18
19if len(sys.argv) != 3: 14if len(sys.argv) != 3:
20 print('Usage: %s name_of_the_rule_and_version folder_to_scan' % sys.argv[0]) 15 print('Usage: %s name_of_the_rule_and_version folder_to_scan' % sys.argv[0])
21 sys.exit(1) 16 sys.exit(1)
@@ -28,19 +23,18 @@ rules = yara.compile('./php.yar', includes=True, error_on_warning=True)
28 23
29output_list = list() 24output_list = list()
30 25
31for cpt, (root, dirnames, filenames) in enumerate(os.walk(sys.argv[2])): 26for curdir, dirnames, filenames in os.walk(sys.argv[2]):
32 for filename in fnmatch.filter(filenames, '*.ph*'): 27 for filename in fnmatch.filter(filenames, '*.ph*'):
33 fname = os.path.join(root, filename) 28 fname = os.path.join(curdir, filename)
34 if os.stat(fname).st_size: 29 if 0 < os.stat(fname).st_size < 5 * 1024 * 1024:
35 matches = rules.match(os.path.join(root, filename), fast=True) 30 matches = rules.match(fname, fast=True)
36 if matches: 31 if matches:
37 hasher = hashlib.sha1() 32 with open(fname, 'rb') as f:
38 with open(fname, 'rb') as ifile: 33 digest = hashlib.sha1(f.read()).hexdigest()
39 hasher.update(ifile.read()) 34 output_list.append('hash.sha1(0, filesize) == "%s" or // %s' % (digest, fname))
40 output_list.append('hash.sha1(0, filesize) == "%s" or // %s' % (hasher.hexdigest(), fname))
41 35
42 36
43output_rule = 'import "hash"\n\nprivate rule %s\n{\n\tcondition:\n\t\t/* %s */\n\t\t' % (sys.argv[1].split(' ')[0], sys.argv[1]) 37output_rule = 'import "hash"\n\nrule %s\n{\n\tcondition:\n\t\t/* %s */\n\t\t' % (sys.argv[1].split(' ')[0], sys.argv[1])
44output_list.append(output_list.pop().replace(' or ', ' ')) 38output_list.append(output_list.pop().replace(' or ', ' '))
45output_rule += '\n\t\t'.join(output_list) 39output_rule += '\n\t\t'.join(output_list)
46output_rule += '\n}' 40output_rule += '\n}'