forked from cycraft-corp/Prometheus-Decryptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus_decrypt.go
298 lines (265 loc) · 7.44 KB
/
prometheus_decrypt.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
MIT License
Copyright (c) 2021 CyCraft Technology
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import(
"golang.org/x/crypto/salsa20"
"github.com/h2non/filetype"
"prometheus_decrypt/csharp_random"
"prometheus_decrypt/examine"
"prometheus_decrypt/winsup"
"fmt"
"io/ioutil"
"log"
"math"
"path/filepath"
"os"
"strings"
"sync"
)
type decOption struct {
inputFile string
outputFile string
startTick int
reversed bool
useCurTick bool
key string
threadCount int
format string
customSearch string
bytesFormat string
}
func genKey(seed int32) [32]byte {
var key [32]byte
cr := csharp_random.Random(seed)
for i:=0; i<32; i++ {
v := cr.Next(33, 127) % 255
if v == 34 || v == 92 {
i -= 1
} else {
key[i] = byte(v)
}
}
return key
}
func writeFile(data []byte, path string, seed int32, key string) error {
dir, file := filepath.Split(path)
writePath := fmt.Sprintf("%s%d_%s", dir, seed, file)
// create all dir
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
// write file
err = ioutil.WriteFile(writePath, data, 0644)
log.Printf("\rDecrypt file with seed %d, key: %s, path: %s\n", seed, key, writePath)
return err
}
func decRoutine(jobs chan int32, result chan int32, file []byte, output string, exam *examine.TypeExam, wg *sync.WaitGroup) {
defer wg.Done()
plain := make([]byte, len(file))
for{
if seed, ok := <-jobs; ok {
go ctrLogger.Printf("\r%d", seed)
key := genKey(seed)
salsa20.XORKeyStream(plain, file, []byte{1, 2, 3, 4, 5, 6, 7, 8}, &key)
if exam.Match(plain) {
err := writeFile(plain, output, seed, string(key[:]))
if err != nil {
log.Println(err)
}
result<-seed
}
} else {
break
}
}
}
func decWithoutKey(opt decOption, quitCh chan bool) int32 {
// catch local error
defer func(){
// do nothing just return
recover()
}()
log.Println("Start decrypt", opt.inputFile)
// local check
if opt.customSearch == "" && opt.bytesFormat == "" && !filetype.IsSupported(opt.format) {
log.Panic("Unsupported format. Please provide a custom search regular expression with -s.")
}
// build examine
exam := examine.Init(opt.format, opt.customSearch, opt.bytesFormat)
// Read input file
file, err := ioutil.ReadFile(opt.inputFile)
if err != nil {
log.Panic(err)
}
// start worker
var wg sync.WaitGroup
wg.Add(opt.threadCount)
jobs := make(chan int32, opt.threadCount)
result := make(chan int32, opt.threadCount)
for i:=0; i<opt.threadCount; i++ {
go decRoutine(jobs, result, file, opt.outputFile, exam, &wg)
}
// send job (seed)
go func(){
end := false
for i:=opt.startTick; !end; {
select {
case <-quitCh:
end = true
default:
jobs<-int32(i)
if opt.reversed {
i--
if i < 0 {
break
}
} else {
i++
if i > math.MaxInt32 {
break
}
}
}
}
close(jobs)
}()
// wait for job
var lastTick int32 = -1
go func(){
for true {
if tick, ok := <-result; ok {
lastTick = tick
} else {
break
}
}
}()
wg.Wait()
close(result)
return lastTick
}
func decWithKey(opt decOption){
// catch local error
defer func(){
// do nothing just return
recover()
}()
file, err := ioutil.ReadFile(opt.inputFile)
if err != nil {
log.Panic(err)
}
plain := make([]byte, len(file))
var key_b [32]byte
copy(key_b[:], []byte(opt.key)[:32])
salsa20.XORKeyStream(plain, file, []byte{1, 2, 3, 4, 5, 6, 7, 8}, &key_b)
err = ioutil.WriteFile(opt.outputFile, plain, 0644)
if err != nil {
log.Panic(err)
}
}
func prometheusDecrypt(opt decOption, quitCh chan bool){
// catch global error
defer func(){
// do nothing just return
recover()
}()
if opt.inputFile == "" || opt.outputFile == "" {
log.Panic("Please provide input file path and output file path")
}
// check file or dir
files := make([]struct{i string; o string}, 0)
if fstatI, err := os.Stat(opt.inputFile); err != nil {
log.Panic("Open input file failed:", err)
} else if fstatI.IsDir() {
if fstatO, err := os.Stat(opt.outputFile); err != nil{
log.Panic("Open output file failed:", err)
} else if !fstatO.IsDir() {
log.Panic("Input path and output path type should be the same.")
} else {
// generate all files in dir
err = filepath.Walk(opt.inputFile, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
path = filepath.Base(path)
files = append(files, struct{i string; o string}{
filepath.Join(opt.inputFile, path),
filepath.Join(opt.outputFile, path),
})
}
return nil
})
}
} else {
files = append(files, struct{i string; o string}{opt.inputFile, opt.outputFile})
}
if opt.key != "" { // decrypt file with the key
for _, file := range files {
tmpOpt := opt
opt.inputFile = file.i
opt.outputFile = file.o
decWithKey(tmpOpt)
}
} else { // guess key
// global check
if opt.threadCount <= 0 {
log.Panic("Please provide a positive integer.")
} else if len(opt.bytesFormat) % 2 == 1 {
log.Panic("Length of bytes format should be a multiple of 2.")
}
// set global startTick
if opt.startTick < 0 {
opt.startTick = - opt.startTick
}
if opt.startTick > math.MaxInt32 {
log.Panic("Tick count should between -2147483648 and 2147483648.")
}
if opt.useCurTick {
opt.startTick = winsup.GetTickCount()
}
// decrypt each files
var tick int = opt.startTick
for _, file := range files {
tmpOpt := opt
tmpOpt.inputFile = file.i
tmpOpt.outputFile = file.o
// file format
if tmpOpt.format == "" {
tmpOpt.format = filepath.Ext(strings.Split(file.i, ".PROM")[0])
if len(tmpOpt.format) != 0 {
tmpOpt.format = tmpOpt.format[1:]
}
}
// startTick
if tmpOpt.reversed {
if tick+1800000 < tmpOpt.startTick {
tmpOpt.startTick = tick+1800000
}
} else {
if tick-1800000 > tmpOpt.startTick {
tmpOpt.startTick = tick-1800000
}
}
lastTick := int(decWithoutKey(tmpOpt, quitCh))
if lastTick != -1 {
tick = lastTick
}
}
}
}