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
74d74c5b
Commit
74d74c5b
authored
Nov 11, 2014
by
devttys0
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added initial regex signature support
parent
c9cec992
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
1 deletions
+22
-1
magic.py
src/binwalk/core/magic.py
+22
-1
No files found.
src/binwalk/core/magic.py
View file @
74d74c5b
...
@@ -71,6 +71,7 @@ class SignatureLine(object):
...
@@ -71,6 +71,7 @@ class SignatureLine(object):
'''
'''
self
.
tags
=
[]
self
.
tags
=
[]
self
.
text
=
line
self
.
text
=
line
self
.
regex
=
False
# Split the line on any white space; for this to work, backslash-escaped
# Split the line on any white space; for this to work, backslash-escaped
# spaces ('\ ') are replaced with their escaped hex value ('\x20').
# spaces ('\ ') are replaced with their escaped hex value ('\x20').
...
@@ -167,6 +168,16 @@ class SignatureLine(object):
...
@@ -167,6 +168,16 @@ class SignatureLine(object):
self
.
value
=
binwalk
.
core
.
compat
.
string_decode
(
self
.
value
)
self
.
value
=
binwalk
.
core
.
compat
.
string_decode
(
self
.
value
)
except
ValueError
as
e
:
except
ValueError
as
e
:
raise
ParserException
(
"Failed to decode string value '
%
s' in line '
%
s'"
%
(
self
.
value
,
line
))
raise
ParserException
(
"Failed to decode string value '
%
s' in line '
%
s'"
%
(
self
.
value
,
line
))
# If a regex was specified, compile it
elif
self
.
type
==
'regex'
:
self
.
regex
=
True
try
:
self
.
value
=
re
.
compile
(
self
.
value
)
except
KeyboardInterrupt
as
e
:
raise
e
except
Exception
as
e
:
raise
ParserException
(
"Invalid regular expression '
%
s':
%
s"
%
(
self
.
value
,
str
(
e
)))
# Non-string types are integer values
# Non-string types are integer values
else
:
else
:
try
:
try
:
...
@@ -190,6 +201,11 @@ class SignatureLine(object):
...
@@ -190,6 +201,11 @@ class SignatureLine(object):
# Else, truncate the string to self.MAX_STRING_SIZE
# Else, truncate the string to self.MAX_STRING_SIZE
else
:
else
:
self
.
size
=
self
.
MAX_STRING_SIZE
self
.
size
=
self
.
MAX_STRING_SIZE
elif
self
.
type
==
'regex'
:
# Regular expressions don't have a struct format value, since they don't have to be unpacked
self
.
fmt
=
None
# The size of a matching regex is unknown until it is applied to some data
self
.
size
=
self
.
MAX_STRING_SIZE
elif
self
.
type
==
'byte'
:
elif
self
.
type
==
'byte'
:
self
.
fmt
=
'b'
self
.
fmt
=
'b'
self
.
size
=
1
self
.
size
=
1
...
@@ -280,7 +296,11 @@ class Signature(object):
...
@@ -280,7 +296,11 @@ class Signature(object):
# Strings and single byte signatures are taken at face value;
# Strings and single byte signatures are taken at face value;
# multi-byte integer values are turned into regex strings based
# multi-byte integer values are turned into regex strings based
# on their data type size and endianess.
# on their data type size and endianess.
if
line
.
type
in
[
'string'
]:
#
# Regex types are already compiled expressions.
if
line
.
type
==
'regex'
:
return
line
.
value
if
line
.
type
==
'string'
:
restr
=
re
.
escape
(
line
.
value
)
restr
=
re
.
escape
(
line
.
value
)
elif
line
.
size
==
1
:
elif
line
.
size
==
1
:
restr
=
re
.
escape
(
chr
(
line
.
value
))
restr
=
re
.
escape
(
chr
(
line
.
value
))
...
@@ -573,6 +593,7 @@ class Magic(object):
...
@@ -573,6 +593,7 @@ class Magic(object):
# Does the data (dvalue) match the specified comparison?
# Does the data (dvalue) match the specified comparison?
if
((
line
.
value
is
None
)
or
if
((
line
.
value
is
None
)
or
(
line
.
regex
and
line
.
value
.
match
(
dvalue
))
or
(
line
.
condition
==
'='
and
dvalue
==
line
.
value
)
or
(
line
.
condition
==
'='
and
dvalue
==
line
.
value
)
or
(
line
.
condition
==
'>'
and
dvalue
>
line
.
value
)
or
(
line
.
condition
==
'>'
and
dvalue
>
line
.
value
)
or
(
line
.
condition
==
'<'
and
dvalue
<
line
.
value
)
or
(
line
.
condition
==
'<'
and
dvalue
<
line
.
value
)
or
...
...
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