① pcap文件怎麼解 python
欄位顯示的都是十六進制,其中的數據部分和wireshark打開,顯示的十六進制窗口一樣。
其實如果程序想到得到某一個或某幾個位元組的十進制數,用struct還是很容易轉換的!
② 如何利用libpcap和Python嗅探數據包
你的python沒有安裝好。.py文件沒有被注冊成正確的python關聯。你選擇「始終使用」這個選項吧。 感覺怪怪的。通常我們安裝都會順利。 另外python所在的目錄一定要放到系統環境變數的path里
③ python3.2 下的抓包庫。。無論是pypcap還是scapy。貌似都沒有py3的版本。。跪求一個可以python3用
有一個py3kcap是pycap的封裝版本,可以用於python3版本。
給你一個使用的示例代碼:
#!/usr/bin/env python3.2
import ctypes,sys
from ctypes.util import find_library
#pcap = ctypes.cdll.LoadLibrary("libpcap.so")
pcap = None
if(find_library("libpcap") == None):
print("We are here!")
pcap = ctypes.cdll.LoadLibrary("libpcap.so")
else:
pcap = ctypes.cdll.LoadLibrary(find_library("libpcap"))
# required so we can access bpf_program->bf_insns
"""
struct bpf_program {
u_int bf_len;
struct bpf_insn *bf_insns;}
"""
class bpf_program(ctypes.Structure):
_fields_ = [("bf_len", ctypes.c_int),("bf_insns", ctypes.c_void_p)]
class sockaddr(ctypes.Structure):
_fields_=[("sa_family",ctypes.c_uint16),("sa_data",ctypes.c_char*14)]
class pcap_pkthdr(ctypes.Structure):
_fields_ = [("tv_sec", ctypes.c_long), ("tv_usec", ctypes.c_long), ("caplen", ctypes.c_uint), ("len", ctypes.c_uint)]
pkthdr = pcap_pkthdr()
program = bpf_program()
# prepare args
snaplen = ctypes.c_int(1500)
#buf = ctypes.c_char_p(filter)
optimize = ctypes.c_int(1)
mask = ctypes.c_uint()
net = ctypes.c_uint()
to_ms = ctypes.c_int(100000)
promisc = ctypes.c_int(1)
filter = bytes(str("port 80"), 'ascii')
buf = ctypes.c_char_p(filter)
errbuf = ctypes.create_string_buffer(256)
pcap_close = pcap.pcap_close
pcap_lookupdev = pcap.pcap_lookupdev
pcap_lookupdev.restype = ctypes.c_char_p
#pcap_lookupnet(dev, &net, &mask, errbuf)
pcap_lookupnet = pcap.pcap_lookupnet
#pcap_t *pcap_open_live(const char *device, int snaplen,int promisc, int to_ms,
#char *errbuf
pcap_open_live = pcap.pcap_open_live
#int pcap_compile(pcap_t *p, struct bpf_program *fp,const char *str, int optimize,
#bpf_u_int32 netmask)
pcap_compile = pcap.pcap_compile
#int pcap_setfilter(pcap_t *p, struct bpf_program *fp);
pcap_setfilter = pcap.pcap_setfilter
#const u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h);
pcap_next = pcap.pcap_next
# int pcap_compile_nopcap(int snaplen, int linktype, struct bpf_program *program,
# const char *buf, int optimize, bpf_u_int32 mask);
pcap_geterr = pcap.pcap_geterr
pcap_geterr.restype = ctypes.c_char_p
#check for default lookup device
dev = pcap_lookupdev(errbuf)
#override it for now ..
dev = bytes(str("wlan0"), 'ascii')
if(dev):
print("{0} is the default interface".format(dev))
else:
print("Was not able to find default interface")
if(pcap_lookupnet(dev,ctypes.byref(net),ctypes.byref(mask),errbuf) == -1):
print("Error could not get netmask for device {0}".format(errbuf))
sys.exit(0)
else:
print("Got Required netmask")
handle = pcap_open_live(dev,snaplen,promisc,to_ms,errbuf)
if(handle is False):
print("Error unable to open session : {0}".format(errbuf.value))
sys.exit(0)
else:
print("Pcap open live worked!")
if(pcap_compile(handle,ctypes.byref(program),buf,optimize,mask) == -1):
# this requires we call pcap_geterr() to get the error
err = pcap_geterr(handle)
print("Error could not compile bpf filter because {0}".format(err))
else:
print("Filter Compiled!")
if(pcap_setfilter(handle,ctypes.byref(program)) == -1):
print("Error couldn't install filter {0}".format(errbuf.value))
sys.exit(0)
else:
print("Filter installed!")
if(pcap_next(handle,ctypes.byref(pkthdr)) == -1):
err = pcap_geterr(handle)
print("ERROR pcap_next: {0}".format(err))
print("Got {0} bytes of data".format(pkthdr.len))
pcap_close(handle)
④ 使用pcap庫寫的抓包程序 抓到數據包之後進行修改在發送給伺服器
一般來說,網卡是收不到自己發送出去的數據包的一份拷貝的。
建議樓主研讀《TCP IP詳解卷一:協議》《TCP IP詳解卷二:實現》。
建議使用wireshark這種比較權威的抓包軟體抓包並且解析一下你發送和接收的包的每個位元組是否如你所願。
用 pcap_sendpacket 發送的包是應該要包含原始的乙太網頭部的吧,你的包貌似只有 IP 頭,沒有乙太網的頭部哦。
⑤ python2.7 能安裝libpcap(抓包)庫嗎
你好:
因該是可以的;
請問抱什麼錯誤嗎!
⑥ python怎樣讀取pcap文件
程序如下:
#!/usr/bin/env python
#coding=utf-8
#讀取pcap文件,解析相應的信息,為了在記事本中顯示的方便,把二進制的信息
import struct
fpcap = open('test.pcap','rb')
ftxt = open('result.txt','w')
string_data = fpcap.read()
#pcap文件包頭解析
pcap_header = {}
pcap_header['magic_number'] = string_data[0:4]
pcap_header['version_major'] = string_data[4:6]
pcap_header['version_minor'] = string_data[6:8]
pcap_header['thiszone'] = string_data[8:12]
pcap_header['sigfigs'] = string_data[12:16]
pcap_header['snaplen'] = string_data[16:20]
pcap_header['linktype'] = string_data[20:24]
#把pacp文件頭信息寫入result.txt
ftxt.write("Pcap文件的包頭內容如下: \n")
for key in ['magic_number','version_major','version_minor','thiszone',
'sigfigs','snaplen','linktype']:
ftxt.write(key+ " : " + repr(pcap_header[key])+'\n')
#pcap文件的數據包解析
step = 0
packet_num = 0
packet_data = []
pcap_packet_header = {}
i =24
while(i<len(string_data)):
#數據包頭各個欄位
pcap_packet_header['GMTtime'] = string_data[i:i+4]
pcap_packet_header['MicroTime'] = string_data[i+4:i+8]
pcap_packet_header['caplen'] = string_data[i+8:i+12]
pcap_packet_header['len'] = string_data[i+12:i+16]
#求出此包的包長len
packet_len = struct.unpack('I',pcap_packet_header['len'])[0]
#寫入此包數據
packet_data.append(string_data[i+16:i+16+packet_len])
i = i+ packet_len+16
packet_num+=1
#把pacp文件里的數據包信息寫入result.txt
for i in range(packet_num):
#先寫每一包的包頭
ftxt.write("這是第"+str(i)+"包數據的包頭和數據:"+'\n')
for key in ['GMTtime','MicroTime','caplen','len']:
ftxt.write(key+' : '+repr(pcap_packet_header[key])+'\n')
#再寫數據部分
ftxt.write('此包的數據內容'+repr(packet_data[i])+'\n')
ftxt.write('一共有'+str(packet_num)+"包數據"+'\n')
ftxt.close()
fpcap.close()
最終得到的result文件如下所示:欄位顯示的都是十六進制,其中的數據部分和wireshark打開,顯示的十六進制窗口一樣。
⑦ 在linux下,python怎麼才能抓到網卡上的所有TCP數據包
Ethereal 自帶許多協議的 decoder,簡單,易用,基於winpcap的一個開源的軟體.但是它的架構並不靈活,如何你要加入一個自己定義的的解碼器,得去修改 Ethereal的代碼,再重新編譯,很煩瑣.對於一般的明文 協議,沒有什麼問題,但是對於加密協議,比如網路游戲,客戶端程序一般會在剛連接上的時候,發送一個隨機密鑰,而後的報文都會用這個密鑰進行加密,如此. 要想破解,得要有一個可編程的抓包器.
libpcap是一個不錯的選擇,但是對於抓包這樣需要反復進行」試 驗->修改」這個過程的操作,c 語言顯然不是明智的選擇.
Python提供了幾個libpcapbind。在windows平台上,你需要先安裝winpcap,如果你已經安裝了Ethereal非常好用
一個規范的抓包過程
import pcap
import dpkt
pc=pcap.pcap() #注,參數可為網卡名,如eth0
pc.setfilter('tcp port 80') #設置監聽過濾器
for ptime,pdata in pc: #ptime為收到時間,pdata為收到數據
print ptime,pdata #...
對抓到的乙太網V2數據包(raw packet)進行解包
p=dpkt.ethernet.Ethernet(pdata)
if p.data.__class__.__name__=='IP':
ip='%d.%d.%d.%d'%tuple(map(ord,list(p.data.dst)))
if p.data.data.__class__.__name__=='TCP':
if data.dport==80:
print p.data.data.data # by gashero
一些顯示參數
nrecv,ndrop,nifdrop=pc.stats()
返回的元組中,第一個參數為接收到的數據包,(by gashero)第二個參數為被核心丟棄的數據包。
⑧ python2.7 怎麼進行抓包和解包
1、抓包,可以下載winpcapy 或者自己載入winpcap動態庫
2、解包,使用dpkt解析
參考程序,基於進程抓包QPA工具
⑨ python pcap 導入之後第一行程序就有問題!急求解決辦法!!
這個東西可能是兩個原因。一個就是少安裝了pcap對應的驅動程序,第二個可能是操作系統版本問題。 你可以在一個xp操作系統上試驗。
另外pcap應該有新版本。支持python2.7的。如果沒有自己下源碼編譯一下。
不過,還是建議你用linux來做試驗。這樣全都變得簡單了。
⑩ 如何利用libpcap和Python嗅探數據包
一提到Python獲取數據包的方式,相信很多Python愛好者會利用Linux的libpcap軟體包或利用Windows下的WinPcap可移植版的方式進行抓取數據包,然後再利用dpkt軟體包進行協議分析,我們這里想換一個角度去思考:
1. Python版本的pcap存儲內存數據過小,也就是說緩存不夠,在高並發下容易發生丟包現象,其實C版本的也同樣存在這樣的問題,只不過Python版本的緩存實在是過低,讓人很郁悶。
2. dpkt協議分析並非必須,如果你對RFC 791和RFC 793等協議熟悉的話,完全可以使用struct.unpack的方式進行分析。
如果你平常習慣使用tcpmp抓取數據包的話,完全可以使用它來代替pcap軟體包,只不過我們需要利用tcpmp將抓取的數據以pcap格式進行保存,說道這里大家一定會想到Wireshark工具,具體命令如下:
tcpmp dst 10.13.202.116 and tcp dst port 80 -s 0 -i eth1 -w ../pcap/tcpmp.pcap -C 1k -W 5
我們首先需要對pcap文件格式有所了解,具體信息大家可以參考其他資料文檔,我這里只說其重要的結構體組成,如下:
sturct pcap_file_header
{
DWORD magic;
WORD version_major;
WORD version_minor;
DWORD thiszone;
DWORD sigfigs;
DWORD snaplen;
DWORD linktype;
}
struct pcap_pkthdr
{
struct timeval ts;
DWORD caplen;
DWORD len;
}
struct timeval
{
DWORD GMTtime;
DWORD microTime;
}
這里需要說明的一點是,因為在Python的世界裡一切都是對象,所以往往Python在處理數據包的時候感覺讓人比較麻煩。Python提供了幾個libpcapbind,http://monkey.org/~gsong/pypcap/這里有 一個最簡單的。在windows平台上,你需要先安裝winpcap,如果你已經安裝了Ethereal非常好用。一個規范的抓包過程:
import pcap
import dpkt
pc=pcap.pcap() #注,參數可為網卡名,如eth0
pc.setfilter('tcp port 80') #設置監聽過濾器
for ptime,pdata in pc: #ptime為收到時間,pdata為收到數據
print ptime,pdata #...
對抓到的乙太網V2數據包(raw packet)進行解包:
p=dpkt.ethernet.Ethernet(pdata)
if p.data.__class__.__name__=='IP':
ip='%d.%d.%d.%d'%tuple(map(ord,list(p.data.dst)))
if p.data.data.__class__.__name__=='TCP':
if data.dport==80:
print p.data.data.data
一些顯示參數nrecv,ndrop,nifdrop=pc.stats()返回的元組中,第一個參數為接收到的數據包,第二個參數為被核心丟棄的數據包。
至於對於如何監控tcpmp生成的pcap文件數據,大家可以通過pyinotify軟體包來實現,如下:
class Packer(pyinotify.ProcessEvent):
def __init__(self, proct):
self.proct = proct
self.process = None
def process_IN_CREATE(self, event):
logger.debug("create file: %s in queue" % self.process_IF_START_THREAD(event))
def process_IN_MODIFY(self, event):
self.process_IF_START_THREAD(event)
logger.debug("modify file: %s in queue" % self.process_IF_START_THREAD(event))
def process_IN_DELETE(self, event):
filename = os.path.join(event.path, event.name)
logger.debug("delete file: %s" % filename)
def process_IF_START_THREAD(self, event):
filename = os.path.join(event.path, event.name)
if filename != self.process:
self.process = filename
self.proct.put(filename)
if self.proct.qsize() > 1:
try:
logger.debug("create consumer proct.qsize: %s" % self.proct.qsize())
consumer = Consumer(self.proct)
consumer.start()
except Exception, errmsg:
logger.error("create consumer failed: %s" % errmsg)
return filename
class Factory(object):
def __init__(self, proct):
self.proct = proct
self.manager = pyinotify.WatchManager()
self.mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY
def work(self):
try:
try:
notifier = pyinotify.ThreadedNotifier(self.manager, Packer(self.proct))
notifier.start()
self.manager.add_watch("../pcap", self.mask, rec = True)
notifier.join()
except Exception, errmsg:
logger.error("create notifier failed: %s" % errmsg)
except KeyboardInterrupt, errmsg:
logger.error("factory has been terminated: %s" % errmsg)
在獲得要分析的pcap文件數據之後,就要對其分析了,只要你足夠了解pcap文件格式就可以了,對於我們來講只需要獲得TCP數據段的數據即可,如下:
class Writer(threading.Thread):
def __init__(self, proct, stack):
threading.Thread.__init__(self)
self.proct = proct
self.stack = stack
self.pcap_pkthdr = {}
def run(self):
while True:
filename = self.proct.get()
try:
f = open(filename, "rb")
readlines = f.read()
f.close()
offset = 24
while len(readlines) > offset:
self.pcap_pkthdr["len"] = readlines[offset+12:offset+16]
try:
length = struct.unpack("I", self.pcap_pkthdr["len"])[0]
self.stack.put(readlines[offset+16:offset+16+length])
offset += length + 16
except Exception, errmsg:
logger.error("unpack pcap_pkthdr failed: %s" % errmsg)
except IOError, errmsg:
logger.error("open file failed: %s" % errmsg)
在獲得TCP數據段的數據包之後,問題就簡單多了,根據大家的具體需求就可以進行相應的分析了,我這里是想分析其HTTP協議數據,同樣也藉助了dpkt軟體包進行分析,如下:
def worker(memcache, packet, local_address, remote_address):
try:
p = dpkt.ethernet.Ethernet(packet)
if p.data.__class__.__name__ == "IP":
srcip = "%d.%d.%d.%d" % tuple(map(ord, list(p.data.src)))
dstip = "%d.%d.%d.%d" % tuple(map(ord, list(p.data.dst)))
if p.data.data.__class__.__name__ == "TCP":
tcpacket = p.data.data
if tcpacket.dport == 80 and dstip == local_address:
srcport = tcpacket.sport
key = srcip + ":" + str(srcport)
if tcpacket.data:
if not memcache.has_key(key):
memcache[key] = {}
if not memcache[key].has_key("response"):
memcache[key]["response"] = None
if memcache[key].has_key("data"):
memcache[key]["data"] += tcpacket.data
else:
memcache[key]["data"] = tcpacket.data
else:
if memcache.has_key(key):
memcache[key]["response"] = dpkt.http.Request(memcache[key]["data"])
try:
stackless.tasklet(connection)(memcache[key]["response"], local_address, remote_address)
stackless.run()
except Exception, errmsg:
logger.error("connect remote remote_address failed: %s", errmsg)
logger.debug("old headers(none content-length): %s", memcache[key]["response"])
memcache.pop(key)
except Exception, errmsg:
logger.error("dpkt.ethernet.Ethernet failed in worker: %s", errmsg)
如果大家只是想單純的獲取IP地址、埠、流量信息,那麼問題就更簡單了,這里只是拋磚引玉。另外再提供一段代碼供參考:
import pcap, dpkt, struct
import binascii
def main():
a = pcap.pcap()
a.setfilter('udp portrange 4000-4050')
try:
for i,pdata in a:
p=dpkt.ethernet.Ethernet(pdata)
src='%d.%d.%d.%d' % tuple(map(ord,list(p.data.src)))
dst='%d.%d.%d.%d' % tuple(map(ord,list(p.data.dst)))
sport = p.data.data.sport
dport = p.data.data.dport
qq = int( binascii.hexlify(p.data.data.data[7:11]) , 16 )
print 'QQ: %d, From: %s:%d , To: %s:%d' % (qq,src,sport,dst,dport)
except Exception,e:
print '%s' % e
n = raw_input()
if __name__ == '__main__':
main()