-
Notifications
You must be signed in to change notification settings - Fork 0
/
brute.go
225 lines (210 loc) · 5.87 KB
/
brute.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"sync"
"time"
)
func main() {
// The file is in between "items/wshield.cel" and "levels/l1data/hero1.dun".
// Since we know it is a DUN file, and since we know it uses the Cathedral
// tileset, it is reasonable to assume that the DUN file is located in the
// "levels\l1data\" subdirectory. Furthermore, the file name will start with a
// character up until 'h', as it should come alphabetically before
// "hero1.dun".
const charsetFirstChar = "ABCDEFGH0123456789-_"
n := runtime.NumCPU()
fmt.Println("num CPU:", n)
m := len(charsetFirstChar) / n
wg := &sync.WaitGroup{}
startTime := time.Now()
for i := 0; i < n; i++ {
start := i * m
end := (i + 1) * m
if end > len(charsetFirstChar) {
end = len(charsetFirstChar)
}
part := charsetFirstChar[start:end]
wg.Add(1)
go brute(part, wg)
}
wg.Wait()
timeTaken := time.Since(startTime)
fmt.Println("timeTaken:", timeTaken)
}
func brute(charsetFirstChar string, wg *sync.WaitGroup) {
const (
charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
)
first := true
const n = 8
fmt.Println("n:", n)
const ext = ".DUN"
dunName := make([]byte, n+len(ext))
for i := 0; i < len(ext); i++ {
j := len(dunName) - len(ext) + i
dunName[j] = ext[i]
}
// Generate precomputed hash of prefix.
prefix := []byte(`LEVELS\L1DATA\`)
preA := hashPrefix(prefix, hashPathA)
preB := hashPrefix(prefix, hashPathB)
for _, a := range charsetFirstChar {
dunName[0] = byte(a)
for _, b := range charset {
dunName[1] = byte(b)
fmt.Printf("b: '%c%c'\n", a, b)
for _, c := range charset {
dunName[2] = byte(c)
for _, d := range charset {
dunName[3] = byte(d)
for _, e := range charset {
dunName[4] = byte(e)
for _, f := range charset {
dunName[5] = byte(f)
for _, g := range charset {
dunName[6] = byte(g)
for _, h := range charset {
dunName[7] = byte(h)
if first {
fmt.Println("dunName (first):", string(dunName))
first = false
}
// copy precomputed hash; pass-by-value.
if check(dunName, preA, preB) {
fmt.Println("FOUND:", string(dunName))
data := []byte(dunName)
const outputPath = "found_hash.txt"
fmt.Println("creating %q", outputPath)
if err := ioutil.WriteFile(outputPath, data, 0644); err != nil {
log.Printf("unable to create file %q", outputPath)
}
os.Exit(1)
}
}
}
}
}
}
}
}
}
wg.Done()
}
func check(relPath []byte, preA, preB prehash) bool {
// hash A 0xB29FC135 and hash B 0x22575C4A
const (
wantHashA = 0xB29FC135
wantHashB = 0x22575C4A
)
hashA := preA.genHash(relPath, hashPathA)
//fmt.Printf("A: 0x%08X\n", hashA)
//fmt.Printf("B: 0x%08X\n", hashB)
foundA := hashA == wantHashA
if foundA {
fmt.Println("relPath (found A):", string(relPath))
hashB := preB.genHash(relPath, hashPathB)
foundB := hashB == wantHashB
if foundB {
fmt.Println("relPath (found A and B):", string(relPath))
return true
}
}
return false
}
// --- [ decrypt ] -------------------------------------------------------------
var (
// Lookup table used during decryption.
cryptTable [0x500]uint32
)
func init() {
// Initialize crypt table.
initCryptTable()
}
// initCryptTable initializes the lookup table used during decryption.
func initCryptTable() {
//start := time.Now()
seed := uint32(0x00100001)
for index1 := 0; index1 < 0x100; index1++ {
index2 := index1
for i := 0; i < 5; i++ {
seed = (seed*125 + 3) % 0x2AAAAB
temp1 := (seed & 0xFFFF) << 0x10
seed = (seed*125 + 3) % 0x2AAAAB
temp2 := (seed & 0xFFFF)
cryptTable[index2] = (temp1 | temp2)
index2 += 0x100
}
}
// Takes on average 60.00 µs.
//dbg.Println("init of crypt tables took:", time.Since(start))
}
// hashType represents the set of hash types.
type hashType uint32
// Hash types.
const (
// Hash of relative file path, which specifies an index hash the hash table,
// from where to start searching for the hash entry associated with the given
// file.
hashTableIndex hashType = 0x000
// Hash of relative file path, using method A.
hashPathA hashType = 0x100
// Hash of relative file path, using method B.
hashPathB hashType = 0x200
// Hash of the file name, which specifies the encryption key of the file.
hashFileKey hashType = 0x300
)
// precomputed hash for prefix of a string.
type prehash struct {
seed1 uint32
seed2 uint32
}
// hashPrefix returns a precomputed hash based on the given prefix.
func hashPrefix(prefix []byte, hashType hashType) prehash {
seed1 := uint32(0x7FED7FED)
seed2 := uint32(0xEEEEEEEE)
for i := 0; i < len(prefix); i++ {
v := uint32(prefix[i])
seed1 = cryptTable[uint32(hashType)+v] ^ (seed1 + seed2)
seed2 = v + seed1 + seed2 + (seed2 << 5) + 3
}
return prehash{
seed1: seed1,
seed2: seed2,
}
}
// genHash returns the hash of the given string, based on the specified hash
// type and the precomputed hash of the string prefix.
func (pre prehash) genHash(s []byte, hashType hashType) uint32 {
seed1 := pre.seed1
seed2 := pre.seed2
for i := 0; i < len(s); i++ {
v := uint32(s[i])
seed1 = cryptTable[uint32(hashType)+v] ^ (seed1 + seed2)
seed2 = v + seed1 + seed2 + (seed2 << 5) + 3
}
return seed1
}
// genHash returns the hash of the given string, based on the specified hash
// type.
func genHash(s []byte, hashType hashType) uint32 {
seed1 := uint32(0x7FED7FED)
seed2 := uint32(0xEEEEEEEE)
for i := 0; i < len(s); i++ {
v := uint32(s[i])
seed1 = cryptTable[uint32(hashType)+v] ^ (seed1 + seed2)
seed2 = v + seed1 + seed2 + (seed2 << 5) + 3
}
return seed1
}
// hashAB contains both the A and B hashes of a given file, to be used as key
// for file listings.
type hashAB struct {
// Hash of relative file path; using method A.
hashA uint32
// Hash of relative file path; using method B.
hashB uint32
}