diff options
| author | jvoisin | 2015-07-13 10:47:41 +0200 |
|---|---|---|
| committer | jvoisin | 2015-07-13 10:47:41 +0200 |
| commit | b5a5f1efe855f8d0878bfb7e74e5578cd42d38b7 (patch) | |
| tree | 911f2de4f3f2210da22cfa7eecd59e3076f19468 | |
| parent | 169d7dc344230ea0afafa2af4d81b11b327ccec0 (diff) | |
Add a whitelist generator
| -rw-r--r-- | generate_whitelist.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/generate_whitelist.py b/generate_whitelist.py new file mode 100644 index 0000000..2671fc7 --- /dev/null +++ b/generate_whitelist.py | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | import fnmatch | ||
| 2 | import hashlib | ||
| 3 | import os | ||
| 4 | import sys | ||
| 5 | |||
| 6 | try: | ||
| 7 | import yara | ||
| 8 | except ImportError: | ||
| 9 | print 'Please install python-yara' | ||
| 10 | sys.exit(0) | ||
| 11 | |||
| 12 | if len(sys.argv) != 3: | ||
| 13 | print 'Usage: %s name_of_the_rule_and_version folder_to_scan' % sys.argv[0] | ||
| 14 | |||
| 15 | rules = yara.compile('./malwares.yara', includes=True, error_on_warning=True) | ||
| 16 | |||
| 17 | output_list = list() | ||
| 18 | |||
| 19 | for cpt, (root, dirnames, filenames) in enumerate(os.walk(sys.argv[2])): | ||
| 20 | for filename in fnmatch.filter(filenames, '*.ph*'): | ||
| 21 | fname = os.path.join(root, filename) | ||
| 22 | if os.stat(fname).st_size: | ||
| 23 | matches = rules.match(os.path.join(root, filename), fast=True) | ||
| 24 | if matches: | ||
| 25 | matches = matches.pop() # only one match, since we're scaning files | ||
| 26 | output_list.append('hash.sha1(0, filename) == %s or // %s' % (hashlib.sha1(fname).hexdigest(), fname)) | ||
| 27 | |||
| 28 | |||
| 29 | output_rule = 'private rule %s\n{\n\tcondition:\n\t\t/* %s */\n\t\t' % (sys.argv[1].split(' ')[0], sys.argv[1]) | ||
| 30 | output_list.append(output_list.pop().replace(' or ', ' ')) | ||
| 31 | output_rule += '\n\t\t'.join(output_list) | ||
| 32 | output_rule +='\n}' | ||
| 33 | print output_rule | ||
