This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathevent2timeline.py
286 lines (204 loc) · 9.03 KB
/
event2timeline.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
########################################################################################################
# #
# Event2Timeline by @tomchop_ for CERT Societe Generale (@CertSG). Some features added by @Jipe_ #
# #
# This work is licensed under the GPL License #
# http://www.gnu.org/licenses/gpl.txt #
# #
# https://cert.societegenerale.com/ - https://github.com/certsocietegenerale/event2timeline/ #
# #
########################################################################################################
### /!\ You must modify the USERNAME_STRING to match the "username" strings in your langage! /!\ ###
import csv, sys, re
import optparse
import datetime
import calendar
import mmap
import contextlib
from dateutil.parser import parse
from Evtx.Evtx import FileHeader
from Evtx.Views import evtx_file_xml_view
__description__ = "Event2Timeline"
__version__ = "0.0.2"
eid_regex = re.compile('<EventID Qualifiers="(?P<qualifiers>.*)">(?P<eid>\d+)</EventID>')
sessid_regex = re.compile('<Data Name="TargetLogonId">(?P<session_id>0x[0-9a-fA-F]+)</Data>')
time_regex = re.compile('SystemTime="(?P<time>.*)"')
EVTX_LOGIN = [
4624, # An account was successfully logged on
4778, # RDP - Session connceted / reconnected
]
EVTX_LOGOFF = [
4647, # User initiated logoff
4634, # An account was logged off
4779, # RDP - Session disconnected
]
EVT_LOGIN = [i-4096 for i in EVTX_LOGIN] + [540] # Successful network logon (=4624 in EVTX)
EVT_LOGOFF = [i-4096 for i in EVTX_LOGOFF]
USERNAME_STRING = 'tilisateur' ###### MODIFY THIS TO WHICHEVER LANGUAGE YOUR CSV FILE IS ######
def get_data(xml, name):
rex = re.compile('<Data Name="%s">(?P<%s>.*)</Data>' % (name, name))
try:
return rex.search(xml).group(name)
except Exception, e:
return None
def import_xml(filename):
# 4624 - Login 528
# 4647 - Logoff 551
#[*] Keys: Category, Description, Data, Domain\User, Date&Time, Source, Computer, Time, Date, Type, Event
sessions = {}
user_sessions = {}
count = 0
with open(filename, 'r') as f:
print "[*] Reading EVTX file %s" % filename
with contextlib.closing(mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)) as buf:
fh = FileHeader(buf, 0x0)
count = 0
for xml, record in evtx_file_xml_view(fh):
if (count % 2000) == 0:
print "%s records read" % count
count +=1
match = eid_regex.search(xml)
eid = int(match.group('eid'))
session_id = get_data(xml, 'TargetLogonId')
# Insert new session in dictionary
if sessions.get(session_id, None) == None:
sessions[session_id] = {}
if eid in EVTX_LOGIN:
if session_id:
sessions[session_id] = {}
else:
continue
info = {}
info['logon_type'] = get_data(xml, 'LogonType')
info['eid'] = str(eid)
info['ip'] = get_data(xml, 'IpAddress') + ':' + get_data(xml, 'IpPort')
info['datetime'] = parse(time_regex.search(xml).group('time')[:-7])
sessions[session_id][str(eid)] = info
username = get_data(xml, 'TargetDomainName') + '\\' + get_data(xml, 'TargetUserName')
sessions[session_id]['username'] = username
elif eid in EVTX_LOGOFF:
# Ignore if orphan session
if not sessions.get(session_id, None) == None:
continue
info = {}
info['eid'] = str(eid)
info['datetime'] = parse(time_regex.search(xml).group('time')[:-7])
sessions[session_id][str(eid)] = info
return sessions
def import_csv(filename, delimiter=';', quotechar='"'):
with open (filename, 'rU') as csvfile:
logs = []
print "[*] Reading CSV file %s" % filename
logreader = csv.DictReader(csvfile, delimiter=delimiter, quotechar=quotechar)
# Audit Success;23/05/2013;09:00:00;23/05/2013 09:00:00;538;Security;Ouverture/Fermeture de session;\S-1-5-21-2052699199-3915784498-1582209984-43253;USER01;"Fermeture de la session utilisateur : Utilisateur : username Domaine : userdomain Id. de la session : (0x0,0xB38D21AB) Type de session : 4";
for log in logreader:
logs.append(log)
print "[*] %s lines imported" % (len(logs))
print '[*] Keys: %s' % ", ".join([i for i in logs[0]])
# return logs[::-1]
sessions = {}
user_sessions = {}
count = 0
for log in logs:
count += 1
try:
session_id = re.search('(?P<session_id>0x([0-9a-fA-F]{2,}))', log['Description']).group('session_id')
except Exception, e:
continue
if sessions.get(session_id, None) == None:
sessions[session_id] = {}
if int(log['Event']) in EVT_LOGIN:
info = {}
info['eid'] = log['Event']
info['datetime'] = parse(log['Date&Time'])
sessions[session_id][log['Event']] = info
username = re.search("%s\W+(?P<username>[\w\.\-$]+)\n" %USERNAME_STRING, log['Description']).group('username')
sessions[session_id]['username'] = username
elif int(log['Event']) in EVT_LOGOFF:
if sessions.get(session_id, None) == None: # Avoid orphan sessions
continue
info = {}
info['eid'] = log['Event']
info['datetime'] = parse(log['Date&Time'])
sessions[session_id][log['Event']] = info
print "[*] %s sessions found (%s lines parsed)" % (len(sessions), count)
return sessions
def sessions2timeline(sessions):
# generate session list by user
user_sessions = {}
for sid in [s for s in sessions if sessions[s].get('username', None) != None]:
s = sessions[sid]
if user_sessions.get(s['username'], None) == None:
user_sessions[s['username']] = {}
user_sessions[s['username']][sid] = s
print "[*] Unique users: %s" % len(user_sessions)
lanes = [u for u in user_sessions]
items = []
for i, username in enumerate(user_sessions):
for user_session in get_sessions(user_sessions[username]):
if user_session['end'] - user_session['start'] < datetime.timedelta(seconds=10):
user_session['end'] = user_session['start'] + datetime.timedelta(seconds=10)
items.append({'info': user_session['info'],'lane': i, 'start': str(user_session['start']), 'end': str(user_session['end'])})
time_begin = min([i['start'] for i in items])
time_end = max([i['end'] for i in items])
return {'time_begin': time_begin, 'time_end': time_end, 'items': items, 'lanes': lanes}
def get_sessions(user_sessions):
sessions = []
for sid in user_sessions:
s = user_sessions[sid]
start, end = None, None
for evt in s:
if evt != 'username':
if int(evt) in EVT_LOGIN or int(evt) in EVTX_LOGIN: # deal with a login event- look for the smallest date for session start
if start == None:
start = s[evt]['datetime']
elif s[evt]['datetime'] < start:
start = s[evt]['datetime']
if int(evt) in EVT_LOGOFF or int(evt) in EVTX_LOGOFF: # deal with a logoff event- look for the biggest date for session start
if end == None:
end = s[evt]['datetime']
elif s[evt]['datetime'] > end:
end = s[evt]['datetime']
if end == None:
end = start
if start == None:
start = end
# remove datetime object, which does not parse well to JS
for i in s:
try:
s[i].pop('datetime')
except Exception, e:
pass
sessions.append({'start': start, 'end': end, 'info': s})
return sessions
def print_log(log):
for key in log:
for entry in log[key]:
for k in entry:
print "%s:\n%s\n" % (k, entry[k])
else:
print "%s:\n%s\n" % (key, log[key])
if __name__ == '__main__':
Parser = optparse.OptionParser(usage='usage: %prog [-c | -e] -f eventlogfile')
Parser.add_option('-f', '--filename', dest="eventlogfile", help='path to the evenlog file')
Parser.add_option('-c', '--csv', action="store_true", default=False, help='Specify the events are in CSV format (for an exported .evt)')
Parser.add_option('-e', '--evtx', action="store_true", default=False, help='Specify the events are in EVTX format (for a native .evtx)')
(options, args) = Parser.parse_args()
if not options.eventlogfile:
Parser.error("You must specify a filename")
if options.csv:
sessions = import_csv(options.eventlogfile)
elif options.evtx:
sessions = import_xml(options.eventlogfile)
else:
Parser.error("You must specify a file format format (csv or xml)")
timeline = sessions2timeline(sessions)
print "[*] Mapped %s sessions from %s to %s" % (len(timeline['items']), timeline['time_begin'], timeline['time_end'])
js = open('timeline/evtdata.js','w+')
js.write("var lanes = %s,\n" % str(timeline['lanes']))
js.write("laneLength = lanes.length,\n")
js.write("items = %s,\n" % timeline['items'])
js.write("timeBegin = \'%s\',\n" % timeline['time_begin'])
js.write("timeEnd = \'%s\',\n" % timeline['time_end'])
js.write("filename = \'%s\';\n" % options.eventlogfile)
js.close()