pythonhnm 发表于 2023-10-27 21:42

python dumphex

本帖最后由 pythonhnm 于 2023-10-27 21:46 编辑

import binascii
def h2bin(x):
    return binascii.unhexlify(x.replace(' ', '').replace('\n', ''))
def hexdump(s: bytes):
    for b in range(0, len(s), 16):
      lin = ]
      hxdat = ' '.join('%02X' % c for c in lin)
      pdat = ''.join((chr(c) if 32 <= c <= 126 else '.' )for c in lin)
      print('%04x: %-48s %s' % (b, hxdat, pdat))
   
    print("")
hello = h2bin('''
16 03 02 00 dc 01 00 00 d8 03 02 53
43 5b 90 9d 9b 72 0b bc0c bc 2b 92 a8 48 97 cf
bd 39 04 cc 16 0a 85 0390 9f 77 04 33 d4 de 00
00 66 c0 14 c0 0a c0 22c0 21 00 39 00 38 00 88
00 87 c0 0f c0 05 00 3500 84 c0 12 c0 08 c0 1c
c0 1b 00 16 00 13 c0 0dc0 03 00 0a c0 13 c0 09
c0 1f c0 1e 00 33 00 3200 9a 00 99 00 45 00 44
c0 0e c0 04 00 2f 00 9600 41 c0 11 c0 07 c0 0c
c0 02 00 05 00 04 00 1500 12 00 09 00 14 00 11
00 08 00 06 00 03 00 ff01 00 00 49 00 0b 00 04
03 00 01 02 00 0a 00 3400 32 00 0e 00 0d 00 19
00 0b 00 0c 00 18 00 0900 0a 00 16 00 17 00 08
00 06 00 07 00 14 00 1500 04 00 05 00 12 00 13
00 01 00 02 00 03 00 0f00 10 00 11 00 23 00 00
00 0f 00 01 01                                 
''')
hexdump(hello)
执行结果:
0000: 16 03 02 00 DC 01 00 00 D8 03 02 53 43 5B 90 9D...........SC[..
0010: 9B 72 0B BC 0C BC 2B 92 A8 48 97 CF BD 39 04 CC.r....+..H...9..
0020: 16 0A 85 03 90 9F 77 04 33 D4 DE 00 00 66 C0 14......w.3....f..
0030: C0 0A C0 22 C0 21 00 39 00 38 00 88 00 87 C0 0F...".!.9.8......
0040: C0 05 00 35 00 84 C0 12 C0 08 C0 1C C0 1B 00 16...5............
0050: 00 13 C0 0D C0 03 00 0A C0 13 C0 09 C0 1F C0 1E................
0060: 00 33 00 32 00 9A 00 99 00 45 00 44 C0 0E C0 04.3.2.....E.D....
0070: 00 2F 00 96 00 41 C0 11 C0 07 C0 0C C0 02 00 05./...A..........
0080: 00 04 00 15 00 12 00 09 00 14 00 11 00 08 00 06................
0090: 00 03 00 FF 01 00 00 49 00 0B 00 04 03 00 01 02.......I........
00a0: 00 0A 00 34 00 32 00 0E 00 0D 00 19 00 0B 00 0C...4.2..........
00b0: 00 18 00 09 00 0A 00 16 00 17 00 08 00 06 00 07................
00c0: 00 14 00 15 00 04 00 05 00 12 00 13 00 01 00 02................
00d0: 00 03 00 0F 00 10 00 11 00 23 00 00 00 0F 00 01.........#......
00e0: 01                                             .
心脏滴血漏洞利用脚本里拿的,感觉在命令行上还挺实用

pythonhnm 发表于 2023-10-28 13:53

yjn866y 发表于 2023-10-28 09:10
有高手没。不懂哎

这代码写的有点紧凑了,而且涉及到格式化。。。总的来说,不适合学习,适合直接复制粘贴(

yjn866y 发表于 2023-10-28 09:10

有高手没。不懂哎

max2012 发表于 2023-10-28 11:37

本帖最后由 max2012 于 2023-10-28 11:43 编辑

简单调整一下,效果还可以
页: [1]
查看完整版本: python dumphex