嗨,大家好我如何收集pcap文件中每個數據包的數據包長度?非常感謝在pcap文件中收集數據包長度
1
A
回答
7
我建議一種高科技方法,很少有人知道:閱讀文檔。
人PCAP告訴我們有可實際上是兩個不同的長度:
caplen a bpf_u_int32 giving the number of bytes of the packet that are available from the capture len a bpf_u_int32 giving the length of the packet, in bytes (which might be more than the number of bytes available from the cap- ture, if the length of the packet is larger than the maximum num- ber of bytes to capture)
用C的一個例子:
/* Grab a packet */ packet = pcap_next(handle, &header); if (packet == NULL) { /* End of file */ break; } printf ("Got a packet with length of [%d] \n", header.len);
另外一個在Python與pcapy library:
import pcapy reader = pcapy.open_offline("packets.pcap") while True: try: (header, payload) = reader.next() print "Got a packet of length %d" % header.getlen() except pcapy.PcapError: break
0
下面的兩個例子工作正常:
- 使用python使用C,WinPcap的
- ,Scapy的
(WinPcap的)(編譯CL,微軟VC) 我已經寫了這個功能(C語言)來獲得數據包的大小並能正常工作。 不要忘了包括編譯器的預處理器
u_int getpkt_size(char * pcapfile){
pcap_t *indesc;
char errbuf[PCAP_ERRBUF_SIZE];
char source[PCAP_BUF_SIZE];
u_int res;
struct pcap_pkthdr *pktheader;
u_char *pktdata;
u_int pktsize=0;
/* Create the source string according to the new WinPcap syntax */
if (pcap_createsrcstr(source, // variable that will keep the source string
PCAP_SRC_FILE, // we want to open a file
NULL, // remote host
NULL, // port on the remote host
pcapfile, // name of the file we want to open
errbuf // error buffer
) != 0)
{
fprintf(stderr,"\nError creating a source string\n");
return 0;
}
/* Open the capture file */
if ((indesc= pcap_open(source, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf)) == NULL)
{
fprintf(stderr,"\nUnable to open the file %s.\n", source);
return 0;
}
/* get the first packet*/
res=pcap_next_ex(indesc, &pktheader, &pktdata);
if (res !=1){
printf("\nError Reading PCAP File");
return 0;
}
/* Get the packet size*/
pktsize=pktheader->len;
/* Close the input file */
pcap_close(indesc);
return pktsize;
}
用精彩SCAPY
from scapy.all import *
pkts=rdpcap("data.pcap",1) # reading only 1 packet from the file
OnePkt=pkts[0]
print len(OnePkt) # prints the length of the packet
+0
爲什麼沒有ntohs(pktheader-> len)? – 2017-10-13 23:04:26
相關問題
- 1. pcap數據包長度值似乎不正確
- 2. 數據包在.pcap文件中存儲錯誤
- 3. 從文件中收集位數據
- 4. 如何使用dpkt獲取.pcap文件的快照長度?
- 5. 我可以使用pcap庫來接收ipv6數據包嗎?
- 6. 在C中,我如何將接收到的rtp數據包存儲到.pcap文件中 - UDP
- 7. python中的網絡數據包長度
- 8. 如何從PCAP文件過濾RTSP數據包
- 9. 將數據包追加到當前PCAP文件
- 10. 如何使用pcap文件繪製數據包vs秒
- 11. ATM PCAP文件以太網PCAP文件
- 12. 如何從pcap文件恢復數據?
- 13. 如何在ASP.NET MVC中收集任意長度的列表數據
- 14. 發送數據包(PCAP,WLAN,C)到接收器,無需連接接收
- 15. 使用Hive從文件收集數據
- 16. 在我的數據包收聽器中未收到數據包
- 17. 瞭解pcap數據包標題
- 18. Pcap數據包的編碼和解碼
- 19. Python:從數據集中收集數據
- 20. 在Elixir中打開並收集多個文件中的數據
- 21. 在每個進程上收集不同長度的數組
- 22. funf調度和數據收集
- 23. 檢測收集是否包含數據
- 24. 當前udp數據包隊列長度
- 25. Python固定長度數據包
- 26. 在pcap中修改數據包的有效載荷
- 27. 收集數據
- 28. 按年在R中合併不同長度的數據集
- 29. 在wireshark中設置數據包長度的過濾器
- 30. 在python中打開pcap文件
首選編程語言Python中的另一個wroking例pcap.h並設置HAVE_REMOTE? – brickner 2010-07-07 16:08:40
-1沒有跟進您的問題。 – bortzmeyer 2010-09-18 16:19:30