-
Notifications
You must be signed in to change notification settings - Fork 163
/
Evtx.py
514 lines (439 loc) · 17 KB
/
Evtx.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/usr/bin/python
# This file is part of python-evtx.
#
# Copyright 2012, 2013 Willi Ballenthin <william.ballenthin@mandiant.com>
# while at Mandiant <http://www.mandiant.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Version v.0.3.0
from __future__ import absolute_import
import re
import sys
import mmap
import binascii
import logging
from functools import wraps
import Evtx.Views as e_views
from .Nodes import RootNode
from .Nodes import TemplateNode
from .Nodes import NameStringNode
from .BinaryParser import Block
from .BinaryParser import ParseException
logger = logging.getLogger(__name__)
class InvalidRecordException(ParseException):
def __init__(self):
super(InvalidRecordException, self).__init__(
"Invalid record structure")
class Evtx(object):
"""
A convenience class that makes it easy to open an
EVTX file and start iterating the important structures.
Note, this class must be used in a context statement
(see the `with` keyword).
Note, this class will mmap the target file, so ensure
your platform supports this operation.
"""
def __init__(self, filename):
"""
@type filename: str
@param filename: A string that contains the path
to the EVTX file to open.
"""
self._filename = filename
self._buf = None
self._f = None
self._fh = None
def __enter__(self):
self._f = open(self._filename, "rb")
self._buf = mmap.mmap(self._f.fileno(), 0, access=mmap.ACCESS_READ)
self._fh = FileHeader(self._buf, 0x0)
return self
def __exit__(self, type, value, traceback):
self._buf.close()
self._f.close()
self._fh = None
def ensure_contexted(func):
"""
This decorator ensure that an instance of the
Evtx class is used within a context statement. That is,
that the `with` statement is used, or `__enter__()`
and `__exit__()` are called explicitly.
"""
@wraps(func)
def wrapped(self, *args, **kwargs):
if self._buf is None:
raise TypeError("An Evtx object must be used with"
" a context (see the `with` statement).")
else:
return func(self, *args, **kwargs)
return wrapped
@ensure_contexted
def chunks(self):
"""
Get each of the ChunkHeaders from within this EVTX file.
@rtype generator of ChunkHeader
@return A generator of ChunkHeaders from this EVTX file.
"""
for chunk in self._fh.chunks():
yield chunk
@ensure_contexted
def records(self):
"""
Get each of the Records from within this EVTX file.
@rtype generator of Record
@return A generator of Records from this EVTX file.
"""
for chunk in self.chunks():
for record in chunk.records():
yield record
@ensure_contexted
def get_record(self, record_num):
"""
Get a Record by record number.
@type record_num: int
@param record_num: The record number of the the record to fetch.
@rtype Record or None
@return The record request by record number, or None if
the record is not found.
"""
return self._fh.get_record(record_num)
@ensure_contexted
def get_file_header(self):
return self._fh
class FileHeader(Block):
def __init__(self, buf, offset):
logger.debug("FILE HEADER at {}.".format(hex(offset)))
super(FileHeader, self).__init__(buf, offset)
self.declare_field("string", "magic", 0x0, length=8)
self.declare_field("qword", "oldest_chunk")
self.declare_field("qword", "current_chunk_number")
self.declare_field("qword", "next_record_number")
self.declare_field("dword", "header_size")
self.declare_field("word", "minor_version")
self.declare_field("word", "major_version")
self.declare_field("word", "header_chunk_size")
self.declare_field("word", "chunk_count")
self.declare_field("binary", "unused1", length=0x4c)
self.declare_field("dword", "flags")
self.declare_field("dword", "checksum")
def __repr__(self):
return "FileHeader(buf={!r}, offset={!r})".format(self._buf, self._offset)
def __str__(self):
return "FileHeader(offset={})".format(hex(self._offset))
def check_magic(self):
"""
@return A boolean that indicates if the first eight bytes of
the FileHeader match the expected magic value.
"""
try:
return self.magic() == "ElfFile\x00"
except UnicodeDecodeError:
return False
def calculate_checksum(self):
"""
@return A integer in the range of an unsigned int that
is the calculated CRC32 checksum off the first 0x78 bytes.
This is consistent with the checksum stored by the FileHeader.
"""
return binascii.crc32(self.unpack_binary(0, 0x78)) & 0xFFFFFFFF
def verify(self):
"""
@return A boolean that indicates that the FileHeader
successfully passes a set of heuristic checks that
all EVTX FileHeaders should pass.
"""
return self.check_magic() and \
self.major_version() == 0x3 and \
self.minor_version() == 0x1 and \
self.header_chunk_size() == 0x1000 and \
self.checksum() == self.calculate_checksum()
def is_dirty(self):
"""
@return A boolean that indicates that the log has been
opened and was changed, though not all changes might be
reflected in the file header.
"""
return self.flags() & 0x1 == 0x1
def is_full(self):
"""
@return A boolean that indicates that the log
has reached its maximum configured size and the retention
policy in effect does not allow to reclaim a suitable amount
of space from the oldest records and an event message could
not be written to the log file.
"""
return self.flags() & 0x2 == 0x2
def first_chunk(self):
"""
@return A ChunkHeader instance that is the first chunk
in the log file, which is always found directly after
the FileHeader.
"""
ofs = self._offset + self.header_chunk_size()
return ChunkHeader(self._buf, ofs)
def current_chunk(self):
"""
@return A ChunkHeader instance that is the current chunk
indicated by the FileHeader.
"""
ofs = self._offset + self.header_chunk_size()
ofs += (self.current_chunk_number() * 0x10000)
return ChunkHeader(self._buf, ofs)
def chunks(self, include_inactive=False):
"""
@return A generator that yields the chunks of the log file
starting with the first chunk, which is always found directly
after the FileHeader.
If `include_inactive` is set to true, enumerate chunks beyond those
declared in the file header (and may therefore be corrupt).
"""
if include_inactive:
chunk_count = sys.maxsize
else:
chunk_count = self.chunk_count()
i = 0
ofs = self._offset + self.header_chunk_size()
while ofs + 0x10000 <= len(self._buf) and i < chunk_count:
yield ChunkHeader(self._buf, ofs)
ofs += 0x10000
i += 1
def get_record(self, record_num):
"""
Get a Record by record number.
@type record_num: int
@param record_num: The record number of the the record to fetch.
@rtype Record or None
@return The record request by record number, or None if the
record is not found.
"""
for chunk in self.chunks():
first_record = chunk.log_first_record_number()
last_record = chunk.log_last_record_number()
if not (first_record <= record_num <= last_record):
continue
for record in chunk.records():
if record.record_num() == record_num:
return record
return None
class Template(object):
def __init__(self, template_node):
self._template_node = template_node
self._xml = None
def _load_xml(self):
"""
TODO(wb): One day, nodes should generate format strings
instead of the XML format made-up abomination.
"""
if self._xml is not None:
return
matcher = "\[(?:Normal|Conditional) Substitution\(index=(\d+), type=\d+\)\]"
self._xml = re.sub(matcher, "{\\1:}",
self._template_node.template_format().replace("{", "{{").replace("}", "}}"))
def make_substitutions(self, substitutions):
"""
@type substitutions: list of VariantTypeNode
"""
self._load_xml()
return self._xml.format(*[n.xml() for n in substitutions])
def node(self):
return self._template_node
class ChunkHeader(Block):
def __init__(self, buf, offset):
logger.debug("CHUNK HEADER at {}.".format(hex(offset)))
super(ChunkHeader, self).__init__(buf, offset)
self._strings = None
self._templates = None
self.declare_field("string", "magic", 0x0, length=8)
self.declare_field("qword", "file_first_record_number")
self.declare_field("qword", "file_last_record_number")
self.declare_field("qword", "log_first_record_number")
self.declare_field("qword", "log_last_record_number")
self.declare_field("dword", "header_size")
self.declare_field("dword", "last_record_offset")
self.declare_field("dword", "next_record_offset")
self.declare_field("dword", "data_checksum")
self.declare_field("binary", "unused", length=0x44)
self.declare_field("dword", "header_checksum")
def __repr__(self):
return "ChunkHeader(buf={!r}, offset={!r})".format(self._buf, self._offset)
def __str__(self):
return "ChunkHeader(offset={})".format(hex(self._offset))
def check_magic(self):
"""
@return A boolean that indicates if the first eight bytes of
the ChunkHeader match the expected magic value.
"""
try:
return self.magic() == "ElfChnk\x00"
except UnicodeDecodeError:
return False
def calculate_header_checksum(self):
"""
@return A integer in the range of an unsigned int that
is the calculated CRC32 checksum of the ChunkHeader fields.
"""
data = self.unpack_binary(0x0, 0x78)
data += self.unpack_binary(0x80, 0x180)
return binascii.crc32(data) & 0xFFFFFFFF
def calculate_data_checksum(self):
"""
@return A integer in the range of an unsigned int that
is the calculated CRC32 checksum of the Chunk data.
"""
data = self.unpack_binary(0x200, self.next_record_offset() - 0x200)
return binascii.crc32(data) & 0xFFFFFFFF
def verify(self):
"""
@return A boolean that indicates that the FileHeader
successfully passes a set of heuristic checks that
all EVTX ChunkHeaders should pass.
"""
return self.check_magic() and \
self.calculate_header_checksum() == self.header_checksum() and \
self.calculate_data_checksum() == self.data_checksum()
def _load_strings(self):
if self._strings is None:
self._strings = {}
for i in range(64):
ofs = self.unpack_dword(0x80 + (i * 4))
while ofs > 0:
string_node = self.add_string(ofs)
ofs = string_node.next_offset()
def strings(self):
"""
@return A dict(offset --> NameStringNode)
"""
if not self._strings:
self._load_strings()
return self._strings
def add_string(self, offset, parent=None):
"""
@param offset An integer offset that is relative to the start of
this chunk.
@param parent (Optional) The parent of the newly created
NameStringNode instance. (Default: this chunk).
@return None
"""
if self._strings is None:
self._load_strings()
string_node = NameStringNode(self._buf, self._offset + offset,
self, parent or self)
self._strings[offset] = string_node
return string_node
def _load_templates(self):
"""
@return None
"""
if self._templates is None:
self._templates = {}
for i in range(32):
ofs = self.unpack_dword(0x180 + (i * 4))
while ofs > 0:
# unclear why these are found before the offset
# this is a direct port from A.S.'s code
token = self.unpack_byte(ofs - 10)
pointer = self.unpack_dword(ofs - 4)
if token != 0x0c or pointer != ofs:
logger.warning("Unexpected token encountered")
ofs = 0
continue
template = self.add_template(ofs)
ofs = template.next_offset()
def add_template(self, offset, parent=None):
"""
@param offset An integer which contains the chunk-relative offset
to a template to load into this Chunk.
@param parent (Optional) The parent of the newly created
TemplateNode instance. (Default: this chunk).
@return Newly added TemplateNode instance.
"""
if self._templates is None:
self._load_templates()
node = TemplateNode(self._buf, self._offset + offset,
self, parent or self)
self._templates[offset] = node
return node
def templates(self):
"""
@return A dict(offset --> Template) of all encountered
templates in this Chunk.
"""
if not self._templates:
self._load_templates()
return self._templates
def first_record(self):
return Record(self._buf, self._offset + 0x200, self)
def records(self):
try:
record = self.first_record()
except InvalidRecordException:
return
while record._offset < self._offset + self.next_record_offset() and record.length() > 0:
yield record
try:
record = Record(self._buf,
record._offset + record.length(),
self)
except InvalidRecordException:
return
class Record(Block):
def __init__(self, buf, offset, chunk):
logger.debug("Record at {}.".format(hex(offset)))
super(Record, self).__init__(buf, offset)
self._chunk = chunk
self.declare_field("dword", "magic", 0x0) # 0x00002a2a
self.declare_field("dword", "size")
self.declare_field("qword", "record_num")
self.declare_field("filetime", "timestamp")
if self.size() > 0x10000:
raise InvalidRecordException()
self.declare_field("dword", "size2", self.size() - 4)
def __repr__(self):
return "Record(buf={!r}, offset={!r})".format(self._buf, self._offset)
def __str__(self):
return "Record(offset={})".format(hex(self._offset))
def root(self):
return RootNode(self._buf, self._offset + 0x18, self._chunk, self)
def length(self):
return self.size()
def verify(self):
return self.size() == self.size2()
def data(self):
"""
Return the raw data block which makes up this record as a bytestring.
@rtype str
@return A string that is a copy of the buffer that makes
up this record.
"""
return self._buf[self.offset():self.offset() + self.size()]
def xml(self):
'''
render the record into XML.
does not include the xml declaration header.
Returns:
str: the rendered xml document.
'''
return e_views.evtx_record_xml_view(self)
def lxml(self):
'''
render the record into a lxml document.
this is useful for querying data from the record using xpath, etc.
note: lxml must be installed.
Returns:
lxml.etree.ElementTree: the rendered and parsed xml document.
Raises:
ImportError: if lxml is not installed.
'''
import lxml.etree
return lxml.etree.fromstring((e_views.XML_HEADER + self.xml()).encode('utf-8'))