-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathyarascanner.py
75 lines (58 loc) · 1.91 KB
/
yarascanner.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
# file: yarascanner.py
import utils
import yara
import sys
from os import path
import pika
import md5
def scanFile(file_id):
print "Scanning file %d" % file_id
filename = '%s' % file_id
filepath = path.join('uploads', filename)
matches = rules.match(filepath)
print matches
for match in matches:
stmt = "SELECT id FROM rules WHERE name = %(name)s AND enabled=1"
cur.execute(stmt, {'name': match})
rule_id = cur.fetchone()[0]
stmt = "INSERT INTO matches (file_id, rule_id) VALUES (%(file_id)s, %(rule_id)s)"
cur.execute(stmt, {'file_id': file_id, 'rule_id': rule_id})
db.commit()
# Get MD5 hash of file
m = md5.new()
with open(filepath, 'rb') as f:
filedata = f.read()
m.update(filedata)
filehash = m.hexdigest()
# Add the hash to the DB
stmt = "UPDATE files SET md5 = %(md5)s WHERE id = %(id)s"
cur.execute(stmt, {'md5': filehash, 'id': file_id})
db.commit()
# Read rules from database
cur, db = utils.connectToDB()
stmt = "SELECT text FROM rules_text rt JOIN rules r ON rt.id = r.id WHERE r.enabled=1"
cur.execute(stmt)
storedRules = cur.fetchall()
# Join them
rulesText = ""
for rule in storedRules:
rulesText += rule[0] + '\n'
# Load them into yara
rules = yara.compile(source=rulesText)
if len(sys.argv) > 1:
scanFile(int(sys.argv[1]))
# RabbitMQ callback
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
scanFile(int(body))
ch.basic_ack(delivery_tag = method.delivery_tag)
# Consume messages from work queue
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
channel.queue_declare(queue='uploaded_files')
channel.basic_consume(callback,
queue='uploaded_files')
print ' [*] Waiting for messages. To exit press CTRL+C'
channel.start_consuming()