2015-05-11 12:24:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
2016-04-25 15:15:02 +00:00
|
|
|
#include "bip39.h"
|
|
|
|
#include "bip32.h"
|
|
|
|
#include "ecdsa.h"
|
|
|
|
#include "curves.h"
|
2015-05-11 12:24:45 +00:00
|
|
|
|
2016-08-15 13:05:32 +00:00
|
|
|
char iter[256];
|
2015-05-11 12:24:45 +00:00
|
|
|
uint8_t seed[512 / 8];
|
|
|
|
uint8_t addr[21], pubkeyhash[20];
|
|
|
|
int count = 0, found = 0;
|
|
|
|
HDNode node;
|
|
|
|
clock_t start;
|
|
|
|
|
2016-08-15 13:05:32 +00:00
|
|
|
// around 280 tries per second
|
2015-05-11 12:24:45 +00:00
|
|
|
|
|
|
|
// testing data:
|
|
|
|
//
|
|
|
|
// mnemonic: "all all all all all all all all all all all all"
|
2016-08-15 13:05:32 +00:00
|
|
|
// address: "1JAd7XCBzGudGpJQSDSfpmJhiygtLQWaGL"
|
|
|
|
// passphrase: ""
|
|
|
|
//
|
|
|
|
// mnemonic: "all all all all all all all all all all all all"
|
2015-05-11 12:24:45 +00:00
|
|
|
// address: "1N3uJ5AU3FTYQ1ZQgTMtYmgSvMBmQiGVBS"
|
|
|
|
// passphrase: "testing"
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2016-08-15 13:05:32 +00:00
|
|
|
if (argc != 2 && argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: bip39bruteforce address [mnemonic]\n");
|
2015-05-11 12:24:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-08-15 13:05:32 +00:00
|
|
|
const char *address = argv[1];
|
|
|
|
const char *mnemonic, *item;
|
|
|
|
if (argc == 3) {
|
|
|
|
mnemonic = argv[2];
|
|
|
|
item = "passphrase";
|
|
|
|
} else {
|
|
|
|
mnemonic = NULL;
|
|
|
|
item = "mnemonic";
|
|
|
|
}
|
|
|
|
if (mnemonic && !mnemonic_check(mnemonic)) {
|
2015-05-11 12:24:45 +00:00
|
|
|
fprintf(stderr, "\"%s\" is not a valid mnemonic\n", mnemonic);
|
|
|
|
return 2;
|
|
|
|
}
|
2017-12-09 17:59:44 +00:00
|
|
|
if (!ecdsa_address_decode(address, 0, HASHER_SHA2, addr)) {
|
2015-05-11 12:24:45 +00:00
|
|
|
fprintf(stderr, "\"%s\" is not a valid address\n", address);
|
|
|
|
return 3;
|
|
|
|
}
|
2016-08-15 13:05:32 +00:00
|
|
|
printf("Reading %ss from stdin ...\n", item);
|
2015-05-11 12:24:45 +00:00
|
|
|
start = clock();
|
|
|
|
for (;;) {
|
2016-08-15 13:05:32 +00:00
|
|
|
if (fgets(iter, 256, stdin) == NULL) break;
|
|
|
|
int len = strlen(iter);
|
2015-05-11 12:24:45 +00:00
|
|
|
if (len <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
count++;
|
2016-08-15 13:05:32 +00:00
|
|
|
iter[len - 1] = 0;
|
|
|
|
if (mnemonic) {
|
|
|
|
mnemonic_to_seed(mnemonic, iter, seed, NULL);
|
|
|
|
} else {
|
|
|
|
mnemonic_to_seed(iter, "", seed, NULL);
|
|
|
|
}
|
2016-04-25 15:15:02 +00:00
|
|
|
hdnode_from_seed(seed, 512 / 8, SECP256K1_NAME, &node);
|
2015-05-11 12:24:45 +00:00
|
|
|
hdnode_private_ckd_prime(&node, 44);
|
|
|
|
hdnode_private_ckd_prime(&node, 0);
|
|
|
|
hdnode_private_ckd_prime(&node, 0);
|
|
|
|
hdnode_private_ckd(&node, 0);
|
|
|
|
hdnode_private_ckd(&node, 0);
|
2016-08-15 13:05:32 +00:00
|
|
|
hdnode_fill_public_key(&node);
|
2017-12-09 17:59:44 +00:00
|
|
|
ecdsa_get_pubkeyhash(node.public_key, HASHER_SHA2, pubkeyhash);
|
2015-05-11 12:24:45 +00:00
|
|
|
if (memcmp(addr + 1, pubkeyhash, 20) == 0) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
float dur = (float)(clock() - start) / CLOCKS_PER_SEC;
|
2016-08-15 13:05:32 +00:00
|
|
|
printf("Tried %d %ss in %f seconds = %f tries/second\n", count, item, dur, (float)count/dur);
|
2015-05-11 12:24:45 +00:00
|
|
|
if (found) {
|
2016-08-15 13:05:32 +00:00
|
|
|
printf("Correct %s found! :-)\n\"%s\"\n", item, iter);
|
2015-05-11 12:24:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-08-15 13:05:32 +00:00
|
|
|
printf("Correct %s not found. :-(\n", item);
|
2015-05-11 12:24:45 +00:00
|
|
|
return 4;
|
|
|
|
}
|