home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / proscan.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  10.4 KB  |  330 lines

  1. /************************************************************************
  2. * proscan.c             PROMISC Ethernet Scanner            *
  3. *                                                                       *
  4. *            Questo tool permette la scansione di un LAN    *
  5. *            alla ricerca di interfacce di rete in modalita' *
  6. *            promiscua. L'effetto viene ottenuto mediante    * 
  7. *            utilizzo di query 'anomale'.            * 
  8. *            Per la spiegazione vedere l'articolo " TCP/IP     *
  9. *            Hacks For Phun And Profit" su BFi5.        *
  10. *                BFi e' disponibile al seguente URL:        *
  11. *                   http://softpj98.bbk.org/bfi/        *
  12. *                                                                       *
  13. * OS:                   Linux (SOCK_PACKET)                       *
  14. * Credits:        Apostols, comp.security.unix, vari sniffer :)   * 
  15. *                                                                       *
  16. *                       NO(C)1998 FuSyS TCP/IP Tools Unlimited      *
  17. ************************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <ctype.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #include <sys/time.h>
  28. #include <netinet/if_ether.h>
  29. #include <net/if.h>
  30. #include <ioctls.h>
  31. #include <sys/ioctl.h>
  32. #include <fcntl.h>
  33. #include <time.h>
  34.  
  35. #define MAX_LEN        1500
  36. #define IF_LEN        14
  37. #define IP_ALEN        4
  38. #define IP_HLEN         20
  39. #define ICMP_HLEN       8
  40.  
  41. struct arp_hack {
  42.     unsigned char h_dest[ETH_ALEN];
  43.     unsigned char h_source[ETH_ALEN];
  44.     unsigned short h_proto;
  45.     unsigned short ar_hrd;
  46.     unsigned short ar_pro;
  47.     unsigned char ar_hln;
  48.     unsigned char ar_pln;
  49.     unsigned short ar_op;
  50.     unsigned char ar_sha[ETH_ALEN];
  51.     unsigned char ar_sip[IP_ALEN];
  52.     unsigned char ar_tha[ETH_ALEN];
  53.     unsigned char ar_tip[IP_ALEN];
  54. };
  55.  
  56. struct pinghack {
  57.         unsigned char h_dest[ETH_ALEN];
  58.         unsigned char h_source[ETH_ALEN];
  59.         unsigned short h_proto;
  60.         unsigned char ihl_ver;
  61.         unsigned char tos;
  62.         unsigned short tot_len;
  63.         unsigned short id;
  64.         unsigned short frag_off;
  65.         unsigned char ttl;
  66.         unsigned char protocol;
  67.         unsigned short check;
  68.         unsigned long saddr;
  69.         unsigned long daddr;
  70.         unsigned char type;
  71.         unsigned char code;
  72.         unsigned short checksum;
  73.         unsigned short icmp_id;
  74.         unsigned short icmp_seq;
  75. };
  76.  
  77. unsigned char mac[ETH_ALEN];
  78. unsigned long dip, sip, netmask, broadcast, dmp, saddr;
  79. char packet[MAX_LEN], *ptr;
  80.  
  81. void uso(void) {
  82.         fprintf(stderr,"Uso: proscan -[a,p] <interfaccia>\n");
  83.         exit (0);
  84. }
  85.  
  86. char *ntoa(unsigned long ip) {
  87.         static char buff[18];
  88.         char *p;
  89.         p = (char *) &ip;
  90.         sprintf(buff, "%d.%d.%d.%d",
  91.                 (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
  92.         return(buff);
  93. }
  94.  
  95. char *dumpHW (unsigned char *hw_s) {
  96.     static char buffer[ETH_ALEN];
  97.     sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
  98.        hw_s[0], hw_s[1], hw_s[2], hw_s[3], hw_s[4], hw_s[5]);
  99.     return buffer;
  100. }
  101.  
  102. unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl) {
  103.         unsigned int sum;
  104.  
  105.         __asm__ __volatile__("
  106.             movl (%1), %0
  107.             subl $4, %2
  108.             jbe 2f
  109.             addl 4(%1), %0
  110.             adcl 8(%1), %0
  111.             adcl 12(%1), %0
  112. 1:          adcl 16(%1), %0
  113.             lea 4(%1), %1
  114.             decl %2
  115.             jne 1b
  116.             adcl $0, %0
  117.             movl %0, %2
  118.             shrl $16, %0
  119.             addw %w2, %w0
  120.             adcl $0, %0
  121.             notl %0
  122. 2:
  123.             "
  124.         : "=r" (sum), "=r" (iph), "=r" (ihl)
  125.         : "1" (iph), "2" (ihl));
  126.         return(sum);
  127. }
  128.  
  129. int main (int argc, char **argv) {
  130.  
  131.     struct arp_hack *hack_p;
  132.     struct pinghack *ping;
  133.     struct ifreq ifr;
  134.     struct sockaddr sa;
  135.     int opt, sockfd, fd_flags, len, sa_len, pid, sent, optA=0, optP=0;
  136.  
  137.         if (geteuid() || getuid()) {
  138.                 fprintf(stderr, "Per utilizzare proscan devi essere Root\n",
  139.             argv[0]);
  140.                 exit(0);
  141.         }
  142.  
  143.       if (argc < 3) uso();
  144.  
  145.     while ((opt = getopt(argc, argv, "ap")) != EOF) {
  146.                 switch(opt)
  147.                 {
  148.                         case 'a':
  149.                                 optA=1;
  150.                                 break;
  151.                         case 'p':
  152.                                 optP =1;
  153.                                 break;
  154.                         default:
  155.                                 exit(1);
  156.                                 break;
  157.                 }
  158.         }
  159.  
  160.     if(optA && optP) {
  161.         fprintf(stderr, "\nNon puoi usare -a e -p insieme !!!\n");
  162.         exit(1);
  163.     }
  164.  
  165.     printf("\n\033[1;32m---]  P R O m i s c  S C A N n e r  [---\033[0m\n");
  166.     printf("\033[1;34mno(C)1999 FuSyS - TCP/IP Tools Unlimited\033[0m\n");
  167.  
  168.     if(optA)
  169.         if((sockfd=socket(AF_INET, SOCK_PACKET, htons(ETH_P_ARP))) <0) {
  170.             perror("SOCK_PACKET: problemi di allocazione\n");
  171.             exit(0);
  172.         }
  173.     if(optP)
  174.     if((sockfd=socket(AF_INET, SOCK_PACKET, htons(ETH_P_IP))) <0) {
  175.                 perror("SOCK_PACKET: problemi di allocazione\n");
  176.                 exit(0);
  177.         }
  178.  
  179.       strcpy (ifr.ifr_name, argv[2]);
  180.       if (ioctl (sockfd, SIOCGIFHWADDR, &ifr) < 0) {
  181.             perror("Non ho trovato l'indirizzo hardware locale !\n");
  182.             exit(1);
  183.       }
  184.     memcpy(mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  185.     printf("\n\033[1;34m MAC Locale: \033[1;32m%s\033[0m\n", 
  186.         dumpHW(mac));
  187.     
  188.     if (ioctl (sockfd, SIOCGIFADDR, &ifr) < 0) {
  189.             perror ("Non ho trovato l'indirizzo IP locale !\n");
  190.             exit(1);
  191.       }
  192.     memcpy ((void *) &sip, (void *) &ifr.ifr_addr.sa_data + 2, IP_ALEN);
  193.     printf ("\033[1;34m IP Locale : \033[1;32m%s\033[0m\n", 
  194.         ntoa(sip));
  195.  
  196.     if (ioctl (sockfd, SIOCGIFNETMASK, &ifr) < 0) 
  197.         fprintf(stderr, "Non ho trovato l'indirizzo NETMASK !");
  198.       memcpy ((void *)&netmask, (void *)&ifr.ifr_netmask.sa_data+2, IP_ALEN);
  199.     printf ("\033[1;34m NETMASK   : \033[1;32m%s\033[0m\n", 
  200.         ntoa(netmask));
  201.  
  202.     if (ioctl (sockfd, SIOCGIFBRDADDR, &ifr) < 0)
  203.             fprintf(stderr, "Non ho trovato l'indirizzo BROADCAST !\n");
  204.       memcpy((void *)&broadcast,(void *)&ifr.ifr_broadaddr.sa_data+2,IP_ALEN);
  205.     printf ("\033[1;34m BROADCAST : \033[1;32m%s\033[0m\n", 
  206.         ntoa(broadcast));
  207.  
  208.     if(optA)
  209.     printf("\033[1;34m TECNICA   : \033[1;32mARPOP_REQUEST\033[0m\n");
  210.     if(optP)
  211.     printf("\033[1;34m TECNICA   : \033[1;32mICMP_ECHO\033[0m\n");
  212.     fcntl(sockfd, F_GETFL);
  213.       fcntl(sockfd, F_SETFL, fd_flags | O_NONBLOCK);
  214.  
  215.     printf("\n\033[1;34m.oO Inizio Scansione Oo.\033[0m\n\n");
  216.     
  217.     if(optA)
  218.      for(dip=(ntohl(sip)&ntohl(netmask))+1;dip<ntohl(broadcast);dip++) {
  219.         if(htonl(dip) == sip) {
  220.                         dip++;
  221.                 }
  222.         memset(&packet, 0, MAX_LEN);
  223.         hack_p = (struct arp_hack *) packet;    
  224.         
  225.         memcpy (hack_p->h_dest, "\0\1\0\1\0\1", ETH_ALEN);       
  226.         memcpy (hack_p->h_source, mac, ETH_ALEN);
  227.         hack_p->h_proto = htons(ETH_P_ARP);
  228.         hack_p->ar_hrd = htons(ARPHRD_ETHER);
  229.         hack_p->ar_pro = htons(ETH_P_IP);
  230.         hack_p->ar_hln = 6;
  231.         hack_p->ar_pln = 4;
  232.         hack_p->ar_op = htons(ARPOP_REQUEST);
  233.         memcpy (hack_p->ar_sha, mac, ETH_ALEN);
  234.         memcpy (hack_p->ar_sip, &sip, IP_ALEN);
  235.         memcpy (hack_p->ar_tha, "\0\0\0\0\0\0", ETH_ALEN);
  236.         dmp=htonl(dip);
  237.         memcpy (hack_p->ar_tip, &dmp, IP_ALEN);
  238.  
  239.         strcpy(sa.sa_data, argv[2]);
  240.               sa.sa_family = AF_UNIX;
  241.  
  242.               if( sendto (sockfd, packet, sizeof (struct arp_hack), 0, 
  243.             &sa, sizeof(sa)) <0)          
  244.                 fprintf(stderr, "errore sendto\n");
  245.  
  246.               usleep (50);
  247.         memset(&packet, 0, MAX_LEN);
  248.                 hack_p = (struct arp_hack *) packet;
  249.  
  250.               len = recvfrom (sockfd, packet, MAX_LEN, 0, &sa, &sa_len);
  251.               if (len <= IF_LEN) continue;
  252.  
  253.         memcpy (&dmp, hack_p->ar_tip, IP_ALEN);
  254.               memcpy (&saddr, hack_p->ar_sip, IP_ALEN);
  255.  
  256.               if ( ntohs(hack_p->ar_op) == ARPOP_REPLY
  257.                   && dmp == sip && (dip-ntohl(saddr) >= 0 )
  258.                   && (dip-ntohl(saddr) <= 2 ) ) {
  259.               printf ("\033[1;32mIP %s, MAC %s - \033[5;32mModalita' Promiscua !\033[0m\n", 
  260.             ntoa(saddr), dumpHW(hack_p->ar_sha));
  261.             }
  262.        }
  263.  
  264.     if(optP)
  265.      for(dip=(ntohl(sip)&ntohl(netmask))+1;dip<ntohl(broadcast);dip++) {
  266.                 if(htonl(dip) == sip) {
  267.                         dip++;
  268.                 }
  269.                 sa_len = sizeof(struct sockaddr);
  270.                 memset(&sa, 0, sa_len);
  271.                 sa.sa_family = AF_UNIX;
  272.                 strcpy(sa.sa_data, argv[2]);
  273.                 
  274.         memset(&packet, 0, MAX_LEN);
  275.                 ping = (struct pinghack *) packet;
  276.                 ptr = packet;
  277.                 
  278.         memcpy(ping->h_dest, "\0\1\0\1\0\1", ETH_ALEN);
  279.                 memcpy(ping->h_source, mac, ETH_ALEN);
  280.                 ping->h_proto = htons(ETH_P_IP);
  281.                 ping->ihl_ver = 0x45;
  282.                 ping->tos = 0x00;
  283.                 ping->tot_len = htons(IP_HLEN+ICMP_HLEN);
  284.                 ping->id = 0x0000;
  285.                 ping->frag_off = 0x0000;
  286.                 ping->ttl = 0xFF;
  287.                 ping->protocol = IPPROTO_ICMP;
  288.                 ptr += 26; *((u_long *)ptr) = sip;
  289.                 ptr += 4; *((u_long *)ptr) = htonl(dip);
  290.                 ping->check = 0;
  291.                 ping->check = ip_fast_csum((unsigned char *)
  292.                         ping+ETH_HLEN, 20);
  293.                 ptr +=4; *((u_char *)ptr) = 8;
  294.                 ptr +=1; *((u_char *)ptr) = 0;
  295.                 ptr +=3; *((u_short *)ptr) = pid;
  296.                 ptr +=2; *((u_short *)ptr) = 0xF001;
  297.                 ping->checksum = 0;
  298.                 ping->checksum = ip_fast_csum((unsigned char *)
  299.                         ping+(ETH_HLEN+IP_HLEN), 8);
  300.  
  301.                 if((sent=sendto(sockfd, &packet, ETH_HLEN+IP_HLEN+ICMP_HLEN, 0,
  302.                         &sa, sa_len)) < 0 ) {
  303.                         fprintf(stderr, "Errore sendto\n");
  304.                         return(-1);
  305.                 }
  306.                 usleep(50);
  307.                 memset(&packet, 0, MAX_LEN);
  308.                 ptr = packet;
  309.  
  310.                 len = recvfrom (sockfd, packet, ETH_HLEN+IP_HLEN+ICMP_HLEN, 0,
  311.                         &sa, &sa_len);
  312.                 if (len <= ETH_HLEN) continue;
  313.  
  314.                 ptr +=34;
  315.                 if(*((u_char*)ptr) == 0) {
  316.                    ptr +=6;
  317.                    if(*((u_short*)ptr) == 0xF001) {
  318.                         ptr -=14;
  319.                         if((dip - ntohl(*((u_long*)ptr)) >= 0) &&
  320.                            (dip - ntohl(*((u_long*)ptr)) <= 2)) {
  321.                       printf ("\033[1;32mIP %s - <ECHOREPLY> \033[5;32mModalita' Promiscua !\033[0m\n", 
  322.                 ntoa(*((u_long*)ptr)));
  323.                                 }
  324.                         }
  325.                 }
  326.         }        
  327.       printf("\n\033[1;34m.oO  Fine Scansione  Oo.\033[0m\n\n");
  328.     exit (0);
  329. }
  330.