Skip to content

Commit

Permalink
Merge pull request #143 from ganyao114/sandhook
Browse files Browse the repository at this point in the history
  • Loading branch information
solohsu authored Mar 9, 2019
2 parents 06577d5 + 96a9bc8 commit 61a0bc9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 58 deletions.
15 changes: 10 additions & 5 deletions dexmaker/src/main/java/external/com/android/dx/DexMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public ClassLoader loadClassDirect(ClassLoader parent, File dexCache, String dex
}

public ClassLoader generateAndLoad(ClassLoader parent, File dexCache) throws IOException {
return generateAndLoad(parent, dexCache, generateFileName());
return generateAndLoad(parent, dexCache, generateFileName(), false);
}

/**
Expand Down Expand Up @@ -512,7 +512,7 @@ public ClassLoader generateAndLoad(ClassLoader parent, File dexCache) throws IOE
* dex files will be written. If null, this class will try to guess the
* application's private data dir.
*/
public ClassLoader generateAndLoad(ClassLoader parent, File dexCache, String dexFileName) throws IOException {
public ClassLoader generateAndLoad(ClassLoader parent, File dexCache, String dexFileName, boolean deleteOld) throws IOException {
if (dexCache == null) {
String property = System.getProperty("dexmaker.dexcache");
if (property != null) {
Expand All @@ -529,9 +529,14 @@ public ClassLoader generateAndLoad(ClassLoader parent, File dexCache, String dex
File result = new File(dexCache, dexFileName);

if (result.exists()) {
try {
deleteOldDex(result);
} catch (Throwable throwable) {}
if (deleteOld) {
try {
deleteOldDex(result);
} catch (Throwable throwable) {
}
} else {
return generateClassLoader(result, dexCache, parent);
}
}

byte[] dex = generate();
Expand Down
1 change: 0 additions & 1 deletion sandhook/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.jakewharton.android.repackaged:dalvik-dx:9.0.0_r3'
implementation 'com.swift.sandhook:hooklib:3.0.1'
compileOnly project(':dexmaker')
compileOnly project(':xposedapi')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private HookWrapper.HookEntity doMake(String className, String dexName) throws E
throw new IllegalArgumentException("dexDirPath should not be empty!!!");
}
// Create the dex file and load it.
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName);
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName, true);
return loadHookerClass(loader, className);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private HookWrapper.HookEntity doMake(String className, String dexName) throws E
throw new IllegalArgumentException("dexDirPath should not be empty!!!");
}
// Create the dex file and load it.
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName);
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName, true);
return loadHookerClass(loader, className);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package com.swift.sandhook.xposedcompat.utils;
import external.com.android.dx.Code;
import external.com.android.dx.Local;
import external.com.android.dx.TypeId;
import com.android.dx.rop.code.Insn;
import com.android.dx.rop.code.PlainInsn;
import com.android.dx.rop.code.RegisterSpec;
import com.android.dx.rop.code.RegisterSpecList;
import com.android.dx.rop.code.Rops;
import com.android.dx.rop.code.SourcePosition;
import com.android.dx.rop.type.Type;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import external.com.android.dx.Code;
import external.com.android.dx.Local;
import external.com.android.dx.TypeId;

public class DexMakerUtils {


Expand Down Expand Up @@ -219,44 +211,6 @@ public static void returnRightValue(Code code, Class<?> returnType, Map<Class, L
code.returnValue(resultLocals.get(returnType));
}

public static void moveException(Code code, Local<?> result) {
addInstruction(code, new PlainInsn(Rops.opMoveException(Type.THROWABLE),
SourcePosition.NO_INFO, spec(result), RegisterSpecList.EMPTY));
}

public static void addInstruction(Code code, Insn insn) {
if (addInstMethod == null) {
try {
addInstMethod = Code.class.getDeclaredMethod("addInstruction", Insn.class);
addInstMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
try {
addInstMethod.invoke(code, insn);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}

public static RegisterSpec spec(Local<?> result) {
if (specMethod == null) {
try {
specMethod = Local.class.getDeclaredMethod("spec");
specMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
try {
return (RegisterSpec) specMethod.invoke(result);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return null;
}
}

public static String MD5(String source) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
Expand Down

0 comments on commit 61a0bc9

Please sign in to comment.