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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liberty888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值