- 在服务器10.10.27.105上安装scapy
一开始尝试使用pip3 install scapy-python3命令,在python3上安装scapy,但是总是报错,报错的原因也没解决,但貌似scapy一开始是基于python2开发的,后来又拓展到了py3上,直接用python2来写吧。。
pip install scapy
2.pcap.py主要是实现将一个pcap包,按照五元组进行划分,统计出它的流的条数
from scapy.all import *
from scapy.utils import PcapReader
import json
pcaps=rdpcap("test-output.pcap")
print 'the number of packets:',pcaps
lists=[]
for packet in pcaps:
if packet.type == 2048:
if packet[IP].proto==6 or packet[IP].proto==17:
five_tuple = "{}:{}_{}:{}_{}".format(packet[IP].src,packet.sport,packet[IP].dst,packet.dport,packet.proto)
#print five_tuple
print 1
lists.append(five_tuple)
f = open("test.txt",'w')
i=0
dicts = {}
for item in lists:
if lists.count(item)>=1:
dicts[item]=lists.count(item)
i = i+1
print i
# print dict[item]
# f.write(dict[item]+"\n")
json_dicts = json.dumps(dicts,indent=1)
f.write(json_dicts+"\n"+"the number of flows:"+str(len(dicts))+"\n"+"the number of tcp/udp packets:"+str(len(lists)))
print json_dicts
print 'the number of flows:',len(dicts)
print 'the number of tcp/udp packets:',len(lists)
- change-mac.py主要是将一个pcap包的mac地址改成我们需要的地址,在修改pcap的mac地址时,使用过wrcap()函数,但是该函数没有追加模式,导致for循环结束后,新生成的pcap包只有一个数据包,于是查到了另外一种写方法。
from scapy.all import *
from scapy.utils import PcapReader
from scapy.utils import PcapWriter
pcaps=rdpcap("all_one.pcap")
for packet in pcaps:
packet.src = "44:a8:42:48:fd:b8"
packet.dst = "a0:36:9f:9e:81:48"
wrpcap("hahahhahhhh.pcap",packet)
方法二:追加模式
from scapy.utils import PcapWriter
pktdump = PcapWriter("banana.pcap", append=True, sync=True)
...
pktdump.write(pkt)
...
这种方法写到60000万个包的时候会遇到python最大递归深度错误:maximum recursion depth exceeded while calling a Python object,其实原因是在Python里的递归调用是有限制的,可以使用sys模块里的getrecursionlimit方法查看的到,即:
sys.getrecursionlimit()
打开终端运行python,可以看到默认值为1000:
muzhitaoshi@tensorflow-2-vm:~$ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getrecursionlimit()
1000
>>>
有get自然有set,使用sys.setrecursionlimit()设置递归深度最大值:
sys.setrecursionlimit(1000000)
网友评论