2010-07-06 3 views
1

안녕 얘들 아, 어떻게 pcap 파일의 각 패킷에 대한 패킷 길이를 수집 할 수 있습니까? 고마워요pcap 파일의 패킷 길이를 수집

+0

프로그래밍 언어 설정을 사용하여 파이썬에서 또 다른 wroking 예에서 pcap.h 및 설정 HAVE_REMOTE을 포함하는 것을 잊지 마세요? – brickner

+0

-1 질문에 대한 답변을 제공하지 않습니다. – bortzmeyer

답변

7

필자는 문서를 읽는 하이테크 방법을 제안합니다. C에서

 
       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) 

예 :

 
/* 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); 

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

남자 PCAP 사용할 수 실제로 두 개의 서로 다른 길이가 우리에게 아래의 두 예제는 잘 작동합니다.

파이썬을 사용하여 C,은 WinPcap
  • 를 사용하여 10
    • , SCAPY
    내가 패킷 크기를 얻기 위해 (C에서)이 기능을 썼다

    (은 WinPcap) (컴파일러 CL, 마이크로 소프트 VC) 를 하고 그것을 잘 작동합니다 . 컴파일러 전처리

    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)? –

    관련 문제