-
Notifications
You must be signed in to change notification settings - Fork 0
/
genonion.go
180 lines (138 loc) · 4.25 KB
/
genonion.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bytes"
"crypto/ed25519"
cryptrand "crypto/rand"
"encoding/base32"
"flag"
"fmt"
"io/ioutil"
mathrand "math/rand"
"os"
"path/filepath"
"sync"
"golang.org/x/crypto/sha3"
)
var newline = []byte("\n")
var onionLenV2 = 16
var allowedCharsV2 = [32]byte{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '2', '3', '4', '5', '6', '7',
}
func init() {
mathrand.Seed(20200701)
}
func genOnionV2() []byte {
address := make([]byte, onionLenV2)
for i := 0; i < onionLenV2; i++ {
// Generate new pseudo-random index.
idx := mathrand.Intn(len(allowedCharsV2))
// Assign character at idx in allowedCharsV2
// to current position in address.
address[i] = allowedCharsV2[idx]
}
return address
}
func genOnionV3() []byte {
checksumArray := make([]byte, 48)
addressArray := make([]byte, 35)
address := make([]byte, 56)
// testKey, _ := hex.DecodeString("d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a")
// var pubKey [32]byte
// copy(pubKey[:], testKey)
// Generate fresh ed25519 key pair,
// but only keep the public key.
pubKey, _, err := ed25519.GenerateKey(cryptrand.Reader)
if err != nil {
fmt.Printf("Error while generating new key pair: %v", err)
os.Exit(1)
}
// Construct the checksum array. It consists of the
// following fields:
// ".onion checksum" (15B) || pubkey (32B) || 3 (1B)
copy(checksumArray, ".onion checksum")
copy(checksumArray[15:], pubKey[:])
checksumArray[47] = 3
// Calculate the SHA3-256 checksum of the array.
checksum := sha3.Sum256(checksumArray)
// Construct the array that makes up the final .onion
// address. It consists of the following fields:
// pubkey (32B) || checksum[0:2] (2B) || 3 (1B)
copy(addressArray, pubKey[:])
copy(addressArray[32:], checksum[0:2])
addressArray[34] = 3
// Encode the address array as Base32 and convert
// the whole address to lower-case.
base32.StdEncoding.Encode(address, addressArray)
address = bytes.ToLower(address)
return address
}
func worker(wg *sync.WaitGroup, workerID int, onionVer int, numAddrGen int, outputDir string) {
defer wg.Done()
addrList := make([][]byte, numAddrGen)
for i := 0; i < numAddrGen; i++ {
// Generate new .onion address.
if onionVer == 2 {
addrList[i] = genOnionV2()
} else if onionVer == 3 {
addrList[i] = genOnionV3()
}
}
// Collapse slice of address slices into
// single slice with newline separator.
addr := bytes.Join(addrList, newline)
// Construct path to output file.
addrFilePath := ""
if onionVer == 2 {
addrFilePath = filepath.Join(outputDir, fmt.Sprintf("v2_%04d.addr", workerID))
} else if onionVer == 3 {
addrFilePath = filepath.Join(outputDir, fmt.Sprintf("v3_%04d.addr", workerID))
}
// Write newline-separated list of generated
// addresses to output file of this worker.
err := ioutil.WriteFile(addrFilePath, addr, 0644)
if err != nil {
fmt.Printf("[worker%d] Could not write to output file for v%d addresses: %v\n", workerID, onionVer, err)
os.Exit(1)
}
}
func main() {
v2Flag := flag.Bool("v2", false, "Append this flag if a file with v2 .onion addresses is supposed to be generated.")
v3Flag := flag.Bool("v3", false, "Append this flag if a file with v3 .onion addresses is supposed to be generated.")
numAddrFlag := flag.Int("numAddr", 5000, "Specify a multiple of 5,000 as the number of v2 and/or v3 addresses to generate.")
outputDirFlag := flag.String("outputDir", "./attack_addr/", "Supply file system location to store the lists.")
flag.Parse()
if !*v2Flag && !*v3Flag {
fmt.Printf("At least one of the flags '-v2' or '-v3' has to be supplied.\n")
os.Exit(1)
}
if (*numAddrFlag % 5000) != 0 {
fmt.Printf("Number of addresses to generate must be a multiple of 5,000.\n")
os.Exit(1)
}
v2 := *v2Flag
v3 := *v3Flag
numAddr := *numAddrFlag
outputDir := *outputDirFlag
v2NumJobs := 0
v3NumJobs := 0
if v2 {
v2NumJobs = numAddr / 5000
}
if v3 {
v3NumJobs = numAddr / 5000
}
// Create new wait group to enable concurrency.
wg := &sync.WaitGroup{}
for id := 0; id < v2NumJobs; id++ {
wg.Add(1)
go worker(wg, id, 2, 5000, outputDir)
}
for id := 0; id < v3NumJobs; id++ {
wg.Add(1)
go worker(wg, id, 3, 5000, outputDir)
}
wg.Wait()
}