hacktricks/mobile-pentesting/android-app-pentesting/frida-tutorial/frida-tutorial-1.md

8.7 KiB
Raw Blame History

Fridaチュートリアル1

☁️ HackTricks Cloud ☁️ -🐦 Twitter 🐦 - 🎙️ Twitch 🎙️ - 🎥 Youtube 🎥

もしあなたがハッキングのキャリアに興味があり、解読不可能なものをハックしたい場合 - 採用中です!(流暢なポーランド語の読み書きが必要です)。

{% embed url="https://www.stmcyber.com/careers" %}

From: https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1
APK: https://github.com/t0thkr1s/frida-demo/releases
Source Code: https://github.com/t0thkr1s/frida-demo

Python

Fridaを使用すると、実行中のアプリケーションの関数にJavaScriptコードを挿入することができます。しかし、pythonを使用してフックを呼び出したりフック対話したりすることもできます。

この簡単なpythonスクリプトは、このチュートリアルで提案されているすべての例に使用できます

#hooking.py
import frida, sys

with open(sys.argv[1], 'r') as f:
jscode = f.read()
process = frida.get_usb_device().attach('infosecadventures.fridademo')
script = process.create_script(jscode)
print('[ * ] Running Frida Demo application')
script.load()
sys.stdin.read()

スクリプトを呼び出す:

python hooking.py <hookN.js>

PythonをFridaと一緒に使う方法を知っておくと便利ですが、この例ではコマンドラインのfridaツールを直接呼び出すこともできます。

frida -U --no-pause -l hookN.js -f infosecadventures.fridademo

フック1 - ブールバイパス

ここでは、クラス「infosecadventures.fridademo.utils.PinUtil」の「checkPin」というブールメソッドをフックする方法を示します。

//hook1.js
Java.perform(function() {
console.log("[ * ] Starting implementation override...")
var MainActivity = Java.use("infosecadventures.fridademo.utils.PinUtil");
MainActivity.checkPin.implementation = function(pin){
console.log("[ + ] PIN check successfully bypassed!")
return true;
}
});
python hooking.py hook1.js

見てみましょう:この関数は文字列をパラメータとして受け取りますが、オーバーロードは必要ありませんか?

フック2 - 関数のブルートフォース

非静的関数

クラスの非静的関数を呼び出す場合、まずそのクラスのインスタンスが必要です。その後、そのインスタンスを使用して関数を呼び出すことができます。
そのためには、既存のインスタンスを見つけて使用することができます:

Java.perform(function() {
console.log("[ * ] Starting PIN Brute-force, please wait...");
Java.choose("infosecadventures.fridademo.utils.PinUtil", {
onMatch: function(instance) {
console.log("[ * ] Instance found in memory: " + instance);
for(var i = 1000; i < 9999; i++){
if(instance.checkPin(i + "") == true){
console.log("[ + ] Found correct PIN: " + i);
break;
}
}
},
onComplete: function() { }
});
});

この場合、インスタンスが存在せず、関数は静的です。

静的関数

関数が静的な場合、単純に呼び出すことができます:

//hook2.js
Java.perform(function () {
console.log("[ * ] Starting PIN Brute-force, please wait...")
var PinUtil = Java.use("infosecadventures.fridademo.utils.PinUtil");

for(var i=1000; i < 9999; i++)
{
if(PinUtil.checkPin(i+"") == true){
console.log("[ + ] Found correct PIN: " + i);
}
}
});

フック3 - 引数と戻り値の取得

関数をフックして、渡された引数戻り値の値表示することができます。

//hook3.js
Java.perform(function() {
console.log("[ * ] Starting implementation override...")

var EncryptionUtil = Java.use("infosecadventures.fridademo.utils.EncryptionUtil");
EncryptionUtil.encrypt.implementation = function(key, value){
console.log("Key: " + key);
console.log("Value: " + value);
var encrypted_ret = this.encrypt(key, value); //Call the original function
console.log("Encrypted value: " + encrypted_ret);
return encrypted_ret;
}
});

重要

このチュートリアルでは、メソッドの名前と_.implementation_を使用してメソッドをフックしました。しかし、もし同じ名前の複数のメソッドがある場合は、フックしたいメソッドを引数のタイプを指定して明示する必要があります

次のチュートリアルでそれを見ることができます。

もしハッキングのキャリアに興味があり、ハッキングできないものをハックしたい場合は、採用中です(流暢なポーランド語の読み書きが必要です)。

{% embed url="https://www.stmcyber.com/careers" %}

☁️ HackTricks Cloud ☁️ -🐦 Twitter 🐦 - 🎙️ Twitch 🎙️ - 🎥 Youtube 🎥