Skip to content

Commit 05cc4d0

Browse files
author
Willi Ballenthin
committed
views: format guids with { and } like in event viewer
1 parent 6734d70 commit 05cc4d0

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

Evtx/Nodes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ def fast_substitutions(self):
984984
sub_def.append(base64.b64encode(self.unpack_binary(ofs, size)))
985985
#[15] = parse_guid_type_node,
986986
elif type_ == 0xF:
987-
sub_def.append(self.unpack_guid(ofs))
987+
sub_def.append('{' + self.unpack_guid(ofs) + '}')
988988
#[16] = parse_size_type_node,
989989
elif type_ == 0x10:
990990
if size == 0x4:
@@ -1422,7 +1422,8 @@ def tag_length(self):
14221422
return 16
14231423

14241424
def string(self):
1425-
return "{{g}}".format(g=self.guid())
1425+
print('{' + self.guid() + '}')
1426+
return '{' + self.guid() + '}'
14261427

14271428

14281429
class SizeTypeNode(VariantTypeNode):

Evtx/Views.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
18+
import string
19+
1820
from .Nodes import RootNode
1921
from .Nodes import TemplateNode
2022
from .Nodes import EndOfStreamNode
@@ -61,9 +63,6 @@ def _make_template_xml_view(root_node, cache=None):
6163
if cache is None:
6264
cache = {}
6365

64-
def escape_format_chars(s):
65-
return s.replace("{", "{{").replace("}", "}}")
66-
6766
def rec(node, acc):
6867
if isinstance(node, EndOfStreamNode):
6968
pass # intended
@@ -90,7 +89,7 @@ def rec(node, acc):
9089
elif isinstance(node, CloseElementNode):
9190
pass # intended
9291
elif isinstance(node, ValueNode):
93-
acc.append(escape_format_chars(node.children()[0].string()))
92+
acc.append(to_xml_string(node.children()[0].string()))
9493
elif isinstance(node, AttributeNode):
9594
pass # intended
9695
elif isinstance(node, CDataSectionNode):
@@ -134,6 +133,11 @@ def rec(node, acc):
134133
return "".join(acc)
135134

136135

136+
class SafeDict(dict):
137+
def __missing__(self, key):
138+
return '{' + key + '}'
139+
140+
137141
def _build_record_xml(record, cache=None):
138142
"""
139143
Note, the cache should be local to the Evtx.Chunk.
@@ -145,21 +149,22 @@ def _build_record_xml(record, cache=None):
145149
"""
146150
if cache is None:
147151
cache = {}
148-
149152
def rec(root_node):
150153
f = _make_template_xml_view(root_node, cache=cache)
151154
subs_strs = []
152155
for sub in root_node.fast_substitutions():
153156
if isinstance(sub, str):
154-
#subs_strs.append(to_xml_string(sub))
155157
subs_strs.append(sub)
156158
elif isinstance(sub, RootNode):
157159
subs_strs.append(rec(sub))
158160
elif sub is None:
159161
subs_strs.append("")
160162
else:
161163
subs_strs.append(str(sub))
162-
return f.format(*subs_strs)
164+
165+
# maintain substrings like {foo} if foo= isn't a kwarg to format
166+
# via: http://stackoverflow.com/a/17215533/87207
167+
return string.Formatter().vformat(f, subs_strs, SafeDict())
163168
xml = rec(record.root())
164169
return xml
165170

0 commit comments

Comments
 (0)