本文将使用python的zlib与base64进行压缩与解压
其中base64模块用于对字节流与字符串的转换,zlib用于数据的压缩与解压
同时在后面附上在JS中遇到需要压缩解压的案例
解压代码:
import base64
import zlib
input_string = 'eJwlzT1OAzEQBeC7pJjSf1rHu0hTRKmQEB0HsLKTjcX6R/YYCQ7DNRAVp8k9MKJ6X/H03sFX8o8rKrh4pn8Efn/2kfD+/XP/+oQ1pET1nHviE3MdHciFQ+ztnFdCrSDXsIX0Une8MZf2IOX2ISIF7j6JS45yuN2CLMlIKH4jNCMqj1nU5ghl93zNNaKGGtrrE73RPtxyZYTe6O/T2ck45ZyB3sOKZnbGklkWo920kPNuvgp9nK1dlNWT0EIJdfgFbwpKlQ=='
# 将字符串解码为字节流
compressed_bytes = base64.b64decode(input_string)
# 解压缩字节流
decompressed_bytes = zlib.decompress(compressed_bytes)
# 将解压后的字节流解码为字符串
decompressed_string = decompressed_bytes.decode('utf-8')
print('result1:', decompressed_string)
压缩代码:
import base64
import zlib
input_string = "areaId=0&cateId=0&cityName=广州&dinnerCountAttrId=&optimusCode=10&originUrl=https://gz.meituan.com/meishi/pn2/&page=2&partner=126&platform=1&riskLevel=1&sort=&userId=754270772&uuid=28725e29921749e7a78f.1685590514.1.0.0"
# 将输入字符串编码为字节流
input_bytes = input_string.encode('utf-8')
# 使用 zlib.compress 方法进行压缩,level 参数指定压缩级别
compressed_bytes = zlib.compress(input_bytes, level=zlib.Z_BEST_COMPRESSION)
# 将压缩后的字节流进行 base64 编码
compressed_string = base64.b64encode(compressed_bytes).decode('utf-8')
print('result2:', compressed_string)
案例网站:aHR0cHM6Ly9nei5tZWl0dWFuLmNvbS9tZWlzaGkvcG4yLw==
目标:该案例网站中的token值
对这个网站(某团)有兴趣的可以看一下这篇文章JS逆向---获取某知名外卖平台数据(_token)_用js美团爬数据_云溪·的博客-CSDN博客
特征:在JS逆向中,将一大串字符串压缩为数组,然后在转为密文,这种加密方式大概率为进制流压缩
在下面案例中:
1、jc为待加密的请求头数据
2、在该代码中先是使用了deflate函数将请求头的数据转换为数组
3、转换为数组后调用iD进行数据加密返回一串密文
这里比较符合进制流压缩加密的特征,下面将尝试对压缩的数据进行解压
过程:
1、将字符串解码为字节流
2、先解压该字节流
3、将解压后的字节流解码为字符串
import base64
import zlib
input_string = 'eJwlzT1OAzEQBeC7pJjSf1rHu0hTRKmQEB0HsLKTjcX6R/YYCQ7DNRAVp8k9MKJ6X/H03sFX8o8rKrh4pn8Efn/2kfD+/XP/+oQ1pET1nHviE3MdHciFQ+ztnFdCrSDXsIX0Une8MZf2IOX2ISIF7j6JS45yuN2CLMlIKH4jNCMqj1nU5ghl93zNNaKGGtrrE73RPtxyZYTe6O/T2ck45ZyB3sOKZnbGklkWo920kPNuvgp9nK1dlNWT0EIJdfgFbwpKlQ=='
compressed_bytes = base64.b64decode(input_string)
print(compressed_bytes) # b'x\x9c%\xcd=N\x031\x10\x05\xe0\xbb\xa4\x98\xd2\x7fZ\xc7\xbbHSD\xa9\x90\x10\x1d\x07\xb0\xb2\x93\x8d\xc5\xfaG\xf6\x18\t\x0e\xc35\x10\x15\xa7\xc9=0\xa2z_\xf1\xf4\xde\xc1W\xf2\x8f+*\xb8x\xa6\x7f\x04~\x7f\xf6\x91\xf0\xfe\xfds\xff\xfa\x845\xa4D\xf5\x9c{\xe2\x13s\x1d\x1d\xc8\x85C\xec\xed\x9cWB\xad \xd7\xb0\x85\xf4Rw\xbc1\x97\xf6 \xe5\xf6!"\x05\xee>\x89K\x8er\xb8\xdd\x82,\xc9H(~#4#*\x8fY\xd4\xe6\x08e\xf7|\xcd5\xa2\x86\x1a\xda\xeb\x13\xbd\xd1>\xdcre\x84\xde\xe8\xef\xd3\xd9\xc98\xe5\x9c\x81\xde\xc3\x8afv\xc6\x92Y\x16\xa3\xdd\xb4\x90\xf3n\xbe\n}\x9c\xad]\x94\xd5\x93\xd0B\tu\xf8\x05o\nJ\x95'
decompressed_bytes = zlib.decompress(compressed_bytes) #
print(decompressed_bytes) # b'"areaId=0&cateId=0&cityName=\xe5\xb9\xbf\xe5\xb7\x9e&dinnerCountAttrId=&optimusCode=10&originUrl=https://gz.meituan.com/meishi/pn2/&page=2&partner=126&platform=1&riskLevel=1&sort=&userId=754270772&uuid=28725e29921749e7a78f.1685590514.1.0.0"'
decompressed_string = decompressed_bytes.decode('utf-8')
print('result1:', decompressed_string) # result1: "areaId=0&cateId=0&cityName=广州&dinnerCountAttrId=&optimusCode=10&originUrl=https://gz.meituan.com/meishi/pn2/&page=2&partner=126&platform=1&riskLevel=1&sort=&userId=754270772&uuid=28725e29921749e7a78f.1685590514.1.0.0"