python使用blackboxprotobuf解析protobuf
python使用blackboxprotobuf解析protobuf
·
安装依赖
pip uninstall crypto pycryptodome pip install pycryptodome 使用这个包
pip install protobuf
pip install blackboxprotobuf
下面代码使用的python3.7
conda create -n python37 python=3.7 虚拟了一个3.7的环境
activte python37
#coding=utf-8
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import binascii
import requests
import line_pb2
import line_req_pb2
from google.protobuf.json_format import MessageToJson
import urllib.parse
import blackboxprotobuf
def unserialize(enc_str):
print('unserialize:',binascii.b2a_hex(enc_str))
root = line_pb2.MsgRoot()
print('eee:', root)
root.ParseFromString(enc_str)
return MessageToJson(root)
def serialize():
root = line_req_pb2.LineReqRoot()
msg1 = root.msg1.add()
msg1.field1="/protoc.Request.Sequence"
msg2 = msg1.msg2.add()
msg2.field1 = "8路"
msg2.field2 = 1
print(root.SerializeToString())
return root.SerializeToString()
def aes_encry(ori):
key = '2fd3028e14a45d1f8b6eb0b2adb7caaf'
iv = '754c8fd584facf6210376b2b72b063e4'
aes = AES.new(binascii.a2b_hex(key), AES.MODE_CBC, binascii.a2b_hex(iv))
return aes.encrypt(pad(ori, 16))
def aes_decry(ori):
key = '2fd3028e14a45d1f8b6eb0b2adb7caaf'
iv = '754c8fd584facf6210376b2b72b063e4'
aes = AES.new(binascii.a2b_hex(key), AES.MODE_CBC, binascii.a2b_hex(iv))
return unpad(aes.decrypt(ori), 16)
def req(url, data):
header = {
'Accept': 'application/json,application/xml,application/xhtml+xml,text/html;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'User-Agent': 'Mozilla/5.0 (Linux; U; Android 6.0; zh-cn; Nexus 6P Build/MDA89D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
'Host': 'lbs.jt.sh.cn:8082'
}
r = requests.post(url=url, data=data, headers=header)
print(r.status_code)
return r.content
def start_with_blackboxprotobuf():
with open(r"D:/code/section/lesson10-protobuf/req.bin", "rb") as fp:
data = fp.read()
message, typedef = blackboxprotobuf.protobuf_to_json(data, message_type=None)
bus_data = blackboxprotobuf.decode_message(data, message_type=None)[0]
print(message)
print(bus_data)
print(typedef)
#bus_data['1']['2']['2'] = bytes('张江1路','utf-8')
bus_data['1']['2']['2'] = bytes('90路','utf-8')
serializedata = blackboxprotobuf.encode_message(bus_data, message_type=typedef)
print(serializedata)
#对序列化后的数据aes加密
aesdata = aes_encry(serializedata)
#b64 aes加密数据
b64_aes_data = base64.b64encode(aesdata)
#抓取
postdata = 'request='+ urllib.parse.quote(b64_aes_data.decode(), safe='')+'%0A'
res_data = req('http://lbs.jt.sh.cn:8082/app/rls/monitor', postdata)
decry_data = aes_decry(res_data)
json_data, type_data = blackboxprotobuf.protobuf_to_json(decry_data, message_type=None)
print(json_data)
def start():
#序列化请求数据
serializedata = serialize()
#对序列化后的数据aes加密
aesdata = aes_encry(serializedata)
#b64 aes加密数据
b64_aes_data = base64.b64encode(aesdata)
#抓取
postdata = 'request='+ urllib.parse.quote(b64_aes_data.decode(), safe='')+'%0A'
res_data = req('http://lbs.jt.sh.cn:8082/app/rls/monitor', postdata)
decry_data = aes_decry(res_data)
json_data = unserialize(decry_data)
print(json_data)
def test_aes():
#with open('C:\Users\40454\Desktop\python\monitor.bin','rb') as f:
# c = f.read()
#dec = base64.b64decode('DR/cdnArHsPdJIp/tFFkChEV81XrINHyEACRVkp432/3ESEY8X0SKjoSpWdfQNMG')
dec = base64.b64decode('jUVuh8aAGi4Ykgcy+AoRJ+epVI/Z8vi0Xtq4/WKeYL2AQoe2+kuwX7yLKEqw65eD')
print(dec)
key = '2fd3028e14a45d1f8b6eb0b2adb7caaf'
#key = '8e02d32f1f5da414b2b06e8bafcab7ad'
#iv = '50d1e5ff43000000c0ae32f7908e30f5'
iv = '754c8fd584facf6210376b2b72b063e4'
aes = AES.new(binascii.a2b_hex(key), AES.MODE_CBC, binascii.a2b_hex(iv))
dec = aes.decrypt(binascii.a2b_hex('8509209294464b3e84a122800c9419068fa44cb5827e4df3db42212a6054243a55793243b8d6479773d67ab74749611d987ab38c274bf716a2c66a8f233e9683667af7e84119d371b9926abc6f8294b266534ddb25f8ef015a16c60b770d3198'))
print(dec)
message, typedef = blackboxprotobuf.protobuf_to_json(unpad(dec,16), message_type=None)
bus_data = blackboxprotobuf.decode_message(unpad(dec,16), message_type=None)[0]
print('33333')
print(message)
print('444444')
print(bus_data)
print('555555')
print(typedef)
#with open('D:\code\section\lesson10-protobuf\monitor.bin','rb') as f:
# c = f.read()
# t = aes.decrypt(c)
# print('ccc:', t)
#print('json:', unserialize(unpad(t,16)))
#enc_str = binascii.a2b_hex('0a240a182f70726f746f632e526571756573742e53657175656e63651208120438e8b7af1801')
#start()
#start_with_blackboxprotobuf()
test_aes()
更多推荐
所有评论(0)