Skip to content

Commit 9df078e

Browse files
author
Willi Ballenthin
committed
binary parser: remove hex_dump, use hexdump package
1 parent 75a7cf4 commit 9df078e

4 files changed

Lines changed: 11 additions & 55 deletions

File tree

Evtx/BinaryParser.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,54 +24,6 @@
2424
from functools import partial
2525

2626

27-
def hex_dump(src, start_addr=0):
28-
"""
29-
see:
30-
http://code.activestate.com/recipes/142812-hex-dumper/
31-
@param src A bytestring containing the data to dump.
32-
@param start_addr An integer representing the start
33-
address of the data in whatever context it comes from.
34-
@return A string containing a classic hex dump with 16
35-
bytes per line. If start_addr is provided, then the
36-
data is interpreted as starting at this offset, and
37-
the offset column is updated accordingly.
38-
"""
39-
FILTER = ''.join([(len(repr(chr(x))) == 3) and
40-
chr(x) or
41-
'.' for x in range(256)])
42-
length = 16
43-
result = []
44-
45-
remainder_start_addr = start_addr
46-
47-
if start_addr % length != 0:
48-
base_addr = start_addr - (start_addr % length)
49-
num_spaces = (start_addr % length)
50-
num_chars = length - (start_addr % length)
51-
52-
spaces = " ".join([" " for i in range(num_spaces)])
53-
s = src[0:num_chars]
54-
hexa = ' '.join(["{:02X}".format(ord(x)) for x in s])
55-
printable = s.translate(FILTER)
56-
57-
result.append("{:04X} {} {} {}{}\n".format(
58-
(base_addr, spaces, hexa,
59-
" " * (num_spaces + 1), printable)))
60-
61-
src = src[num_chars:]
62-
remainder_start_addr = base_addr + length
63-
64-
for i in range(0, len(src), length):
65-
s = src[i:i + length]
66-
hexa = ' '.join(["{:02X}".format(ord(x)) for x in s])
67-
printable = s.translate(FILTER)
68-
result.append("{:04X} {:<} {:{l}}\n".format(
69-
remainder_start_addr + i,
70-
hexa, printable, l=length*3))
71-
72-
return ''.join(result)
73-
74-
7527
class memoize(object):
7628
"""cache the return value of a method
7729

Evtx/Nodes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818
import re
19-
import itertools
2019
import base64
20+
import itertools
21+
22+
import hexdump
2123

2224
from .BinaryParser import Block
23-
from .BinaryParser import hex_dump
2425
from .BinaryParser import ParseException
2526
from .BinaryParser import memoize
2627

@@ -88,8 +89,8 @@ def __str__(self):
8889
return "BXmlNode(offset={})".format(hex(self.offset()))
8990

9091
def dump(self):
91-
return hex_dump(self._buf[self.offset():self.offset() + self.length()],
92-
start_addr=self.offset())
92+
b = self._buf[self.offset():self.offset() + self.length()]
93+
return hexdump.hexdump(b, result='return')
9394

9495
def tag_length(self):
9596
"""

scripts/record_structure.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import hexdump
2+
13
from Evtx.Evtx import Evtx
24
from Evtx.Nodes import RootNode
35
from Evtx.Nodes import BXmlTypeNode
46
from Evtx.Nodes import TemplateInstanceNode
57
from Evtx.Nodes import VariantTypeNode
6-
from Evtx.BinaryParser import hex_dump
78
from Evtx.Views import evtx_record_xml_view
89

910

@@ -81,7 +82,7 @@ def main():
8182
args = parser.parse_args()
8283

8384
with Evtx(args.evtx) as evtx:
84-
print(hex_dump(evtx.get_record(args.record).data()))
85+
hexdump.hexdump(evtx.get_record(args.record).data())
8586

8687
print(("record(absolute_offset=%s)" % \
8788
(evtx.get_record(args.record).offset())))

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212
author_email="willi.ballenthin@gmail.com",
1313
url="https://github.com/williballenthin/python-evtx",
1414
license="Apache 2.0 License",
15-
packages=setuptools.find_packages())
15+
packages=setuptools.find_packages(),
16+
install_requires=['hexdump'],
17+
)

0 commit comments

Comments
 (0)