-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJsUnpacker.java
131 lines (115 loc) · 3.25 KB
/
JsUnpacker.java
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
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JsUnpacker {
private String packedJS = null;
/**
* @param packedJS javascript P.A.C.K.E.R. coded.
*
*/
public JsUnpacker(String packedJS) {
this.packedJS = packedJS;
}
/**
* Detects whether the javascript is P.A.C.K.E.R. coded.
*
* @return true if it's P.A.C.K.E.R. coded.
*
*/
public boolean detect() {
String js = packedJS.replace(" ", "");
Pattern p = Pattern.compile("eval\\(function\\(p,a,c,k,e,(?:r|d)");
Matcher m = p.matcher(js);
return m.find();
}
/**
* Unpack the javascript
*
* @return the javascript unpacked or null.
*
*/
public String unpack() {
String js = new String(packedJS);
try {
Pattern p = Pattern.compile("\\}\\s*\\('(.*)',\\s*(.*?),\\s*(\\d+),\\s*'(.*?)'\\.split\\('\\|'\\)", Pattern.DOTALL);
Matcher m = p.matcher(js);
if(m.find() && m.groupCount() == 4) {
String payload = m.group(1).replace("\\'", "'");
String radixStr = m.group(2);
String countStr = m.group(3);
String[] symtab = m.group(4).split("\\|");
int radix = 36;
int count = 0;
try {
radix = Integer.parseInt(radixStr);
} catch(Exception e) {
}
try {
count = Integer.parseInt(countStr);
} catch(Exception e) {
}
if(symtab.length != count) {
throw new Exception ("Unknown p.a.c.k.e.r. encoding");
}
Unbase unbase = new Unbase(radix);
p = Pattern.compile("\\b\\w+\\b");
m = p.matcher(payload);
StringBuilder decoded = new StringBuilder(payload);
int replaceOffset = 0;
while(m.find()) {
String word = m.group(0);
int x = unbase.unbase(word);
String value = null;
if(x < symtab.length) {
value = symtab[x];
}
if(value != null && value.length() > 0) {
decoded.replace(m.start() + replaceOffset, m.end() + replaceOffset, value);
replaceOffset += (value.length() - word.length());
}
}
return decoded.toString();
}
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
private class Unbase {
private final String ALPHABET_62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private final String ALPHABET_95 = " !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
private String alphabet = null;
private HashMap<String, Integer> dictionnary = null;
private int radix;
Unbase(int radix) {
this.radix = radix;
if (radix > 36) {
if (radix < 62) {
alphabet = ALPHABET_62.substring(0, radix);
} else if (radix > 62 && radix < 95) {
alphabet = ALPHABET_95.substring(0, radix);
} else if (radix == 62) {
alphabet = ALPHABET_62;
} else if (radix == 95) {
alphabet = ALPHABET_95;
}
dictionnary = new HashMap<>(95);
for (int i = 0; i < alphabet.length(); i++) {
dictionnary.put(alphabet.substring(i, i + 1), i);
}
}
}
int unbase(String str) {
int ret = 0;
if (alphabet == null) {
ret = Integer.parseInt(str, radix);
} else {
String tmp = new StringBuilder(str).reverse().toString();
for (int i = 0; i < tmp.length(); i++) {
ret += Math.pow(radix, i) * dictionnary.get(tmp.substring(i, i + 1));
}
}
return ret;
}
}
}