-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyseWithYaraRules.py
75 lines (57 loc) · 2.18 KB
/
AnalyseWithYaraRules.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
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
import argparse
import time
import datetime
import guestfs
from threading import Thread
import os
import yara
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
LOCAL_MOUNT = '/tmp/libguestfs-local-mount/'
g = guestfs.GuestFS(python_return_dict=True)
def threaded_function():
g.mount_local_run()
def main():
#Definition der Argumente
parser = argparse.ArgumentParser(
description=u'Analyse des Inhalts einer virtuellen Festplatte mittels YARA-Regeln')
parser.add_argument('--image', help=u'Dateipfad des Festplattenabbildes', required=True)
parser.add_argument('--yaraRules', help=u'Dateipfad der Datei mit den YARA-Regeln',
required=True)
args = vars(parser.parse_args())
image = args.get('image')
rulesFileName = args.get('yaraRules')
#Messen der benötigten Zeit
startTime = time.time()
g.add_drive_ro(image)
g.launch()
#Erstellen des temporären Mount-Verzeichnisses falls dieses nicht existiert
if not os.path.exists(LOCAL_MOUNT):
os.makedirs(LOCAL_MOUNT)
#Laden der YARA-Regeln aus der Datei
rules = yara.compile(rulesFileName)
for partition, fileSystem in g.list_filesystems().iteritems():
#unbekannte (zB extended-Partitionen oder unbekannte Dateisysteme) und
#swap-Partitionen werden ignoriert
if fileSystem != 'swap' and fileSystem != 'unknown':
g.mount_ro(partition, '/')
g.mount_local(LOCAL_MOUNT)
thread = Thread(target=threaded_function)
thread.start()
filePaths = g.find('/')
for filePath in filePaths:
if g.is_file('/' + filePath) and g.filesize('/' + filePath) > 0:
matches = rules.match(LOCAL_MOUNT + filePath)
if len(matches) > 0: #wenn eine YARA-Regel zuschlägt
match = matches['main']
print '{0} gefunden: {1} - {2}'.format(match[0]['rule'], partition, filePath)
g.umount_local()
g.umount(partition, force=True)
endTime = time.time()
print ''
print u'Skript ausgeführt in {0}'.format(
datetime.timedelta(seconds=round(endTime - startTime, 0)))
if __name__ == '__main__':
main()