-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransversals.py
More file actions
291 lines (220 loc) · 7.29 KB
/
transversals.py
File metadata and controls
291 lines (220 loc) · 7.29 KB
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
import argparse
import traceback
import multiprocessing
from itertools import repeat
from utils.logging import Logger
from utils.queries import ESConnection
from utils.tree import splitFile, buildTree
from utils.segment_tree import SegmentTree
from sys import exit
def subTreeToExplore(old, new):
oldS = splitFile(old)
newS = splitFile(new)
l = min(len(oldS), len(newS))
i = 0
while i < l and oldS[i] == newS[i]:
i = i + 1
return oldS[0 : i + 1]
def isBFS(listFiles):
order = list(map(lambda f: len(f.split("/")), listFiles))
greatest = -1
for i in order:
if i < greatest:
return False
greatest = i
return True
def isDFS(listFiles):
tree = buildTree(listFiles)
n = tree.size()
list = [None] * (n + 1)
i = 0
for file in listFiles:
s = splitFile(file)
t = tree.getTree(s, len(s))
idx = t.start
list[idx] = i
i += 1
st = SegmentTree(n, list)
for file in listFiles:
s = splitFile(file)
t = tree.getTree(s, len(s))
b = st.minInRange(t.end + 1, n - 1)
a = st.maxInRange(t.start, t.end)
st.update(t.start, None)
if (a is not None and b is not None) and a > b:
return False
return True
def remove_consecutive_duplicates(lst):
last_seen = None
res = []
for x in lst:
if x != last_seen:
res.append(x)
last_seen = x
return res
def foldersFromFiles(files):
files = list(filter(lambda f: f.startswith("/app/files"), files))
folders = list(map(lambda s: s.rsplit("/", 1)[0], files))
return remove_consecutive_duplicates(folders)
def classification(folders):
res = ""
if isDFS(folders):
res = "DFS"
if isBFS(folders):
if res == "":
res = "BFS"
else:
res = "DFS/BFS"
if res == "":
res = "Unknown"
if folders == []:
res = "No transversal"
return res
def treeToArrayDict(tree, parent=None):
res = (
[{"id": tree.start, "name": tree.node}]
if parent == None
else [{"id": tree.start, "name": tree.node, "parent": parent}]
)
for st in tree.subtrees:
res = res + treeToArrayDict(tree.subtrees[st], parent=tree.start)
return res
def generate_tid_transversal_classification(files, tid):
folders = foldersFromFiles(files)
return {"tid": tid, "transversal": classification(folders)}
def generate_transversal_classification(es_conn, args, logger, index, transversalIndex):
index = "criba_trace_" + args.session
tids = list(map(lambda x: x, es_conn.getTIDsInIndex(index)[0]))
nThreads = len(tids)
logger.info(f"Found {nThreads} threads")
files = {}
for tid in tids:
files[tid] = es_conn.getOpenPaths(index, tid, args.size, args.maxQueries)
# Create a list of tuples corresponding to the arguments to be passed to
# generate_tid_transversal_classification in each subprocess
functionArgs = [(v, k) for k, v in files.items()]
with multiprocessing.Pool(args.nProc) as pool:
result = list(
pool.starmap(generate_tid_transversal_classification, functionArgs)
)
id=0
for doc in result:
if args.debug:
logger.info(doc)
doc["session_name"] = args.session
es_conn.docIndex(transversalIndex, doc, args.session + "_tid_" + str(doc["tid"]))
id += 1
def generate_file_system_tree(es_conn, args, logger, transversalIndex, bulk_size=1000):
index = "criba_trace_" + args.session
logger.info(f"Beginning filesystem tree generation on index {args.session}")
files = es_conn.getAbsoluteFilePaths(index, args.size)[0]
logger.info(f"File paths gathered")
tree = buildTree(files)
tree.numerate(0)
arr = treeToArrayDict(tree)
logger.info(f"Tree computed. Saving to Elastic Search")
id = 0
bulk = []
bulk_start_index = 0
bulk_end_index = 0
if not args.local:
for node in arr:
id += 1
if len(bulk) == bulk_size:
es_conn.bulkIndex(bulk, bulk_start_index, transversalIndex, args.session)
logger.debug("bulking {} records ({} to {})...".format(len(bulk), bulk_start_index, bulk_end_index))
bulk_start_index = bulk_end_index
bulk = []
doc = {}
if "parent" in node:
doc = {"id": node["id"], "session_name": args.session, "name": node["name"], "parent": node["parent"]}
else:
doc = {"id": node["id"], "session_name": args.session, "name": node["name"]}
bulk.append(doc)
bulk_end_index += 1
if len(bulk) > 0:
es_conn.bulkIndex(bulk, bulk_start_index, transversalIndex, args.session)
logger.info("bulking {} records ({} to {})...".format(len(bulk), bulk_start_index, bulk_end_index))
bulk_start_index = bulk_end_index
bulk = []
def main():
parser = argparse.ArgumentParser(
description="Search for syscalls sequences in ElasticSearch."
)
parser.add_argument(
'-u',
"--url",
metavar="url",
default="http://192.168.112.77:31111/",
type=str,
help="elasticSearch URL",
)
parser.add_argument(
'-d',
"--debug",
action="store_true",
help="Whether to send results to stdout (debug) or to ES (not debug)",
)
parser.add_argument(
"--omit-index",
action="store_true",
help="Whether to omit the creation of the new index",
)
parser.add_argument(
'-p',
"--nProc",
metavar="nProc",
default=1,
type=int,
help="Number of processes to use when classifying transversals",
)
parser.add_argument(
'-sz',
"--size",
metavar="size",
default=1000,
type=int,
help="Number of events to fetch at once",
)
parser.add_argument(
"-mq",
"--maxQueries",
metavar="maxQueries",
default=1000,
type=int,
help="Maximum number of queries to make for one operation",
)
parser.add_argument(
'-l',
'--local',
action='store_true',
default=False,
help='Run locally only (no ES)'
)
parser.add_argument(
'session',
help='session name',
default=None,
nargs='?'
)
logger = Logger("TRANSVERSALS")
args = parser.parse_args()
try:
if args.debug:
logger.setLevel("debug")
es_conn = ESConnection(args.url)
transversalIndex = "criba_transversals"
if not args.omit_index and not args.debug:
logger.info(f"Creating new index {transversalIndex}")
es_conn.createNewIndex(transversalIndex)
generate_file_system_tree(es_conn, args, logger, transversalIndex, args.size)
logger.info("Saved to Elastic. Beginning transversal classification")
generate_transversal_classification(
es_conn, args, logger, args.session, transversalIndex
)
logger.info("Script finished")
except Exception as e:
logger.error("Got an unexpected error: %s" % e)
traceback.print_exception(type(e), e, e.__traceback__)
if __name__ == "__main__":
main()