python抓包保存為pcap文件并解析的實例-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供花山網(wǎng)站建設(shè)、花山做網(wǎng)站、花山網(wǎng)站設(shè)計、花山網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、花山企業(yè)網(wǎng)站模板建站服務(wù),十年花山做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

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.muchs.cn/article42/deoeec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(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)

成都網(wǎng)站建設(shè)公司