Frida Tutorial

Frida Tutorial

Lernen Sie AWS-Hacking von Grund auf mit htARTE (HackTricks AWS Red Team Expert)!

Andere Möglichkeiten, HackTricks zu unterstützen:

Bug-Bounty-Tipp: Registrieren Sie sich bei Intigriti, einer Premium-Bug-Bounty-Plattform, die von Hackern für Hacker entwickelt wurde! Treten Sie uns noch heute unter https://go.intigriti.com/hacktricks bei und verdienen Sie Prämien von bis zu 100.000 $!

Installation

Installieren Sie die Frida-Tools:

pip install frida-tools
pip install frida

Herunterladen und installieren Sie den Frida-Server auf dem Android-Gerät (Laden Sie die neueste Version herunter). Einzeiler zum Neustarten von adb im Root-Modus, Verbindung herstellen, frida-server hochladen, Ausführungsberechtigungen erteilen und im Hintergrund ausführen:

adb root; adb connect localhost:6000; sleep 1; adb push frida-server /data/local/tmp/; adb shell "chmod 755 /data/local/tmp/frida-server"; adb shell "/data/local/tmp/frida-server &"

Überprüfen, ob es funktioniert:

frida-ps -U #List packages and processes
frida-ps -U | grep -i <part_of_the_package_name> #Get all the package name

Tutorials

Von: https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1 APK: https://github.com/t0thkr1s/frida-demo/releases Quellcode: https://github.com/t0thkr1s/frida-demo

Folgen Sie dem Link, um es zu lesen.

Von: https://11x256.github.io/Frida-hooking-android-part-2/ (Teile 2, 3 & 4) APKs und Quellcode: https://github.com/11x256/frida-android-examples

Folgen Sie dem Link, um es zu lesen.

Von: https://joshspicer.com/android-frida-1 APK: https://github.com/OWASP/owasp-mstg/blob/master/Crackmes/Android/Level_01/UnCrackable-Level1.apk

Folgen Sie dem Link, um es zu lesen.

Weitere großartige Frida-Skripte finden Sie hier: https://codeshare.frida.re/

Schnellbeispiele

Frida von der Befehlszeile aufrufen

frida-ps -U

#Basic frida hooking
frida -l disableRoot.js -f owasp.mstg.uncrackable1

#Hooking before starting the app
frida -U --no-pause -l disableRoot.js -f owasp.mstg.uncrackable1
#The --no-pause and -f options allow the app to be spawned automatically,
#frozen so that the instrumentation can occur, and the automatically
#continue execution with our modified code.

Grundlegendes Python-Skript

import frida

# Connect to the USB device
device = frida.get_usb_device()

# Attach to the target process
pid = device.spawn(["com.example.app"])
session = device.attach(pid)

# Load the JavaScript code
with open("script.js", "r") as file:
    script_code = file.read()

# Create the script
script = session.create_script(script_code)

# Load the script into the target process
script.load()

# Resume the target process
device.resume(pid)

# Keep the script running
input("Press Enter to stop...")

# Clean up
session.detach()
device.kill(pid)

Dieses Python-Skript stellt eine grundlegende Vorlage dar.

import frida

# Verbindung zum USB-Gerät herstellen
device = frida.get_usb_device()

# An die Zielprozess anhängen
pid = device.spawn(["com.example.app"])
session = device.attach(pid)

# JavaScript-Code laden
with open("script.js", "r") as file:
    script_code = file.read()

# Skript erstellen
script = session.create_script(script_code)

# Skript in den Zielprozess laden
script.load()

# Zielprozess fortsetzen
device.resume(pid)

# Skript am Laufen halten
input("Drücken Sie Enter, um anzuhalten...")

# Aufräumen
session.detach()
device.kill(pid)
import frida, sys

jscode = open(sys.argv[0]).read()
process = frida.get_usb_device().attach('infosecadventures.fridademo')
script = process.create_script(jscode)
print('[ * ] Running Frida Demo application')
script.load()
sys.stdin.read()

Hooking von Funktionen ohne Parameter

Hooken Sie die Funktion a() der Klasse sg.vantagepoint.a.c

Java.perform(function () {
;  rootcheck1.a.overload().implementation = function() {
rootcheck1.a.overload().implementation = function() {
send("sg.vantagepoint.a.c.a()Z   Root check 1 HIT!  su.exists()");
return false;
};
});

Frida Tutorial: Hooking exit() in Java

In this tutorial, we will learn how to hook the exit() function in Java using Frida. By hooking this function, we can intercept and modify the behavior of the application when it tries to exit.

Prerequisites

Before we begin, make sure you have the following:

  • A rooted Android device or an emulator

  • Frida installed on your machine

  • Basic knowledge of JavaScript and Java

Steps

  1. Start by creating a new JavaScript file, for example hook_exit.js, and open it in a text editor.

  2. Import the necessary Frida modules:

const { Java } = require('frida');
  1. Attach to the target process:

Java.perform(function () {
  // Your code here
});
  1. Find the class and method that contains the exit() function. You can use the Java.use() function to get a reference to the class:

const System = Java.use('java.lang.System');
  1. Hook the exit() method and modify its behavior:

System.exit.implementation = function (code) {
  console.log('Exit code:', code);
  // Modify the behavior here
};
  1. Save the JavaScript file.

  2. Launch the target application on your Android device or emulator.

  3. Open a terminal and navigate to the directory where the JavaScript file is located.

  4. Use the following command to start the Frida server:

frida-server -D
  1. Use the following command to attach Frida to the target process:

frida -U -l hook_exit.js <package_name>

Replace <package_name> with the package name of the target application.

  1. You should see the output from the console.log() statement when the exit() function is called.

  2. Modify the behavior of the exit() function as desired.

  3. Test the modified behavior by triggering the application to exit.

Conclusion

By hooking the exit() function in Java using Frida, we can intercept and modify the behavior of the application when it tries to exit. This technique can be useful for various purposes, such as bypassing certain checks or adding additional functionality to the application.

var sysexit = Java.use("java.lang.System");
sysexit.exit.overload("int").implementation = function(var_0) {
send("java.lang.System.exit(I)V  // We avoid exiting the application  :)");
};

Frida Tutorial: Hook MainActivity .onStart() & .onCreate()

In this tutorial, we will learn how to hook the .onStart() and .onCreate() methods of the MainActivity class in an Android app using Frida.

Prerequisites

Before we begin, make sure you have the following:

  • A rooted Android device or an emulator

  • Frida installed on your machine

  • Basic knowledge of JavaScript and Android app development

Step 1: Set Up the Environment

First, we need to set up our environment. Follow these steps:

  1. Connect your Android device to your machine or start the emulator.

  2. Install the target app on your device or emulator.

Step 2: Write the Frida Script

Next, we will write the Frida script to hook the .onStart() and .onCreate() methods. Open your favorite text editor and create a new file called hook.js. Copy and paste the following code into the file:

Java.perform(function() {
  var MainActivity = Java.use('com.example.app.MainActivity');

  MainActivity.onStart.implementation = function() {
    console.log('MainActivity.onStart() hooked');
    this.onStart();
  };

  MainActivity.onCreate.implementation = function() {
    console.log('MainActivity.onCreate() hooked');
    this.onCreate();
  };
});

Step 3: Run the Frida Script

Now, we will run the Frida script to hook the methods. Follow these steps:

  1. Open a terminal and navigate to the directory where the hook.js file is located.

  2. Run the following command to start the Frida server:

    frida-server
  3. In another terminal, run the following command to attach Frida to the target app:

    frida -U -l hook.js com.example.app

    Replace com.example.app with the package name of the target app.

Step 4: Test the Hook

Finally, we will test the hook by launching the target app. Follow these steps:

  1. Launch the target app on your device or emulator.

  2. Check the logs in the terminal where you ran the Frida script. You should see the messages MainActivity.onStart() hooked and MainActivity.onCreate() hooked.

Congratulations! You have successfully hooked the .onStart() and .onCreate() methods of the MainActivity class using Frida. You can now modify the behavior of these methods or perform any other actions you desire. Happy hacking!

var mainactivity = Java.use("sg.vantagepoint.uncrackable1.MainActivity");
mainactivity.onStart.overload().implementation = function() {
send("MainActivity.onStart() HIT!!!");
var ret = this.onStart.overload().call(this);
};
mainactivity.onCreate.overload("android.os.Bundle").implementation = function(var_0) {
send("MainActivity.onCreate() HIT!!!");
var ret = this.onCreate.overload("android.os.Bundle").call(this,var_0);
};

Frida Tutorial: Hook android .onCreate()

In diesem Tutorial lernen Sie, wie Sie die .onCreate()-Methode in einer Android-App mit Frida abfangen können. Die .onCreate()-Methode wird normalerweise verwendet, um eine Aktivität zu initialisieren, wenn sie erstellt wird.

Voraussetzungen

Um dieses Tutorial durchzuführen, benötigen Sie Folgendes:

  • Ein Android-Gerät oder einen Emulator

  • Frida-Server auf dem Gerät oder Emulator installiert

  • Frida-Client auf Ihrem Computer installiert

Schritt 1: Frida-Server starten

Starten Sie den Frida-Server auf Ihrem Android-Gerät oder Emulator. Stellen Sie sicher, dass der Server erfolgreich gestartet wurde, bevor Sie mit dem nächsten Schritt fortfahren.

Schritt 2: Frida-Skript erstellen

Erstellen Sie ein Frida-Skript mit dem Namen hook_oncreate.js und fügen Sie den folgenden Code ein:

Java.perform(function() {
    var Activity = Java.use('android.app.Activity');
    Activity.onCreate.implementation = function(savedInstanceState) {
        console.log('onCreate() wurde abgefangen');
        this.onCreate(savedInstanceState);
    };
});

Dieses Skript verwendet die Frida-API, um die .onCreate()-Methode der Activity-Klasse abzufangen. Wenn die Methode aufgerufen wird, wird eine Meldung in der Konsole ausgegeben und die ursprüngliche Methode wird weiterhin aufgerufen.

Schritt 3: Frida-Skript ausführen

Führen Sie das Frida-Skript mit dem folgenden Befehl aus:

frida -U -l hook_oncreate.js <package_name>

Ersetzen Sie <package_name> durch den Paketnamen der App, in der Sie die .onCreate()-Methode abfangen möchten.

Schritt 4: Ergebnisse überprüfen

Öffnen Sie die App auf Ihrem Android-Gerät oder Emulator. Wenn die .onCreate()-Methode aufgerufen wird, sehen Sie die Meldung "onCreate() wurde abgefangen" in der Konsole.

Herzlichen Glückwunsch! Sie haben erfolgreich die .onCreate()-Methode in einer Android-App mit Frida abgefangen. Sie können diese Technik verwenden, um den App-Initialisierungsprozess zu überwachen und möglicherweise Angriffspunkte zu identifizieren.

var activity = Java.use("android.app.Activity");
activity.onCreate.overload("android.os.Bundle").implementation = function(var_0) {
send("Activity HIT!!!");
var ret = this.onCreate.overload("android.os.Bundle").call(this,var_0);
};

Hooking von Funktionen mit Parametern und Abrufen des Werts

Hooking einer Entschlüsselungsfunktion. Drucken Sie die Eingabe, rufen Sie die ursprüngliche Funktion auf, entschlüsseln Sie die Eingabe und drucken Sie schließlich die Klardaten:

function getString(data){
var ret = "";
for (var i=0; i < data.length; i++){
ret += data[i].toString();
}
return ret
}
var aes_decrypt = Java.use("sg.vantagepoint.a.a");
aes_decrypt.a.overload("[B","[B").implementation = function(var_0,var_1) {
send("sg.vantagepoint.a.a.a([B[B)[B   doFinal(enc)  // AES/ECB/PKCS7Padding");
send("Key       : " + getString(var_0));
send("Encrypted : " + getString(var_1));
var ret = this.a.overload("[B","[B").call(this,var_0,var_1);
send("Decrypted : " + ret);

var flag = "";
for (var i=0; i < ret.length; i++){
flag += String.fromCharCode(ret[i]);
}
send("Decrypted flag: " + flag);
return ret; //[B
};

Hooking Funktionen und Aufrufen mit unserer Eingabe

Haken Sie eine Funktion, die einen String empfängt, und rufen Sie sie mit einem anderen String auf (von hier).

var string_class = Java.use("java.lang.String"); // get a JS wrapper for java's String class

my_class.fun.overload("java.lang.String").implementation = function(x){ //hooking the new function
var my_string = string_class.$new("My TeSt String#####"); //creating a new String by using `new` operator
console.log("Original arg: " +x );
var ret =  this.fun(my_string); // calling the original function with the new String, and putting its return value in ret variable
console.log("Return value: "+ret);
return ret;
};

Abrufen eines bereits erstellten Objekts einer Klasse

Wenn Sie ein Attribut eines erstellten Objekts extrahieren möchten, können Sie dies verwenden.

In diesem Beispiel sehen Sie, wie Sie das Objekt der Klasse my_activity abrufen und die Funktion .secret() aufrufen, die ein privates Attribut des Objekts ausgibt:

Java.choose("com.example.a11x256.frida_test.my_activity" , {
onMatch : function(instance){ //This function will be called for every instance found by frida
console.log("Found instance: "+instance);
console.log("Result of secret func: " + instance.secret());
},
onComplete:function(){}
});

Weitere Frida-Tutorials

Bug-Bounty-Tipp: Registriere dich bei Intigriti, einer erstklassigen Bug-Bounty-Plattform, die von Hackern für Hacker entwickelt wurde! Werde noch heute Mitglied unter https://go.intigriti.com/hacktricks und verdiene Prämien von bis zu $100.000!

Lerne AWS-Hacking von Null auf Held mit htARTE (HackTricks AWS Red Team Expert)!

Weitere Möglichkeiten, HackTricks zu unterstützen:

Last updated