用python将LTE-NB NAS消息转换成wireshark文件格式解析
2017-12-21 21:33
931 查看
主要实现十六进制字符型NAS消息数据,通过python脚本转换为wireshark可以识别的pcap文件格式,使用wireshark打开生成的文件后既可以得到相应信元的配置。
原始数据
转换脚本
注:在pcap文件中DLT类型填写为0x93即147,需要在wireshark中设置DLT=147的协议类型为nas-eps。
wireshark设置
编辑=>首选项=>Protocols=>DLT_USER=>增加"NAS-eps"协议解析;对应修改DLT=147的payload protocal为nas-eps。
wireshark打开脚本转换的文件
关于RRC消息解析
同样可以修改Payload protocal的类型进行RRC消息的解析,目前存在问题尚未找到同时解析不同协议消息的方式(????)。由于DLT类型是在文件的Global Header设置的,而设置类型只能选择一个,同样在User DLTs设置时每个DLT只能选择一个对应的Payload protocal协议,所以每个文件对应只有一个协议类型。
遗留问题
拼接才是最好的方式:目前的转换代码的实现,需要将packet data的字节数设置为word对齐,其好处是处理方便,导致的问题是在不满整个word的数据会补充为0,在解析时没法解析补0部分的数据结构,影响阅读。
原始数据
转换脚本
class ConvertEngine(object): '''''' def __init__(self): self.outfile = open("nas.pcap",'wb') #general global header b = 0xa1b2c3d4 covertValue = struct.pack("L",b) self.outfile.write(covertValue) b = 0x00040002 covertValue = struct.pack("L",b) self.outfile.write(covertValue) b = 0x00000000 covertValue = struct.pack("L",b) self.outfile.write(covertValue) b = 0x00000000 covertValue = struct.pack("L",b) self.outfile.write(covertValue) b = 0x00019000 covertValue = struct.pack("L",b) self.outfile.write(covertValue) b = 0x00000093 covertValue = struct.pack("L",b) self.outfile.write(covertValue) #pass def __del__(self): self.outfile.close() def convertLineToPcap(self,line): if(len(line)>0): tmpInfile = open("tmp.txt",'w') line = line.replace("\n",'') tmpInfile.write("%s"%line) tmpInfile.close() tmpOutfile = open("tmp.txt",'r') dataLen = (len(line))>>1 #bytes number,word align dataLen = (dataLen + 3)>>2 dataLen = dataLen << 2 print(dataLen) if(dataLen > 0): print("generate headers") #general packet header b = 0x00000000 b4a1 covertValue = struct.pack("L",b) self.outfile.write(covertValue) #b = 0x00000000 #covertValue = struct.pack("L",b) self.outfile.write(covertValue) b = dataLen covertValue = struct.pack("L",b) self.outfile.write(covertValue) b = dataLen covertValue = struct.pack("L",b) self.outfile.write(covertValue) else: return #general packet Data while True: a = tmpOutfile.read(8) #print(type(a)) #print(b); if not a: break a = a.replace("\n",'') if(len(a)!= 0): if(len(a) != 8): shiftValue = 8 - len(a) shiftValue = shiftValue >> 1 while(shiftValue > 0): a = a+'00' shiftValue = shiftValue - 1 #16进制转换10进制数 #b=int(a[::-1],16) #反向步进 b=int(a[6:8]+a[4:6]+a[2:4]+a[0:2],16) covertValue = struct.pack("L",b) self.outfile.write(covertValue) tmpOutfile.close() def convertFileToPcap(self,filename): srcFile = open(filename,'r') for eachline in srcFile.readlines(): self.convertLineToPcap(eachline) srcFile.close() if __name__ == '__main__': covertFromTxt = ConvertEngine() covertFromTxt.convertFileToPcap("srcData.txt")
注:在pcap文件中DLT类型填写为0x93即147,需要在wireshark中设置DLT=147的协议类型为nas-eps。
wireshark设置
编辑=>首选项=>Protocols=>DLT_USER=>增加"NAS-eps"协议解析;对应修改DLT=147的payload protocal为nas-eps。
wireshark打开脚本转换的文件
关于RRC消息解析
同样可以修改Payload protocal的类型进行RRC消息的解析,目前存在问题尚未找到同时解析不同协议消息的方式(????)。由于DLT类型是在文件的Global Header设置的,而设置类型只能选择一个,同样在User DLTs设置时每个DLT只能选择一个对应的Payload protocal协议,所以每个文件对应只有一个协议类型。
遗留问题
拼接才是最好的方式:目前的转换代码的实现,需要将packet data的字节数设置为word对齐,其好处是处理方便,导致的问题是在不满整个word的数据会补充为0,在解析时没法解析补0部分的数据结构,影响阅读。
相关文章推荐
- Ubuntu 14.04 64bit上解析wireshark抓包pcap文件格式和源码实现
- python 在linux中把doc转换为docx格式文件(支持word97和word2003)
- xslt格式转换——解析指定文件 转换为指定格式
- 用 python 脚本,把当前目录及子目录下的 wav 音频文件转换为 flac 格式
- Python:将utf-8格式的文件转换成gbk格式的文件
- python 对文件编码格式转换小工具
- Python实现把json格式转换成文本或sql文件
- Python3 将configparser从ini文件中读取的内容转换成字典格式
- 使用PYTHON解析Wireshark的PCAP文件
- python实现txt文件格式转换为arff格式
- 二进制文件处理 WAV文件格式解析【利用python 将wav文件音量变小】
- Python:将utf-8格式的文件转换成gbk格式的文件
- Python实现把utf-8格式的文件转换成gbk格式的文件
- Python实现把json格式转换成文本或sql文件
- python将json格式的数据转换成文本格式的数据或sql文件
- python转换格式,Matlab读取文件
- 用Python实现CSV格式文件转换为HTML文件[Python3程序开发指南实现]
- CASIA中文手写体字库gnt文件格式解析(python)
- Wireshark的Pcap文件格式分析及解析源码
- python将.ppm格式图片转换成.jpg格式文件的方法