summer506hai / auto_test

自动化测试:UI自动化测试(Android 、pc(Windows+h5)混合);技术调研;技术难点解决方案
0 stars 0 forks source link

Android hook方案调研 #4

Open summer506hai opened 1 year ago

summer506hai commented 1 year ago

frida hook

  1. Windows 安装 frida pip install frida pip install frida-tools

  2. 安卓安装 adb 连接后输入以下命令查看CPU型号 getprop ro.product.cpu.abi image

  3. https://github.com/frida/frida/releases 中下载对应型号且与电脑安装Frida版本一致的Frida-server版本 image

  4. 下载完成后将其解压出来,然后重命名为frida-server 然后通过adb将其上传到手机 adb push .\frida-server /data/local/tmp 然后再给其授予777权限 chmod 777 frida-server

  5. 在手机端启动(设备需root) ./frida-server

    image
  6. 然后在windows上执行 Frida-ps -U 如下图所示,则表示安装成功

    image
  7. 自己写一个apk

    • MainActivity中,每隔5秒打印 50 + 30 的值 image image
import time
import frida

device = frida.get_usb_device()
pid = device.spawn(["com.example.myapplication"])
device.resume(pid)
time.sleep(1)  # Without it Java.perform silently fails
session = device.attach(pid)
with open("s1.js") as f:
    script = session.create_script(f.read())
script.load()

# prevent the python script from terminating
input()

image

image

Java.perform(
    function () {
      // 获得Toast组件
        var Toast = Java.use('android.widget.Toast')
        var makeText = Toast.makeText
        var String = Java.use('java.lang.String')
        makeText.overload('android.content.Context', 'java.lang.CharSequence', 'int').implementation=function (context,content,time) {
            var hookContent = String.$new('hook hook')
            return this.makeText(context,hookContent,time)
        }
    }
)
import time
import frida

device = frida.get_usb_device()
pid = device.spawn(["com.example.myapplication"])
device.resume(pid)
time.sleep(1)  # Without it Java.perform silently fails
session = device.attach(pid)
with open("s3.js") as f:
    script = session.create_script(f.read())
script.load()

# prevent the python script from terminating
input()
summer506hai commented 1 year ago

virtual xposed

import android.util.Log;
import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        Log.i("hnzhu2","MainActivity");
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, toastMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    public String toastMessage() {
        return "我未被劫持";
    }
}

运行该apk安装到设备上

image

image image image image image

summer506hai commented 1 year ago

whale 框架

项目地址: https://github.com/asLody/whale

  1. 把whale项目里的java文件夹的代码复制到自己的项目中
  2. 复制 built/Android 下所需的abi到自己项目的src/main/jniLibs下

image

  1. 新建一个类Test,写一个测试的方法get,用于hook测试

image

  1. 新建一个hook的类,用于写hook的方法
image
  1. 将hook在程序启动的时候执行,在MainActivity的onCreate方法里执行,只能在app内进行hook,无法hook第三方应用

image

hook第三方应用

https://github.com/asLody/whale/pull/36