首先是抓包,使用scapy模塊,

sniff()函數(shù) 在其中參數(shù)為本地文件路徑時,操作為打開本地文件
若參數(shù)為BPF過濾規(guī)則和回調(diào)函數(shù),則進行Sniff,回調(diào)函數(shù)用于對Sniff到的數(shù)據(jù)包進行處理
import os
from scapy.all import *
pkts=[]
count=0
pcapnum=0
filename=''
def test_dump_file(dump_file):
print "Testing the dump file..."
if os.path.exists(dump_file):
print "dump fie %s found." %dump_file
pkts=sniff(offline=dump_file)
count = 0
while (count<=2):
print "----Dumping pkt:%s----" %dump_file
print hexdump(pkts[count])
count +=1
else:
print "dump fie %s not found." %dump_file
def write_cap(x):
global pkts
global count
global pcapnum
global filename
pkts.append(x)
count +=1
if count ==3: <span >#每3個TCP操作封為一個包(為了檢測正確性,使用時盡量增多)</span>
pcapnum +=1
pname="pcap%d.pcap"%pcapnum
wrpcap(pname,pkts)
filename ="./pcap%d.pcap"%pcapnum
test_dump_file(filename)
pkts=[]
count=0
if __name__=='__main__':
print "Start packet capturing and dumping ..."
sniff(filter="dst net 127.0.0.1 and tcp",prn=write_cap) #BPF過濾規(guī)則
當前名稱:python抓包保存為pcap文件并解析的實例-創(chuàng)新互聯(lián)
本文鏈接:http://www.yijiale78.com/article42/deoeec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、網(wǎng)站建設(shè)、用戶體驗、App開發(fā)、全網(wǎng)營銷推廣、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容