Skip to content

Commit 2ff9dd2

Browse files
authored
Merge pull request #4 from airbus-cert/fix-issue-003
Fix issue 003, improve xml render, fix unit test
2 parents 4dfa447 + c223eff commit 2ff9dd2

File tree

9 files changed

+50
-23
lines changed

9 files changed

+50
-23
lines changed

bin/etl2xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ from construct import ListContainer, Struct, Container
99
from etl.error import GroupNotFound, VersionNotFound, EventTypeNotFound, EtwVersionNotFound, EventIdNotFound, \
1010
GuidNotFound, TlMetaDataNotFound, InvalidType
1111
from etl.etl import IEtlFileObserver, build_from_stream
12+
from etl.parsers.etw.core import Guid
1213
from etl.wintrace import WinTrace
1314
from etl.event import Event
1415
from etl.parsers.kernel import FileIo_V2_Name, ImageLoad, DiskIo_TypeGroup1, \
@@ -122,7 +123,7 @@ def log_construct_pattern(xml: Element, pattern: Struct, source: Container):
122123
raise InvalidType()
123124
elif isinstance(source[field.name], ListContainer):
124125
add_attribute(xml, field.name, bytearray(source[field.name]).hex())
125-
elif isinstance(source[field.name], bytes):
126+
elif isinstance(source[field.name], bytes):
126127
add_attribute(xml, field.name, source[field.name].hex())
127128
elif isinstance(source[field.name], Container):
128129
continue
@@ -137,8 +138,11 @@ def log_tracelogging(obj: TraceLogging) -> Element:
137138
"""
138139
xml = ElementTree.Element("tracelogging")
139140
xml.set("name", obj.get_name())
140-
for k,v in obj.items():
141-
add_attribute(xml, k, str(v))
141+
for k, v in obj.items():
142+
if hasattr(v, "type") and v.type == "Guid":
143+
add_attribute(xml, k, str(Guid(v.inner.data1, v.inner.data2, v.inner.data3, v.inner.data4)))
144+
else:
145+
add_attribute(xml, k, str(v))
142146
return xml
143147

144148

etl/parsers/kernel/file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ def get_file_name(self) -> str:
2424
"""
2525
:return: Associate filename
2626
"""
27-
return bytearray(self.source.FileName[:-2]).decode("utf-16le")
27+
return bytearray(self.source.FileName.string[:-2]).decode("utf-16le")

etl/parsers/kernel/header.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ def get_session_name(self) -> str:
4141
"""
4242
:return: ETW Session name
4343
"""
44-
return bytearray(self.source.SessionNameString[:-2]).decode("utf-16le")
44+
return bytearray(self.source.SessionNameString.string[:-2]).decode("utf-16le")
4545

4646
def get_log_filename(self) -> str:
4747
"""
4848
:return: Return path of log file name
4949
"""
50-
return bytearray(self.source.LogFileNameString[:-2]).decode("utf-16le")
50+
return bytearray(self.source.LogFileNameString.string[:-2]).decode("utf-16le")
5151

5252

5353
@declare(group=EventTraceGroup.EVENT_TRACE_GROUP_HEADER, version=2, event_types=[5, 32])

etl/parsers/kernel/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def get_image_filename(self) -> str:
6262
"""
6363
:return: Return image file name
6464
"""
65-
return bytearray(self.source.FileName[:-2]).decode("utf-16le")
65+
return bytearray(self.source.FileName.string[:-2]).decode("utf-16le")
6666

6767
def get_process_id(self) -> int:
6868
"""

etl/parsers/kernel/process.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ def get_image_file_name(self) -> str:
3333
"""
3434
:return: Image file name
3535
"""
36-
return bytearray(self.source.ImageFileName[:-1]).decode("utf8")
36+
return bytearray(self.source.ImageFileName.string[:-1]).decode("utf8")
3737

3838
def get_command_line(self) -> str:
3939
"""
4040
:return: Associate command line of starting process
4141
"""
42-
return bytearray(self.source.CommandLine[:-2]).decode("utf-16le")
42+
return bytearray(self.source.CommandLine.string[:-2]).decode("utf-16le")
4343

4444
def get_package_full_name(self) -> str:
4545
"""
4646
:return: Package full name
4747
"""
48-
return bytearray(self.source.PackageFullName[:-2]).decode("utf-16le")
48+
return bytearray(self.source.PackageFullName.string[:-2]).decode("utf-16le")
4949

5050
def get_application_id(self) -> str:
5151
"""
5252
:return: Application id
5353
"""
54-
return bytearray(self.source.PackageFullName[:-2]).decode("utf-16le")
54+
return bytearray(self.source.PackageFullName.string[:-2]).decode("utf-16le")
5555

5656
def get_exit_status(self) -> int:
5757
"""
@@ -102,25 +102,25 @@ def get_image_file_name(self) -> str:
102102
"""
103103
:return: Image file name
104104
"""
105-
return bytearray(self.source.ImageFileName[:-1]).decode("utf8")
105+
return bytearray(self.source.ImageFileName.string[:-1]).decode("utf8")
106106

107107
def get_command_line(self) -> str:
108108
"""
109109
:return: Associate command line of starting process
110110
"""
111-
return bytearray(self.source.CommandLine[:-2]).decode("utf-16le")
111+
return bytearray(self.source.CommandLine.string[:-2]).decode("utf-16le")
112112

113113
def get_package_full_name(self) -> str:
114114
"""
115115
:return: Package full name
116116
"""
117-
return bytearray(self.source.PackageFullName[:-2]).decode("utf-16le")
117+
return bytearray(self.source.PackageFullName.string[:-2]).decode("utf-16le")
118118

119119
def get_application_id(self) -> str:
120120
"""
121121
:return: Application id
122122
"""
123-
return bytearray(self.source.PackageFullName[:-2]).decode("utf-16le")
123+
return bytearray(self.source.PackageFullName.string[:-2]).decode("utf-16le")
124124

125125
def get_exit_status(self) -> int:
126126
"""
@@ -181,7 +181,7 @@ def get_image_filename(self) -> str:
181181
"""
182182
:return: Return image file name
183183
"""
184-
return bytearray(self.source.FileName[:-2]).decode("utf-16le")
184+
return bytearray(self.source.FileName.string[:-2]).decode("utf-16le")
185185

186186
def get_process_id(self) -> int:
187187
"""

etl/parsers/tracelogging.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from enum import Enum
1212
from io import BytesIO
1313

14-
from construct import Int16ul, Int8ul, CString, If, GreedyRange, LazyBound, Struct
14+
from construct import Int16ul, Int8ul, CString, If, GreedyRange, LazyBound, Struct, Int32ul
1515
from etl.error import TlMetaDataNotFound, TlUnhandledTag
1616
from etl.utils import Guid, SystemTime
1717

@@ -52,12 +52,14 @@ class TagIn(Enum):
5252
TlMetaDataField = Struct(
5353
"name" / CString("ascii"),
5454
"tag_in" / Int8ul,
55-
"tag_out" / If(lambda this: this.tag_in & TagIn.CHAIN.value, LazyBound(lambda: Int8ul))
55+
"tag_out" / If(lambda this: this.tag_in & TagIn.CHAIN.value, LazyBound(lambda: Int8ul)),
56+
"unknown" / If(lambda this: this.tag_out and bool(this.tag_out & 0x80), LazyBound(lambda: Int32ul))
5657
)
5758

5859
TlMetaData = Struct(
5960
"size" / Int16ul,
6061
"tag" / Int8ul,
62+
"unknown" / If(lambda this: this.tag & 0x80, LazyBound(lambda: Int8ul)),
6163
"name" / CString("ascii"),
6264
"fields" / GreedyRange(TlMetaDataField)
6365
)
@@ -96,6 +98,10 @@ def read_field(stream, tag):
9698
# Encode in ascii and ignore last null byte
9799
return b"".join(current).decode("ascii")[:-1]
98100

101+
elif tag & 0x1f == TagIn.COUNTEDANSISTRING.value:
102+
length = struct.unpack("b", stream.read_exact(1))[0]
103+
return stream.read_exact(length + 1).decode("ascii")
104+
99105
elif tag & 0x1f == TagIn.INT8.value:
100106
return struct.unpack("b", stream.read_exact(1))[0]
101107
elif tag & 0x1f == TagIn.UINT8.value:

etl/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
Global Unique Identifier
1212
"""
1313
Guid = Struct(
14-
"data1" / Int32ul,
15-
"data2" / Int16ul,
16-
"data3" / Int16ul,
17-
"data4" / Byte[8]
14+
"type" / Computed("Guid"),
15+
"inner" / Struct (
16+
"data1" / Int32ul,
17+
"data2" / Int16ul,
18+
"data3" / Int16ul,
19+
"data4" / Byte[8]
20+
)
1821
)
1922

2023
"""

tests/example/lxcore_kernel.etl

24 KB
Binary file not shown.

tests/test_tracelogging_parser.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_tracelogging_guid(self):
7575
tl = build_tracelogging(event)
7676
self.assertEqual(tl.get_name(), "Test", "Invalid Name")
7777
parsed_guid = tl["Engine"]
78-
self.assertEqual(Guid(parsed_guid.data1, parsed_guid.data2, parsed_guid.data3, parsed_guid.data4), guid("00000000-0000-0000-0000-000000000000"), "Invalid GUID")
78+
self.assertEqual(Guid(parsed_guid.inner.data1, parsed_guid.inner.data2, parsed_guid.inner.data3, parsed_guid.inner.data4), guid("00000000-0000-0000-0000-000000000000"), "Invalid GUID")
7979

8080
def test_array_uint8(self):
8181
"""
@@ -103,4 +103,18 @@ def test_array_uint16(self):
103103
event.user_data = b'\x10\x00' + b'\x00' * 32
104104
tl = build_tracelogging(event)
105105
self.assertEqual(tl.get_name(), "Test", "Invalid Name")
106+
self.assertEqual(tl["Engine"], [0] * 16)
107+
108+
def test_extended_flag(self):
109+
"""
110+
Test the deserialization of an array UINT16 element
111+
"""
112+
event = Container()
113+
meta = Container()
114+
meta.ext_type = 11
115+
meta.data_item = b'\x10\x00\x80\x00Test\x00Engine\x00\x26'
116+
event.extended_data = ListContainer([meta])
117+
event.user_data = b'\x10\x00' + b'\x00' * 32
118+
tl = build_tracelogging(event)
119+
self.assertEqual(tl.get_name(), "Test", "Invalid Name")
106120
self.assertEqual(tl["Engine"], [0] * 16)

0 commit comments

Comments
 (0)