Skip to content

Commit dab3cca

Browse files
author
RLib
committed
Move class HookHelper
1 parent 8300cf0 commit dab3cca

File tree

13 files changed

+432
-417
lines changed

13 files changed

+432
-417
lines changed

app/src/main/java/andhook/test/B.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package andhook.test;
22

3-
import andhook.lib.AndHook.HookHelper;
3+
import andhook.lib.HookHelper;
44

55
import android.util.Log;
66

app/src/main/java/andhook/test/Constructor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package andhook.test;
22

3-
import andhook.lib.AndHook.HookHelper;
4-
import andhook.lib.AndHook.HookHelper.Hook;
3+
import andhook.lib.HookHelper;
4+
import andhook.lib.HookHelper.Hook;
55

66
import android.util.Log;
77

app/src/main/java/andhook/test/Static.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.lang.reflect.Method;
44

55
import andhook.lib.AndHook;
6-
import andhook.lib.AndHook.HookHelper;
6+
import andhook.lib.HookHelper;
77

88
import android.util.Log;
99

@@ -53,9 +53,12 @@ private static void hookMethodFromDifferentClassUsingApi() {
5353

5454
final Method m1 = A.class.getDeclaredMethod("AA", String.class);
5555
final Method m2 = B.class.getDeclaredMethod("BB", Class.class, String.class);
56+
Log.i(AndTest.LOG_TAG, "modifiers of A:AA is 0x" + Integer.toHexString(m1.getModifiers()));
5657
Log.i(AndTest.LOG_TAG, "begin hook public static method A::AA...");
5758
HookHelper.hook(m1, m2);
5859
Log.i(AndTest.LOG_TAG, "end hook public static method A::AA");
60+
Log.i(AndTest.LOG_TAG, "modifiers of A:AA is 0x" +
61+
Integer.toHexString(A.class.getDeclaredMethod("AA", String.class).getModifiers()));
5962

6063
Log.i(AndTest.LOG_TAG, "calling public static method A::AA...");
6164
Log.i(AndTest.LOG_TAG,

app/src/main/java/andhook/test/SystemClass.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package andhook.test;
22

3-
import andhook.lib.AndHook.HookHelper;
4-
import andhook.lib.AndHook.HookHelper.Hook;
3+
import andhook.lib.HookHelper;
4+
import andhook.lib.HookHelper.Hook;
55

66
import android.content.ContentResolver;
77
import android.os.Bundle;

app/src/main/java/andhook/test/Virtual.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.lang.reflect.Method;
44

55
import andhook.lib.AndHook;
6-
import andhook.lib.AndHook.HookHelper;
6+
import andhook.lib.HookHelper;
77

88
import android.util.Log;
99

app/src/main/java/andhook/test/WideningConversion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package andhook.test;
22

3-
import andhook.lib.AndHook.HookHelper;
4-
import andhook.lib.AndHook.HookHelper.Hook;
3+
import andhook.lib.HookHelper;
4+
import andhook.lib.HookHelper.Hook;
55

66
import android.util.Log;
77

app/src/main/java/andhook/test/Xposed.java

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
import java.lang.reflect.Method;
44

5-
import andhook.lib.AndHook.HookHelper;
5+
import andhook.lib.HookHelper;
66
import andhook.lib.xposed.XC_MethodReplacement;
7-
import andhook.lib.xposed.XposedBridge;
87
import andhook.lib.xposed.XposedHelpers;
98
import andhook.lib.xposed.XC_MethodHook;
10-
import andhook.lib.xposed.XC_MethodHook.MethodHookParam;
119

12-
import android.annotation.SuppressLint;
1310
import android.annotation.TargetApi;
1411
import android.os.Build;
1512
import android.system.OsConstants;
@@ -107,57 +104,59 @@ protected Object replaceHookedMethod(MethodHookParam param)
107104
}
108105
}
109106

110-
@TargetApi(Build.VERSION_CODES.O)
111-
private static void hook_IoBridge_O() {
112-
final Class<?> IoBridge = HookHelper.findClass("libcore/io/IoBridge");
113-
final Method socket = HookHelper.findMethodHierarchically(IoBridge,
114-
"socket", int.class, int.class, int.class);
115-
116-
try {
117-
Log.i(AndTest.LOG_TAG, "\npre call socket...");
118-
if (socket.invoke(null, OsConstants.AF_INET, OsConstants.SOCK_STREAM, OsConstants.IPPROTO_TCP) == null) {
119-
Log.w(AndTest.LOG_TAG, "********socket returns null unexpectedly");
107+
private static final class O {
108+
@TargetApi(Build.VERSION_CODES.O)
109+
private static void hook_IoBridge() {
110+
final Class<?> IoBridge = HookHelper.findClass("libcore/io/IoBridge");
111+
final Method socket = HookHelper.findMethodHierarchically(IoBridge,
112+
"socket", int.class, int.class, int.class);
113+
114+
try {
115+
Log.i(AndTest.LOG_TAG, "\npre call socket...");
116+
if (socket.invoke(null, OsConstants.AF_INET, OsConstants.SOCK_STREAM, OsConstants.IPPROTO_TCP) == null) {
117+
Log.w(AndTest.LOG_TAG, "********socket returns null unexpectedly");
118+
}
119+
} catch (final Exception e) {
120+
Log.e(AndTest.LOG_TAG, "socket test error", e);
120121
}
121-
} catch (final Exception e) {
122-
Log.e(AndTest.LOG_TAG, "socket test error", e);
123-
}
124-
125-
final XC_MethodHook.Unhook uk = XposedHelpers.findAndHookMethod(IoBridge, "socket", int.class, int.class, int.class,
126-
new XC_MethodReplacement() {
127-
@Override
128-
protected Object replaceHookedMethod(MethodHookParam param)
129-
throws Throwable {
130-
Log.i(AndTest.LOG_TAG,
131-
"!!!!!!!!socket hit, returning null...");
132-
return null;
133-
}
134-
});
135122

136-
try {
137-
Log.i(AndTest.LOG_TAG, "post call socket...");
138-
if (socket.invoke(null, OsConstants.AF_INET, OsConstants.SOCK_STREAM, OsConstants.IPPROTO_TCP) != null) {
139-
Log.w(AndTest.LOG_TAG,
140-
"********socket returns non-null unexpectedly");
141-
} else {
142-
Log.i(AndTest.LOG_TAG, "test call socket passed 0.");
123+
final XC_MethodHook.Unhook uk = XposedHelpers.findAndHookMethod(IoBridge, "socket", int.class, int.class, int.class,
124+
new XC_MethodReplacement() {
125+
@Override
126+
protected Object replaceHookedMethod(MethodHookParam param)
127+
throws Throwable {
128+
Log.i(AndTest.LOG_TAG,
129+
"!!!!!!!!socket hit, returning null...");
130+
return null;
131+
}
132+
});
133+
134+
try {
135+
Log.i(AndTest.LOG_TAG, "post call socket...");
136+
if (socket.invoke(null, OsConstants.AF_INET, OsConstants.SOCK_STREAM, OsConstants.IPPROTO_TCP) != null) {
137+
Log.w(AndTest.LOG_TAG,
138+
"********socket returns non-null unexpectedly");
139+
} else {
140+
Log.i(AndTest.LOG_TAG, "test call socket passed 0.");
141+
}
142+
} catch (final Exception e) {
143+
Log.e(AndTest.LOG_TAG, "socket test error", e);
143144
}
144-
} catch (final Exception e) {
145-
Log.e(AndTest.LOG_TAG, "socket test error", e);
146-
}
147145

148-
// cancels hook
149-
uk.unhook();
150-
151-
try {
152-
Log.i(AndTest.LOG_TAG, "final call socket...");
153-
if (socket.invoke(null, OsConstants.AF_INET, OsConstants.SOCK_STREAM, OsConstants.IPPROTO_TCP) == null) {
154-
Log.w(AndTest.LOG_TAG,
155-
"********socket returns null unexpectedly");
156-
} else {
157-
Log.i(AndTest.LOG_TAG, "test call socket passed 1.");
146+
// cancels hook
147+
uk.unhook();
148+
149+
try {
150+
Log.i(AndTest.LOG_TAG, "final call socket...");
151+
if (socket.invoke(null, OsConstants.AF_INET, OsConstants.SOCK_STREAM, OsConstants.IPPROTO_TCP) == null) {
152+
Log.w(AndTest.LOG_TAG,
153+
"********socket returns null unexpectedly");
154+
} else {
155+
Log.i(AndTest.LOG_TAG, "test call socket passed 1.");
156+
}
157+
} catch (final Exception e) {
158+
Log.e(AndTest.LOG_TAG, "socket test error", e);
158159
}
159-
} catch (final Exception e) {
160-
Log.e(AndTest.LOG_TAG, "socket test error", e);
161160
}
162161
}
163162

@@ -169,7 +168,7 @@ public static void doHook() {
169168
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
170169
hook_IoBridge();
171170
} else {
172-
hook_IoBridge_O();
171+
O.hook_IoBridge(); // for Android O and later
173172
}
174173
} catch (final Exception e) {
175174
Log.e(AndTest.LOG_TAG, "xposed test error", e);

app/src/main/java/andhook/ui/MainActivity.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package andhook.ui;
22

3+
import andhook.test.A;
34
import andhook.test.AndTest;
45
import andhook.test.Constructor;
56
import andhook.test.Xposed;
@@ -9,9 +10,8 @@
910
import android.provider.Settings.Secure;
1011
import android.util.Log;
1112

12-
import andhook.ui.R;
13-
1413
public class MainActivity extends Activity {
14+
@SuppressWarnings("all")
1515
@Override
1616
protected void onCreate(Bundle savedInstanceState) {
1717
Log.i("AndHook_" + this.getClass().getName(), "MainActivity::onCreate start");
@@ -24,7 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
2424
@Override
2525
public void onClick(android.view.View v) {
2626
// in case of GC, JIT-Compile
27-
android.util.Log.i("AndHook_" + MainActivity.class.getName(), "begin...");
27+
Log.i("AndHook_" + MainActivity.class.getName(), "begin...");
2828
for (int i = 0; i < 3; ++i) {
2929
andhook.test.Static.a1(i + "");
3030
Secure.getString(MainActivity.this.getContentResolver(),
@@ -34,8 +34,14 @@ public void onClick(android.view.View v) {
3434
Log.w(AndTest.LOG_TAG,
3535
"********print returns wrong value unexpectedly");
3636
}
37+
try {
38+
Log.i(AndTest.LOG_TAG, "modifiers of A:AA is 0x" +
39+
Integer.toHexString(A.class.getDeclaredMethod("AA", String.class).getModifiers()));
40+
} catch (final Exception e) {
41+
Log.e(AndTest.LOG_TAG, e.getMessage(), e);
42+
}
3743
}
38-
android.util.Log.i("AndHook_" + MainActivity.class.getName(), "end.");
44+
Log.i("AndHook_" + MainActivity.class.getName(), "end.");
3945
}
4046
});
4147
}

app/src/main/java/andhook/ui/MainApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public final class MainApplication extends Application {
77
@Override
88
public void onCreate() {
9+
super.onCreate();
910
Log.i(MainApplication.class.toString(), "onCreate");
1011
andhook.test.AndTest.RunTest(this, this.getContentResolver());
1112
}

app/src/main/jni/Application.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
APP_ABI := armeabi-v7a x86
1+
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
22
APP_PLATFORM := android-27
33
APP_STL := stlport_static
44
APP_OPTIM := release

0 commit comments

Comments
 (0)