您的位置:首页 > 编程语言 > Python开发

用python将LTE-NB NAS消息转换成wireshark文件格式解析

2017-12-21 21:33 931 查看
主要实现十六进制字符型NAS消息数据,通过python脚本转换为wireshark可以识别的pcap文件格式,使用wireshark打开生成的文件后既可以得到相应信元的配置。
原始数据



转换脚本
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部分的数据结构,影响阅读。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: