forked from williballenthin/python-evtx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevtx_structure.py
More file actions
executable file
·201 lines (168 loc) · 6.72 KB
/
evtx_structure.py
File metadata and controls
executable file
·201 lines (168 loc) · 6.72 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
"""
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.
"""
import mmap
import contextlib
from Evtx.Evtx import FileHeader
from Evtx.Nodes import RootNode
from Evtx.Nodes import BXmlTypeNode
from Evtx.Nodes import AttributeNode
from Evtx.Nodes import VariantTypeNode
from Evtx.Nodes import TemplateInstanceNode
from Evtx.Nodes import OpenStartElementNode
class EvtxFormatter(object):
def __init__(self):
super(EvtxFormatter, self).__init__()
self._indent_stack = []
self._indent_unit = " "
def _indent(self):
self._indent_stack.append(self._indent_unit)
def _dedent(self):
if len(self._indent_stack) > 0:
self._indent_stack = self._indent_stack[:-1]
def save_indent(self):
return self._indent_stack[:]
def restore_indent(self, indent):
self._indent_stack = indent
def _l(self, s):
return "".join(self._indent_stack) + s
def format_header(self, fh):
yield self._l("File header")
self._indent()
yield self._l("magic: %s" % (fh.magic()))
for num_field in [
"oldest_chunk",
"current_chunk_number",
"next_record_number",
"header_size",
"minor_version",
"major_version",
"header_chunk_size",
"chunk_count",
"flags",
"checksum"]:
yield self._l("%s: %s" % (num_field, hex(getattr(fh, num_field)())))
yield self._l("verify: %s" % (fh.verify()))
yield self._l("dirty: %s" % (fh.is_dirty()))
yield self._l("full: %s" % (fh.is_full()))
for chunk in fh.chunks():
for line in self.format_chunk(chunk):
yield line
self._dedent()
def format_chunk(self, chunk):
yield self._l("Chunk")
self._indent()
yield self._l("offset: %s" % (hex(chunk.offset())))
yield self._l("magic: %s" % (chunk.magic()))
for num_field in [
"file_first_record_number",
"file_last_record_number",
"log_first_record_number",
"log_last_record_number",
"header_size",
"last_record_offset",
"next_record_offset",
"data_checksum",
"header_checksum"]:
yield self._l("%s: %s" % (num_field, hex(getattr(chunk, num_field)())))
yield self._l("verify: %s" % (chunk.verify()))
yield self._l("templates: %d" % (len(chunk.templates())))
for record in chunk.records():
for line in self.format_record(record):
yield line
self._dedent()
def format_record(self, record):
yield self._l("Record")
self._indent()
yield self._l("offset: %s" % (hex(record.offset())))
yield self._l("magic: %s" % (hex(record.magic())))
yield self._l("size: %s" % (hex(record.size())))
yield self._l("number: %s" % (hex(record.record_num())))
yield self._l("timestamp: %s" % (record.timestamp()))
yield self._l("verify: %s" % (record.verify()))
try:
s = self.save_indent()
for line in self.format_node(record, record.root()):
yield line
except Exception as e:
self.restore_indent(s)
yield "ERROR: " + str(e)
self._dedent()
def _format_node_name(self, record, node, extra=None):
"""
note: this doesn't yield, it returns
"""
line = ""
if extra is not None:
line = "%s(offset=%s, %s)" % (node.__class__.__name__, hex(node.offset() - record.offset()), extra)
else:
line = "%s(offset=%s)" % (node.__class__.__name__, hex(node.offset() - record.offset()))
if isinstance(node, VariantTypeNode):
line += " --> %s" % (node.string())
if isinstance(node, OpenStartElementNode):
line += " --> %s" % (node.tag_name().string())
if isinstance(node, AttributeNode):
line += " --> %s" % (node.attribute_name().string())
return line
def format_node(self, record, node):
extra = None
if isinstance(node, TemplateInstanceNode) and node.is_resident_template():
extra = "resident=True, length=%s" % (hex(node.template().data_length()))
elif isinstance(node, TemplateInstanceNode):
extra = "resident=False"
yield self._l(self._format_node_name(record, node, extra=extra))
if isinstance(node, BXmlTypeNode):
self._indent()
for line in self.format_node(record, node._root):
yield line
self._dedent()
elif isinstance(node, TemplateInstanceNode) and node.is_resident_template():
self._indent()
for line in self.format_node(record, node.template()):
yield line
self._dedent()
self._indent()
for child in node.children():
for line in self.format_node(record, child):
yield line
self._dedent()
if isinstance(node, RootNode):
ofs = node.tag_and_children_length()
yield self._l("Substitutions(offset=%s)" % (hex(node.offset() - record.offset() + ofs)))
self._indent()
for sub in node.substitutions():
for line in self.format_node(record, sub):
yield line
self._dedent()
def main():
from argparse import ArgumentParser
parser = ArgumentParser(
description="Dump the structure of an EVTX file."
)
parser.add_argument(
"evtx"
, type=str
, help="Path to the Windows EVTX event log file"
)
args = parser.parse_args()
with open(args.evtx, 'r') as f:
with contextlib.closing(mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)) as buf:
fh = FileHeader(buf, 0x0)
formatter = EvtxFormatter()
for line in formatter.format_header(fh):
print(line)
if __name__ == "__main__":
main()