Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
binwalk
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-gitdep
binwalk
Commits
58446a92
Commit
58446a92
authored
Nov 08, 2014
by
devttys0
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for nested offset expressions
parent
9e0a0c1f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
21 deletions
+38
-21
magic.py
src/binwalk/core/magic.py
+38
-21
No files found.
src/binwalk/core/magic.py
View file @
58446a92
...
@@ -93,13 +93,6 @@ class SignatureLine(object):
...
@@ -93,13 +93,6 @@ class SignatureLine(object):
# value (e.g., '(4.l+16)').
# value (e.g., '(4.l+16)').
self
.
offset
=
parts
[
0
]
.
replace
(
'>'
,
''
)
self
.
offset
=
parts
[
0
]
.
replace
(
'>'
,
''
)
# Check to see if the up-level character ('&') has been specified
if
self
.
offset
.
startswith
(
'&'
):
self
.
uplevel
=
True
self
.
offset
=
self
.
offset
[
1
:]
else
:
self
.
uplevel
=
False
# Check if the offset is an indirect offset
# Check if the offset is an indirect offset
self
.
is_indirect_offset
=
self
.
offset
.
startswith
(
'('
)
self
.
is_indirect_offset
=
self
.
offset
.
startswith
(
'('
)
...
@@ -369,6 +362,8 @@ class Magic(object):
...
@@ -369,6 +362,8 @@ class Magic(object):
self
.
printable
=
re
.
compile
(
"[ -~]*"
)
self
.
printable
=
re
.
compile
(
"[ -~]*"
)
# Regex rule to find format strings
# Regex rule to find format strings
self
.
fmtstr
=
re
.
compile
(
"
%
[^
%
]"
)
self
.
fmtstr
=
re
.
compile
(
"
%
[^
%
]"
)
# Regex rule to find periods (see self._do_math)
self
.
period
=
re
.
compile
(
"
\
."
)
def
_filtered
(
self
,
text
):
def
_filtered
(
self
,
text
):
'''
'''
...
@@ -417,12 +412,26 @@ class Magic(object):
...
@@ -417,12 +412,26 @@ class Magic(object):
Returns an integer value that is the result of the evaluated expression.
Returns an integer value that is the result of the evaluated expression.
'''
'''
# Does the expression contain an offset (e.g., "(4.l+12)")?
# Does the expression contain an offset (e.g., "(4.l+12)")?
if
'.'
in
expression
:
if
'.'
in
expression
and
'('
in
expression
:
# Split the offset field into the integer offset and type values (o and t respsectively)
replacements
=
{}
(
o
,
t
)
=
expression
.
split
(
'.'
,
1
)
o
=
offset
+
int
(
o
.
split
(
'('
,
1
)[
1
],
0
)
for
period
in
[
match
.
start
()
for
match
in
self
.
period
.
finditer
(
expression
)]:
t
=
t
[
0
]
# Separate the offset field into the integer offset and type values (o and t respsectively)
s
=
expression
[:
period
]
.
rfind
(
'('
)
+
1
o
=
int
(
expression
[
s
:
period
],
0
)
t
=
expression
[
period
+
1
]
# Re-build just the parsed offset portion of the expression
text
=
"
%
s.
%
c"
%
(
expression
[
s
:
period
],
t
)
# Have we already evaluated this offset expression? If so, skip it.
if
binwalk
.
core
.
common
.
has_key
(
replacements
,
text
):
continue
# The offset specified in the expression is relative to the starting offset inside self.data
o
+=
offset
# Read the value from self.data at the specified offset
try
:
try
:
# Big and little endian byte format
# Big and little endian byte format
if
t
in
[
'b'
,
'B'
]:
if
t
in
[
'b'
,
'B'
]:
...
@@ -443,9 +452,14 @@ class Magic(object):
...
@@ -443,9 +452,14 @@ class Magic(object):
except
struct
.
error
as
e
:
except
struct
.
error
as
e
:
v
=
0
v
=
0
# Once the value at the specified offset is read from self.data, re-build the expression
# Keep track of all the recovered values from self.data
# (e.g., "(4.l+12)" might be converted into "(256+12)".
replacements
[
text
]
=
v
v
=
"(
%
d
%
s"
%
(
v
,
expression
.
split
(
t
,
1
)[
1
])
# Finally, replace all offset expressions with their corresponding text value
v
=
expression
for
(
text
,
value
)
in
binwalk
.
core
.
common
.
iterator
(
replacements
):
v
=
v
.
replace
(
text
,
"
%
d"
%
value
)
# If no offset, then it's just an evaluatable math expression (e.g., "(32+0x20)")
# If no offset, then it's just an evaluatable math expression (e.g., "(32+0x20)")
else
:
else
:
v
=
expression
v
=
expression
...
@@ -481,17 +495,20 @@ class Magic(object):
...
@@ -481,17 +495,20 @@ class Magic(object):
line_offset
=
line
.
offset
line_offset
=
line
.
offset
# Else, evaluate the complex expression
# Else, evaluate the complex expression
else
:
else
:
line_offset
=
self
.
_do_math
(
offset
,
line
.
offset
)
# Format the previous_line_end value into a string. Add the '+' sign to explicitly
# state that this value is to be added to any subsequent values in the expression
# (e.g., '&0' becomes '4+0').
ple
=
'
%
d+'
%
previous_line_end
# Allow users to use either the '&0' (libmagic) or '&+0' (explcit addition) sytaxes;
# replace both with the ple text.
line_offset_text
=
line
.
offset
.
replace
(
'&+'
,
ple
)
.
replace
(
'&'
,
ple
)
# Evaluate the expression
line_offset
=
self
.
_do_math
(
offset
,
line_offset_text
)
# Sanity check
# Sanity check
if
not
isinstance
(
line_offset
,
int
):
if
not
isinstance
(
line_offset
,
int
):
raise
ParserException
(
"Failed to convert offset '
%
s' to a number"
%
line
.
offset
)
raise
ParserException
(
"Failed to convert offset '
%
s' to a number"
%
line
.
offset
)
# If the uplevel delimiter was set in the signature line, then the specified offset
# is relative to the end of the last line's data (the '>>&0' offset syntax).
if
line
.
uplevel
:
line_offset
+=
previous_line_end
# The start of the data needed by this line is at offset + line_offset.
# The start of the data needed by this line is at offset + line_offset.
# The end of the data will be line.size bytes later.
# The end of the data will be line.size bytes later.
start
=
offset
+
line_offset
start
=
offset
+
line_offset
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment