|
1 | 1 | package jadx.plugins.stringdecoder; |
2 | 2 |
|
3 | | -import java.nio.ByteBuffer; |
4 | | -import java.nio.charset.CharacterCodingException; |
5 | | -import java.nio.charset.CharsetDecoder; |
6 | | -import java.nio.charset.CodingErrorAction; |
7 | | -import java.nio.charset.StandardCharsets; |
8 | | -import java.util.Base64; |
9 | 3 | import java.util.List; |
10 | | -import java.util.regex.Pattern; |
11 | 4 |
|
12 | 5 | import jadx.api.data.CommentStyle; |
13 | 6 | import jadx.api.plugins.pass.JadxPassInfo; |
|
24 | 17 |
|
25 | 18 | public class B64DeobfuscatePass implements JadxDecompilePass { |
26 | 19 |
|
27 | | - private static final Pattern BASE64_STANDARD = Pattern.compile("^[A-Za-z0-9+/]+=*$"); |
28 | | - private static final Pattern BASE64_URL_SAFE = Pattern.compile("^[A-Za-z0-9_-]+=*$"); |
29 | | - private static final int MIN_LENGTH = 8; |
30 | | - private static final double MIN_PRINTABLE_RATIO = 0.75; |
31 | | - private static final int MAX_COMMENT_LENGTH = 100; |
32 | | - |
33 | 20 | @Override |
34 | 21 | public JadxPassInfo getInfo() { |
35 | 22 | return new OrderedJadxPassInfo( |
@@ -67,59 +54,9 @@ public void visit(MethodNode mth) { |
67 | 54 | } |
68 | 55 |
|
69 | 56 | private static void processConstString(ConstStringNode insn) { |
70 | | - String str = insn.getString(); |
71 | | - if (str.length() < MIN_LENGTH) { |
72 | | - return; |
73 | | - } |
74 | | - if (!BASE64_STANDARD.matcher(str).matches() && !BASE64_URL_SAFE.matcher(str).matches()) { |
75 | | - return; |
76 | | - } |
77 | | - String decoded = tryDecode(str); |
| 57 | + String decoded = B64Detector.detect(insn.getString()); |
78 | 58 | if (decoded != null) { |
79 | | - insn.addAttr(AType.CODE_COMMENTS, new CodeComment("b64: " + truncate(decoded), CommentStyle.LINE)); |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - private static String tryDecode(String str) { |
84 | | - String result = attemptDecode(Base64.getDecoder(), str); |
85 | | - if (result != null) { |
86 | | - return result; |
87 | | - } |
88 | | - return attemptDecode(Base64.getUrlDecoder(), str); |
89 | | - } |
90 | | - |
91 | | - private static String attemptDecode(Base64.Decoder decoder, String str) { |
92 | | - try { |
93 | | - byte[] bytes = decoder.decode(str); |
94 | | - CharsetDecoder utf8 = StandardCharsets.UTF_8.newDecoder() |
95 | | - .onMalformedInput(CodingErrorAction.REPORT) |
96 | | - .onUnmappableCharacter(CodingErrorAction.REPORT); |
97 | | - String decoded = utf8.decode(ByteBuffer.wrap(bytes)).toString(); |
98 | | - if (isPrintable(decoded)) { |
99 | | - return decoded; |
100 | | - } |
101 | | - } catch (CharacterCodingException ignored) { |
102 | | - // decoded bytes are not valid UTF-8 |
103 | | - } catch (Exception ignored) { |
104 | | - } |
105 | | - return null; |
106 | | - } |
107 | | - |
108 | | - private static boolean isPrintable(String str) { |
109 | | - if (str.isEmpty()) { |
110 | | - return false; |
111 | | - } |
112 | | - long printableCount = str.chars() |
113 | | - .filter(c -> c >= 32 && c <= 126) |
114 | | - .count(); |
115 | | - return (double) printableCount / str.length() >= MIN_PRINTABLE_RATIO; |
116 | | - } |
117 | | - |
118 | | - private static String truncate(String str) { |
119 | | - String safe = str.replace("\n", "\\n").replace("\r", "\\r"); |
120 | | - if (safe.length() > MAX_COMMENT_LENGTH) { |
121 | | - return safe.substring(0, MAX_COMMENT_LENGTH) + "..."; |
| 59 | + insn.addAttr(AType.CODE_COMMENTS, new CodeComment("b64: " + decoded, CommentStyle.LINE)); |
122 | 60 | } |
123 | | - return safe; |
124 | 61 | } |
125 | 62 | } |
0 commit comments