Advertisement
Guest User

libnet_stuff

a guest
Aug 21st, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include <pcap.h>
  2. #include <libnet.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include "pcap_structs.h"
  9.  
  10. //OTHER FIELDS W LIBNET AND PCAP
  11. pcap_t *pcap, *pcap_sender;
  12. char pcap_errbuf[PCAP_ERRBUF_SIZE];
  13. char libnet_errbuf[LIBNET_ERRBUF_SIZE];
  14. const unsigned char *packet;
  15. struct pcap_pkthdr header;
  16. libnet_t *l;
  17. struct libnet_link_int *network;
  18.  
  19. //LIBNET TAG FIELDS
  20. libnet_ptag_t eth_tag, ip_tag, tcp_tag, udp_tag;
  21.  
  22.  
  23. //OTHER FIELDS
  24. char filename[100];
  25. char *type_of_packet;
  26.  
  27. //DECLARE STRUCTS
  28. const struct sniff_ethernet *ethernet;
  29. const struct sniff_ip *ip;
  30. const struct sniff_tcp *tcp;
  31. const struct sniff_udp *udp;
  32. const char *payload;
  33.  
  34. ////////////////////////
  35.  
  36. void hex_view(unsigned char *packet){
  37.     printf("\n\nHEX VIEW: ");
  38.     int i;
  39.     i = 0;
  40.     while(i != 76){
  41.         if(i%8 == 0){
  42.             printf(" ");
  43.         }
  44.         if(i%16 == 0){
  45.             printf("\n");
  46.         }
  47.         printf("%x ", packet[i]);
  48.         i++;
  49.     }
  50.     printf("\n\n#################\n");
  51. }
  52.  
  53. unsigned char* add_to_new_packet(const unsigned char *packet){
  54.     unsigned char *new_packet = NULL;
  55.     int i;
  56.     i = 17;
  57.     while(i != 76){
  58.        
  59.         new_packet[i-17] = packet[i];
  60.        
  61.         i++;
  62.     }
  63.     return new_packet;
  64. }
  65.  
  66. int main(){
  67.     l = libnet_init(LIBNET_RAW4_ADV, "eth0", libnet_errbuf);
  68.     //
  69.     pcap = pcap_open_offline((char*)"c.pcap", pcap_errbuf);
  70.     pcap_sender = pcap_open_live((char*)"eth0", BUFSIZ, 1, 1000, pcap_errbuf);
  71.     packet = pcap_next(pcap, &header);
  72.     hex_view(packet);
  73.  
  74.     //pcap_sendpacket(pcap_sender, packet, 76);
  75.     //pcap_inject(pcap_sender, packet,1);
  76.     //new packet
  77.     int i;
  78.     unsigned char new_packet[76-16];
  79.     for(i=0; i<76-16; i++){
  80.         new_packet[i] = packet[i+16];
  81.         if(i%8 == 0){
  82.             printf(" ");
  83.         }
  84.         if(i%16 == 0){
  85.             printf("\n");
  86.         }
  87.         printf("%x ", packet[i]);
  88.     }
  89.     printf("\nNEWPACKET ");
  90.     hex_view(new_packet);
  91.    
  92.    
  93.    
  94.     //pcap_inject(pcap_sender, new_packet, 76 - 16);
  95.     //pcap_sendpacket(pcap_sender, new_packet, 76 - 16);
  96.     libnet_adv_write_raw_ipv4(l, new_packet, 76 - 16);
  97.    
  98.    
  99.    
  100.    
  101.     printf("\n%s\n%s", libnet_geterror(l), pcap_geterr(pcap_sender));
  102.     libnet_destroy(l);
  103.     //
  104.     //unsigned char *new_packet =
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement