Advertisement
linux

syn-scanner.c

Jul 16th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.62 KB | None | 0 0
  1. #include <libnet.h>
  2. #include <stdio.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <pcap.h>
  9. #include <time.h>
  10.  
  11.  
  12. int main(int argc, char **argv) {
  13.     libnet_t *l;
  14.     libnet_ptag_t t;
  15.     unsigned short burst=50;
  16.     unsigned short ct=0;
  17.     char errbuff[LIBNET_ERRBUF_SIZE];
  18.     unsigned long myip;
  19.     struct in_addr sc;
  20.     unsigned char tcpopt[]="\x02\x04\x05\xb4\x01\x01\x04\x02";
  21.  
  22.     unsigned short port;
  23.     unsigned long usec;
  24.     char cc;
  25.     int i;
  26.     pid_t pid;
  27.     pcap_t *handle;
  28.     char *temp_char;
  29.     bpf_u_int32 mask;
  30.     bpf_u_int32 net;
  31.     char errbuf[PCAP_ERRBUF_SIZE];
  32.     char filter[1024];
  33.     struct bpf_program cfilter;
  34.     struct pcap_pkthdr header;
  35.     const unsigned char *packet;
  36.     struct in_addr ekkt;
  37.     unsigned char ip[50];
  38.  
  39.     unsigned long dstip=0;
  40.     unsigned short sport;
  41.     char *interface=NULL;
  42.     unsigned char bclass=0;
  43.     unsigned char aclass=0;
  44.     unsigned char rclass=1;
  45.     unsigned int a=0,b=0,c=0,d=0;
  46.  
  47.     srand(time(NULL));
  48.     sport=rand();
  49.     usec=1000000;
  50.     if(argc<2) {
  51.         printf("usage: %s <port> [-a <a class> | -b <b class>] [-i <interface] [-s <speed>]\n",argv[0]);
  52.         printf("speed 10 -> as fast as possible, 1 -> it will take bloody ages (about 50 syns/s)\n");
  53.     }
  54.     for(i=1; i<argc; i++) {
  55.         if(strstr(argv[i],"-s")) {
  56.             if(i+1<argc) {
  57.                 switch (atoi(argv[i+1])) {
  58.                 case 1:
  59.                     usec=1000000;
  60.                     break;
  61.                 case 2:
  62.                     usec=500000;
  63.                     break;
  64.                 case 3:
  65.                     usec=250000;
  66.                     break;
  67.                 case 4:
  68.                     usec=125000;
  69.                     break;
  70.                 case 5:
  71.                     usec=60000;
  72.                     break;
  73.                 case 6:
  74.                     usec=30000;
  75.                     break;
  76.                 case 7:
  77.                     usec=10000;
  78.                     break;
  79.                 case 8:
  80.                     usec=1000;
  81.                     break;
  82.                 case 9:
  83.                     usec=100;
  84.                     break;
  85.                 case 10:
  86.                     usec=0;
  87.                     burst=65535;
  88.                 }
  89.  
  90.             } else {
  91.                 printf("-s requires an argument\n");
  92.                 exit(0x01);
  93.             }
  94.         }
  95.  
  96.         if(strstr(argv[i],"-i")) {
  97.             if(i+1<argc) interface=argv[i+1];
  98.             else {
  99.                 printf("-i requires an argument\n");
  100.                 exit(0x01);
  101.             }
  102.         }
  103.         if(strstr(argv[i],"-a")) {
  104.             if(i+1<argc)  {
  105.                 aclass=1;
  106.                 bclass=0;
  107.                 rclass=0;
  108.                 a=atoi(argv[i+1]);
  109.                 b=0;
  110.                 c=0;
  111.                 d=0;
  112.                 if((a<1) || (a>254)) {
  113.                     printf("A must be between 1 and 254\n");
  114.                     exit(0x02);
  115.                 }
  116.                 printf("scanning network %d.*.*.*\n",a);
  117.             } else {
  118.                 printf("-a requires an A network as argument\n");
  119.                 exit(0x01);
  120.             }
  121.         }
  122.         if(strstr(argv[i],"-b")) {
  123.             if(i+1<argc) {
  124.                 aclass=0;
  125.                 bclass=1;
  126.                 rclass=0;
  127.                 a=atoi(strtok(argv[i+1],"."));
  128.                 temp_char=strtok(NULL,".");
  129.                 if(temp_char==NULL)
  130.                     b=0;
  131.                 else b=atoi(temp_char);
  132.                 c=0;
  133.                 d=0;
  134.                 if((a<1) || (a>254))  {
  135.                     printf("A must be between 1 and 254\n");
  136.                     exit(0x02);
  137.                 }
  138.                 printf("scanning network %d.%d.*.*\n",a,b);
  139.             } else {
  140.                 printf("-b requires an B network as argument(e.g. 192.168)\n");
  141.                 exit(0x01);
  142.             }
  143.         }
  144.     }
  145.     printf("usec: %ld, burst packets %d\n",usec,burst);
  146.     port=(unsigned short)atoi(argv[1]);
  147.     if((port<1) || (port>65535)) exit(printf("damn dude, port numbers are in 1 .. 65535\n"));
  148.     if(interface!=NULL) printf("using inteface %s\n",interface);
  149.  
  150.     l=libnet_init(LIBNET_RAW4,interface,errbuff);
  151.     if(!l) {
  152.         printf("ERROR: %s\n",errbuff);
  153.         exit(0x02);
  154.     }
  155.     myip=libnet_get_ipaddr4(l);
  156.     sc.s_addr=myip;
  157.     sprintf(filter,"(tcp[tcpflags]=0x12) and (src port %d) and (dst port %d)",port,sport);
  158.     printf("using \"%s\" as pcap filter\n",filter);
  159.     printf("my detected ip on %s is %s\n",l->device,inet_ntoa(sc));
  160.     pcap_lookupnet(l->device, &net, &mask, errbuf);
  161.     pid=fork();
  162.     handle=NULL;
  163.     handle = pcap_open_live(l->device, BUFSIZ, 1, 0, errbuf);
  164.     if(handle==NULL) {
  165.         printf("ERROR: pcap_open_live() : %s\n",errbuff);
  166.         exit(0x05);
  167.     }
  168.     cc=pcap_compile(handle, &cfilter, filter, 0, net);
  169.     if(cc!=0) {
  170.         printf("ERROR: pcap_compile() failed!!!\n");
  171.         exit(0);
  172.     }
  173.     cc=pcap_setfilter(handle, &cfilter);
  174.     if(cc!=0) {
  175.         printf("ERROR: pcap_setfilter() failed!!!\n");
  176.         exit(0);
  177.     }
  178.     if(pid==0) {
  179.         while(1) {
  180.             packet = pcap_next(handle, &header);
  181.             memcpy(&ekkt.s_addr,packet+26,4);
  182.             printf("%s\n",inet_ntoa(ekkt));
  183.             FILE * fp;
  184.             fp=fopen("bios.txt","a+");
  185.             fprintf(fp,"%s\n",inet_ntoa(ekkt));
  186.             fclose(fp);
  187.         }
  188.     }
  189.     if(pid > 0) {
  190.         printf("capturing process started pid %d\n",pid);
  191.         usleep(500000);
  192.         while(1) {
  193.             t=LIBNET_PTAG_INITIALIZER;
  194.             t=libnet_build_tcp_options(tcpopt, 8, l,0);
  195.             t=libnet_build_tcp(sport,port,rand(),rand(),TH_SYN,65535,0,0,LIBNET_TCP_H+8,NULL,0,l,0);
  196.             if(rclass) dstip=rand();
  197.             if(aclass) {
  198.                 if(d==0) printf("scanning %d.%d.%d.*\n",a,b,c);
  199.                 d++;
  200.                 if(d>255) {
  201.                     c++;
  202.                     d=0;
  203.                 }
  204.                 if(c>255) {
  205.                     b++;
  206.                     c=0;
  207.                 }
  208.                 sprintf(ip,"%d.%d.%d.%d\n",a,b,c,d);
  209.  
  210.                 if((b==255)&& (c==255) && (d==255)) {
  211.                     printf("aici trebuie stop\n");
  212.                     sleep(10);
  213.                     kill(pid,2);
  214.                     return 0;
  215.                 }
  216.                 sc.s_addr=inet_addr(ip);
  217.                 dstip=sc.s_addr;
  218.             }
  219.             if(bclass) {
  220.                 if(d==0) printf("scanning %d.%d.%d.*\n",a,b,c);
  221.                 d++;
  222.                 if(d>255) {
  223.                     c++;
  224.                     d=0;
  225.                 }
  226.                 sprintf(ip,"%d.%d.%d.%d",a,b,c,d);
  227.                 if((c==255) && (d==255)) {
  228.                     printf("%s\n",ip);
  229.                     printf("aici trebuie stop\n");
  230.                     sleep(10);
  231.                     kill(pid,2);
  232.                     return 0;
  233.                 }
  234.                 sc.s_addr=inet_addr(ip);
  235.                 dstip=sc.s_addr;
  236.             }
  237.  
  238.             libnet_build_ipv4(LIBNET_TCP_H+LIBNET_IPV4_H+8,0,rand(),0,128,IPPROTO_TCP,0,myip,dstip,NULL,0,l,0);
  239.             cc=libnet_write(l);
  240.             if(cc<=0) printf("libnet_write() wtf %d\n",cc);
  241.             libnet_clear_packet(l);
  242.             if(ct==burst) {
  243.                 usleep(usec);
  244.                 ct=0;
  245.             };
  246.             ct++;
  247.         }
  248.  
  249.     }
  250.     if(pid<0) {
  251.         printf("cannot fork()\n");
  252.         exit(0x05);
  253.     }
  254.     return 0;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement