Advertisement
IRunYourModerm

[ C ] LDAP AMP Scanner

Apr 13th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.52 KB | None | 0 0
  1. //working LDAP scanner
  2. //Uploaded by pepperhaxz1337, i did not code / make / create this.
  3.  
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <signal.h>
  12. #include <sys/time.h>
  13. #include <sys/types.h>
  14. #include <math.h>
  15. #include <ctype.h>
  16. #include <errno.h>
  17. #include <arpa/inet.h>
  18. #include <netinet/ip.h>
  19. #include <netinet/udp.h>
  20.  
  21. volatile int running_threads = 0;
  22. volatile int found_srvs = 0;
  23. volatile unsigned long per_thread = 0;
  24. volatile unsigned long start = 0;
  25. volatile unsigned long scanned = 0;
  26. volatile int sleep_between = 0;
  27. volatile int bytes_sent = 0;
  28. volatile unsigned long hosts_done = 0;
  29. FILE *fd;
  30. char payload[] =
  31. "\x30\x84\x00\x00\x00\x2d\x02\x01\x07\x63\x84\x00\x00\x00\x24\x04\x00\x0a\x01\x00\x0a\x01\x00\x02\x01\x00\x02\x01\x64\x01\x01\x00\x87\x0b\x6f\x62\x6a\x65\x63\x74\x43\x6c\x61\x73\x73\x30\x84\x00\x00\x00\x00";
  32.  
  33. size = sizeof(payload);
  34.  
  35. void *flood(void *par1)
  36. {
  37.     running_threads++;
  38.     int thread_id = (int)par1;
  39.     unsigned long start_ip = htonl(ntohl(start)+(per_thread*thread_id));
  40.     unsigned long end = htonl(ntohl(start)+(per_thread*(thread_id+1)));
  41.     unsigned long w;
  42.     int y;
  43.     unsigned char buf[65536];
  44.     memset(buf, 0x01, 51);
  45.     int sizeofpayload = 51;
  46.     int sock;
  47.     if((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))<0) {
  48.         perror("cant open socket");
  49.         exit(-1);
  50.     }
  51.     for(w=ntohl(start_ip);w<htonl(end);w++)
  52.     {
  53.         struct sockaddr_in servaddr;
  54.         bzero(&servaddr, sizeof(servaddr));
  55.         servaddr.sin_family = AF_INET;
  56.         servaddr.sin_addr.s_addr=htonl(w);
  57.         servaddr.sin_port=htons(389);
  58.         sendto(sock,payload,size,0, (struct sockaddr *)&servaddr,sizeof(servaddr));
  59.         bytes_sent+=size;
  60.         scanned++;
  61.         hosts_done++;
  62.     }
  63.     close(sock);
  64.     running_threads--;
  65.     return;
  66. }
  67.  
  68. void sighandler(int sig)
  69. {
  70.     fclose(fd);
  71.     printf("\n");
  72.     exit(0);
  73. }
  74.  
  75. void *recievethread()
  76. {
  77.     printf("\n");
  78.     int saddr_size, data_size, sock_raw;
  79.     struct sockaddr_in saddr;
  80.     struct in_addr in;
  81.  
  82.     unsigned char *buffer = (unsigned char *)malloc(65536);
  83.     sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_UDP);
  84.     if(sock_raw < 0)
  85.     {
  86.         printf("Socket Error\n");
  87.         exit(1);
  88.     }
  89.     while(1)
  90.     {
  91.         saddr_size = sizeof saddr;
  92.         data_size = recvfrom(sock_raw , buffer , 65536 , 0 , (struct sockaddr *)&saddr , &saddr_size);
  93.         if(data_size <0 )
  94.         {
  95.             printf("Recvfrom error , failed to get packets\n");
  96.             exit(1);
  97.         }
  98.         struct iphdr *iph = (struct iphdr*)buffer;
  99.         if(iph->protocol == 17)
  100.         {
  101.             unsigned short iphdrlen = iph->ihl*4;
  102.             struct udphdr *udph = (struct udphdr*)(buffer + iphdrlen);
  103.             unsigned char* payload = buffer + iphdrlen + 51;
  104.             if(ntohs(udph->source) == 389)
  105.             {
  106.                 int body_length = data_size - iphdrlen - 51;
  107.                 if (body_length > 40)
  108.                 {
  109.                 found_srvs++;
  110.                 fprintf(fd,"%s %d\n",inet_ntoa(saddr.sin_addr),body_length);
  111.                 fflush(fd);
  112.                 }
  113.             }
  114.         }
  115.     }
  116.     close(sock_raw);
  117. }
  118. int main(int argc, char *argv[ ])
  119. {
  120.     if(argc < 6){
  121.     fprintf(stderr, "Invalid parameters!\n");
  122.     fprintf(stdout, "Usage: %s <ip range start (1.0.0.0)> <ip range end (255.255.255)> <outfile> <threads> <scan delay in ms>\n", argv[0]);
  123.     exit(-1);
  124.     }
  125.     fd = fopen(argv[3], "a");
  126.     sleep_between = atoi(argv[5]);
  127.     signal(SIGINT, &sighandler);
  128.     int threads = atoi(argv[4]);
  129.     pthread_t thread;
  130.     pthread_t listenthread;
  131.     pthread_create( &listenthread, NULL, &recievethread, NULL);
  132.     char *str_start = malloc(18);
  133.     memset(str_start, 0, 18);
  134.     str_start = argv[1];
  135.     char *str_end = malloc(18);
  136.     memset(str_end, 0, 18);
  137.     str_end = argv[2];
  138.     start = inet_addr(str_start);
  139.     per_thread = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start))) / threads;
  140.     unsigned long toscan = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start)));
  141.     int i;
  142.     for(i = 0;i<threads;i++){
  143.         pthread_create( &thread, NULL, &flood, (void *) i);
  144.     }
  145.     sleep(1);
  146.     printf("Scan in Progress \n");
  147.     char *temp = (char *)malloc(17);
  148.     memset(temp, 0, 17);
  149.     sprintf(temp, "Found");
  150.     printf("%-16s", temp);
  151.     memset(temp, 0, 17);
  152.     sprintf(temp, "IP/s");
  153.     printf("%-16s", temp);
  154.     memset(temp, 0, 17);
  155.     sprintf(temp, "Bytes/s");
  156.     printf("%-16s", temp);
  157.     memset(temp, 0, 17);
  158.     sprintf(temp, "Threads");
  159.     printf("%-16s", temp);
  160.     memset(temp, 0, 17);
  161.     sprintf(temp, "Percent Done");
  162.     printf("%s", temp);
  163.     printf("\n");
  164.     char *new;
  165.     new = (char *)malloc(16*6);
  166.     while (running_threads > 0)
  167.     {
  168.         printf("\r");
  169.         memset(new, '\0', 16*6);
  170.         sprintf(new, "%s|%-15lu", new, found_srvs);
  171.         sprintf(new, "%s|%-15d", new, scanned);
  172.         sprintf(new, "%s|%-15d", new, bytes_sent);
  173.         sprintf(new, "%s|%-15d", new, running_threads);
  174.         memset(temp, 0, 17);
  175.         int percent_done=((double)(hosts_done)/(double)(toscan))*100;
  176.         sprintf(temp, "%d%%", percent_done);
  177.         sprintf(new, "%s|%s", new, temp);
  178.         printf("%s", new);
  179.         fflush(stdout);
  180.         bytes_sent=0;
  181.         scanned = 0;
  182.         sleep(1);
  183.     }
  184.     printf("\n");
  185.     fclose(fd);
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement