2

こんにちは、pcap ファイル内の各パケットのパケット長を収集するにはどうすればよいですか? どうもありがとう

4

2 に答える 2

7

ほとんどの人が知らないハイテクな方法をお勧めします: ドキュメントを読むことです。

man pcap は、実際には2つの異なる長さが利用可能であることを教えてくれます:

              caplen a bpf_u_int32 は、パケットのバイト数を示します。
                     キャプチャーから入手可能

              len a bpf_u_int32 パケットの長さをバイト単位で示します (これは
                     キャップから利用可能なバイト数を超える可能性があります-
                     ture、パケットの長さが最大数よりも大きい場合
                     キャプチャするバイト数)

C での例:

/* パケットを取得します */
                パケット = pcap_next(ハンドル, &header);
                if (packet == NULL) { /* ファイルの終わり */
                        壊す;
                }
                printf ("[%d] の長さのパケットを取得しました\n",
                                     header.len);

pcapy ライブラリを使用した Python の別のもの:

インポート pcapy

リーダー = pcapy.open_offline("packets.pcap")

True の間:
    試す:
        (ヘッダー、ペイロード) = reader.next()
        print "長さ %d のパケットを取得しました" % header.getlen()
    pcapy.PcapError を除く:
        壊す

于 2010-09-18T16:19:13.833 に答える
0

以下の2つの例は正常に機能します。

  • C、WinPcapを使用
  • Python、SCAPYを使用

(WinPcap)(Compiler CL、Microsoft VC)パケットサイズを取得するためにこの関数を(Cで)作成しましたが、正常に動作します。pcap.hをインクルードし、コンパイラプリプロセッサにHAVE_REMOTEを設定することを忘れないでください

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を使用したPythonのもう1つの厄介な例

    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
于 2011-11-01T08:12:28.063 に答える