Advertisement
opexxx

dnsgoblin.c

May 15th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.19 KB | None | 0 0
  1. /*******************************************************************************
  2.  *                                                                             *
  3.  *         ~    .__ °.__   0       o                    ^   .__ °__  `´        *
  4.  *  °____) __ __|  | | °|   ______°____ 0 ____  __ _________|__|/  |_ ___.__.  *
  5.  *  /    \|  | °\  |°|  | °/  ___// __ \_/ ___\|  | °\_  __ \ o\   __<   |  |  *
  6.  * | o°|  \  |  /  |_|  |__\___ \\  ___/\ °\___| o|  /|  | \/  ||  |° \___ O|  *
  7.  * |___|  /____/|____/____/____ °>\___  >\___  >____/ |__|° |__||__|  / ____|  *
  8.  * `´´`´\/´`nullsecurity team`´\/`´´`´\/`´``´\/  ``´```´```´´´´`´``0_o\/´´`´´  *
  9.  *                                                                             *
  10.  * dnsgoblin.c - nasty creature constantly searching for DNS servers           *
  11.  *                                                                             *
  12.  * DATE                                                                        *
  13.  * 03/11/2011                                                                  *
  14.  *                                                                             *
  15.  * DESCRIPTION                                                                 *
  16.  * dnsgoblin uses standard dns querys and waits for the replies.               *
  17.  *                                                                             *
  18.  * AUTHOR                                                                      *
  19.  * atzeton - http://www.nullsecurity.net/                                      *
  20.  *                                                                             *
  21.  * COMPILE                                                                     *
  22.  * gcc dnsgoblin.c -O2 -lpthread -Wall -Wextra -pedantic \                     *
  23.  * --std=gnu99 -D_REENTRANT                                                    *
  24.  *                                                                             *
  25.  * NOTES                                                                       *
  26.  * This tool is for educational and testing purposes only.                     *
  27.  * The author is NOT responsible for any kind of misusage!                     *
  28.  * You may pipe stdout into a file: ./dnsgoblin > dnslist                      *
  29.  *                                                                             *
  30.  ******************************************************************************/
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <time.h>
  35. #include <stdlib.h>
  36. #include <signal.h>
  37. #include <string.h>
  38. #include <netinet/udp.h>
  39. #include <sys/socket.h>
  40. #include <unistd.h>
  41. #include <sys/time.h>
  42. #include <arpa/inet.h>
  43. #include <linux/ip.h>
  44. #include <inttypes.h>
  45. #include <pthread.h>
  46.  
  47.  
  48. /* the ip header struct */
  49. struct ipheader {
  50.     uint8_t v;
  51.     uint8_t tos;
  52.     uint16_t len;
  53.     uint16_t id;
  54.     uint16_t off;
  55.     u_char ttl;
  56.     u_char p;
  57.     uint16_t sum;
  58.     uint32_t src;
  59.     uint32_t dst;
  60. };
  61.  
  62. /* _beginning_ of dns header */
  63. struct dnsheader {
  64.     uint16_t trans_id;
  65.     /* incomplete */
  66. };
  67.  
  68.  
  69. void     sig_int(int sig);
  70. char    *human_addr(uint32_t ip_addr);
  71. void    *lstn(void *ptr);
  72. int8_t   check_ip_addr(char *ptr);
  73. int      main(int argc, char **argv);
  74.  
  75.  
  76. /* quit if SIGINT is received */
  77. void
  78. sig_int(int sig)
  79. {
  80.     if (sig != SIGINT) {
  81.         exit(EXIT_FAILURE);
  82.     }
  83.    
  84.     exit(EXIT_SUCCESS);
  85. }
  86.  
  87. /* check if the given ip is valid */
  88. int8_t
  89. check_ip_addr(char *ptr)
  90. {
  91.     if( strlen(ptr) > 16) {
  92.         printf("error: ip addr too long\n");
  93.         exit(EXIT_FAILURE);
  94.     }
  95.    
  96.     if( (int)inet_addr(ptr) == -1 ) {
  97.         printf("error: ip addr not correct\n");
  98.         exit(EXIT_FAILURE);
  99.     }
  100.    
  101.     return(0);
  102. }
  103.  
  104.  
  105. /* int ip -> dotted decimals */
  106. char *
  107. human_addr(uint32_t ip_addr)
  108. {
  109.     char *ptr    = calloc(1,16);
  110.     uint8_t oct1 = 0;
  111.     uint8_t oct2 = 0;
  112.     uint8_t oct3 = 0;
  113.     uint8_t oct4 = 0;;
  114.    
  115.     oct1 = ( ip_addr >> 24 ) & 0xFF;
  116.     oct2 = ( ip_addr >> 16 ) & 0xFF;
  117.     oct3 = ( ip_addr >> 8  ) & 0xFF;
  118.     oct4 =   ip_addr         & 0xFF;
  119.    
  120.     sprintf(ptr,"%d.%d.%d.%d",oct4,oct3,oct2,oct1);
  121.     return(ptr);
  122. }
  123.  
  124.  
  125. /* listen for dns responses */
  126. void
  127. *lstn(void *ptr)
  128. {
  129.     int *sptr             = ptr;
  130.     int sockfd            = *sptr;
  131.     struct ipheader *ip   = NULL;
  132.     uint8_t *pkt_recv     = calloc(1,2048);
  133.     char *cptr            = NULL;
  134.    
  135.     while(1==1) {
  136.         memset(pkt_recv,0x00,2047);
  137.        
  138.         if( recv(sockfd, pkt_recv, 2047, 0) > 0) {
  139.             ip = (struct ipheader *)pkt_recv;
  140.             cptr = human_addr(ip->src);
  141.             printf("%s\n",cptr);
  142.             free(cptr);
  143.         }
  144.     }
  145.    
  146. }
  147.    
  148.  
  149. int main(int argc, char **argv)
  150. {  
  151.     int32_t sockfd;         /* raw socket to inject packets */
  152.     int32_t sockfd2;        /* prevent ICMP port unreach msgs creating a layer4 udp sock on iface */
  153.    
  154.     struct sockaddr_in ifcfg;
  155.     struct sockaddr_in sin;
  156.    
  157.     int        one = 1;
  158.     const int *val = &one;
  159.    
  160.     /* what can you find out here? */
  161.     char packet[]  = "\x45\x00\x00\x3b\x6c\xbb\x40\x00\x40\x11\x6b\xd4\xc0\xa8\x02\x65\x55\xd6\x49\x3f\x13\x37\x00\x35\x00\x27\x00\x00\xd6\x88\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03\x77\x77\x77\x06\x61\x6d\x61\x7a\x6f\x6e\x02\x64\x65\x00\x00\x01\x00\x01";
  162.  
  163.     struct ipheader  *ip  = NULL;
  164.     struct udphdr    *udp = NULL;
  165.     struct dnsheader *dns = NULL;
  166.    
  167.     uint16_t local_port   = 0;
  168.     pthread_t trd_lstn;
  169.    
  170.     if( argc < 2) {
  171.         printf("error: need local ip as arg\n");
  172.         exit(EXIT_FAILURE);
  173.     }  
  174.    
  175.     check_ip_addr(argv[1]);
  176.    
  177.     srand ( time(NULL)    );
  178.     signal(SIGINT, sig_int); /* set up a signal handler */
  179.  
  180.    
  181.     /* get random port */
  182.     local_port = (uint32_t)rand();
  183.    
  184.     /* pseudo socket */
  185.     sockfd2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  186.  
  187.     /* bind to port (used to prevent icmp bla) */
  188.     ifcfg.sin_family      = AF_INET;
  189.     ifcfg.sin_port        = htons(local_port);
  190.     ifcfg.sin_addr.s_addr = htonl(INADDR_ANY);
  191.    
  192.     bind(sockfd2, (struct sockaddr *)&ifcfg, sizeof(ifcfg));
  193.    
  194.     /* create main raw sock */
  195.     sockfd         = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
  196.     sin.sin_family = AF_INET;
  197.     sin.sin_port   = htons (local_port);
  198.  
  199.     if (setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
  200.         printf("err\n");
  201.     }
  202.  
  203.     if(sockfd < 0) {
  204.         fprintf(stderr,"error: rawsock cannot be created. No permission.\n");
  205.         exit(EXIT_FAILURE);
  206.     }
  207.    
  208.     /* change the UDI (-> nobody) */
  209.     setuid(65534);
  210.    
  211.     /* create thread which is listening for dns responses */
  212.     pthread_create(&trd_lstn, NULL, lstn, (void *)&sockfd);
  213.    
  214.     ip = (struct ipheader *)packet;
  215.     ip->id  = ntohs( rand() );
  216.     ip->v   = 0x45;
  217.     ip->tos = 0x0054;
  218.     ip->len = 30;
  219.     ip->off = 0x0000;
  220.     ip->ttl = 0xff;
  221.     ip->p   = 17;
  222.     ip->sum = 0x0000;
  223.    
  224.     ip->src = inet_addr(argv[1]);
  225.    
  226.     udp         = (struct udphdr *)(packet + sizeof(struct ipheader));
  227.     udp->source = ntohs( local_port );
  228.     udp->dest   = ntohs(53);
  229.    
  230.     dns = (struct dnsheader *)(packet + sizeof(struct ipheader) + sizeof(struct udphdr) );
  231.    
  232.     while(1==1) {
  233.         /* do some modifications */
  234.         ip->id  = ntohs( rand() );
  235.         ip->dst = ntohl( rand() );
  236.  
  237.         dns->trans_id = ntohs( rand() );
  238.        
  239.         if( sendto(sockfd, &packet, sizeof(packet)-1 , 0, (struct sockaddr *) &sin, sizeof (sin)) < 0){
  240.             printf("error: sendto failed\n");
  241.             exit(EXIT_FAILURE);
  242.         }
  243.        
  244.         usleep(5000); /* you may increase/decrease this */
  245.     }
  246.  
  247.     close(sockfd);
  248.  
  249.     return 0;
  250. }
  251.  
  252. /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement