Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
refactor: remove ZipEntryMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mkx173 committed Apr 11, 2024
1 parent ce08f27 commit 8e16428
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.zip.ZipFile;

import brut.androlib.AndrolibException;
import brut.androlib.res.data.value.ResFileValue;
import brut.androlib.res.data.value.ResStringValue;
import brut.androlib.res.decoder.ARSCDecoder;
import gm.tieba.tabswitch.XposedContext;
Expand All @@ -37,34 +36,6 @@ public void setMatchers(final List<Matcher> matchers) {
this.matchers.addAll(matchers);
}

public void unzip(final PublishSubject<Float> progress, final Context context)
throws IOException {
packageResource = context.getPackageResourcePath();

final var sizeToZipEntryMatcher = new HashMap<Long, ZipEntryMatcher>();
for (final var matcher : matchers) {
if (matcher instanceof final ZipEntryMatcher zipEntryMatcher) {
sizeToZipEntryMatcher.put(zipEntryMatcher.getSize(), zipEntryMatcher);
}
}

final var zipFile = new ZipFile(packageResource);
final var enumeration = zipFile.entries();
var entryCount = 0;
final var entrySize = zipFile.size();
while (enumeration.hasMoreElements()) {
entryCount++;
progress.onNext((float) entryCount / entrySize);

final var ze = enumeration.nextElement();
final var matcher = sizeToZipEntryMatcher.get(ze.getSize());
if (matcher != null) {
matcher.setEntryName(ze.getName());
}
}
zipFile.close();
}

private <T> void forEachProgressed(final PublishSubject<Float> progress,
final Collection<T> collection,
final Consumer<? super T> action) {
Expand All @@ -77,17 +48,15 @@ private <T> void forEachProgressed(final PublishSubject<Float> progress,
}
}

public void decodeArsc(final PublishSubject<Float> progress)
public void decodeArsc(final PublishSubject<Float> progress, final Context context)
throws IOException, AndrolibException {
packageResource = context.getPackageResourcePath();
progress.onNext(0F);
final var strToResMatcher = new HashMap<String, ResMatcher>();
final var entryNameToZipEntryMatcher = new HashMap<String, ZipEntryMatcher>();
final var resIdentifierToResMatcher = new HashMap<String, ResIdentifierMatcher>();
for (final var matcher : matchers) {
if (matcher instanceof ResMatcher) {
if (matcher instanceof final ZipEntryMatcher zipEntryMatcher) {
entryNameToZipEntryMatcher.put(zipEntryMatcher.getEntryName(), zipEntryMatcher);
} else if (matcher instanceof final ResIdentifierMatcher resIdentifierMatcher) {
if (matcher instanceof final ResIdentifierMatcher resIdentifierMatcher) {
resIdentifierToResMatcher.put(resIdentifierMatcher.toResIdentifier(), resIdentifierMatcher);
} else {
strToResMatcher.put(((StringResMatcher) matcher).toResIdentifier(), (ResMatcher) matcher);
Expand All @@ -112,12 +81,6 @@ public void decodeArsc(final PublishSubject<Float> progress)
if (matcher != null) {
matcher.setId(resResSpec.getId().id);
}
} else if (resValue instanceof ResFileValue) {
final var path = resValue.toString();
final var matcher = entryNameToZipEntryMatcher.get(path);
if (matcher != null) {
matcher.setId(resResSpec.getId().id);
}
}
} catch (final AndrolibException e) {
// should not happen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,12 @@ public void afterHookedMethod(final MethodHookParam param) throws Throwable {

new Thread(() -> {
try {
setMessage("(1/3) 解析安装包");
setMessage("(1/2) 解析安装包资源");
viewModel.deobfuscateStep1(mActivity, mMatchers);

setMessage("(2/3) 解析资源");
setMessage("(2/2) 搜索资源,字符串和方法调用");
viewModel.deobfuscateStep2();

setMessage("(3/3) 搜索字符串、资源 id 和方法调用");
viewModel.deobfuscateStep3();

XposedBridge.log("Deobfuscation complete, current version: "
+ DeobfuscationHelper.getTbVersion(mActivity));
hooks.forEach(Unhook::unhook);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ public class DeobfuscationViewModel {
public final Observable<Float> progress = _progress;
private final Deobfuscation deobfuscation = new Deobfuscation();

public void deobfuscateStep1(final Context context, final List<Matcher> matchers) throws IOException {
public void deobfuscateStep1(final Context context, final List<Matcher> matchers) throws IOException, AndrolibException {
deobfuscation.setMatchers(matchers);
deobfuscation.unzip(_progress, context);
deobfuscation.decodeArsc(_progress, context);
}

public void deobfuscateStep2() throws IOException, AndrolibException {
deobfuscation.decodeArsc(_progress);
}

public void deobfuscateStep3() throws IOException {
public void deobfuscateStep2() throws IOException {
deobfuscation.dexkit(_progress);
deobfuscation.saveDexSignatureHashCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ class StringResMatcher @JvmOverloads constructor(val str: String, properties: Ma
override fun toResIdentifier(): String = str
}

class ZipEntryMatcher @JvmOverloads constructor(val size: Long, properties: MatcherProperties? = null) : ResMatcher(properties = properties) {
var entryName: String = ""
override fun toString(): String = super.toString() + size.toString()
override fun toResIdentifier(): String = size.toString()
}

class ResIdentifierMatcher @JvmOverloads constructor(val name: String, val defType: String, properties: MatcherProperties? = null) : ResMatcher(properties = properties) {
override fun toString(): String = super.toString() + String.format("%s.%s", defType, name)
override fun toResIdentifier(): String = String.format("%s.%s", defType, name)
Expand Down

0 comments on commit 8e16428

Please sign in to comment.