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-depend
binwalk
Commits
19818c80
Unverified
Commit
19818c80
authored
4 years ago
by
devttys0
Committed by
GitHub
4 years ago
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #466 from STKFLT/patch-1
Improves speed of entropy scan
parents
a43d935b
674cc7f2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
5 deletions
+32
-5
entropy.py
src/binwalk/modules/entropy.py
+32
-5
No files found.
src/binwalk/modules/entropy.py
View file @
19818c80
# Calculates and optionally plots the entropy of input files.
# Calculates and optionally plots the entropy of input files.
import
os
import
os
import
sys
import
math
import
math
import
zlib
import
zlib
import
binwalk.core.common
import
binwalk.core.common
from
binwalk.core.compat
import
*
from
binwalk.core.compat
import
*
from
binwalk.core.module
import
Module
,
Option
,
Kwarg
from
binwalk.core.module
import
Module
,
Option
,
Kwarg
try
:
import
numpy
as
np
except
ImportError
:
pass
try
:
from
numba
import
njit
except
ImportError
:
def
njit
(
func
):
return
func
class
Entropy
(
Module
):
class
Entropy
(
Module
):
...
@@ -89,7 +99,10 @@ class Entropy(Module):
...
@@ -89,7 +99,10 @@ class Entropy(Module):
if
self
.
use_zlib
:
if
self
.
use_zlib
:
self
.
algorithm
=
self
.
gzip
self
.
algorithm
=
self
.
gzip
else
:
else
:
self
.
algorithm
=
self
.
shannon
if
'numpy'
in
sys
.
modules
:
self
.
algorithm
=
self
.
shannon_numpy
else
:
self
.
algorithm
=
self
.
shannon
# Get a list of all other module's results to mark on the entropy graph
# Get a list of all other module's results to mark on the entropy graph
for
(
module
,
obj
)
in
iterator
(
self
.
modules
):
for
(
module
,
obj
)
in
iterator
(
self
.
modules
):
...
@@ -225,19 +238,33 @@ class Entropy(Module):
...
@@ -225,19 +238,33 @@ class Entropy(Module):
entropy
=
0
entropy
=
0
if
data
:
if
data
:
data
=
data
.
encode
(
'latin-1'
)
length
=
len
(
data
)
length
=
len
(
data
)
seen
=
[
0
]
*
256
seen
=
dict
(((
chr
(
x
),
0
)
for
x
in
range
(
0
,
256
)))
for
byte
in
data
:
for
byte
in
data
:
seen
[
byte
]
+=
1
seen
[
byte
]
+=
1
for
x
in
range
(
0
,
256
)
:
for
x
in
seen
:
p_x
=
float
(
seen
[
chr
(
x
)]
)
/
length
p_x
=
float
(
x
)
/
length
if
p_x
>
0
:
if
p_x
>
0
:
entropy
-=
p_x
*
math
.
log
(
p_x
,
2
)
entropy
-=
p_x
*
math
.
log
(
p_x
,
2
)
return
(
entropy
/
8
)
return
(
entropy
/
8
)
def
shannon_numpy
(
self
,
data
):
if
data
:
return
self
.
_shannon_numpy
(
data
.
encode
(
'latin-1'
))
else
:
return
0
@staticmethod
@njit
def
_shannon_numpy
(
data
):
A
=
np
.
frombuffer
(
data
,
dtype
=
np
.
uint8
)
pA
=
np
.
bincount
(
A
)
/
len
(
A
)
entropy
=
-
np
.
nansum
(
pA
*
np
.
log2
(
pA
))
return
(
entropy
/
8
)
def
gzip
(
self
,
data
,
truncate
=
True
):
def
gzip
(
self
,
data
,
truncate
=
True
):
'''
'''
Performs an entropy analysis based on zlib compression ratio.
Performs an entropy analysis based on zlib compression ratio.
...
...
This diff is collapsed.
Click to expand it.
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