home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / files / exploits / halfscan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-15  |  4.3 KB  |  170 lines

  1. /**********************************************************************
  2. ** This is a real simple half opened connection scanner I
  3. ** put together. It scans ports 0 to 1024 for listening
  4. ** processes, and writes to stdout the ports that are 
  5. ** listening.
  6. **
  7. ** halflife@saturn.net
  8. **
  9. ** NOTE: You have to define MY_IP as your ip. If you have
  10. ** a dynamic ip, this is gonna bite :-).
  11. *********************************************************************/
  12.  
  13. /* define the following as your ip address */
  14. #define MY_IP        "193.62.1.250"
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <signal.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <netinet/in.h>
  22. #include <sys/socket.h>
  23. #include <arpa/inet.h>
  24. #include <linux/ip.h>
  25. #include <linux/tcp.h>
  26.  
  27. int syn_timeout = 0;
  28.  
  29. unsigned short in_cksum(unsigned short *, int);
  30. int scan_port(unsigned short, unsigned int, unsigned int);
  31. void alarm_handler(int);
  32.  
  33. void alarm_handler(int s)
  34. {
  35.    alarm(0);
  36.    syn_timeout = 1;
  37. }
  38.  
  39. int scan_port(unsigned short port, unsigned int src_addr, unsigned int dst_addr)
  40. {
  41.    struct tcphdr send_tcp;
  42.    struct recv_tcp
  43.    {
  44.       struct iphdr ip;
  45.       struct tcphdr tcp;
  46.       unsigned char blah[65535];
  47.    }recv_tcp;
  48.    struct pseudo_header
  49.    {
  50.       unsigned int source_address;
  51.       unsigned int dest_address;
  52.       unsigned char placeholder;
  53.       unsigned char protocol;
  54.       unsigned short tcp_length;
  55.       struct tcphdr tcp;
  56.    }pseudo_header;
  57.    int tcp_socket;
  58.    struct sockaddr_in sin;
  59.    int sinlen;
  60.    static int blah = 0;
  61.    
  62.    blah++;
  63.    send_tcp.source = getpid() + blah;
  64.    send_tcp.dest = htons(port);
  65.    send_tcp.seq = getpid() + blah;
  66.    send_tcp.ack_seq = 0;
  67.    send_tcp.res1 = 0;
  68.    send_tcp.doff = 5;
  69.    send_tcp.res2 = 0;
  70.    send_tcp.fin = 0;
  71.    send_tcp.syn = 1;
  72.    send_tcp.rst = 0;
  73.    send_tcp.psh = 0;
  74.    send_tcp.ack = 0;
  75.    send_tcp.urg = 0;
  76.    send_tcp.window = htons(512);
  77.    send_tcp.check = 0;
  78.    send_tcp.urg_ptr = 0;               
  79.    pseudo_header.source_address = src_addr;
  80.    pseudo_header.dest_address = dst_addr;
  81.    pseudo_header.placeholder = 0;
  82.    pseudo_header.protocol = IPPROTO_TCP;
  83.    pseudo_header.tcp_length = htons(20);
  84.    bcopy(&send_tcp, &pseudo_header.tcp, 20);
  85.    send_tcp.check = in_cksum((unsigned short *)&pseudo_header, 32);
  86.    sin.sin_family = AF_INET;
  87.    sin.sin_port = htons(port);
  88.    sin.sin_addr.s_addr = dst_addr;
  89.    sinlen=sizeof(sin);
  90.    signal(SIGALRM, alarm_handler);
  91.    tcp_socket = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  92.    if(tcp_socket < 0)
  93.    {
  94.       fprintf(stderr, "couldnt open raw socket\n");
  95.       exit(1);
  96.    }
  97.    sendto(tcp_socket, &send_tcp, 20, 0, (struct sockaddr *)&sin, sinlen);
  98.    syn_timeout = 0;
  99.    alarm(10);
  100.    while(1)
  101.    {
  102.       read(tcp_socket, (struct recv_tcp *)&recv_tcp, 65535);
  103.       if(syn_timeout == 1) {close(tcp_socket);syn_timeout=0;return -1;}
  104.       if(recv_tcp.tcp.dest == (getpid() + blah))
  105.       {
  106.          alarm(0);
  107.          close(tcp_socket);
  108.          if(recv_tcp.tcp.rst == 1) return 0;
  109.          else return 1;
  110.       }
  111.    }
  112. }
  113.  
  114. unsigned short in_cksum(unsigned short *ptr, int nbytes)
  115. {
  116.     register long        sum;        /* assumes long == 32 bits */
  117.     u_short            oddbyte;
  118.     register u_short    answer;        /* assumes u_short == 16 bits */
  119.  
  120.     /*
  121.      * Our algorithm is simple, using a 32-bit accumulator (sum),
  122.      * we add sequential 16-bit words to it, and at the end, fold back
  123.      * all the carry bits from the top 16 bits into the lower 16 bits.
  124.      */
  125.  
  126.     sum = 0;
  127.     while (nbytes > 1)  {
  128.         sum += *ptr++;
  129.         nbytes -= 2;
  130.     }
  131.  
  132.                 /* mop up an odd byte, if necessary */
  133.     if (nbytes == 1) {
  134.         oddbyte = 0;        /* make sure top half is zero */
  135.         *((u_char *) &oddbyte) = *(u_char *)ptr;   /* one byte only */
  136.         sum += oddbyte;
  137.     }
  138.  
  139.     /*
  140.      * Add back carry outs from top 16 bits to low 16 bits.
  141.      */
  142.  
  143.     sum  = (sum >> 16) + (sum & 0xffff);    /* add high-16 to low-16 */
  144.     sum += (sum >> 16);            /* add carry */
  145.     answer = ~sum;        /* ones-complement, then truncate to 16 bits */
  146.     return(answer);
  147. }
  148.  
  149. main(int argc, char **argv)
  150. {   
  151.    unsigned short i;
  152.    if(argc < 2)
  153.    {
  154.       fprintf(stderr, "%s target_ip\n", argv[0]);
  155.       exit(0);
  156.    }
  157.    if(geteuid() != 0)
  158.    {
  159.       fprintf(stderr, "this program requires root\n");
  160.       exit(0);
  161.    }
  162.    printf("Scanning %s\n", argv[1]);
  163.    for(i=0;i < 1025;i++)
  164.    {
  165.       if(scan_port(i, inet_addr(MY_IP), inet_addr(argv[1]))==1)
  166.          printf("Port %d active\n", i);
  167.    }
  168. }
  169.  
  170.