Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: rembolso en 9 Febrero 2011, 02:15 am



Título: error copilando WinPcap
Publicado por: rembolso en 9 Febrero 2011, 02:15 am
hola . estoy con dev c++ 5.0 utilizando la libreria winpcap .  y busque un ejemplo de filtrar paquetes .
Código
  1. /*
  2.  * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
  3.  * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  * notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  * notice, this list of conditions and the following disclaimer in the
  14.  * documentation and/or other materials provided with the distribution.
  15.  * 3. Neither the name of the Politecnico di Torino, CACE Technologies
  16.  * nor the names of its contributors may be used to endorse or promote
  17.  * products derived from this software without specific prior written
  18.  * permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  */
  33.  
  34.  
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37.  
  38. #include <pcap.h>
  39.  
  40. #define MAX_PRINT 80
  41. #define MAX_LINE 16
  42.  
  43.  
  44. void usage();
  45.  
  46.  
  47. void main(int argc, char **argv)
  48. {
  49. pcap_t *fp;
  50. char errbuf[PCAP_ERRBUF_SIZE];
  51. char *source=NULL;
  52. char *ofilename=NULL;
  53. char *filter=NULL;
  54. int i;
  55. pcap_dumper_t *dumpfile;
  56. struct bpf_program fcode;
  57. bpf_u_int32 NetMask;
  58. int res;
  59. struct pcap_pkthdr *header;
  60. const u_char *pkt_data;
  61.  
  62.    if (argc == 1)
  63.    {
  64.        usage();
  65.        return;
  66.    }
  67.  
  68.    for(i=1;i < argc; i+= 2)
  69.    {
  70.  
  71.        switch (argv[i] [1])
  72.        {
  73.            case 's':
  74.            {
  75.                source=argv[i+1];
  76.            };
  77.            break;
  78.  
  79.            case 'o':
  80.            {
  81.                ofilename=argv[i+1];
  82.            };
  83.            break;
  84.  
  85.            case 'f':
  86.            {
  87.                filter=argv[i+1];
  88.            };
  89.            break;
  90.        }
  91.    }
  92.  
  93.    // open a capture from the network
  94.    if (source != NULL)
  95.    {
  96.        if ( (fp= pcap_open(source,
  97.                            1514 /*snaplen*/,
  98.                            PCAP_OPENFLAG_PROMISCUOUS /*flags*/,
  99.                            20 /*read timeout*/,
  100.                            NULL /* remote authentication */,
  101.                            errbuf)
  102.                            ) == NULL)
  103.        {
  104.            fprintf(stderr,"\nUnable to open the adapter.\n");
  105.            return;
  106.        }
  107.    }
  108.  
  109.    else usage();
  110.  
  111.    if (filter != NULL)
  112.    {
  113.        // We should loop through the adapters returned by the pcap_findalldevs_ex()
  114.        // in order to locate the correct one.
  115.        //
  116.        // Let's do things simpler: we suppose to be in a C class network ;-)
  117.        NetMask=0xffffff;
  118.  
  119.        //compile the filter
  120.        if(pcap_compile(fp, &fcode, filter, 1, NetMask) < 0)
  121.        {
  122.            fprintf(stderr,"\nError compiling filter: wrong syntax.\n");
  123.            return;
  124.        }
  125.  
  126.        //set the filter
  127.        if(pcap_setfilter(fp, &fcode)<0)
  128.        {
  129.            fprintf(stderr,"\nError setting the filter\n");
  130.            return;
  131.        }
  132.  
  133.    }
  134.  
  135.    //open the dump file
  136.    if (ofilename != NULL)
  137.    {
  138.        dumpfile= pcap_dump_open(fp, ofilename);
  139.  
  140.        if (dumpfile == NULL)
  141.        {
  142.            fprintf(stderr,"\nError opening output file\n");
  143.            return;
  144.        }
  145.    }
  146.    else usage();
  147.  
  148.    //start the capture
  149.    while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)
  150.    {
  151.  
  152.        if(res == 0)
  153.        /* Timeout elapsed */
  154.        continue;
  155.  
  156.        //save the packet on the dump file
  157.        pcap_dump((unsigned char *) dumpfile, header, pkt_data);
  158.  
  159.    }
  160. }
  161.  
  162.  
  163. void usage()
  164. {
  165.  
  166.    printf("\npf - Generic Packet Filter.\n");
  167.    printf("\nUsage:\npf -s source -o output_file_name [-f filter_string]\n\n");
  168.    exit(0);
  169. }
  170.  
  171.  
Código
  1. [code=bash]In file included from C:/Dev-Cpp/include/pcap.h:14,
  2.                 from main.c:38:
  3. C:/Dev-Cpp/include/pcap++/Packet.h:16: error: syntax error before "pcappp"
  4. C:/Dev-Cpp/include/pcap++/Packet.h:16: error: syntax error before '{' token
  5.  
  6. C:/Dev-Cpp/include/pcap++/Packet.h:36: error: syntax error before "Data"
  7. C:/Dev-Cpp/include/pcap++/Packet.h:36: warning: data definition has no type or storage class
  8. C:/Dev-Cpp/include/pcap++/Packet.h:42: warning: data definition has no type or storage class
  9. C:/Dev-Cpp/include/pcap++/Packet.h:50: error: syntax error before "const"
  10. C:/Dev-Cpp/include/pcap++/Packet.h:50: warning: data definition has no type or storage class
  11. C:/Dev-Cpp/include/pcap++/Packet.h:56: error: syntax error before "const"
  12. C:/Dev-Cpp/include/pcap++/Packet.h:69: error: syntax error before '&' token
  13. C:/Dev-Cpp/include/pcap++/Packet.h:84: error: `packet' undeclared here (not in a function)
  14. C:/Dev-Cpp/include/pcap++/Packet.h:84: warning: data definition has no type or storage class
  15. C:/Dev-Cpp/include/pcap++/Packet.h:85: error: `false' undeclared here (not in a function)
  16. C:/Dev-Cpp/include/pcap++/Packet.h:85: warning: data definition has no type or storage class
  17. C:/Dev-Cpp/include/pcap++/Packet.h:86: error: syntax error before '}' token
  18. C:/Dev-Cpp/include/pcap++/Packet.h: In function `get_seconds':
  19. C:/Dev-Cpp/include/pcap++/Packet.h:116: error: syntax error before '{' token
  20. C:/Dev-Cpp/include/pcap++/Packet.h:168: error: syntax error before "Data"
  21.  
  22. In file included from C:/Dev-Cpp/include/pcap++/DataLink.h:15,
  23.  
  24.                 from C:/Dev-Cpp/include/pcap.h:15,
  25.                 from main.c:38:
  26. C:/Dev-Cpp/include/string.h:37: warning: `__pure__' attribute ignored
  27. C:/Dev-Cpp/include/string.h:42: warning: `__pure__' attribute ignored
  28. C:/Dev-Cpp/include/string.h:43: warning: `__pure__' attribute ignored
  29. C:/Dev-Cpp/include/string.h:46: warning: `__pure__' attribute ignored
  30. C:/Dev-Cpp/include/string.h:49: warning: `__pure__' attribute ignored
  31. C:/Dev-Cpp/include/string.h:51: warning: `__pure__' attribute ignored
  32. C:/Dev-Cpp/include/string.h:53: warning: `__pure__' attribute ignored
  33. C:/Dev-Cpp/include/string.h:54: warning: `__pure__' attribute ignored
  34. C:/Dev-Cpp/include/string.h:55: warning: `__pure__' attribute ignored
  35. C:/Dev-Cpp/include/string.h:56: warning: `__pure__' attribute ignored
  36. C:/Dev-Cpp/include/string.h:67: warning: `__malloc__' attribute ignored
  37. C:/Dev-Cpp/include/string.h:92: warning: `__malloc__' attribute ignored
  38. C:/Dev-Cpp/include/string.h:97: error: storage class specified for parameter `strcasecmp'
  39. C:/Dev-Cpp/include/string.h:97: error: syntax error before '{' token
  40.  
  41. C:/Dev-Cpp/include/string.h:103: error: storage class specified for parameter `strncasecmp'
  42. C:/Dev-Cpp/include/string.h:103: error: syntax error before '{' token
  43. C:/Dev-Cpp/include/string.h:170: error: storage class specified for parameter `wcscmpi'
  44. C:/Dev-Cpp/include/string.h:170: error: redefinition of parameter 'wcscmpi'
  45. C:/Dev-Cpp/include/string.h:167: error: previous definition of 'wcscmpi' was here
  46. C:/Dev-Cpp/include/string.h:170: error: syntax error before '{' token
  47. In file included from C:/Dev-Cpp/include/pcap.h:15,
  48.                 from main.c:38:
  49. C:/Dev-Cpp/include/pcap++/DataLink.h:17: error: syntax error before "namespace"
  50. C:/Dev-Cpp/include/pcap++/DataLink.h:482: error: parameter `desc' is initialized
  51. C:/Dev-Cpp/include/pcap++/DataLink.h:482: error: `m_type' undeclared (first use in this function)
  52. C:/Dev-Cpp/include/pcap++/DataLink.h:482: error: (Each undeclared identifier is reported only once
  53. C:/Dev-Cpp/include/pcap++/DataLink.h:482: error: for each function it appears in.)
  54. C:/Dev-Cpp/include/pcap++/DataLink.h:482: confused by earlier errors, bailing out
  55.  
  56. make.exe: *** [main.o] Error 1
  57.  
  58. Ejecución Terminada
ALGUNA solucion que me ofrescan
documentacion:http://www.winpcap.org/docs/docs_40_2/html/group__wpcapsamps.html

surce code de winpcap: http://www.winpcap.org/install/bin/PacketCE.zip[/code]


Título: Re: error copilando WinPcap
Publicado por: Garfield07 en 9 Febrero 2011, 18:37 pm
Pues este code es de John Ericson...
1. Code sniffer
2. Hacking-network.h
3. Hacking.h
 Todos los codes...  (http://anayamultimedia.com/catalogos/complementos/MU0023501_9999997451.zip)
Código
  1. #include <pcap.h>
  2. #include "hacking.h"
  3. #include "hacking-network.h"
  4.  
  5. void pcap_fatal(const char *, const char *);
  6. void decode_ethernet(const u_char *);
  7. void decode_ip(const u_char *);
  8. u_int decode_tcp(const u_char *);
  9.  
  10. void caught_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
  11.  
  12. int main() {
  13. struct pcap_pkthdr cap_header;
  14. const u_char *packet, *pkt_data;
  15. char errbuf[PCAP_ERRBUF_SIZE];
  16. char *device;
  17.  
  18. pcap_t *pcap_handle;
  19.  
  20. device = pcap_lookupdev(errbuf);
  21. if(device == NULL)
  22. pcap_fatal("pcap_lookupdev", errbuf);
  23.  
  24. printf("Sniffing on device %s\n", device);
  25.  
  26. pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);
  27. if(pcap_handle == NULL)
  28. pcap_fatal("pcap_open_live", errbuf);
  29.  
  30. pcap_loop(pcap_handle, 3, caught_packet, NULL);
  31.  
  32. pcap_close(pcap_handle);
  33. }
  34.  
  35. void caught_packet(u_char *user_args, const struct pcap_pkthdr *cap_header, const u_char *packet) {
  36. int tcp_header_length, total_header_size, pkt_data_len;
  37. u_char *pkt_data;
  38.  
  39. printf("==== Got a %d byte packet ====\n", cap_header->len);
  40.  
  41.  
  42. decode_ethernet(packet);
  43. decode_ip(packet+ETHER_HDR_LEN);
  44. tcp_header_length = decode_tcp(packet+ETHER_HDR_LEN+sizeof(struct ip_hdr));
  45.  
  46. total_header_size = ETHER_HDR_LEN+sizeof(struct ip_hdr)+tcp_header_length;
  47. pkt_data = (u_char *)packet + total_header_size;  // pkt_data points to the data portion
  48. pkt_data_len = cap_header->len - total_header_size;
  49. if(pkt_data_len > 0) {
  50. printf("\t\t\t%u bytes of packet data\n", pkt_data_len);
  51. dump(pkt_data, pkt_data_len);
  52. } else
  53. printf("\t\t\tNo Packet Data\n");
  54. }
  55.  
  56. void pcap_fatal(const char *failed_in, const char *errbuf) {
  57. printf("Fatal Error in %s: %s\n", failed_in, errbuf);
  58. exit(1);
  59. }
  60.  
  61. void decode_ethernet(const u_char *header_start) {
  62. int i;
  63. const struct ether_hdr *ethernet_header;
  64.  
  65. ethernet_header = (const struct ether_hdr *)header_start;
  66. printf("[[  Layer 2 :: Ethernet Header  ]]\n");
  67. printf("[ Source: %02x", ethernet_header->ether_src_addr[0]);
  68. for(i=1; i < ETHER_ADDR_LEN; i++)
  69. printf(":%02x", ethernet_header->ether_src_addr[i]);
  70.  
  71. printf("\tDest: %02x", ethernet_header->ether_dest_addr[0]);
  72. for(i=1; i < ETHER_ADDR_LEN; i++)
  73. printf(":%02x", ethernet_header->ether_dest_addr[i]);
  74. printf("\tType: %hu ]\n", ethernet_header->ether_type);
  75. }
  76.  
  77. void decode_ip(const u_char *header_start) {
  78. const struct ip_hdr *ip_header;
  79.  
  80. ip_header = (const struct ip_hdr *)header_start;
  81. printf("\t((  Layer 3 ::: IP Header  ))\n");
  82. printf("\t( Source: %s\t", inet_ntoa(ip_header->ip_src_addr));
  83. printf("Dest: %s )\n", inet_ntoa(ip_header->ip_dest_addr));
  84. printf("\t( Type: %u\t", (u_int) ip_header->ip_type);
  85. printf("ID: %hu\tLength: %hu )\n", ntohs(ip_header->ip_id), ntohs(ip_header->ip_len));
  86. }
  87.  
  88. u_int decode_tcp(const u_char *header_start) {
  89. u_int header_size;
  90. const struct tcp_hdr *tcp_header;
  91.  
  92. tcp_header = (const struct tcp_hdr *)header_start;
  93. header_size = 4 * tcp_header->tcp_offset;
  94.  
  95. printf("\t\t{{  Layer 4 :::: TCP Header  }}\n");
  96. printf("\t\t{ Src Port: %hu\t", ntohs(tcp_header->tcp_src_port));
  97. printf("Dest Port: %hu }\n", ntohs(tcp_header->tcp_dest_port));
  98. printf("\t\t{ Seq #: %u\t", ntohl(tcp_header->tcp_seq));
  99. printf("Ack #: %u }\n", ntohl(tcp_header->tcp_ack));
  100. printf("\t\t{ Header Size: %u\tFlags: ", header_size);
  101. if(tcp_header->tcp_flags & TCP_FIN)
  102. printf("FIN ");
  103. if(tcp_header->tcp_flags & TCP_SYN)
  104. printf("SYN ");
  105. if(tcp_header->tcp_flags & TCP_RST)
  106. printf("RST ");
  107. if(tcp_header->tcp_flags & TCP_PUSH)
  108. printf("PUSH ");
  109. if(tcp_header->tcp_flags & TCP_ACK)
  110. printf("ACK ");
  111. if(tcp_header->tcp_flags & TCP_URG)
  112. printf("URG ");
  113. printf(" }\n");
  114.  
  115. return header_size;
  116. }
  117.  
Código
  1. /* This function accepts a socket FD and a ptr to the null terminated
  2.  * string to send.  The function will make sure all the bytes of the
  3.  * string are sent.  Returns 1 on success and 0 on failure.
  4.  */
  5. int send_string(int sockfd, unsigned char *buffer) {
  6.   int sent_bytes, bytes_to_send;
  7.   bytes_to_send = strlen(buffer);
  8.   while(bytes_to_send > 0) {
  9.      sent_bytes = send(sockfd, buffer, bytes_to_send, 0);
  10.      if(sent_bytes == -1)
  11.         return 0; // return 0 on send error
  12.      bytes_to_send -= sent_bytes;
  13.      buffer += sent_bytes;
  14.   }
  15.   return 1; // return 1 on success
  16. }
  17.  
  18. /* This function accepts a socket FD and a ptr to a destination
  19.  * buffer.  It will receive from the socket until the EOL byte
  20.  * sequence in seen.  The EOL bytes are read from the socket, but
  21.  * the destination buffer is terminated before these bytes.
  22.  * Returns the size of the read line (without EOL bytes).
  23.  */
  24. int recv_line(int sockfd, unsigned char *dest_buffer) {
  25. #define EOL "\r\n" // End-Of-Line byte sequence
  26. #define EOL_SIZE 2
  27.   unsigned char *ptr;
  28.   int eol_matched = 0;
  29.  
  30.   ptr = dest_buffer;
  31.   while(recv(sockfd, ptr, 1, 0) == 1) { // read a single byte
  32.      if(*ptr == EOL[eol_matched]) { // does this byte match terminator
  33.         eol_matched++;
  34.         if(eol_matched == EOL_SIZE) { // if all bytes match terminator,
  35.            *(ptr+1-EOL_SIZE) = '\0'; // terminate the string
  36.            return strlen(dest_buffer); // return bytes recevied
  37.         }
  38.      } else {
  39.         eol_matched = 0;
  40.      }  
  41.      ptr++; // increment the pointer to the next byter;
  42.   }
  43.   return 0; // didn't find the end of line characters
  44. }
  45.  
  46.  
  47. /* Structure for Ethernet headers */
  48. #define ETHER_ADDR_LEN 6
  49. #define ETHER_HDR_LEN 14
  50.  
  51. struct ether_hdr {
  52.   unsigned char ether_dest_addr[ETHER_ADDR_LEN]; // Destination MAC address
  53.   unsigned char ether_src_addr[ETHER_ADDR_LEN];  // Source MAC address
  54.   unsigned short ether_type; // Type of Ethernet packet
  55. };
  56.  
  57. /* Structure for Internet Protocol (IP) headers */
  58. struct ip_hdr {
  59.   unsigned char ip_version_and_header_length; // version and header length combined
  60.   unsigned char ip_tos;          // type of service
  61.   unsigned short ip_len;         // total length
  62.   unsigned short ip_id;          // identification number
  63.   unsigned short ip_frag_offset; // fragment offset and flags
  64.   unsigned char ip_ttl;          // time to live
  65.   unsigned char ip_type;         // protocol type
  66.   unsigned short ip_checksum;    // checksum
  67.   unsigned int ip_src_addr;      // source IP address
  68.   unsigned int ip_dest_addr;     // destination IP address
  69. };
  70.  
  71. /* Structure for Transmission Control Protocol (TCP) headers */
  72. struct tcp_hdr {
  73.   unsigned short tcp_src_port;   // source TCP port
  74.   unsigned short tcp_dest_port;  // destination TCP port
  75.   unsigned int tcp_seq;          // TCP sequence number
  76.   unsigned int tcp_ack;          // TCP acknowledgement number
  77.   unsigned char reserved:4;      // 4-bits from the 6-bits of reserved space
  78.   unsigned char tcp_offset:4;    // TCP data offset for little endian host
  79.   unsigned char tcp_flags;       // TCP flags (and 2-bits from reserved space)
  80. #define TCP_FIN   0x01
  81. #define TCP_SYN   0x02
  82. #define TCP_RST   0x04
  83. #define TCP_PUSH  0x08
  84. #define TCP_ACK   0x10
  85. #define TCP_URG   0x20
  86.   unsigned short tcp_window;     // TCP window size
  87.   unsigned short tcp_checksum;   // TCP checksum
  88.   unsigned short tcp_urgent;     // TCP urgent pointer
  89. };
  90.  
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // A function to display an error message and then exit
  6. void fatal(char *message) {
  7.   char error_message[100];
  8.  
  9.   strcpy(error_message, "[!!] Fatal Error ");
  10.   strncat(error_message, message, 83);
  11.   perror(error_message);
  12.   exit(-1);
  13. }
  14.  
  15. // An error checked malloc() wrapper function
  16. void *ec_malloc(unsigned int size) {
  17.   void *ptr;
  18.   ptr = malloc(size);
  19.   if(ptr == NULL)
  20.      fatal("in ec_malloc() on memory allocation");
  21.   return ptr;
  22. }
  23.  
  24. // dumps raw memory in hex byte and printable split format
  25. void dump(const unsigned char *data_buffer, const unsigned int length) {
  26. unsigned char byte;
  27. unsigned int i, j;
  28. for(i=0; i < length; i++) {
  29. byte = data_buffer[i];
  30. printf("%02x ", data_buffer[i]);  // display byte in hex
  31. if(((i%16)==15) || (i==length-1)) {
  32. for(j=0; j < 15-(i%16); j++)
  33. printf("   ");
  34. printf("| ");
  35. for(j=(i-(i%16)); j <= i; j++) {  // display printable bytes from line
  36. byte = data_buffer[j];
  37. if((byte > 31) && (byte < 127)) // outside printable char range
  38. printf("%c", byte);
  39. else
  40. printf(".");
  41. }
  42. printf("\n"); // end of the dump line (each line 16 bytes)
  43. } // end if
  44. } // end for
  45. }
  46.  


Va como la seda :P
Suerte!