import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import re
#you can used it in all code
def ChangeHex(element): #只要十六进制
return len(element)>=5 and element[3] or ‘True’
#you can used it in split lines for analysis datas
def MySub(data): #构造正则规则函数 creat re regular function
return re.sub(r'[()"'';]',' ',data)
def MySplit(data): #数据分裂函数 this function can split data by used ‘,’ in datas
return data.split(’,’)
def ChangeTimes(element): #只要时间 just display times
return len(element)>=10 and element[10:-3] or ‘True’
def TheNumberOfColumn(SplitData): #每行列数共有几种 statistical how many different number of columns in datas
return set([len(i) for i in SplitData])
def SelectRowByLength(SplitData,Number): #通过长度选行 display data by datalength
return [i for i in SplitData if len(i) == Number]
def HowManyUpOrDownloadInData(SplitData): #统计有多少上传和下载 how many up or download
Element = [i[0] for i in SplitData] #上传和下载列
CountedForEveryElements = list(set(Element)) # 上传和下载标识
return [(str(Element.count(i))+str(i)) for i in CountedForEveryElements]
#you can used it ananlysis hex datas
def SelectColumnFormTheSameNumberOfColumns(The_length_of,NO): #确定相同长度数据包后筛选需要的长度的数据 display data by same length
return [e[NO] for e in The_length_of]
#打开并处理文件’’’
data = list(map(MySub,(open(’/Users/deleter/Downloads/gongyioutput.txt’).read().split(’/r/n’)))) #打开数据 opend datas
SplitData = list(map(MySplit,data)) #行数据分裂 lines data split
SetNumber = TheNumberOfColumn(SplitData) #显示有几种长度分类
ClassifyDataOfLength = [SelectRowByLength(SplitData,i)for i in SetNumber] #构造按长度分类的数据数据排序
The_length_of_1,The_length_of_5,The_length_of_6,The_length_of_7,The_length_of_8,The_length_of_18,The_length_of_19 = ClassifyDataOfLength #拆包显示不同长度数据,并且一一赋值、
ChooseFromHexDataOfLength5InData = SelectColumnFormTheSameNumberOfColumns(The_length_of_5,3) #以下分析以此为例
‘’‘开始统计’’’
def DisplayHexByLength(HexData): #统计十六进制数据长度共有多少 how many kinds of hex data length
HexDataLength = [len(i) for i in HexData ]
SetOfHexDataLength = set(HexDataLength)
DictOfLengthAndCounted = {}
for i in SetOfHexDataLength:
DictOfLengthAndCounted[HexDataLength.count(i)] = i
SortedHex = sorted(DictOfLengthAndCounted.items(),key = lambda x:x[0],reverse = True)
return SortedHex
def ChooseFromHexDataOfLengthByMyChoice(HexData,ArbitraryLength): #只显示相同长度的十六进制数据 just display hex data by the same length
return [e for e in HexData if len(e) == ArbitrryLength]
def Percent(HexData): #显示相同长度的
HexDataLength = DisplayHexByLength(HexData)
SumForEveryLongth = sum([k for k,v in HexDataLength])
return [‘percent: {:.2%}’.format(k/SumForEveryLongth) for k,v in HexDataLength]
def FrequencyTableOfHexDatas(HexData): # 16进制长度频率分布
HexDataLength = DisplayHexByLength(HexData) #长度为5的数据包里的16进制数据有几种长度
PercentHexData = Percent(HexData)
LoopRange = range(len(HexDataLength)-1)
return [(HexDataLength[i],PercentHexData[i]) for i in LoopRange]
def DisplayHexDataByTheSameLength(HexData,ArbitraryLength): #按长度选择十六进制数据
return [e for e in HexData if len(e) == ArbitraryLength]
#’’‘进入双字节统计’’’
def Dword(HexData,Number1,Number2): #列双字节截取
return [i[Number1:Number2] for i in HexData]
def RowsToDowrd(HexDataRows): #行十六进制转双字节
return [HexDataRows[i-2:i] for i in range(1,len(HexDataRows),2)]
def MapHexMatrix(Hexdata): #双字节转矩阵函数
return np.matrix(list(map(RowsToDowrd,Hexdata)))
pass #没想好怎么写循环命名函数
def LoopNameHexData(HexData):
No = 0
Names = []
Myrange = len(HexData)+1
for i in range(1,Myrange,2):
No+=1
b = ‘hex_’ + str(int(i-No))
Names.append(b)
exec(b + ‘= %r’ % [Dword(HexData,int(i-2),i)])
def TestTimes(MapHexMatrix): #时间测验函数
Number1 = np.array([int(i[0,4]+i[0,3]+i[0,2]+i[0,1],16) for i in MapHexMatrix])
Number2 = [Number1[i] for i in range(-1,len(Number1)-1)]
return Number2-Number1
The_length_of_1,The_length_of_5,The_length_of_6,The_length_of_7,The_length_of_8,The_length_of_18,The_length_of_19 = ClassifyDataOfLength #拆包显示不同长度数据,并且一一赋值、
ChooseFromHexDataOfLength5InData = SelectColumnFormTheSameNumberOfColumns(The_length_of_5,3) #选择长度为5数据行中的16进制数据包
HexDataLength = DisplayHexByLength(ChooseFromHexDataOfLength5InData) #长度为5的数据包里的16进制数据有几种长度
PercentHexData = Percent(ChooseFromHexDataOfLength5InData) #16进制长度频率分布
GroupByHexDataLength = FrequencyTableOfHexDatas(ChooseFromHexDataOfLength5InData)
JustDisplayHexDataByLengthOf64 = DisplayHexDataByTheSameLength(ChooseFromHexDataOfLength5InData,64) #只显示长度为64的十六进制数据
HexDataMatrix = MapHexMatrix(JustDisplayHexDataByLengthOf64) #十六进制矩阵
#循环赋值并命名16进制列
No = 0
Names = []
for i in range(1,65,2):
No+=1
b = 'hex_' + str(int(i-No))
Names.append(b)
exec(b + ‘= %r’ % [Dword(JustDisplayHexDataByLengthOf64,int(i-2),i)])
#打印出已赋值列名
print(Names)
print(TestTimes(HexDataMatrix)) # 测验时间验证
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import re
#you can used it in all code
def ChangeHex(element): #只要十六进制
return len(element)>=5 and element[3] or ‘True’
#you can used it in split lines for analysis datas
def MySub(data): #构造正则规则函数 creat re regular function
return re.sub(r’[()"’’;]’,’ ‘,data)
def MySplit(data): #数据分裂函数 this function can split data by used ‘,’ in datas
return data.split(’,’)
def ChangeTimes(element): #只要时间 just display times
return len(element)>=10 and element[10:-3] or ‘True’
def TheNumberOfColumn(SplitData): #每行列数共有几种 statistical how many different number of columns in datas
return set([len(i) for i in SplitData])
def SelectRowByLength(SplitData,Number): #通过长度选行 display data by datalength
return [i for i in SplitData if len(i) == Number]
def HowManyUpOrDownloadInData(SplitData): #统计有多少上传和下载 how many up or download
Element = [i[0] for i in SplitData] #上传和下载列
CountedForEveryElements = list(set(Element)) # 上传和下载标识
return [(str(Element.count(i))+str(i)) for i in CountedForEveryElements]
#you can used it ananlysis hex datas
def SelectColumnFormTheSameNumberOfColumns(The_length_of,NO): #确定相同长度数据包后筛选需要的长度的数据 display data by same length
return [e[NO] for e in The_length_of]
‘’‘打开并处理文件’’’
data = list(map(MySub,(open(’/Users/deleter/Downloads/gongyioutput.txt’).read().split(’/r/n’)))) #打开数据 opend datas
SplitData = list(map(MySplit,data)) #行数据分裂 lines data split
SetNumber = TheNumberOfColumn(SplitData) #显示有几种长度分类
ClassifyDataOfLength = [SelectRowByLength(SplitData,i)for i in SetNumber] #构造按长度分类的数据数据排序
The_length_of_1,The_length_of_5,The_length_of_6,The_length_of_7,The_length_of_8,The_length_of_18,The_length_of_19 = ClassifyDataOfLength #拆包显示不同长度数据,并且一一赋值、
ChooseFromHexDataOfLength5InData = SelectColumnFormTheSameNumberOfColumns(The_length_of_5,3) #以下分析以此为例
‘’‘开始统计’’’
def DisplayHexByLength(HexData): #统计十六进制数据长度共有多少 how many kinds of hex data length
HexDataLength = [len(i) for i in HexData ]
SetOfHexDataLength = set(HexDataLength)
DictOfLengthAndCounted = {}
for i in SetOfHexDataLength:
DictOfLengthAndCounted[HexDataLength.count(i)] = i
SortedHex = sorted(DictOfLengthAndCounted.items(),key = lambda x:x[0],reverse = True)
return SortedHex
def ChooseFromHexDataOfLengthByMyChoice(HexData,ArbitraryLength): #只显示相同长度的十六进制数据 just display hex data by the same length
return [e for e in HexData if len(e) == ArbitrryLength]
def Percent(HexData): #显示相同长度的
HexDataLength = DisplayHexByLength(HexData)
SumForEveryLongth = sum([k for k,v in HexDataLength])
return [‘percent: {:.2%}’.format(k/SumForEveryLongth) for k,v in HexDataLength]
def FrequencyTableOfHexDatas(HexData): # 16进制长度频率分布
HexDataLength = DisplayHexByLength(HexData) #长度为5的数据包里的16进制数据有几种长度
PercentHexData = Percent(HexData)
LoopRange = range(len(HexDataLength)-1)
return [(HexDataLength[i],PercentHexData[i]) for i in LoopRange]
def DisplayHexDataByTheSameLength(HexData,ArbitraryLength): #按长度选择十六进制数据
return [e for e in HexData if len(e) == ArbitraryLength]
#’’‘进入双字节统计’’’
def Dword(HexData,Number1,Number2): #列双字节截取
return [i[Number1:Number2] for i in HexData]
def RowsToDowrd(HexDataRows): #行十六进制转双字节
return [HexDataRows[i-2:i] for i in range(1,len(HexDataRows),2)]
def MapHexMatrix(Hexdata): #双字节转矩阵函数
return np.matrix(list(map(RowsToDowrd,Hexdata)))
pass #没想好怎么写循环命名函数
def LoopNameHexData(HexData):
No = 0
Names = []
Myrange = len(HexData)+1
for i in range(1,Myrange,2):
No+=1
b = ‘hex_’ + str(int(i-No))
Names.append(b)
exec(b + ‘= %r’ % [Dword(HexData,int(i-2),i)])
def TestTimes(MapHexMatrix): #时间测验函数
Number1 = np.array([int(i[0,4]+i[0,3]+i[0,2]+i[0,1],16) for i in MapHexMatrix])
Number2 = [Number1[i] for i in range(-1,len(Number1)-1)]
return Number2-Number1
The_length_of_1,The_length_of_5,The_length_of_6,The_length_of_7,The_length_of_8,The_length_of_18,The_length_of_19 = ClassifyDataOfLength #拆包显示不同长度数据,并且一一赋值、
ChooseFromHexDataOfLength5InData = SelectColumnFormTheSameNumberOfColumns(The_length_of_5,3) #选择长度为5数据行中的16进制数据包
HexDataLength = DisplayHexByLength(ChooseFromHexDataOfLength5InData) #长度为5的数据包里的16进制数据有几种长度
PercentHexData = Percent(ChooseFromHexDataOfLength5InData) #16进制长度频率分布
GroupByHexDataLength = FrequencyTableOfHexDatas(ChooseFromHexDataOfLength5InData)
JustDisplayHexDataByLengthOf64 = DisplayHexDataByTheSameLength(ChooseFromHexDataOfLength5InData,64) #只显示长度为64的十六进制数据
HexDataMatrix = MapHexMatrix(JustDisplayHexDataByLengthOf64) #十六进制矩阵
#循环赋值并命名16进制列
No = 0
Names = []
for i in range(1,65,2):
No+=1
b = ‘hex_’ + str(int(i-No))
Names.append(b)
exec(b + ‘= %r’ % [Dword(JustDisplayHexDataByLengthOf64,int(i-2),i)])
#打印出已赋值列名
print(Names)
print(TestTimes(HexDataMatrix)) # 测验时间验证