-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate_cdm_to_streamspot.py
409 lines (354 loc) · 18.3 KB
/
translate_cdm_to_streamspot.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
#!/usr/bin/env python
import argparse
import json
import pdb
from pykafka import KafkaClient
from pykafka.exceptions import OffsetOutOfRangeError, RequestTimedOut
from pykafka.partitioners import HashingPartitioner
import sys
from tc.schema.serialization import Utils
from tc.schema.records.parsing import CDMParser
from tc.schema.serialization.kafka import KafkaAvroGenericDeserializer
from constants import *
parser = argparse.ArgumentParser()
parser.add_argument('--url', help='Input filename or Kafka URL', required=True)
parser.add_argument('--format', help='Consume Avro or JSON serialised CDM',
choices=['avro', 'json'], required=True)
parser.add_argument('--source', help='Consume from Kafka or a file',
choices=['kafka', 'file'], required=True)
parser.add_argument('--concise', help='Single-byte types, needed by StreamSpot',
required=False, action='store_true')
parser.add_argument('--kafka-topic', help='Kafka topic to consume from',
required=False)
parser.add_argument('--kafka-group', help='Kafka consumer group', required=False)
args = vars(parser.parse_args())
input_url = args['url']
input_source = args['source']
input_format = args['format']
uuid_to_pid = {}
uuid_to_pname = {}
uuid_to_url = {}
uuid_to_sockid = {}
uuid_to_addr = {}
pid_to_graph_id = {}
current_graph_id = 0
filename_to_dest_id = {}
current_dest_id = 0
def print_streamspot_edge(streamspot_edge, concise):
# HACK: Don't print self-loop edges
if concise and streamspot_edge['source_id'] == streamspot_edge['dest_id']:
return
if not concise:
print str(streamspot_edge['source_id']) + '\t' +\
str(streamspot_edge['source_name']) + '\t' +\
str(streamspot_edge['source_type']) + '\t' +\
str(streamspot_edge['dest_id']) + '\t' +\
str(streamspot_edge['dest_name']) + '\t' +\
str(streamspot_edge['dest_type']) + '\t' +\
str(streamspot_edge['edge_type']) + '\t' +\
str(streamspot_edge['graph_id'])
else:
print str(streamspot_edge['source_id']) + '\t' +\
type_map[streamspot_edge['source_type']] + '\t' +\
str(streamspot_edge['dest_id']) + '\t' +\
type_map[streamspot_edge['dest_type']] + '\t' +\
type_map[streamspot_edge['edge_type']] + '\t' +\
str(streamspot_edge['graph_id'])
def read_field(object, format):
if format == 'avro':
return str(object)
elif format == 'json':
if type(object) is int:
return object
return object.encode('utf-8')
else:
print 'Unknown format:', format
sys.exit(-1)
# initialise empty streamspot edge
event_metadata_buffer = {} # filled/cleared on every new event
streamspot_edge = {'event_uuid': None,
'source_id': None,
'source_name': None,
'source_type': None,
'dest_id': None,
'dest_name': 'NA',
'dest_type': None,
'edge_type': None,
'graph_id': None
} # filled/cleared on every new event
# setup input source and format
if input_source == 'kafka':
if not input_format == 'avro':
print 'Input format must be Avro for Kafka source'
sys.exit(-1)
if args['kafka_topic'] is None:
print 'Argument --kafka-topic is required'
sys.exit(-1)
if args['kafka_group'] is None:
print 'Argument --kafka-group is required'
sys.exit(-1)
kafka_client = KafkaClient(input_url)
kafka_topic = kafka_client.topics[args['kafka_topic']]
consumer = kafka_topic.get_balanced_consumer(
consumer_group=args['kafka_group'], auto_commit_enable=True,
auto_commit_interval_ms=1000, reset_offset_on_start=False,
consumer_timeout_ms=100, fetch_wait_max_ms=0, managed=True)
schema = Utils.load_schema(SCHEMA_FILE)
deserializer = KafkaAvroGenericDeserializer(schema, schema)
parser = CDMParser(schema)
f = consumer
elif input_source == 'file':
if input_format == 'avro':
ifile = open(input_url, 'rb')
schema = Utils.load_schema(SCHEMA_FILE)
deserializer = KafkaAvroGenericDeserializer(schema, input_file=ifile)
parser = CDMParser(schema)
f = deserializer.deserialize_from_file()
elif input_format == 'json':
f = open(input_url, 'r')
# process records
lineno = 0
while True:
try:
for line in f:
if input_format == 'json':
cdm_record = json.loads(line.strip())
cdm_record_type = cdm_record['datum'].keys()[0]
cdm_record_values = cdm_record['datum'][cdm_record_type]
elif input_format == 'avro':
if input_source == 'kafka':
if line.value is None:
continue
cdm_record = deserializer.deserialize(args['kafka_topic'],
line.value)
elif input_source == 'file':
cdm_record = line
cdm_record_type = 'com.bbn.tc.schema.avro.' +\
parser.get_record_type(cdm_record)
cdm_record_values = cdm_record['datum']
lineno += 1
#print lineno, cdm_record_type, cdm_record
if cdm_record_type == CDM_TYPE_PRINCIPAL:
continue # we don't care about PRINCIPALs
elif cdm_record_type == CDM_TYPE_SRCSINK:
# treat this like a file with filename UUID
uuid = read_field(cdm_record_values['uuid'], input_format)
if not uuid in filename_to_dest_id:
filename_to_dest_id[uuid] = current_dest_id
while current_dest_id in filename_to_dest_id.values():
current_dest_id += 1
elif cdm_record_type == CDM_TYPE_SUBJECT:
uuid = read_field(cdm_record_values['uuid'], input_format)
subject_type = read_field(cdm_record_values['type'], input_format)
if subject_type == 'SUBJECT_PROCESS':
pid = read_field(cdm_record_values['pid'], input_format)
ppid = read_field(cdm_record_values['ppid'], input_format)
if input_format == 'avro':
pname = read_field(cdm_record_values['properties']['name'],
input_format)
unitid = read_field(cdm_record_values['unitId'], input_format)
elif input_format == 'json':
pname = read_field(cdm_record_values['properties']['map']['name'],
input_format)
unitid = read_field(cdm_record_values['unitId']['int'],
input_format)
#uuid_to_pid[uuid] = str(pid) + '/' + pname + '/' + str(unitid)
uuid_to_pid[uuid] = pid
uuid_to_pname[uuid] = pname
if not pid in pid_to_graph_id: # pid has no gid assigned
if not ppid in pid_to_graph_id: # ppid has no gid assigned
pid_to_graph_id[pid] = current_graph_id
pid_to_graph_id[ppid] = current_graph_id
current_graph_id += 1
else: # parent has a gid assigned
pid_to_graph_id[pid] = pid_to_graph_id[ppid]
else:
print "Unknown subject type:", subject_type
sys.exit(-1)
elif cdm_record_type == CDM_TYPE_FILE:
uuid = read_field(cdm_record_values['uuid'], input_format)
url = read_field(cdm_record_values['url'], input_format)
uuid_to_url[uuid] = url
if not url in filename_to_dest_id:
filename_to_dest_id[url] = current_dest_id
while current_dest_id in filename_to_dest_id.values():
current_dest_id += 1
elif cdm_record_type == CDM_TYPE_SOCK:
uuid = read_field(cdm_record_values['uuid'], input_format)
src = read_field(cdm_record_values['srcAddress'], input_format)
dest = read_field(cdm_record_values['destAddress'], input_format)
src_port = read_field(cdm_record_values['srcPort'], input_format)
dest_port = read_field(cdm_record_values['destPort'], input_format)
sock_id = src + ':' + str(src_port) + ':' + dest + ':' + str(dest_port)
if not sock_id in filename_to_dest_id:
filename_to_dest_id[sock_id] = current_dest_id
while current_dest_id in filename_to_dest_id.values():
current_dest_id += 1
uuid_to_sockid[uuid] = sock_id
elif cdm_record_type == CDM_TYPE_MEM:
uuid = read_field(cdm_record_values['uuid'], input_format)
addr = read_field(cdm_record_values['memoryAddress'], input_format)
uuid_to_addr[uuid] = addr
elif cdm_record_type == CDM_TYPE_EVENT:
# print previous streamspot edge if it is ready
if not None in streamspot_edge.values():
print_streamspot_edge(streamspot_edge, args['concise'])
# clear old edge data
streamspot_edge = {'event_uuid': None,
'source_id': None,
'source_name': None,
'source_type': None,
'dest_id': None,
'dest_name': 'NA',
'dest_type': None,
'edge_type': None,
'graph_id': None
}
uuid = read_field(cdm_record_values['uuid'], input_format)
event_type = read_field(cdm_record_values['type'], input_format)
streamspot_edge['edge_type'] = event_type
streamspot_edge['event_uuid'] = uuid # to map metadata
elif cdm_record_type == CDM_TYPE_EDGE:
edge_type = read_field(cdm_record_values['type'], input_format)
if edge_type == 'EDGE_SUBJECT_HASLOCALPRINCIPAL':
pass
elif edge_type == 'EDGE_OBJECT_PREV_VERSION':
pass
elif edge_type == 'EDGE_FILE_AFFECTS_EVENT':
# HACK! FIXME
# Special case for
# - EVENT_UPDATE
# - EVENT_RENAME
if streamspot_edge['edge_type'] == 'EVENT_UPDATE' or \
streamspot_edge['edge_type'] == 'EVENT_RENAME':
assert read_field(cdm_record_values['toUuid'],
input_format) == \
streamspot_edge['event_uuid']
from_uuid = read_field(cdm_record_values['fromUuid'],
input_format)
url = uuid_to_url[from_uuid]
dest_id = filename_to_dest_id[url]
streamspot_edge['dest_id'] = dest_id
streamspot_edge['dest_name'] = url
streamspot_edge['dest_type'] = 'OBJECT_FILE'
else:
assert read_field(cdm_record_values['fromUuid'],
input_format) == \
streamspot_edge['event_uuid']
to_uuid = read_field(cdm_record_values['toUuid'],
input_format)
url = uuid_to_url[to_uuid]
dest_id = filename_to_dest_id[url]
streamspot_edge['dest_id'] = dest_id
streamspot_edge['dest_name'] = url
streamspot_edge['dest_type'] = 'OBJECT_FILE'
elif edge_type == 'EDGE_EVENT_AFFECTS_FILE':
assert read_field(cdm_record_values['fromUuid'],
input_format) ==\
streamspot_edge['event_uuid']
to_uuid = read_field(cdm_record_values['toUuid'], input_format)
url = uuid_to_url[to_uuid]
dest_id = filename_to_dest_id[url]
streamspot_edge['dest_id'] = dest_id
streamspot_edge['dest_name'] = url
streamspot_edge['dest_type'] = 'OBJECT_FILE'
elif edge_type == 'EDGE_MEMORY_AFFECTS_EVENT':
assert read_field(cdm_record_values['fromUuid'],
input_format) ==\
streamspot_edge['event_uuid']
to_uuid = read_field(cdm_record_values['toUuid'], input_format)
addr = uuid_to_addr[to_uuid]
streamspot_edge['dest_id'] = addr
streamspot_edge['dest_name'] = addr
streamspot_edge['dest_type'] = 'OBJECT_MEM'
elif edge_type == 'EDGE_EVENT_AFFECTS_MEMORY':
assert read_field(cdm_record_values['fromUuid'],
input_format) ==\
streamspot_edge['event_uuid']
to_uuid = read_field(cdm_record_values['toUuid'], input_format)
addr = uuid_to_addr[to_uuid]
streamspot_edge['dest_id'] = addr
streamspot_edge['dest_name'] = addr
streamspot_edge['dest_type'] = 'OBJECT_MEM'
elif edge_type == 'EDGE_EVENT_AFFECTS_NETFLOW':
assert read_field(cdm_record_values['fromUuid'],
input_format) ==\
streamspot_edge['event_uuid']
to_uuid = read_field(cdm_record_values['toUuid'], input_format)
sock_id = uuid_to_sockid[to_uuid]
dest_id = filename_to_dest_id[sock_id]
streamspot_edge['dest_id'] = dest_id
streamspot_edge['dest_name'] = sock_id
streamspot_edge['dest_type'] = 'OBJECT_SOCK'
elif edge_type == 'EDGE_EVENT_AFFECTS_SRCSINK':
assert read_field(cdm_record_values['fromUuid'],
input_format) ==\
streamspot_edge['event_uuid']
to_uuid = read_field(cdm_record_values['toUuid'], input_format)
dest_id = filename_to_dest_id[to_uuid]
streamspot_edge['dest_id'] = dest_id
streamspot_edge['dest_name'] = dest_id
streamspot_edge['dest_type'] = 'OBJECT_SRCSINK'
elif edge_type == 'EDGE_SRCSINK_AFFECTS_EVENT':
assert read_field(cdm_record_values['fromUuid'],
input_format) ==\
streamspot_edge['event_uuid']
to_uuid = read_field(cdm_record_values['toUuid'], input_format)
dest_id = filename_to_dest_id[to_uuid]
streamspot_edge['dest_id'] = dest_id
streamspot_edge['dest_name'] = dest_id
streamspot_edge['dest_type'] = 'OBJECT_SRCSINK'
elif edge_type == 'EDGE_EVENT_AFFECTS_SUBJECT' or \
edge_type == 'EDGE_EVENT_ISGENERATEDBY_SUBJECT':
assert read_field(cdm_record_values['fromUuid'],
input_format) ==\
streamspot_edge['event_uuid']
to_uuid = read_field(cdm_record_values['toUuid'], input_format)
pid = uuid_to_pid[to_uuid]
pname = uuid_to_pname[to_uuid]
if edge_type == 'EDGE_EVENT_AFFECTS_SUBJECT':
streamspot_edge['dest_id'] = pid
streamspot_edge['dest_name'] = pname
streamspot_edge['dest_type'] = 'SUBJECT_PROCESS'
elif edge_type == 'EDGE_EVENT_ISGENERATEDBY_SUBJECT':
streamspot_edge['source_id'] = pid
streamspot_edge['source_name'] = pname
streamspot_edge['source_type'] = 'SUBJECT_PROCESS'
# graph ID assignment to streamspot edge
if edge_type == 'EDGE_EVENT_ISGENERATEDBY_SUBJECT':
streamspot_edge['graph_id'] = \
pid_to_graph_id[streamspot_edge['source_id']]
# handle graph ID change on EXECUTE
if streamspot_edge['edge_type'] == 'EVENT_EXECUTE':
pid_to_graph_id[streamspot_edge['source_id']] = \
current_graph_id # change graph ID of caller process
current_graph_id += 1
else:
print 'Unknown edge type:', edge_type
sys.exit(-1)
else:
print 'Unknown CDM record type', cdm_record_type
sys.exit(-1)
# for line in f
# last event in buffer
if not None in streamspot_edge.values():
print_streamspot_edge(streamspot_edge, args['concise'])
streamspot_edge = {'event_uuid': None,
'source_id': None,
'source_name': None,
'source_type': None,
'dest_id': None,
'dest_name': 'NA',
'dest_type': None,
'edge_type': None,
'graph_id': None
}
except RequestTimedOut:
continue # continue waiting
except OffsetOutOfRangeError:
continue # ignore error
# try
sys.stdout.flush()
if input_source == "file":
break
# while True