Skip to content

Instantly share code, notes, and snippets.

@naezith
Created January 4, 2016 12:21
Show Gist options
  • Save naezith/f88e45dd8e5db69983da to your computer and use it in GitHub Desktop.
Save naezith/f88e45dd8e5db69983da to your computer and use it in GitHub Desktop.
Brute Force Password Cracker
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#define PASSWORD_TO_CRACK "vK6b"
// Increment the lowest digit by 1, check if it passed the last character in the char set
int incrementPass(char* password, int maxChar, int currLength) {
++password[0];
// Check if any of digits are full
int i = 0;
while(i < currLength && password[i] >= maxChar) {
password[i] = 0;
if(i < currLength - 1) ++password[++i];
else return 0;
}
return 1;
}
// Convert count string to readable characters in the character set
void convertPassword(char* password, const char* table, char* iteration, int length) {
int i;
for(i = 0; i < length; ++i) iteration[i] = table[ password[i] ];
iteration[length + 1] = '\0';
}
// Check if current iteration matches the real password
int tryThePassword(char* iteration, char* realPW, unsigned int* counter, unsigned int* billion) {
if(strcmp(iteration, realPW) == 0) {
printf("\nFOUND %s at try #%d%d\n", iteration, *billion, *counter);
return 1;
} else if(++(*counter) % 100000000 == 0) { // 100 Million
printf(".");
if((*counter) % 1000000000 == 0) { // 1 Billion
++(*billion);
*counter = 0;
printf("\nTried %d billion combinations, still not found..\n", *billion);
}
}
return 0;
}
// Main
int main() {
const char* table = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const int minLength = 1, maxLength = 12, maxChar = strlen(table);
char* password = (char*)calloc(maxLength + 1, sizeof(char)),
*iteration = (char*)calloc(maxLength + 1, sizeof(char));
unsigned int counter = 1, billion = 0;
int i;
for(i = minLength; i <= maxLength; ++i) {
do { // Iterate every single combination with i length
convertPassword(password, table, iteration, i);
if(tryThePassword(iteration, PASSWORD_TO_CRACK, &counter, &billion)) return 0;
} while(incrementPass(password, maxChar, i));
// Clear password to start searching i + 1 length passwords
memset(password, 0, i);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment