|
| 1 | +package jadx.plugins.example; |
| 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 | +import java.util.List; |
| 10 | +import java.util.regex.Pattern; |
| 11 | + |
| 12 | +import jadx.api.data.CommentStyle; |
| 13 | +import jadx.api.plugins.pass.JadxPassInfo; |
| 14 | +import jadx.api.plugins.pass.impl.OrderedJadxPassInfo; |
| 15 | +import jadx.api.plugins.pass.types.JadxDecompilePass; |
| 16 | +import jadx.core.codegen.utils.CodeComment; |
| 17 | +import jadx.core.dex.attributes.AType; |
| 18 | +import jadx.core.dex.instructions.ConstStringNode; |
| 19 | +import jadx.core.dex.nodes.BlockNode; |
| 20 | +import jadx.core.dex.nodes.ClassNode; |
| 21 | +import jadx.core.dex.nodes.InsnNode; |
| 22 | +import jadx.core.dex.nodes.MethodNode; |
| 23 | +import jadx.core.dex.nodes.RootNode; |
| 24 | + |
| 25 | +public class B64DeobfuscatePass implements JadxDecompilePass { |
| 26 | + |
| 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 | + @Override |
| 34 | + public JadxPassInfo getInfo() { |
| 35 | + return new OrderedJadxPassInfo( |
| 36 | + "B64Deobfuscate", |
| 37 | + "Detect and decode likely Base64-encoded string constants") |
| 38 | + .after("SSATransform") |
| 39 | + .before("ConstInlineVisitor"); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void init(RootNode root) { |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean visit(ClassNode cls) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void visit(MethodNode mth) { |
| 53 | + if (mth.isNoCode()) { |
| 54 | + return; |
| 55 | + } |
| 56 | + List<BlockNode> blocks = mth.getBasicBlocks(); |
| 57 | + if (blocks == null) { |
| 58 | + return; |
| 59 | + } |
| 60 | + for (BlockNode block : blocks) { |
| 61 | + for (InsnNode insn : block.getInstructions()) { |
| 62 | + if (insn instanceof ConstStringNode) { |
| 63 | + processConstString((ConstStringNode) insn); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + 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); |
| 78 | + 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) + "..."; |
| 122 | + } |
| 123 | + return safe; |
| 124 | + } |
| 125 | +} |
0 commit comments