python解析WCF协议中application/msbin1格式数据

整理了一下之前遇到的数据格式转换的问题,供他人参考。

一次使用Fiddler抓包时,发现数据乱码:


请忽略WCF Binary按钮,这是后来装的插件,在此之前对网站调试时,右键显示:


搜索后知道这是silverlight应用的WCF协议是微软的(Windows Communication Foundation),使用了application/msbin1格式的数据,然后关键字msbin1,找到:

java解析WCF协议中application/msbin1格式数据

C# WCF BinaryXML(application/msbin1) Silverlight网站数据编解码COM接口及源码和Autohotkey调用示例

没有找到python的数据格式转换方法,但是,博文中提到:


于是找该插件:

waf/WCF-Binary-Message-Inspector

README.md中有安装步骤:


重启后,点击WCF Binary按钮后就看见xml格式的明文,效果:


由于水平有限,看不懂c#,没法转为python代码,只能根据博文二的建议:




经过查找插件源码发现WCF-Binary-Message-Inspector-master\BinaryMessageFiddlerExtension\WcfBinaryConverter.cs为编码/解码源码


现在应该将cs文件转文dll文件,搜索得

如何用CSC.exe来编译Visual C#的代码文件

使用python调用由c#编写的cs生成的dll,搜索后发现需要IronPython调用,安装IronPython,

python与C#的互相调用文中提到:


于是编写代码如下:

import clr
from System import Array, Byte
# from System.Xml import XmlDocument

clr.AddReference("System.Xml")
clr.AddReferenceToFile('WcfBinaryConverter.dll')
from BinaryMessageFiddlerExtension import WcfBinaryConverter as wc

def msbin1_to_str(bin_xml_str):
    byte_array = Array[Byte](tuple(Byte(ord(c)) for c in bin_xml_str))
    root = wc.ConvertWcfBinaryMessageToXml(byte_array).InnerXml
    return root

def str_to_msbin1(xml_str):
    xml_doc = XmlDocument()
    xml_doc.LoadXml(xml_str)
    bin_array = wc.ConvertXmlToWcfBinary(xml_doc)
    msbin1 = ''.join(map(lambda x: chr(int(x)), bin_array))
    return msbin1

if __name__ == '__main__':
    xml_str = open('e://msf.log', 'rb').read()
    print msbin1_to_str(xml_str)

msf.log为,使用str_to_msbin1(xml_str)函数生成,不知道什么情况在IronPython终端可以引入System.Xml,运行py文件时提示找不到Xml包。但是,使用cpython间接调用时不会存在问题


使用终端运行,其中ipy为IronPython

E:\pycharm_workplace\black_list>ipy my_tcp.py

<Get_HistoryPaging xmlns="http://tempuri.org/"><searchtype>2</searchtype><username i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"></username><groupid i:nil="true" xmlns:i="http://www.w3.org/2001/
XMLSchema-instance"></groupid><isStarCond i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"></isStarCond><carid>{car_id}</carid><startTime>{day}T00:00:00</startTime><endTime>{end_day}T23:59:59</endT
ime><pageindex>0</pageindex><pagesize>25000</pagesize></Get_HistoryPaging>

至此,格式转化完成,遗憾的是没有找到直接使用Cpython运行该dll的方法,只能使用os.popen间接运行,如果你有其他方法解决这个问题请留言,谢谢。

WCF-Binary-Message-Inspector-master\BinaryMessageFiddlerExtension

猜你喜欢

转载自blog.csdn.net/fsh_walwal/article/details/79361693