-
Notifications
You must be signed in to change notification settings - Fork 4
/
kpulp.py
407 lines (358 loc) · 15.4 KB
/
kpulp.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
import logging
import re
import argparse
import glob
import json
import time
import sys
import win32evtlog
import win32api
import win32con
import pywintypes
import os
import bunch
import evtx
from datetime import datetime
from lxml import etree
import tzlocal
import pytz
from bs4 import BeautifulSoup
OUTPUT_FORMATS = "json".split(" ")
LANGID = win32api.MAKELANGID(win32con.LANG_NEUTRAL, win32con.SUBLANG_NEUTRAL)
DLLCACHE = {}
DLLMSGCACHE = {}
LOGGER = logging.getLogger("kpulp")
LOGGER.setLevel(logging.INFO)
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def loadDLLsInCache(directory=None):
if directory:
for source in os.listdir(directory):
dirpath = os.path.join(directory, source)
if os.path.isdir(dirpath):
for e in os.listdir(dirpath):
if e.lower().endswith(".dll"):
# dllHandle = loadDLL(dllName)
dllPath = os.path.join(dirpath, e)
LOGGER.debug(
"Loading {} for {}".format(dllPath, source))
if source not in DLLCACHE:
DLLCACHE[source] = {}
try:
dllHandle = loadDLL(dllPath)
DLLCACHE[source][e] = dllHandle
except pywintypes.error as exc:
LOGGER.warn(
"Error loading {}: {}".format(dllPath, exc))
return
keyName = u'SYSTEM\\CurrentControlSet\\Services\\EventLog'
h1 = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyName)
for (typeName, _, __, ___) in win32api.RegEnumKeyEx(h1):
keyName = u'SYSTEM\\CurrentControlSet\\Services\\EventLog\\{}'.format(
typeName)
h2 = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyName)
for (sourceName, _, __, ___) in win32api.RegEnumKeyEx(h2):
keyName = u'SYSTEM\\CurrentControlSet\\Services\\EventLog\\{}\\{}'.format(
typeName, sourceName)
h3 = win32api.RegOpenKeyEx(
win32con.HKEY_LOCAL_MACHINE, keyName, 0, win32con.KEY_READ)
LOGGER.debug("Enumerating {}".format(keyName))
try:
dllNames = win32api.RegQueryValueEx(
h3, "EventMessageFile")[0].split(";")
if sourceName not in DLLCACHE:
DLLCACHE[sourceName] = {}
for dllName in dllNames:
if dllName:
dllHandle = loadDLL(dllName)
DLLCACHE[sourceName][dllName] = dllHandle
except pywintypes.error as e:
if e.args[0] == 2: # value not found
pass
else:
raise e
def loadDLL(dllName):
dllPath = win32api.ExpandEnvironmentStrings(dllName)
LOGGER.debug("Loading library {}".format(dllPath))
dllHandle = win32api.LoadLibraryEx(
dllPath, 0, win32con.LOAD_LIBRARY_AS_DATAFILE)
return dllHandle
def expandString(event):
cachekey = event.SourceName + "/" + str(event.EventID)
try:
if cachekey in DLLMSGCACHE:
dllName = DLLMSGCACHE[cachekey]
if dllName is None:
return ""
dllHandle = DLLCACHE[event.SourceName][dllName]
data = win32api.FormatMessageW(win32con.FORMAT_MESSAGE_FROM_HMODULE,
dllHandle, event.EventID, LANGID, event.StringInserts)
return data
elif event.SourceName not in DLLCACHE:
LOGGER.debug("Event {}/{} not in cache".format(
event.SourceName, event.EventID))
DLLMSGCACHE[cachekey] = None
else:
for (dllName, dllHandle) in DLLCACHE[event.SourceName].items():
try:
data = win32api.FormatMessageW(win32con.FORMAT_MESSAGE_FROM_HMODULE,
dllHandle, event.EventID, LANGID, event.StringInserts)
DLLMSGCACHE[cachekey] = dllName
return data
except win32api.error:
pass # not in this DLL
except SystemError:
pass
except pywintypes.error:
pass
LOGGER.debug("Unable to expand data for {} EventID: {}".format(
event.SourceName, event.EventID))
DLLMSGCACHE[cachekey] = None # no DLLs known to expand this message
# from IPython import embed
# embed()
return ""
def readevents(path):
logHandle = None
try:
logHandle = win32evtlog.OpenBackupEventLog(
None, path) # None=NULL means local host
flags = win32evtlog.EVENTLOG_BACKWARDS_READ | win32evtlog.EVENTLOG_SEQUENTIAL_READ
total = win32evtlog.GetNumberOfEventLogRecords(logHandle)
LOGGER.info("Total number of records for {} is: {}".format(path, total))
# if "security" in path.lower():
# logType = "Security"
# elif "application" in path.lower():
# logType = "Application"
# elif "system" in path.lower():
# logType = "System"
# else:
# LOGGER.error("Unknown log type - put something in path")
# sys.exit(-1)
event_dict = None
local_tz = tzlocal.get_localzone()
while True:
events = win32evtlog.ReadEventLog(logHandle, flags, 0)
if events:
for event in events:
event_dict = {}
# print(event.TimeGenerated)
# from IPython import embed
# embed()
# event_dict['TimeGenerated'] = event.TimeGenerated.strftime("%#c")
dt = local_tz.localize(
event.TimeGenerated).astimezone(pytz.utc)
event_dict['TimeGenerated'] = dt.isoformat()
event_dict['SourceName'] = event.SourceName
# See https://social.msdn.microsoft.com/Forums/sqlserver/en-US/67e49b0b-a9b8-4263-9233-079776f4cbbc/systemdiagnosticseventlogentry-is-showing-wrong-eventid-in-the-eventlogentrymessage-string-?forum=vbgeneral
# EventID might be Instance ID and so we 0xFFFF it to bring back to EventID
event_dict['Id'] = event.EventID & 0xFFFF
event_dict['EventType'] = event.EventType
event_dict['ComputerName'] = event.ComputerName
if event.StringInserts:
event_dict['data'] = "|".join(event.StringInserts)
description = expandString(event)
event_dict['Description'] = description
if description:
event_dict.update(description_to_fields(description))
first_line = description.split("\r\n")[0]
event_dict['Short Description'] = first_line
yield event_dict
else:
break
except pywintypes.error as e:
LOGGER.error(str(e))
if e.winerror == 1722:
LOGGER.error("Check that Windows Event Log service is running")
finally:
# if logHandle is not None:
# win32api.CloseHandle(logHandle)
pass
return
def readeventsXML(path):
try:
parser = evtx.PyEvtxParser(path)
for enventOrder,event in enumerate(parser):
datefmts=[
"%Y-%m-%d %H:%M:%S.%f %Z",
"%Y-%m-%d %H:%M:%S %Z"]
for df in datefmts:
try:
d=datetime.strptime(
event['timestamp'], df).isoformat()
except ValueError:
continue
break
else:
LOGGER.error(f"Unable to parse date '{event['timestamp']}'")
event_dict = {
'TimeGenerated':d
}
ns = {'e': 'http://schemas.microsoft.com/win/2004/08/events/event'}
stringInserts = []
event_dict['EventData'] = {}
try:
parser="lxml"
et = etree.fromstring(event['data']
.encode('utf8')
)
system = et.find("e:System", ns)
event_dict['SourceName'] = system.find("e:Provider", ns).attrib['Name']
event_dict['Id'] = int(system.find("e:EventID", ns).text)
event_dict['EventType'] = system.find("e:Level", ns).text
event_dict['ComputerName'] = system.find("e:Computer", ns).text
eventdata = et.find("e:EventData", ns)
except (KeyError,etree.XMLSyntaxError) as e:
LOGGER.warning(f"{path}:{enventOrder} is damaged, using BeautifulSoup4")
LOGGER.debug(event['data'])
try:
parser="bs"
et=BeautifulSoup(event['data'],"xml")
system = et.find("System")
event_dict['SourceName'] = system.find("Provider")['Name']
event_dict['Id'] = int(system.find("EventID").text)
event_dict['EventType'] = system.find("Level").text
event_dict['ComputerName'] = system.find("Computer").text
eventdata = et.find("EventData")
except (KeyError,AttributeError) as e:
LOGGER.error(f"Parsing using {parser} failed too {e}: {event}")
continue
if eventdata is not None:
if parser=="lxml":
it= eventdata.findall("e:Data", ns)
else:
it = eventdata.findAll("Data")
for elem in it:
try:
if parser=="lxml":
k = elem.attrib['Name']
else:
k = elem['Name']
except KeyError:
# print(event['data'])
k = 'Data'
# An anomalous message, we're probably not handling it well
#raise Exception("Eeek!")
v = elem.text
if v is None:
v = ''
stringInserts.append(v)
event_dict['EventData'][k] = v
else: # Handle UserData and such
# See https://eventlogxp.com/blog/the-fastest-way-to-filter-events-by-description/
try:
eventdata = et[1]
except (KeyError,IndexError) as e:
LOGGER.error(f"Unable to parse event data: {e} {et}")
continue
for e in eventdata.iter():
if len(e.getchildren()) == 0:
tag = e.tag.split("}")[1]
if e.text is None:
val = ''
else:
val = e.text
stringInserts.append(val)
event_dict['EventData'][tag] = val
b = bunch.Bunch()
b.SourceName = event_dict['SourceName']
b.StringInserts = tuple(stringInserts)
b.EventID = event_dict['Id']
event_dict['data'] = stringInserts
try:
description = expandString(b)
except SystemError as e:
LOGGER.warning(str(e))
event_dict['Description'] = description
if description:
event_dict.update(description_to_fields(description))
first_line = description.split("\r\n")[0]
event_dict['Short Description'] = first_line
yield event_dict
except RuntimeError as e:
LOGGER.error(str(e))
def description_to_fields(description):
event_dict = {}
prefix = ''
for l in description.split("\r\n"):
#####
# WHY, oh Why? Well, Imagine the following record sample
###
# An account failed to log on.
# Subject:
# Security ID: S-1-5-21-3333333333-4444444444-5555555555-6666
# Account Name: joebloggs
# Account Domain: DOMAIN
# Logon ID: 0x8be966a
# Logon Type: 2
# Account For Which Logon Failed:
# Security ID: NULL SID
# Account Name: administrator
# Account Domain: SYSEM
###
# See that Security ID and Account Name are mentioned twice? So what we will do
# is we will prefix the first one with "Subject" and 2nd one with Account For Which....
#
m = re.match(r"^([A-Za-z ]+):\s*$", l)
if m: # we've hit a prefix like "Subject:"
prefix = m.group(1)
continue
if prefix and l == '': # end of prefix
prefix = ''
continue
m = re.match(r"^\t*([A-Za-z ]+):\t{1,}(.+)", l)
if m:
(k, v) = m.groups()
if prefix:
new_key = prefix + " " + k
else:
new_key = k
if new_key in event_dict:
LOGGER.warn(
"Key {} already in dict with value: {}".format(
new_key, event_dict[new_key]))
event_dict[new_key] = v
return event_dict
def main():
parser = argparse.ArgumentParser(description='Parse EVTX files')
parser.add_argument('--output', "-o", metavar='OUTPUT', type=str,
help='Destination, if - then STDOUT (default)', default='-')
parser.add_argument('logfiles', metavar='LOGFILE.evtx', type=str, nargs='+',
help='List of logfiles to parse. Will expand wildcards.')
parser.add_argument('--output-format', "-f", type=str, dest="format", choices=OUTPUT_FORMATS,
default="json",
help='Output format, choices are:' + ",".join(OUTPUT_FORMATS))
parser.add_argument('--additional-dlls', type=str, dest="extradllpath",
help='Directory with additoinal DLLs to load (as created by dllraider)')
parser.add_argument('--debug', "-d", action="store_true",
help='Debug level messages')
parser.add_argument('--mode', help="Parsing mode: xml or native", choices=["xml", "native"],
default="native")
args = parser.parse_args()
if args.debug:
LOGGER.setLevel(logging.DEBUG)
if args.output == "-":
output = sys.stdout
else:
output = open(args.output, "w")
loadDLLsInCache(args.extradllpath)
all_logs = [item for sublist in [
glob.glob(k) for k in args.logfiles] for item in sublist]
if args.mode == "native":
parsefunc = readevents
elif args.mode == "xml":
parsefunc = readeventsXML
counter=0
for lf in all_logs:
LOGGER.info("Processing {}".format(lf))
try:
for record in parsefunc(lf):
if args.format == "json":
txt = json.dumps(record)
output.write(txt+"\n")
LOGGER.debug(txt)
except (pywintypes.error,RuntimeError) as e:
LOGGER.error(str(e))
counter+=1
LOGGER.info(f"Processed {counter} out of {len(all_logs)} files")
if __name__ == '__main__':
main()