-
Notifications
You must be signed in to change notification settings - Fork 10
/
bitcoin-wallet-bruteforce-offline.go
151 lines (119 loc) · 3.78 KB
/
bitcoin-wallet-bruteforce-offline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//offline version, use any database u want to achieve this, I used http://alladdresses.loyce.club/
package main
import (
"bufio"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/hex"
"fmt"
"log"
"os"
"strconv"
"sync"
"github.com/btcsuite/btcutil/base58"
"golang.org/x/crypto/ripemd160"
"crypto/sha256"
)
func readAddresses(filePath string) (map[string]bool, error) {
addresses := make(map[string]bool)
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
addresses[scanner.Text()] = true
}
if err := scanner.Err(); err != nil {
return nil, err
}
return addresses, nil
}
func generateKeyAndAddress() (string, string, error) {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return "", "", err
}
publicKey := privateKey.PublicKey
address, err := publicKeyToAddress(publicKey)
if err != nil {
return "", "", err
}
return hex.EncodeToString(privateKey.D.Bytes()), address, nil
}
func publicKeyToAddress(publicKey ecdsa.PublicKey) (string, error) {
pubKeyBytes := append(publicKey.X.Bytes(), publicKey.Y.Bytes()...)
sha256Hash := sha256.New()
sha256Hash.Write(pubKeyBytes)
sha256Result := sha256Hash.Sum(nil)
ripemd160Hash := ripemd160.New()
ripemd160Hash.Write(sha256Result)
ripemd160Result := ripemd160Hash.Sum(nil)
networkVersion := byte(0x00)
addressBytes := append([]byte{networkVersion}, ripemd160Result...)
checksum := sha256Checksum(addressBytes)
fullAddress := append(addressBytes, checksum...)
return base58.Encode(fullAddress), nil
}
func sha256Checksum(input []byte) []byte {
firstSHA := sha256.New()
firstSHA.Write(input)
result := firstSHA.Sum(nil)
secondSHA := sha256.New()
secondSHA.Write(result)
finalResult := secondSHA.Sum(nil)
return finalResult[:4]
}
func worker(id int, wg *sync.WaitGroup, mutex *sync.Mutex, outputFile string, btcAddresses map[string]bool) {
defer wg.Done()
for {
privateKey, publicAddress, err := generateKeyAndAddress()
if err != nil {
log.Printf("Worker %d: Failed to generate key and address: %s", id, err)
continue
}
matchFound := "No"
if _, exists := btcAddresses[publicAddress]; exists {
matchFound = "Yes"
fmt.Printf("Match Found! Privatekey: %s Publicaddress: %s\n", privateKey, publicAddress)
mutex.Lock()
file, err := os.OpenFile(outputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Printf("Worker %d: Failed to open file: %s", id, err)
mutex.Unlock()
continue
}
if _, err := file.WriteString(fmt.Sprintf("%s:%s\n", privateKey, publicAddress)); err != nil {
log.Printf("Worker %d: Failed to write to file: %s", id, err)
}
file.Close()
mutex.Unlock()
}
fmt.Printf("Private Key: %s Public Address: %s Match: %s\n", privateKey, publicAddress, matchFound)
}
}
func main() {
if len(os.Args) != 4 {
fmt.Println("Usage: ./golangscript <threads> <output-file.txt> <btc-address-file.txt>")
os.Exit(1)
}
numThreads, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatalf("Invalid number of threads: %s", err)
}
outputFile := os.Args[2]
btcAddressesFile := os.Args[3]
btcAddresses, err := readAddresses(btcAddressesFile)
if err != nil {
log.Fatalf("Failed to read BTC addresses: %s", err)
}
var wg sync.WaitGroup
var mutex sync.Mutex
for i := 0; i < numThreads; i++ {
wg.Add(1)
go worker(i, &wg, &mutex, outputFile, btcAddresses)
}
wg.Wait()
}