Skip to content

Instantly share code, notes, and snippets.

@1mm0rt41PC
Forked from 7h3rAm/hexdump.py
Last active October 12, 2023 09:02
Show Gist options
  • Select an option

  • Save 1mm0rt41PC/c340564823f283fe530b to your computer and use it in GitHub Desktop.

Select an option

Save 1mm0rt41PC/c340564823f283fe530b to your computer and use it in GitHub Desktop.
hexdump with full support for python2.x and python 3.x
def hexdump( src, length=16, sep='.' ):
'''
@brief Return {src} in hex dump.
@param[in] length {Int} Nb Bytes by row.
@param[in] sep {Char} For the text part, {sep} will be used for non ASCII char.
@return {Str} The hexdump
@note Full support for python2 and python3 !
'''
result = [];
# Python3 support
try:
xrange(0,1);
except NameError:
xrange = range;
for i in xrange(0, len(src), length):
subSrc = src[i:i+length];
hexa = '';
isMiddle = False;
for h in xrange(0,len(subSrc)):
if h == length/2:
hexa += ' ';
h = subSrc[h];
if not isinstance(h, int):
h = ord(h);
h = hex(h).replace('0x','');
if len(h) == 1:
h = '0'+h;
hexa += h+' ';
hexa = hexa.strip(' ');
text = '';
for c in subSrc:
if not isinstance(c, int):
c = ord(c);
if 0x20 <= c < 0x7F:
text += chr(c);
else:
text += sep;
result.append(('%08X: %-'+str(length*(2+1)+1)+'s |%s|') % (i, hexa, text));
return '\n'.join(result);
################################################################################
# Basic usage:
from binascii import unhexlify, hexlify;
def h2bin( x ):
return unhexlify(str.encode(x.replace(' ', '').replace('\n', '')));
hello_openvpn = h2bin('''
16 03 01 00 df 01 00 00 db 03 01 95 a3 8a 7f 46
a9 1c 78 99 21 ae 92 6d 2d 14 5a 8f 2b c8 ee e2
0b 9e 38 34 ec 3d 66 2b 9c d5 63 00 00 68 c0 14
c0 0a c0 22 c0 21 00 39 00 38 00 88 00 87 c0 0f
c0 05 00 35 00 84 c0 12 c0 08 c0 1c c0 1b 00 16
00 13 c0 0d c0 03 00 0a c0 13 c0 09 c0 1f c0 1e
00 33 00 32 00 9a 00 99 00 45 00 44 c0 0e c0 04
00 2f 00 96 00 41 00 07 c0 11 c0 07 c0 0c c0 02
00 05 00 04 00 15 00 12 00 09 00 14 00 11 00 08
00 06 00 03 00 ff 02 01 00 00 49 00 0b 00 04 03
00 01 02 00 0a 00 34 00 32 00 0e 00 0d 00 19 00
0b 00 0c 00 18 00 09 00 0a 00 16 00 17 00 08 00
06 00 07 00 14 00 15 00 04 00 05 00 12 00 13 00
01 00 02 00 03 00 0f 00 10 00 11 00 23 00 00 00
0f 00 01 01
''');
print(hexdump(hello_openvpn));
# Will print:
'''
00000000: 16 03 01 00 df 01 00 00 db 03 01 95 a3 8a 7f 46 |...............F|
00000010: a9 1c 78 99 21 ae 92 6d 2d 14 5a 8f 2b c8 ee e2 |..x.!..m-.Z.+...|
00000020: 0b 9e 38 34 ec 3d 66 2b 9c d5 63 00 00 68 c0 14 |..84.=f+..c..h..|
00000030: c0 0a c0 22 c0 21 00 39 00 38 00 88 00 87 c0 0f |...".!.9.8......|
00000040: c0 05 00 35 00 84 c0 12 c0 08 c0 1c c0 1b 00 16 |...5............|
00000050: 00 13 c0 0d c0 03 00 0a c0 13 c0 09 c0 1f c0 1e |................|
00000060: 00 33 00 32 00 9a 00 99 00 45 00 44 c0 0e c0 04 |.3.2.....E.D....|
00000070: 00 2f 00 96 00 41 00 07 c0 11 c0 07 c0 0c c0 02 |./...A..........|
00000080: 00 05 00 04 00 15 00 12 00 09 00 14 00 11 00 08 |................|
00000090: 00 06 00 03 00 ff 02 01 00 00 49 00 0b 00 04 03 |..........I.....|
000000A0: 00 01 02 00 0a 00 34 00 32 00 0e 00 0d 00 19 00 |......4.2.......|
000000B0: 0b 00 0c 00 18 00 09 00 0a 00 16 00 17 00 08 00 |................|
000000C0: 06 00 07 00 14 00 15 00 04 00 05 00 12 00 13 00 |................|
000000D0: 01 00 02 00 03 00 0f 00 10 00 11 00 23 00 00 00 |............#...|
000000E0: 0f 00 01 01 |....|
'''
@JonathonReinhart
Copy link
Copy Markdown

What is going on with all of those semicolons?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment