初试Frida

工具说明:

1
2
3
4
Frida
Android Studio
jadx-gui
一部测试机(已Root)

尝试Frida Hook一个简单的Android程序
Android Stdio新建一个项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本框"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮"
tools:ignore="MissingConstraints"/>


</androidx.constraintlayout.widget.ConstraintLayout>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.textView);
findViewById(R.id.button).setOnClickListener(this);
}


@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
int number1 = 1;
int number2 = 2;
tv.setText("value1 + value2 = " + addab(number1, number2));
}

public int addab(int value1, int value2){
return value1 + value2;
}
}

连接PC端运行会自动安装在测试机
本地也会输出一个apk文件

拖入jadx-gui逆向代码(因为我们直接用源代码,这一步可有可不有)

入口:com.example.hook_demo.MainActivity
双击进入代码


点击按钮呈现的效果

addab函数有value1、value2两个int值,返回value1、value2相加的值
按钮被触发之后onClick函数将1、2传入addab函数,addab函数返回相加值

hook效果:改变返回值,或者改变传入addab函数的值从而改变结果

第一种改变addab的返回值

1
2
3
4
5
6
7
8
Java.perform(function(){
console.log("Hook 开始!");
var MainActivity = Java.use("com.example.hook_demo.MainActivity");
MainActivity.addab.implementation = function(a, b){
console.log("--", "返回100");
return 100;
}
});

测试机启动Frida-sever
frida-ps -Ua查看进程
frida -U Hook_Demo -l hook.js


第二种改变a, b的值(刚学习Frida,写的不好的地方还请多多指教)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Java.perform(function(){
console.log("Hook 开始!");
var MainActivity = Java.use("com.example.hook_demo.MainActivity");
// 第一种改变addab的返回值
// MainActivity.addab.implementation = function(a, b){
// console.log("--", "返回100");
// return 100;
// }

// 第二种改变a, b的值
MainActivity.addab.overload("int", "int").implementation = function(value1, value2){
var result = this.value1 + this.value2;
// 改变前的值 1、2
console.log("改变前的值:", value1, value2);
value1 = 100, value2 = 200;
// 改后后的值 100、200
console.log("改后后的值:", value1, value2,);
return value1 + value2;
}
});



初试Frida
http://example.com/2023/11/06/初试Frida/
作者
John Doe
发布于
2023年11月6日
许可协议