Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scapy
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CodeLinaro
public-release-test-restored
platform
external
scapy
Commits
9fab5d4d
Commit
9fab5d4d
authored
8 years ago
by
Sebastien Mainand
Browse files
Options
Downloads
Patches
Plain Diff
Add dump option to [line|c]hexdump to get the result in a variable
parent
c6e1e6cc
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
scapy/utils.py
+61
-16
61 additions, 16 deletions
scapy/utils.py
with
61 additions
and
16 deletions
scapy/utils.py
+
61
−
16
View file @
9fab5d4d
...
@@ -64,37 +64,83 @@ def lhex(x):
...
@@ -64,37 +64,83 @@ def lhex(x):
return
x
return
x
@conf.commands.register
@conf.commands.register
def
hexdump
(
x
):
def
hexdump
(
x
,
dump
=
False
):
x
=
str
(
x
)
"""
Build a tcpdump like hexadecimal view
:param x: a Packet
:param dump: define if the result must be printed or returned in a variable
:returns: a String only when dump=True
"""
s
=
""
x
=
str
(
x
)
l
=
len
(
x
)
l
=
len
(
x
)
i
=
0
i
=
0
while
i
<
l
:
while
i
<
l
:
print
"
%04x
"
%
i
,
s
+=
"
%04x
"
%
i
for
j
in
xrange
(
16
):
for
j
in
xrange
(
16
):
if
i
+
j
<
l
:
if
i
+
j
<
l
:
print
"
%02X
"
%
ord
(
x
[
i
+
j
])
,
s
+=
"
%02X
"
%
ord
(
x
[
i
+
j
])
else
:
else
:
print
"
"
,
s
+=
"
"
if
j
%
16
==
7
:
if
j
%
16
==
7
:
print
""
,
s
+=
""
print
"
"
,
s
+=
"
"
print
sane_color
(
x
[
i
:
i
+
16
])
s
+=
sane_color
(
x
[
i
:
i
+
16
])
i
+=
16
i
+=
16
s
+=
"
\n
"
# remove trailing \n
if
s
.
endswith
(
"
\n
"
):
s
=
s
[:
-
1
]
if
dump
:
return
s
else
:
print
s
@conf.commands.register
@conf.commands.register
def
linehexdump
(
x
,
onlyasc
=
0
,
onlyhex
=
0
):
def
linehexdump
(
x
,
onlyasc
=
0
,
onlyhex
=
0
,
dump
=
False
):
"""
Build an equivalent view of hexdump() on a single line
Note that setting both onlyasc and onlyhex to 1 results in a empty output
:param x: a Packet
:param onlyasc: 1 to display only the ascii view
:param onlyhex: 1 to display only the hexadecimal view
:param dump: print the view if False
:returns: a String only when dump=True
"""
s
=
""
x
=
str
(
x
)
x
=
str
(
x
)
l
=
len
(
x
)
l
=
len
(
x
)
if
not
onlyasc
:
if
not
onlyasc
:
for
i
in
xrange
(
l
):
for
i
in
xrange
(
l
):
print
"
%02X
"
%
ord
(
x
[
i
]),
s
+=
"
%02X
"
%
ord
(
x
[
i
])
print
""
,
if
not
onlyhex
:
# separate asc & hex if both are displayed
s
+=
"
"
if
not
onlyhex
:
if
not
onlyhex
:
print
sane_color
(
x
)
s
+=
sane_color
(
x
)
if
dump
:
return
s
else
:
print
s
def
chexdump
(
x
):
def
chexdump
(
x
,
dump
=
False
):
x
=
str
(
x
)
"""
Build a per byte hexadecimal representation
print
"
,
"
.
join
(
map
(
lambda
x
:
"
%#04x
"
%
ord
(
x
),
x
))
Example:
>>>
chexdump
(
IP
())
0x45
,
0x00
,
0x00
,
0x14
,
0x00
,
0x01
,
0x00
,
0x00
,
0x40
,
0x00
,
0x7c
,
0xe7
,
0x7f
,
0x00
,
0x00
,
0x01
,
0x7f
,
0x00
,
0x00
,
0x01
:
param
x
:
a
Packet
:
param
dump
:
print
the
view
if
False
:
returns
:
a
String
only
if
dump
=
True
"""
x
=
str
(
x
)
s
=
str
(
"
,
"
.
join
(
map
(
lambda
x
:
"
%#04x
"
%
ord
(
x
),
x
)))
if
dump
:
return
s
else
:
print
s
def
hexstr
(
x
,
onlyasc
=
0
,
onlyhex
=
0
):
def
hexstr
(
x
,
onlyasc
=
0
,
onlyhex
=
0
):
s
=
[]
s
=
[]
...
@@ -104,7 +150,6 @@ def hexstr(x, onlyasc=0, onlyhex=0):
...
@@ -104,7 +150,6 @@ def hexstr(x, onlyasc=0, onlyhex=0):
s
.
append
(
sane
(
x
))
s
.
append
(
sane
(
x
))
return
"
"
.
join
(
s
)
return
"
"
.
join
(
s
)
@conf.commands.register
@conf.commands.register
def
hexdiff
(
x
,
y
):
def
hexdiff
(
x
,
y
):
"""
Show differences between 2 binary strings
"""
"""
Show differences between 2 binary strings
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment