-
Notifications
You must be signed in to change notification settings - Fork 18
/
role_generator.py
427 lines (376 loc) · 20.4 KB
/
role_generator.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!python
# -*- coding: utf-8 -*-
# @author: Kun
'''
Author: Kun
Date: 2021-09-16 11:21:47
LastEditTime: 2021-09-23 17:56:16
LastEditors: Kun
Description:
FilePath: /ThreatReportExtractor/role_generator.py
'''
from nlp_extractor.subject_verb_object_extract import SubjectVerbObjectExtractor
# nlp = spacy.load("en_core_web_lg")
from allennlp.predictors.predictor import Predictor
from nltk.stem.wordnet import WordNetLemmatizer
from preprocessings import *
from data_loader.ioc_loader import iocs
from data_loader.pattern_loader import load_lists
from project_config import SEC_PATTERNS_FILE_PATH
# main_verbs = load_lists(SEC_PATTERNS_FILE_PATH)['verbs']
# main_verbs = main_verbs.replace("'", "").strip('][').split(', ')
class RoleGenerator(object):
def __init__(self, nlp, main_verbs) -> None:
super(RoleGenerator, self).__init__()
self.nlp = nlp # self.svo_extractor.nlp
self.svo_extractor = SubjectVerbObjectExtractor(nlp)
self.main_verbs = main_verbs
def colon_seprator_multiplication(self, stri):
print("[colon_seprator_multiplication] stri: ", stri)
coref_reference_list = load_lists(SEC_PATTERNS_FILE_PATH)['TFL']
coref_reference_list = coref_reference_list.replace(
"'", "").strip('][').split(', ')
stri = stri.rstrip()
stri = stri.rstrip('.')
result = ""
for item in sent_tokenize(stri):
flag = False
for refrence in coref_reference_list:
if refrence in item and ":" in item:
sentence_splits = item.split(":", 1)
y = iocs.list_of_iocs(item.split(":", 1)[1])
if y:
sentence_replicas = [
sentence_splits[0].rstrip(":")] * len(y)
for i in range(len(sentence_replicas)):
result += sentence_replicas[i].replace(
refrence, y[i]) + " . "
else:
item = sentence_splits[0].replace(
refrence, sentence_splits[1])
result += item
flag = True
break
if flag == False:
result += item
result += " "
print("[colon_seprator_multiplication] result: ", result)
if result.rstrip("")[-1] != ".":
result += "."
return result
def roles(self, sentences):
my_svo_triplet = []
all_nodes = []
for i in range(len(sentences)):
# public SRL model https://s3-us-west-2.amazonaws.com/allennlp/models/srl-model-2018.05.25.tar.gz
predictor = Predictor.from_path("srl-model.tar.gz")
predictions = predictor.predict(sentences[i])
lst = []
nodes = []
for k in predictions['verbs']:
if k['description'].count('[') > 1:
lst.append(k['description'])
for jj in range(len(lst)):
nodes.append([])
for j in re.findall(r"[^[]*\[([^]]*)\]", lst[jj]):
nodes[jj].append(j)
print("*****sentence:", sentences[i], '*****nodes: ', nodes)
for lis_ in nodes:
for indx in range(len(lis_)):
if lis_[0].split(":", 1)[0].lower().strip() == "v" and lis_[0].split(":", 1)[1].lower().strip() in self.main_verbs:
n = len(lis_)
for j in range(1, len(lis_)):
if lis_[j].split(":", 1)[0].lower() != "v":
if len(iocs.list_of_iocs(lis_[j].split(":", 1)[1])) > 0:
lis_.insert(0, " ARG-NEW: *")
maxlength = 0
if nodes:
maxlength = max((len(i) for i in nodes))
if nodes == [] or maxlength < 3:
print("****DP SVO****")
tokens = self.nlp(sentences[i])
svos = self.svo_extractor.findSVOs(tokens)
if svos:
for sv in range(len(svos)):
if len(svos[sv]) == 3:
print('Dependency SVO(s):', [
"ARG0: " + svos[sv][0], "V: " + svos[sv][1], "ARG1: " + svos[sv][2]])
nodes.append(
["ARG0: " + svos[sv][0], "V: " + svos[sv][1], "ARG1: " + svos[sv][2]])
print("Dependency-SVO added nodes: ", nodes)
print("****Naive SVO****")
breakers = []
subj, obj = '', ''
doc = self.nlp(sentences[i])
for token in doc:
if token.pos_ == 'VERB':
breakers.append(str(token))
if len(breakers) != 0:
for vb in breakers:
subj = "subj: " + sentences[i].split(vb)[0]
obj = "obj: " + sentences[i].split(vb)[1]
vrb = "v: " + vb
lst = []
lst.append(subj)
lst.append(vrb)
lst.append(obj)
nodes.append(lst)
print("Naive Nodes: ", nodes)
if nodes != []:
zero_dunplicate_removed = []
for i in nodes:
zero_dunplicate_removed.append(list(dict.fromkeys(i)))
no_zero_nodes = []
for i in zero_dunplicate_removed:
if '.' in i:
i.remove('.')
no_zero_nodes.append(i)
else:
no_zero_nodes = zero_dunplicate_removed
no_zero_nodes_plus_3 = []
for i in no_zero_nodes:
if len(i) > 2:
no_zero_nodes_plus_3.append(i)
removeable_items_list = ['both', 'also', 'that', 'would', 'could', 'immediately', 'usually', 'for',
'when', 'then', 'will', 'which', 'first', 'second', 'third', 'forth',
'fifth', 'internally', 'where', 'while', 'either', 'nither', 'when',
'sever', 'successfully', 'also', 'to', 'above', 'already', 'recently',
'may', 'however', 'can', 'once loaded', 'in fact', 'in this way', 'all',
'actually', 'inadvertently', 'instead', 'when copying themselves',
'automatically', 'should', 'can', 'could', 'necessarily', 'if found',
'randomly', 'again', 'still', 'generally', 'slowly', 'ever', 'shall', 'newly', 'However',
'when executed', 'subsequently']
# lammarizer
for i in range(len(no_zero_nodes_plus_3)):
for index, item in enumerate(no_zero_nodes_plus_3[i]):
if item.split(': ')[0] == 'V':
word = item.split(': ')[1]
no_zero_nodes_plus_3[i][index] = "V: " + WordNetLemmatizer(
).lemmatize(item.split(": ")[1].lower(), 'v')
for i in range(len(no_zero_nodes_plus_3)):
if no_zero_nodes_plus_3[i]:
for index, item in enumerate(no_zero_nodes_plus_3[i]):
if 'ARGM-MOD:' in item:
if item.split(': ', 1)[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'ARGM-ADV:' in item:
if item.split(': ', 1)[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'ARGM-TMP:' in item:
if item.split(': ', 1)[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'ARGM-MNR:' in item:
if item.split(': ', 1)[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'R-ARG1:' in item:
if item.split(': ', 1)[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'R-ARG0:' in item:
if item.split(': ', 1)[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'ARGM-DIS:' in item:
if item.split(': ', 1)[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'ARGM-PRP:' in item:
if item.split(': ', 1)[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
for i in range(len(no_zero_nodes_plus_3)):
if no_zero_nodes_plus_3[i]:
for index, item in enumerate(no_zero_nodes_plus_3[i]):
if 'ARGM-MOD:' in item:
if item.split(': ')[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'ARGM-ADV:' in item:
if item.split(': ')[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'ARGM-TMP:' in item:
if item.split(': ')[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'ARGM-MNR:' in item:
if item.split(': ')[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'R-ARG1:' in item:
if item.split(': ')[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
if 'R-ARG0:' in item:
if item.split(': ')[1].lower() in removeable_items_list:
del no_zero_nodes_plus_3[i][index]
else:
print("##### NEW Exception: ", item)
v_unlink = ['delete', 'clear', 'remove',
'erase', 'wipe', 'purge', 'expunge']
v_write = ['entrench', 'exfiltrate', 'store', 'drop', 'drops', 'install', 'place', 'deploy', 'implant', 'write', 'putfile', 'compose',
'create', 'creates', 'copy', 'copies', 'save', 'saved', 'saves', 'add', 'adds', 'modify', 'modifies', 'append', 'appends']
v_read = ['survey', 'download', 'navigate', 'locate', 'read', 'gather', 'extract', 'extracts',
'obtain', 'acquire', 'check', 'checks', 'detect', 'detects', 'record', 'records']
v_exec = ['use', 'execute', 'executed', 'run', 'ran', 'launch', 'call', 'perform',
'list', 'invoke', 'inject', 'open', 'opened', 'target', 'resume', 'exec']
v_mmap = ['allocate', 'assign']
v_fork = ['clone', 'clones', 'spawned',
'spawn', 'spawns', 'issue', 'set']
v_setuid = ['elevate', 'impersonated']
v_send = ['send', 'sent', 'transfer', 'post', 'postsinformation',
'postsinformations', 'move', 'transmit', 'deliver', 'push', 'redirect', 'redirects']
v_receive = ['receive', 'accept',
'take', 'get', 'gets', 'collect']
v_connect = ['click', 'browse', 'browses', 'connect', 'connected',
'portscan', 'connects', 'alerts', 'communicates', 'communicate']
v_chmod = ['chmod', 'change permission', 'changes permission',
'permision-modifies', 'modifies permission', 'modify permission']
v_load = ['load', 'loads']
v_exit = ['terminate', 'terminates', 'stop', 'stops',
'end', 'finish', 'break off', 'abort', 'conclude']
v_2D = {'collect': ('read', 'receive'),
'open': ('exec', 'fork')}
for i in range(len(no_zero_nodes_plus_3)):
for index, item in enumerate(no_zero_nodes_plus_3[i]):
if item.split(': ')[0] == 'V':
if item.split(': ')[1] in v_unlink:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'unlink'
elif item.split(': ')[1] in v_write:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'write'
elif item.split(': ')[1] in v_read:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'read'
elif item.split(': ')[1] in v_exec:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'exec'
elif item.split(': ')[1] in v_mmap:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'mmap'
elif item.split(': ')[1] in v_fork:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'fork'
elif item.split(': ')[1] in v_setuid:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'setuid'
elif item.split(': ')[1] in v_send:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'send'
elif item.split(': ')[1] in v_receive:
no_zero_nodes_plus_3[i][index] = 'V: ' + \
'receive'
elif item.split(': ')[1] in v_connect:
no_zero_nodes_plus_3[i][index] = 'V: ' + \
'connect'
elif item.split(': ')[1] in v_chmod:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'chmod'
elif item.split(': ')[1] in v_load:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'load'
elif item.split(': ')[1] in v_exit:
no_zero_nodes_plus_3[i][index] = 'V: ' + 'exit'
else:
continue
all_nodes += no_zero_nodes_plus_3
if my_svo_triplet:
all_nodes += my_svo_triplet
print('*****all_nodes:::', all_nodes)
return all_nodes
def fix_srl_spacing(self, li):
new_lst = []
for i in li:
x = []
for j in i:
files = re.findall(load_patterns(
RE_PATTERNS_FILE_PATH)['*_extension'], j)
if files:
for k in files:
j = j.replace(k, re.sub(' ', ' *', k))
x.append(j)
else:
x.append(j)
new_lst.append(x)
return new_lst
def negation_clauses(self, srl_input):
conditional_clause = ['if', 'otherwise', 'when']
purged = []
purged[:] = [
x for x in srl_input if "ARGM-NEG" in str(x) if "if" and "when" not in str(x).lower()]
positive_sentences = [i for i in srl_input if i not in purged]
return positive_sentences
####################################################################################
def group_partials(self, strings):
try:
s = ''
it = iter(sorted(strings))
prev = next(it)
for s in it:
if s.find(prev) == -1:
yield prev
prev = s
yield s
except StopIteration:
return
def process_convert(self, lst):
lst = ["/-" + i.strip() + "-/" for i in lst]
return [i.replace(" ", "-") for i in lst]
####################################################################################
def astriks(self, lis):
apps_process = load_lists(SEC_PATTERNS_FILE_PATH)['APPs-PROCESS']
apps_process = apps_process.replace("'", "").strip('][').split(' , ')
updated_list = [[] for x in range(len(lis))]
for jj, lst in enumerate(lis):
for i in range(len(lst)):
if ":" not in lst[i]:
lst[i] = "TMP: " + lst[i]
leftnode = lst[i].split(":", 1)[0]
rightnode = lst[i].split(":", 1)[1]
lOFioc = iocs.list_of_iocs(rightnode)
found_app = [
app for app in apps_process if app in rightnode.lower()]
if len(found_app) > 1:
found_app = list(self.group_partials(found_app))
found_app = self.process_convert(found_app)
else:
found_app = self.process_convert(found_app)
if not lOFioc:
if leftnode.lower() != "v" and not found_app:
updated_list[jj].append(leftnode + ": *")
elif leftnode.lower() == "v":
updated_list[jj].append(lst[i])
elif leftnode.lower() != "v" and found_app:
for process in found_app:
updated_list[jj].append(leftnode + ": " + process)
elif len(lOFioc) == 1:
updated_list[jj].append(leftnode + ": " + lOFioc[0])
elif len(lOFioc) >= 2:
updated_list[jj].append(leftnode + ": " + lOFioc[0])
for index in range(1, len(lOFioc)):
updated_list[jj].append("ARG-NEW: " + lOFioc[index])
return updated_list
####################################################################################
def triplet_builder(self, mylist):
triplet = []
for i in range(len(mylist)):
xv = mylist[i]
if len(mylist[i]) > 3:
for index, value in enumerate(mylist[i]):
l = len(mylist[i])
if "v" in value.lower() and index != 0 and index != l-1:
for k in mylist[i][:index]:
for j in mylist[i][index+1:]:
triplet.append((k, value, j))
elif len(mylist[i]) == 3:
triplet.append(mylist[i])
return list(map(list, triplet))
####################################################################################