Skip to content

Commit

Permalink
升级版本,完善插件
Browse files Browse the repository at this point in the history
  • Loading branch information
陈少平 committed Jun 10, 2021
1 parent 5d53013 commit 453393e
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 34 deletions.
7 changes: 0 additions & 7 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ void createHookThreadByteCode(MethodVisitor mv, String className) {
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/quwan/tt/asmdemoapp/ThreadCheck", "printThread", "(Ljava/lang/String;)V", false)
}

void createHookServiceByteCode(MethodVisitor mv) {
mv.visitTypeInsn(Opcodes.NEW, "com/quwan/tt/asmdemoapp/ServiceHook")
mv.visitInsn(Opcodes.DUP)
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/quwan/tt/asmdemoapp/ServiceHook", "<init>", "()V", false)
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/quwan/tt/asmdemoapp/ServiceHook", "printStartServiceStackTrace", "()V", false)
}

/**
* 返回需要被hook的方法
*/
Expand All @@ -30,11 +37,21 @@ List<HookMethod> getHookMethods() {
hookMethodList.add(new HookMethod("android/os/HandlerThread", "<init>", "(Ljava/lang/String;)V", { MethodVisitor mv -> createHookThreadByteCode(mv, "android/os/HandlerThread") }))
hookMethodList.add(new HookMethod("com/quwan/tt/asmdemoapp/MainActivity", "onCreate", "(Landroid/os/Bundle;)V", { MethodVisitor mv ->
println("====>com/quwan/tt/asmdemoapp/MainActivity")
methodVisitor.visitLdcInsn("====>MainActivity")
methodVisitor.visitLdcInsn("\u6d4b\u8bd5\u63d2\u5165\u65e5\u5fd7")
methodVisitor.visitMethodInsn(INVOKESTATIC, "android/util/Log", "i", "(Ljava/lang/String;Ljava/lang/String;)I", false)
methodVisitor.visitInsn(POP)
mv.visitLdcInsn("====>MainActivity123")
mv.visitLdcInsn("\u6d4b\u8bd5\u63d2\u5165\u65e5\u5fd7")
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "android/util/Log", "i", "(Ljava/lang/String;Ljava/lang/String;)I", false)
mv.visitInsn(Opcodes.POP)
}))

hookMethodList.add(new HookMethod("android/app/Service", "startForeground", "(ILandroid/app/Notification;)V", { MethodVisitor mv ->
println("====>startForeground")
createHookServiceByteCode(mv)
}))
hookMethodList.add(new HookMethod("android/app/Service", "startForeground", "(ILandroid/app/Notification;I)V", { MethodVisitor mv ->
println("====>startForeground")
createHookServiceByteCode(mv)
}))

return hookMethodList
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".TestService"
android:enabled="true"
android:exported="true"
/>

</application>

</manifest>
33 changes: 28 additions & 5 deletions app/src/main/java/com/quwan/tt/asmdemoapp/A.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
package com.quwan.tt.asmdemoapp;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class A {
@RequiresApi(api = Build.VERSION_CODES.Q)
void test(){
new ThreadCheck().printThread("sAS");
AsyncTask asyncTask = new AsyncTask() {

// Service service = null;
// service.startForeground(0, new Notification());

BaseService baseService = new BaseService() {
@Nullable
@Override
protected Object doInBackground(Object[] objects) {
public IBinder onBind(Intent intent) {
return null;
}
}.execute();
System.out.println("====>AsyncTask");
};

baseService.startForegroundIfNeed(0,new Notification(),true);

// new ThreadCheck().printThread("sAS");
// AsyncTask asyncTask = new AsyncTask() {
// @Override
// protected Object doInBackground(Object[] objects) {
// return null;
// }
// }.execute();
// System.out.println("====>AsyncTask");
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/quwan/tt/asmdemoapp/BaseService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.quwan.tt.asmdemoapp

import android.app.Notification
import android.app.Service
import android.os.Build

abstract class BaseService: Service() {

fun startForegroundIfNeed(id: Int, notification: Notification, alwaysStart: Boolean = false) {
if (alwaysStart || Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(id, notification)
}
}

}
30 changes: 17 additions & 13 deletions app/src/main/java/com/quwan/tt/asmdemoapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package com.quwan.tt.asmdemoapp

import android.os.AsyncTask
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.HandlerThread
import androidx.annotation.RequiresApi


class MainActivity : BaseActivity() {

@RequiresApi(Build.VERSION_CODES.Q)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// val imageView: ImageView =findViewById(R.id.iv)
// imageView.setImageDrawable(getDrawable(R.drawable.ic_launcher_background))
println("====>MainActivity")
Thread()
object : AsyncTask<Void?, Void?, Void?>() {
override fun doInBackground(vararg params: Void?): Void? {
return null
}

}.execute()
Thread()
Thread()
HandlerThread("test")
// Thread()
// object : AsyncTask<Void?, Void?, Void?>() {
// override fun doInBackground(vararg params: Void?): Void? {
// return null
// }
//
// }.execute()
// Thread()
// Thread()
// HandlerThread("test")
// A().test()
val startIntent = Intent(this, TestService::class.java)
startService(startIntent)
}


Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/quwan/tt/asmdemoapp/TestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.quwan.tt.asmdemoapp;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;

public class TestService extends Service {

@Override
public void onCreate() {
super.onCreate();
Log.i("====>TestService:", "onCreate");
startForeground(0, new Notification());
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id 'maven-publish'
}

version = "1.0.2"
version = "1.0.4"
group = "com.tt.plugin.blackHook"


Expand Down
Empty file added buildSrc/setting.gradle
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class AllClassVisitor extends ClassVisitor {
private String className;
private BlackHook blackHook;
private String superClassName;

public AllClassVisitor(ClassVisitor classVisitor, BlackHook blackHook) {
super(ASM6, classVisitor);
Expand All @@ -18,11 +19,12 @@ public AllClassVisitor(ClassVisitor classVisitor, BlackHook blackHook) {
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
className = name;
superClassName = superName;
}

@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
MethodVisitor mv = cv.visitMethod(access, name, descriptor, signature, exceptions);
return new AllMethodVisitor(blackHook, mv, access, name, descriptor, className);
return new AllMethodVisitor(blackHook, mv, access, name, descriptor, className, superClassName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ class AllMethodVisitor extends AdviceAdapter {
private final String methodName;
private final String className;
private BlackHook blackHook;
private String superClassName;

protected AllMethodVisitor(BlackHook blackHook, org.objectweb.asm.MethodVisitor methodVisitor, int access, String name, String descriptor, String className) {
protected AllMethodVisitor(BlackHook blackHook, org.objectweb.asm.MethodVisitor methodVisitor, int access, String name, String descriptor, String className, String superClassName) {
super(ASM5, methodVisitor, access, name, descriptor);
this.blackHook = blackHook;
this.methodName = name;
this.className = name;
this.className = className;
this.superClassName = superClassName;
System.out.println("====>className:"+className);
System.out.println("====>superClassName:"+superClassName);
}

@Override
Expand All @@ -28,7 +32,7 @@ public void visitMethodInsn(int opcode, String owner, String methodName, String
if (blackHook != null && blackHook.hookMethodList != null && blackHook.hookMethodList.size() > 0) {
for (int i = 0; i < blackHook.hookMethodList.size(); i++) {
HookMethod hookMethod = blackHook.hookMethodList.get(i);
if (owner.equals(hookMethod.className) && methodName.equals(hookMethod.methodName) && descriptor.equals(hookMethod.descriptor)) {
if ((owner.equals(hookMethod.className) || superClassName.equals(hookMethod.className)) && methodName.equals(hookMethod.methodName) && descriptor.equals(hookMethod.descriptor)) {
hookMethod.createBytecode.call(mv);
break;
}
Expand Down

0 comments on commit 453393e

Please sign in to comment.