十六进制数据流一般是我们在进行抓包或者接口进行了加密后,接口返回的数据信息,我们需要对其进行特殊处理后才是我们能直观识别的信息,我们就用Python语言进行解析,这里需要用到以下几个函数。
函数名 | 实例 | 打印 | 解释 |
---|---|---|---|
hex | hex(0x7b) | "0x7b" | 用于将10进制整数转换成16进制,以字符串形式表示. |
int | int("0x7b",16) | 123 | 用于将字符串转换成整数型. |
chr | chr(123) | "{" | 用于整数型或者16进制转换成对应的ascii字符. |
isascii | "{".isascii() | True | 若字符含有ascii字符则返回真. |
binascii | binascii.a2b_hex("e590af") | b'\xe5\x90\xaf' | 16进制字符串转换成2进制数据表示. |
decode | b'\xe5\x90\xaf'.decode() | 启 | 指定编码格式默认为UTF-8 |
注意:chr(0x7b)我们可以直接转成ascii字符,为何我们要介绍那么多呢,因为我们接收到的数据里面可能含有中文字符,所以我们要先进行整数型转换,然后再分流转换。
代码示例如下所示:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#@Users: LiMu
#@Files:SixteenAnalysis.py
#@Times: 2022/11/29
#@Software:PyCharm
import os
import sys
import time
import binascii
__curr_dir = os.path.dirname(os.path.abspath(__file__))
if __curr_dir not in sys.path:
sys.path.insert(0, __curr_dir)
def to_string(source: list):
cour = 0
rlt = []
source_tmp = [hex(i) for i in source]
while cour < len(source):
ele = chr(int(source_tmp[cour], 16))
if ele.isascii():
rlt.append(ele)
cour += 1
else:
tmp = [x[2:] for x in source_tmp[cour: cour + 3]]
rlt.append(binascii.a2b_hex("".join(tmp)).decode())
cour += 3
print("".join(rlt))
if __name__ == '__main__':
arr = [
0x7b, 0x22, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x3a, 0x22, 0x63, 0x6f, 0x64,
0x65, 0x5f, 0x31, 0x36, 0x36, 0x39, 0x36, 0x39, 0x30, 0x31, 0x34, 0x31, 0x32, 0x35, 0x33, 0x22, 0x2c, 0x22,
0x50, 0x75, 0x73, 0x68, 0x4b, 0x65, 0x79, 0x22, 0x3a, 0x6e, 0x75, 0x6c, 0x6c, 0x2c, 0x22, 0x52, 0x65, 0x73,
0x75, 0x6c, 0x74, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x3a, 0x31, 0x32, 0x37, 0x35, 0x2c, 0x22, 0x53,
0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x3a, 0x31, 0x32, 0x37, 0x35, 0x2c, 0x22, 0x53, 0x74, 0x61, 0x74, 0x75,
0x73, 0x4d, 0x73, 0x67, 0x22, 0x3a, 0x22, 0xe6, 0xb4, 0xbb, 0xe5, 0x8a, 0xa8, 0xe6, 0x9c, 0xaa, 0xe5, 0xbc,
0x80, 0xe5, 0x90, 0xaf, 0x22, 0x2c, 0x22, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x3a, 0x6e, 0x75, 0x6c, 0x6c,
0x2c, 0x22, 0x54, 0x69, 0x6d, 0x65, 0x54, 0x69, 0x63, 0x6b, 0x22, 0x3a, 0x31, 0x36, 0x36, 0x39, 0x36, 0x39,
0x30, 0x31, 0x37, 0x36, 0x2c, 0x22, 0x45, 0x78, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x3a, 0x6e, 0x75,
0x6c, 0x6c, 0x7d
]
to_string(arr)
- 执行打印:
D:\Python37\python.exe E:/pythonFiles/Interfacetest/ShellEditing/sixteenAnalysis.py
{"HandleCode":"code_1669690141253","PushKey":null,"ResultStatus":1275,"Status":1275,"StatusMsg":"活动未开启","Value":null,"TimeTick":1669690176,"ExtValue":null}
Process finished with exit code 0