/* * Bitcoin P2PK Bruteforce - Ricerca chiavi private * Utilizza libsecp256k1 per massima efficienza * * DISCLAIMER: Solo per scopi educativi e di ricerca */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Configurazione #define BATCH_SIZE 10000 #define SAVE_INTERVAL 300 // Salva progresso ogni 5 minuti #define PROGRESS_INTERVAL 1000000 // Mostra progresso ogni N tentativi #define MAX_THREADS 256 // Massimo numero di thread supportati // Struttura per memorizzare le chiavi pubbliche target struct TargetKey { uint8_t pubkey[65]; // Chiave pubblica non compressa (65 bytes) char hex[131]; // Rappresentazione hex }; // Variabili globali static volatile int keep_running = 1; static secp256k1_context* ctx = NULL; static std::vector target_keys; static std::unordered_set target_set; static uint64_t total_attempts = 0; static uint64_t attempts_per_thread[MAX_THREADS] = {0}; static time_t start_time; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static FILE* log_file = NULL; static int num_threads = 0; // Numero effettivo di thread da usare // Struttura per i thread struct ThreadData { int thread_id; uint64_t seed; }; // Rileva numero di thread/core disponibili int get_num_threads() { int num = (int)sysconf(_SC_NPROCESSORS_ONLN); if (num < 1) num = 1; if (num > MAX_THREADS) num = MAX_THREADS; return num; } // Signal handler per chiusura pulita void sigint_handler(int sig) { (void)sig; keep_running = 0; printf("\n\n[!] Interruzione rilevata, chiusura in corso...\n"); } // Converti bytes in hex void bytes_to_hex(const uint8_t* bytes, size_t len, char* hex) { for (size_t i = 0; i < len; i++) { sprintf(hex + (i * 2), "%02x", bytes[i]); } hex[len * 2] = '\0'; } // Converti hex in bytes int hex_to_bytes(const char* hex, uint8_t* bytes, size_t len) { if (strlen(hex) != len * 2) return 0; for (size_t i = 0; i < len; i++) { sscanf(hex + (i * 2), "%2hhx", &bytes[i]); } return 1; } // Carica le chiavi pubbliche P2PK dal file int load_target_keys(const char* filename) { std::ifstream file(filename); if (!file.is_open()) { fprintf(stderr, "[ERROR] Impossibile aprire %s\n", filename); return 0; } std::string line; int count = 0; // Skip header se presente std::getline(file, line); while (std::getline(file, line)) { if (line.empty()) continue; // Estrai la chiave pubblica (formato: hex della pubkey) // Il file dovrebbe contenere una pubkey per riga std::string pubkey_hex = line; // Rimuovi spazi bianchi pubkey_hex.erase(remove_if(pubkey_hex.begin(), pubkey_hex.end(), isspace), pubkey_hex.end()); // P2PK non compresso: 65 bytes (130 caratteri hex) // Formato: 04 + 32 bytes X + 32 bytes Y if (pubkey_hex.length() != 130 && pubkey_hex.length() != 128) { continue; // Skip se non è una pubkey valida } // Aggiungi 04 se manca (formato non compresso) if (pubkey_hex.length() == 128) { pubkey_hex = "04" + pubkey_hex; } TargetKey key; if (hex_to_bytes(pubkey_hex.c_str(), key.pubkey, 65)) { strcpy(key.hex, pubkey_hex.c_str()); target_keys.push_back(key); target_set.insert(std::string((char*)key.pubkey, 65)); count++; } } file.close(); printf("[+] Caricate %d chiavi pubbliche target\n", count); return count; } // Genera una chiave privata casuale void generate_random_privkey(uint8_t* privkey, uint64_t* seed) { // Usa un PRNG veloce (xorshift64) *seed ^= *seed << 13; *seed ^= *seed >> 7; *seed ^= *seed << 17; // Riempi i 32 bytes della privkey uint64_t* key64 = (uint64_t*)privkey; for (int i = 0; i < 4; i++) { *seed ^= *seed << 13; *seed ^= *seed >> 7; *seed ^= *seed << 17; key64[i] = *seed; } // Assicurati che la chiave sia valida (< ordine della curva) // In pratica, quasi tutti i 256 bit casuali sono validi } // Incrementa la chiave privata void increment_privkey(uint8_t* privkey) { for (int i = 31; i >= 0; i--) { if (++privkey[i] != 0) break; } } // Verifica se la pubkey corrisponde a un target int check_match(const uint8_t* pubkey) { std::string key_str((char*)pubkey, 65); return target_set.find(key_str) != target_set.end(); } // Salva una chiave trovata void save_found_key(const uint8_t* privkey, const uint8_t* pubkey) { pthread_mutex_lock(&mutex); char priv_hex[65], pub_hex[131]; bytes_to_hex(privkey, 32, priv_hex); bytes_to_hex(pubkey, 65, pub_hex); // Stampa a schermo printf("\n\n"); printf("========================================\n"); printf("🎯 CHIAVE TROVATA! 🎯\n"); printf("========================================\n"); printf("Private Key: %s\n", priv_hex); printf("Public Key: %s\n", pub_hex); printf("========================================\n\n"); // Salva su file FILE* found_file = fopen("found_keys.txt", "a"); if (found_file) { time_t now = time(NULL); fprintf(found_file, "\n=== FOUND at %s", ctime(&now)); fprintf(found_file, "Private Key: %s\n", priv_hex); fprintf(found_file, "Public Key: %s\n", pub_hex); fprintf(found_file, "========================================\n"); fclose(found_file); } pthread_mutex_unlock(&mutex); } // Formatta numero con suffisso K, M, G, T void format_number(uint64_t num, char* buffer) { if (num >= 1000000000000ULL) { sprintf(buffer, "%.2fT", num / 1000000000000.0); } else if (num >= 1000000000ULL) { sprintf(buffer, "%.2fG", num / 1000000000.0); } else if (num >= 1000000ULL) { sprintf(buffer, "%.2fM", num / 1000000.0); } else if (num >= 1000ULL) { sprintf(buffer, "%.2fK", num / 1000.0); } else { sprintf(buffer, "%lu", num); } } // Log progresso void log_progress() { pthread_mutex_lock(&mutex); time_t now = time(NULL); double elapsed = difftime(now, start_time); if (elapsed < 1) elapsed = 1; uint64_t total = 0; for (int i = 0; i < num_threads; i++) { total += attempts_per_thread[i]; } double rate = total / elapsed; char total_str[32]; char rate_str[32]; format_number(total, total_str); format_number((uint64_t)rate, rate_str); printf("[INFO] Tentativi: %s | Velocità: %s keys/sec | Tempo: %.0fs\n", total_str, rate_str, elapsed); if (log_file) { fprintf(log_file, "%ld,%lu,%.2f\n", now, total, rate); fflush(log_file); } pthread_mutex_unlock(&mutex); } // Thread worker void* worker_thread(void* arg) { ThreadData* data = (ThreadData*)arg; int thread_id = data->thread_id; uint64_t seed = data->seed; uint8_t privkey[32]; uint8_t pubkey[65]; secp256k1_pubkey pubkey_obj; size_t pubkey_len = 65; uint64_t local_attempts = 0; printf("[+] Thread %d avviato (seed: %lu)\n", thread_id, seed); while (keep_running) { // Genera batch di chiavi for (int batch = 0; batch < BATCH_SIZE && keep_running; batch++) { // Genera chiave privata casuale generate_random_privkey(privkey, &seed); // Genera chiave pubblica non compressa usando secp256k1 if (secp256k1_ec_pubkey_create(ctx, &pubkey_obj, privkey)) { // Serializza in formato non compresso (65 bytes) secp256k1_ec_pubkey_serialize(ctx, pubkey, &pubkey_len, &pubkey_obj, SECP256K1_EC_UNCOMPRESSED); // Verifica corrispondenza if (check_match(pubkey)) { save_found_key(privkey, pubkey); } } local_attempts++; // Incrementa la chiave privata per il prossimo tentativo increment_privkey(privkey); } // Aggiorna contatore globale pthread_mutex_lock(&mutex); attempts_per_thread[thread_id] = local_attempts; pthread_mutex_unlock(&mutex); // Mostra progresso periodicamente if (local_attempts % PROGRESS_INTERVAL == 0) { log_progress(); } } printf("[+] Thread %d terminato (%lu tentativi)\n", thread_id, local_attempts); return NULL; } int main(int argc, char** argv) { printf("========================================\n"); printf(" Bitcoin P2PK Bruteforce v1.0\n"); printf(" SOLO PER SCOPI EDUCATIVI\n"); printf("========================================\n\n"); // Gestisci argomenti const char* target_file = "target_keys.txt"; if (argc > 1) { target_file = argv[1]; } // Inizializza secp256k1 printf("[+] Inizializzazione secp256k1...\n"); ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); if (!ctx) { fprintf(stderr, "[ERROR] Impossibile creare contesto secp256k1\n"); return 1; } // Carica chiavi target printf("[+] Caricamento chiavi target da %s...\n", target_file); if (load_target_keys(target_file) == 0) { fprintf(stderr, "[ERROR] Nessuna chiave target caricata\n"); secp256k1_context_destroy(ctx); return 1; } // Setup signal handler signal(SIGINT, sigint_handler); signal(SIGTERM, sigint_handler); // Apri file di log log_file = fopen("progress.csv", "w"); if (log_file) { fprintf(log_file, "timestamp,attempts,keys_per_sec\n"); } // Rileva numero di thread disponibili num_threads = get_num_threads(); printf("[+] CPU rilevata: %d thread disponibili\n", num_threads); // Inizializza timestamp start_time = time(NULL); // Crea threads pthread_t threads[MAX_THREADS]; ThreadData thread_data[MAX_THREADS]; printf("[+] Avvio %d thread worker...\n\n", num_threads); for (int i = 0; i < num_threads; i++) { thread_data[i].thread_id = i; thread_data[i].seed = (uint64_t)time(NULL) + i * 12345; pthread_create(&threads[i], NULL, worker_thread, &thread_data[i]); } // Loop principale - mostra progresso while (keep_running) { sleep(10); log_progress(); } // Attendi terminazione threads printf("[+] Attesa terminazione threads...\n"); for (int i = 0; i < num_threads; i++) { pthread_join(threads[i], NULL); } // Statistiche finali printf("\n========================================\n"); printf(" STATISTICHE FINALI\n"); printf("========================================\n"); uint64_t total = 0; char thread_str[32]; for (int i = 0; i < num_threads; i++) { total += attempts_per_thread[i]; format_number(attempts_per_thread[i], thread_str); printf("Thread %d: %s tentativi\n", i, thread_str); } time_t end_time = time(NULL); double elapsed = difftime(end_time, start_time); if (elapsed < 1) elapsed = 1; char total_str[32]; char rate_str[32]; format_number(total, total_str); format_number((uint64_t)(total / elapsed), rate_str); printf("----------------------------------------\n"); printf("Totale tentativi: %s\n", total_str); printf("Tempo totale: %.0f secondi\n", elapsed); printf("Velocità media: %s keys/sec\n", rate_str); printf("========================================\n\n"); // Cleanup if (log_file) fclose(log_file); secp256k1_context_destroy(ctx); printf("[+] Programma terminato\n"); return 0; }