-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathweek.go
424 lines (337 loc) · 10.7 KB
/
week.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package deobfuscator
import (
"crypto/md5"
"errors"
"fmt"
"github.com/dop251/goja"
"log"
"regexp"
"runtime/debug"
"strings"
"sync"
)
type rotateFunctionStruct struct {
script []byte
scriptReduced string
functionName string
function string
pusherName string
objectName string
windowName string
webhookRotateFunction string
operationFunctions map[string][2]string
nativeFunction string
runInVm func(string) (goja.Value, error)
}
var mathMethods = []string{
"E", "LN10", "LN2", "LOG10E", "LOG2E", "PI", "SQRT1_2", "SQRT2", "abs", "acos", "acosh", "asin", "asinh", "atan",
"atan2", "atanh", "cbrt", "ceil", "clz32", "cos", "cosh", "exp", "expm1", "floor", "fround", "hypot", "imul", "log",
"log10", "log1p", "log2", "max", "min", "pow", "random", "round", "sign", "sin", "sinh", "sqrt", "tan", "tanh", "trunc",
}
func contains(element string, list []string) bool {
for _, el := range list {
if el == element {
return true
}
}
return false
}
var isWord = regexp.MustCompile(`^([;|,:]*|\w{2,}|\d+)$`)
var rotateFunctionCallReg = regexp.MustCompile(`(\w{2,}|})\(\)\(`)
func ParseRotateFunction(script []byte, runInVm func(string) (goja.Value, error)) (string, string, error, []Error) {
index := rotateFunctionCallReg.FindIndex(script)
if index == nil {
return "", "", errors.New("rotate function not found"), nil
}
var start, end int
if script[index[0]] == '}' {
start, end = getFunctionBounds(script, index[0], true)
} else {
var name []byte
var i = index[0]
for script[i] != '(' {
name = append(name, script[i])
i++
}
index2 := regexp.MustCompile(`(function ` + string(name) + `\(|` + string(name) + `=)`).FindIndex(script)
start, end = getFunctionBounds(script, index2[0], false)
}
functionMinified := minifyAll.ReplaceAll(script[start:end], []byte{})
hash := fmt.Sprintf("%x", md5.Sum(functionMinified))
if value := GetSavedFunction(hash); value != "" {
return value, hash, nil, nil
}
pushIndex := deviceIdReg.FindSubmatch(script)
length := len(pushIndex)
pushName := string(pushIndex[length-2])
windowName := regexp.MustCompile(`(\w+)=window`).FindSubmatch(script)[1]
var randomSub = regexp.MustCompile(`return \['(\w{2,3})',`).FindSubmatch(script)[1]
rf := &rotateFunctionStruct{
script: script,
pusherName: pushName,
function: "function rf()" + string(script[start:end]),
windowName: string(windowName),
runInVm: runInVm,
objectName: string(regexp.MustCompile(`(\w+)\.` + string(randomSub)).FindSubmatch(script)[1]),
}
function, err, stack := rf.deobRotateFunction()
if err == nil {
SaveFunction(hash, function)
}
return function, hash, err, stack
}
var minifyAll = regexp.MustCompile(`(\.\w+(\.call|\.apply|)\((([!\s]*)(null|\w+|,|\((\w+(\((\w+|{}|\[])\))?|\{}|\[])\)|[\[\]]|[{}]|\s))+\))|\w{1,3}`)
func (rf *rotateFunctionStruct) deobRotateFunction() (string, error, []Error) {
rf.getDependencies(map[string]bool{})
rf.replaceStrings()
rf.addNativeFunctionsDependencies()
var err, stack = rf.completeDependencies()
return "var window;" + rf.function, err, stack
}
func (rf *rotateFunctionStruct) replaceStrings() {
var pushReg = regexp.MustCompile(rf.pusherName + `\.push\([\w\d]+\);`)
var allPush = pushReg.FindAllString(rf.function, -1)
var objectCallReg = regexp.MustCompile(rf.objectName + `\.\w+(\.call|\.apply|)\((([!\s]*)(null|\w+|\((\w+(\((\w+|{}|\[])\))?|\{}|\[])\)|[\[\]]|[{}]|,|\s))+\)`)
var objectCallReplaced []string
allCalls := objectCallReg.FindAllString(rf.function, -1)
objectCallReplaced = make([]string, len(allCalls))
lock := sync.Mutex{}
wg := sync.WaitGroup{}
wg.Add(len(allPush) + 2)
for _, push := range allPush {
go func(push string) {
defer func() {
if r := recover(); r != nil {
log.Print(r)
debug.PrintStack()
}
}()
vm := goja.New()
_, _ = vm.RunString(tryPrefix + string(rf.script) + trySuffix)
listName := pushValueRegex.FindSubmatch(rf.script)[1]
_, _ = vm.RunString("var " + string(listName) + "=[];" + push)
for i, el := range allCalls {
if objectCallReplaced[i] != "" {
continue
}
value, err := vm.RunString(el)
if err != nil {
panic(err)
}
var word = value.String()
if isWord.MatchString(word) {
if i > 0 && objectCallReplaced[i-1] == "Math" {
if !contains(word, mathMethods) {
continue
}
}
objectCallReplaced[i] = word
unescaped := `"` + stringUnescape.ReplaceAllStringFunc(word, func(s string) string {
if strings.Count(s, `\`)%2 == 0 {
s = `\` + s
}
if s[len(s)-1] == '\\' {
s += `\`
}
return s
}) + `"`
lock.Lock()
rf.function = strings.Replace(rf.function, el, unescaped, -1)
lock.Unlock()
}
}
wg.Done()
}(push)
}
go func() {
defer func() {
if r := recover(); r != nil {
log.Print(r)
debug.PrintStack()
}
}()
lock.Lock()
defer lock.Unlock()
rf.function = pushReg.ReplaceAllString(rf.function, "")
wg.Done()
}()
go func() {
defer func() {
if r := recover(); r != nil {
log.Print(r)
debug.PrintStack()
}
}()
lock.Lock()
defer lock.Unlock()
rf.function = regexp.MustCompile(rf.pusherName+`\.pop\(\)[,;]\s?`).ReplaceAllString(rf.function, "")
wg.Done()
}()
wg.Wait()
}
var functionCallReg = regexp.MustCompile(`(function |\.|)(\w+)\(`)
func (rf *rotateFunctionStruct) getDependencies(functionSet map[string]bool) map[string]bool {
var allFunctions = functionCallReg.FindAllStringSubmatch(rf.function, -1)
var script = []byte(rf.script)
for _, f := range allFunctions {
if f[1] != "" || f[2] == "if" || f[2] == "for" || f[2] == "while" {
continue
}
var name = f[2]
if _, ok := functionSet[name]; ok {
continue
}
functionSet[name] = true
}
nbFunctions := len(functionSet)
var functions = make([]string, nbFunctions)
var i = 0
var lock = &sync.Mutex{}
var group = &sync.WaitGroup{}
group.Add(nbFunctions)
for name, value := range functionSet {
if !value {
group.Done()
continue
}
go func(name string) {
defer func() {
if r := recover(); r != nil {
log.Print(r)
debug.PrintStack()
}
}()
defer group.Done()
var index int
var indexes []int
if indexes = regexp.MustCompile(`function ` + name + `\(`).FindIndex(rf.script); len(indexes) > 0 {
index = indexes[0] + 10 + len(name)
} else if indexes = regexp.MustCompile(name + `=function\(`).FindIndex(rf.script); len(indexes) > 0 {
index = indexes[0] + 10 + len(name)
}
var _, end = getFunctionBounds(script, index, false)
rf2 := &rotateFunctionStruct{
script: rf.script,
function: `function ` + name + `(` + string(script[index:end]),
}
tmp := map[string]bool{}
for k, _ := range functionSet {
tmp[k] = false
}
rf2.getDependencies(tmp)
lock.Lock()
functions[i] = rf2.function
i++
lock.Unlock()
}(name)
}
group.Wait()
for _, function := range functions {
rf.function = function + rf.function
}
return functionSet
}
func (rf *rotateFunctionStruct) addNativeFunctionsDependencies() {
rf.nativeFunction = fmt.Sprintf(window, "")
if strings.Contains(rf.function, "btoa") || strings.Contains(rf.function, "atob") {
rf.nativeFunction += base64
}
}
func getFunctionBounds(buffer []byte, index int, backward bool) (start, end int) {
var unclosedParentheses uint8 = 0
for unclosedParentheses == 0 {
if buffer[index] == '}' && backward ||
buffer[index] == '{' && !backward {
unclosedParentheses++
}
if backward {
index--
} else {
index++
}
}
if backward {
end = index + 2
} else {
start = index - 1
}
for unclosedParentheses > 0 {
switch buffer[index] {
case '{':
if backward {
unclosedParentheses--
} else {
unclosedParentheses++
}
break
case '}':
if backward {
unclosedParentheses++
} else {
unclosedParentheses--
}
break
}
if backward {
index--
} else {
index++
}
}
if backward {
start = index + 1
} else {
end = index
}
return
}
var errRefErrName = regexp.MustCompile(`ReferenceError: (\w+) is not defined`)
type Error struct {
Err string
Solved bool
}
func testFunction(rf RotateFunction) error {
_, err := rf.GetResult(
4444,
"181:;:0:;:0:;:0,;,278:;:1440,;,139:;:12147,;,284:;:2256,;,257:;:8105,;,186:;:2560,;,246:;:0,;,119:;:3614607,;,132:;:412159,;,138:;:1096,;,179:;:,;,129:;:1440,;,183:;:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36,;,224:;:837561806501.5,;,231:;:20030107:;:en-US:;:Gecko:;:5,;,265:;:2256,;,212:;:,cpen:0,i1:0,dm:0,cwen:0,non:1,opc:0,fc:0,sc:0,wrc:1,isc:0,vib:1,bat:1,x11:0,x12:1,;,171:;:0.917187746458,;,198:;:2560",
"0,1,2875,2054,680;1,1,2881,2055,678;2,1,2888,2057,676;3,1,2895,2059,673;4,1,2901,2063,669;5,1,2907,2066,665;6,1,2914,2070,661;7,1,2920,2072,657;8,1,2927,2074,654;9,1,2933,2075,651;10,1,2939,2074,649;11,1,2945,2073,648;12,1,2951,2070,648;13,1,2957,2066,649;14,1,2964,2062,651;15,1,2971,2057,653;16,1,2978,2053,656;17,1,2985,2048,659;18,1,2992,2044,662;19,1,2999,2041,665;20,1,3006,2038,667;21,1,3012,2036,669;22,1,3018,2034,671;23,1,3024,2033,672;24,1,3031,2032,673;25,1,3037,2031,672;26,1,3043,2031,672;27,1,3050,2032,670;28,1,3057,2033,668;29,1,3064,2035,666;30,1,3070,2037,663;31,1,3077,2039,660;32,1,3084,2042,656;",
4444,
4444)
return err
}
func (rf *rotateFunctionStruct) completeDependencies() (error, []Error) {
rf.function = CleanFinalScript(rf.function, rf.windowName, false)
rfTest := RotateFunction{}
var vm = goja.New()
rfTest.vm = vm
_, _ = vm.RunString(rf.nativeFunction + rf.function)
err := testFunction(rfTest)
var finalValue goja.Value
var errorStack []Error
for err != nil {
var currentError = Error{
Err: err.(*goja.Exception).Value().String(),
}
if strings.Contains(currentError.Err, "ReferenceError") {
undefinedName := errRefErrName.FindStringSubmatch(currentError.Err)[1]
finalValue, err = rf.runInVm(`JSON.stringify(` + undefinedName + `)`)
if err != nil {
return errors.New("error when accessing to a variable : " + err.Error()), errorStack
}
rf.function = "var " + undefinedName + "=" + finalValue.String() + ";" + rf.function
rf.webhookRotateFunction = "var " + undefinedName + "=" + finalValue.String() + ";" + rf.webhookRotateFunction
_, _ = vm.RunString(rf.nativeFunction + rf.function)
} else {
return errors.New("non reference error found, error"), errorStack
}
err = testFunction(rfTest)
if err == nil || err.Error() != currentError.Err {
currentError.Solved = true
errorStack = append(errorStack, currentError)
continue
}
errorStack = append(errorStack, currentError)
return errors.New("loop on same error : " + fmt.Sprint(currentError.Err)), errorStack
}
return nil, errorStack
}