-
Notifications
You must be signed in to change notification settings - Fork 0
/
yara_sweeper.py
executable file
·223 lines (182 loc) · 6.73 KB
/
yara_sweeper.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
#!/usr/bin/env python
import os
import sys
import yara
import platform
import hashlib
import validators
from dulwich import porcelain
from configparser import SafeConfigParser
import argparse
import json
not_win = platform.system() != 'Windows'
if(not_win):
import syslog
LOG_ERR = syslog.LOG_ERR if not_win else 0
LOG_ALERT = syslog.LOG_ALERT if not_win else 0
# config file must be in the same directory
CONF_FILE = 'config.ini'
def clone_rules(r_path, l_path):
try:
# redirect output/err to devnull
f = open(os.devnull, 'wb')
if(validators.url(r_path)):
repo = porcelain.clone(r_path, l_path, errstream=f)
return True
else:
return False
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
def update_rules(r_path, l_path):
try:
f = open(os.devnull, 'wb')
if(os.path.isdir(l_path) and validators.url(r_path)):
porcelain.pull(l_path, r_path, b'refs/heads/master', errstream=f)
return True
else:
return False
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
def rest_log(endpoint, payload):
import requests
try:
requests.post(endpoint, json=(payload))
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
def sys_log(msg, level):
if(platform.system() != 'Windows'):
syslog.syslog(level, msg)
else:
return False
def init_rules(path):
rules = []
if os.path.isdir(path):
for root, dirs, filenames in os.walk(path):
for name in filenames:
try:
if not name.startswith('.') and (name.endswith('yar') or name.endswith('yara')):
file_path = os.path.join(root, name)
rules.append(yara.compile(filepath = file_path, includes=True))
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
continue
elif os.path.isfile(path):
try:
rules.append(yara.compile(filepath = path))
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
return rules
def generate_hash(file):
with open(file, 'rb') as f:
filedata = f.read()
try:
sha256 = hashlib.sha256()
sha256.update(filedata)
return sha256.hexdigest()
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
return "Hash error"
def yr_file(file, rules):
log = []
description = "no description"
for rule in rules:
try:
matches = rule.match(filepath = file)
if matches:
for match in matches:
if hasattr(match, 'meta'):
if 'description' in match.meta:
description = match.meta['description']
sha256 = generate_hash(file)
log.append({ "rulename" : str(match), "desc" : description, "filename" : file, "sha256" : sha256, "hostname" : platform.node()})
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
continue
return log
def yr_dir(dir, rules, level):
log = []
description = "no description"
# sorry for this..
for root, dirs, filenames in os.walk(dir):
if root[len(dir)+1:].count(os.sep) < level:
for name in filenames:
try:
file_path = os.path.join(root, name)
for rule in rules:
matches = rule.match(filepath = file_path)
if matches:
for match in matches:
if hasattr(match, 'meta'):
if 'description' in match.meta:
description = match.meta['description']
sha256 = generate_hash(file_path)
log.append({ "rulename" : str(match), "desc" : description, "filename" : file_path, "sha256" : sha256, "hostname" : platform.node()})
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
continue
return log
def yr_proc(pid, rules):
import psutil
try:
proc_name = psutil.Process(pid).name()
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
log = []
description = "no description"
for rule in rules:
try:
matches = rule.match(pid=pid)
if matches:
for match in matches:
if hasattr(match, 'meta'):
if 'description' in match.meta:
description = match.meta['description']
log.append({ "rulename" : str(match), "desc" : description, "proc" : proc_name, "pid" : pid, "hostname" : platform.node()})
except:
sys_log(str(sys.exc_info()[1]), LOG_ERR)
continue
return log
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-a', action='store_true', default=False, dest='all_rules', help='Compile all rules.')
parser.add_argument('-r', action='store', default=False, dest='rules', help='Compile single rule file or dir.')
parser.add_argument('-t', action='store', default=False, dest='target', help='Run yara rule against target (pid/file/dir).', required=True)
args = parser.parse_args()
if(args.all_rules is False and args.rules is False):
parser.print_help()
sys.exit(0)
parser = SafeConfigParser()
parser.read(CONF_FILE)
rpath = parser.get('main', 'rules_path')
repo_path = parser.get('git', 'repo_dir_path')
repo_url = parser.get('git', 'repo_url')
level = int(parser.get('main', 'scan_dir_level'))
endpoint = parser.get('rest', 'endpoint')
if(os.path.exists(repo_path)):
update_rules(repo_url, repo_path)
else:
clone_rules(repo_url, repo_path)
# run all rules
if(args.all_rules):
rules = init_rules(rpath)
if(os.path.exists(args.target)):
if(os.path.isdir(args.target)):
matches = yr_dir(args.target, rules, level)
else:
matches = yr_file(args.target, rules)
else:
matches = yr_proc(int(args.target), rules)
if(args.rules):
rules = init_rules(args.rules)
if(os.path.exists(args.target)):
if(os.path.isdir(args.target)):
matches = yr_dir(args.target, rules, level)
else:
matches = yr_file(args.target, rules)
else:
matches = yr_proc(int(args.target), rules)
if(matches):
sys_log(json.dumps((matches)), LOG_ALERT)
rest_log(endpoint, matches)
if __name__ == '__main__':
main()