-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathload.py
More file actions
178 lines (168 loc) · 7.91 KB
/
load.py
File metadata and controls
178 lines (168 loc) · 7.91 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
#!/usr/bin/env python
# coding: utf-8
import json
import pandas
import datetime
import time
import re
import ipaddress
import math
def sort_by_field(sort_list, sort_field):
sorted_list = sorted(sort_list, key=lambda k: k[sort_field])
return sorted_list
def check_ip_format(ip_address):
version = ipaddress.ip_address(ip_address).version
if version == 6:
# ipv6
addr = ipaddress.ip_address(ip_address)
#int_ip = int(ipaddress.IPv6Address(addr.exploded))
return addr.exploded
else:
# ipv4
#int_ip = int(ipaddress.IPv4Address(ip_address))
return ip_address
def load_conn(f, idx, attacker_ip):
cnt = 0
conn = []
anomaly_idx = []
for line in f.readlines():
tmp = {}
# destination ip = attacker ip -> anomaly
log = json.loads(line)
if '_source' in log.keys():
keys = [
'ts', 'id_orig_h', 'id_orig_p', 'id_resp_h', 'id_resp_p',
'uid', 'service', 'proto', 'duration', 'orig_bytes',
'resp_bytes', 'orig_ip_bytes', 'resp_pkts', 'resp_ip_bytes'
]
check = [
'duration', 'orig_bytes', 'resp_bytes', 'orig_ip_bytes',
'resp_pkts', 'resp_ip_bytes'
]
for key in keys:
if key in log['_source'].keys():
if key == 'ts':
tmp['time'] = datetime.datetime.strptime(
(log['_source']['@timestamp'].replace(
'T', ' '))[:-1], '%Y-%m-%d %H:%M:%S.%f')
elif key == 'id_orig_h':
ipa = check_ip_format(log['_source'][key])
tmp[key] = ipa
elif key == 'id_resp_h':
ipa = check_ip_format(log['_source'][key])
if str(ipa).strip() == attacker_ip:
anomaly_idx.append(idx)
tmp[key] = ipa
elif key in check:
# not exist -> replace it to 0
if log['_source'][key] == '-':
tmp[key] = 0
else:
tmp[key] = log['_source'][key]
else:
tmp[key] = log['_source'][key]
# add an index to each log
tmp['idx'] = idx
idx += 1
conn.append(tmp)
conn = sort_by_field(conn,'time')
return conn, anomaly_idx, idx
def load_system(f, idx):
# system
system = []
# network
security_5156 = []
sysmon_3 = []
# traceback
sysmon = []
microsoft_powershell = []
powershell = []
security_trace = []
for line in f.readlines():
tmp = {}
log = json.loads(line)
# system
system.append(log)
# traceback
if log['_source']['winlog']['provider_name']=='Microsoft-Windows-Sysmon':
sysmon.append(log['_source'])
elif log['_source']['winlog']['provider_name']=='Microsoft-Windows-PowerShell':
microsoft_powershell.append(log['_source'])
elif log['_source']['winlog']['provider_name']=='PowerShell':
powershell.append(log['_source'])
elif 'Security' in log['_source']['winlog']['provider_name']:
security_trace.append(log['_source'])
# network map to security and sysmon
if '_source' in log.keys():
if 'event' in log['_source'].keys():
if log['_source']['event'][
'provider'] == 'Microsoft-Windows-Security-Auditing':
# check security event code 5156
if log['_source']['event']['code'] == 5156:
tmp['time'] = datetime.datetime.strptime(
(log['_source']['@timestamp'].replace(
'T', ' '))[:-1], '%Y-%m-%d %H:%M:%S.%f')
tmp['ProcessID'] = log['_source']['winlog'][
'event_data']['ProcessID']
if 'Protocol' in log['_source']['winlog'][
'event_data'].keys():
if log['_source']['winlog']['event_data'][
'Protocol'] == '6':
tmp['Protocol'] = 'tcp'
elif log['_source']['winlog']['event_data'][
'Protocol'] == '17':
tmp['Protocol'] = 'udp'
elif (log['_source']['winlog']['event_data']
['Protocol'] == '58'
or log['_source']['winlog']['event_data']
['Protocol'] == '2'):
tmp['Protocol'] = 'icmp'
else:
tmp['Protocol'] = log['_source']['winlog'][
'event_data']['Protocol']
tmp['SourceIp']= check_ip_format(
log['_source']['winlog']['event_data']
['SourceAddress'])
tmp['SourcePort'] = log['_source']['winlog'][
'event_data']['SourcePort']
tmp['DestinationIp'] = check_ip_format(
log['_source']['winlog']['event_data']
['DestAddress'])
tmp['DestinationPort'] = log['_source']['winlog'][
'event_data']['DestPort']
#### message
tmp['message'] = log['_source']['message']
####
tmp['idx'] = idx
tmp['record_id'] = log['_source']['winlog']['record_id']
tmp['ProcessId'] = log['_source']['winlog']['event_data']['ProcessID']
security_5156.append(tmp)
elif 'provider' in log['_source']['event']:
if log['_source']['event'][
'provider'] == 'Microsoft-Windows-Sysmon':
# check sysmon event code 3
if log['_source']['event']['code'] == 3:
keys = [
'UtcTime', 'ProcessId', 'Protocol', 'SourceIp',
'SourcePort', 'DestinationIp',
'DestinationPort'
]
message = log['_source']['message'].split('\n')
for msg in message:
content = msg.split(':', 1)
if content[0] == 'UtcTime':
tmp['time'] = datetime.datetime.strptime(
(log['_source']['@timestamp'].replace(
'T',
' '))[:-1], '%Y-%m-%d %H:%M:%S.%f')
elif content[0] == 'SourceIp' or content[
0] == 'DestinationIp':
tmp[content[0]] = check_ip_format(
str(content[1].strip()))
elif content[0] in keys:
tmp[content[0]] = content[1].strip()
tmp['record_id'] = log['_source']['winlog']['record_id']
tmp['idx'] = idx
sysmon_3.append(tmp)
idx += 1
return system, sysmon_3, security_5156, sysmon, microsoft_powershell,powershell,security_trace, idx