Translated ['mobile-pentesting/android-app-pentesting/frida-tutorial/REA

This commit is contained in:
Translator 2024-01-13 18:46:03 +00:00
parent 88c6164c29
commit 922aed6f33

View file

@ -2,19 +2,21 @@
<details>
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks Cloud ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
<summary><strong>Apprenez le hacking AWS de zéro à héros avec</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
* Travaillez-vous dans une **entreprise de cybersécurité** ? Voulez-vous voir votre **entreprise annoncée dans HackTricks** ? Ou voulez-vous avoir accès à la **dernière version de PEASS ou télécharger HackTricks en PDF** ? Consultez les [**PLANS D'ABONNEMENT**](https://github.com/sponsors/carlospolop) !
* Découvrez [**La famille PEASS**](https://opensea.io/collection/the-peass-family), notre collection exclusive de [**NFT**](https://opensea.io/collection/the-peass-family)
* Obtenez le [**swag officiel PEASS & HackTricks**](https://peass.creator-spring.com)
* **Rejoignez le** [**💬**](https://emojipedia.org/speech-balloon/) [**groupe Discord**](https://discord.gg/hRep4RUj7f) ou le [**groupe Telegram**](https://t.me/peass) ou **suivez** moi sur **Twitter** [**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
* **Partagez vos astuces de piratage en soumettant des PR au** [**repo hacktricks**](https://github.com/carlospolop/hacktricks) **et au** [**repo hacktricks-cloud**](https://github.com/carlospolop/hacktricks-cloud).
Autres moyens de soutenir HackTricks :
* Si vous souhaitez voir votre **entreprise annoncée dans HackTricks** ou **télécharger HackTricks en PDF**, consultez les [**PLANS D'ABONNEMENT**](https://github.com/sponsors/carlospolop)!
* Obtenez le [**merchandising officiel PEASS & HackTricks**](https://peass.creator-spring.com)
* Découvrez [**La Famille PEASS**](https://opensea.io/collection/the-peass-family), notre collection d'[**NFTs**](https://opensea.io/collection/the-peass-family) exclusifs
* **Rejoignez le** 💬 [**groupe Discord**](https://discord.gg/hRep4RUj7f) ou le [**groupe Telegram**](https://t.me/peass) ou **suivez** moi sur **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**.**
* **Partagez vos astuces de hacking en soumettant des PR aux dépôts github** [**HackTricks**](https://github.com/carlospolop/hacktricks) et [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud).
</details>
<figure><img src="../../../.gitbook/assets/i3.png" alt=""><figcaption></figcaption></figure>
**Astuce de bug bounty** : **inscrivez-vous** sur **Intigriti**, une plateforme premium de **bug bounty créée par des hackers, pour des hackers** ! Rejoignez-nous sur [**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks) dès aujourd'hui et commencez à gagner des primes allant jusqu'à **100 000 $** !
**Conseil pour les bug bounties** : **inscrivez-vous** sur **Intigriti**, une plateforme de bug bounties premium créée par des hackers, pour des hackers ! Rejoignez-nous sur [**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks) aujourd'hui, et commencez à gagner des bounties jusqu'à **100 000 $** !
{% embed url="https://go.intigriti.com/hacktricks" %}
@ -25,16 +27,18 @@ Installez les **outils frida** :
pip install frida-tools
pip install frida
```
**Téléchargez et installez** sur l'Android le **serveur frida** ([Téléchargez la dernière version](https://github.com/frida/frida/releases)).\
Une ligne de commande pour redémarrer adb en mode root, se connecter à celui-ci, télécharger frida-server, donner les permissions d'exécution et l'exécuter en arrière-plan :
**Téléchargez et installez** sur l'android le **frida server** ([Téléchargez la dernière version](https://github.com/frida/frida/releases)).\
Une ligne de commande pour redémarrer adb en mode root, s'y connecter, uploader frida-server, lui donner les permissions d'exécution et le lancer en arrière-plan :
{% code overflow="wrap" %}
```bash
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 &"
```
```markdown
{% endcode %}
**Vérifiez** si cela **fonctionne** :
```
```bash
frida-ps -U #List packages and processes
frida-ps -U | grep -i <part_of_the_package_name> #Get all the package name
@ -43,30 +47,30 @@ frida-ps -U | grep -i <part_of_the_package_name> #Get all the package name
### [Tutoriel 1](frida-tutorial-1.md)
**Depuis**: [https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1](https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1)\
**APK**: [https://github.com/t0thkr1s/frida-demo/releases](https://github.com/t0thkr1s/frida-demo/releases)\
**Code source**: [https://github.com/t0thkr1s/frida-demo](https://github.com/t0thkr1s/frida-demo)
**Depuis** : [https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1](https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1)\
**APK** : [https://github.com/t0thkr1s/frida-demo/releases](https://github.com/t0thkr1s/frida-demo/releases)\
**Code Source** : [https://github.com/t0thkr1s/frida-demo](https://github.com/t0thkr1s/frida-demo)
Suivez le [lien pour le lire](frida-tutorial-1.md).
### [Tutoriel 2](frida-tutorial-2.md)
**Depuis**: [https://11x256.github.io/Frida-hooking-android-part-2/](https://11x256.github.io/Frida-hooking-android-part-2/) (Parties 2, 3 et 4)\
**APKs et code source**: [https://github.com/11x256/frida-android-examples](https://github.com/11x256/frida-android-examples)
**Depuis** : [https://11x256.github.io/Frida-hooking-android-part-2/](https://11x256.github.io/Frida-hooking-android-part-2/) (Parties 2, 3 & 4)\
**APKs et Code Source** : [https://github.com/11x256/frida-android-examples](https://github.com/11x256/frida-android-examples)
Suivez le [lien pour le lire](frida-tutorial-2.md).
Suivez le [lien pour le lire.](frida-tutorial-2.md)
### [Tutoriel 3](owaspuncrackable-1.md)
**Depuis**: [https://joshspicer.com/android-frida-1](https://joshspicer.com/android-frida-1)\
**APK**: [https://github.com/OWASP/owasp-mstg/blob/master/Crackmes/Android/Level\_01/UnCrackable-Level1.apk](https://github.com/OWASP/owasp-mstg/blob/master/Crackmes/Android/Level\_01/UnCrackable-Level1.apk)
**Depuis** : [https://joshspicer.com/android-frida-1](https://joshspicer.com/android-frida-1)\
**APK** : [https://github.com/OWASP/owasp-mstg/blob/master/Crackmes/Android/Level\_01/UnCrackable-Level1.apk](https://github.com/OWASP/owasp-mstg/blob/master/Crackmes/Android/Level\_01/UnCrackable-Level1.apk)
Suivez le [lien pour le lire](owaspuncrackable-1.md).\
**Vous pouvez trouver des scripts Frida impressionnants ici:** [**https://codeshare.frida.re/**](https://codeshare.frida.re)
**Vous pouvez trouver des scripts Frida impressionnants ici :** [**https://codeshare.frida.re/**](https://codeshare.frida.re)
## Exemples rapides
## Exemples Rapides
Ici, vous pouvez trouver les fonctionnalités les plus basiques et intéressantes de Frida pour créer rapidement un script :
Ici, vous pouvez trouver les fonctionnalités les plus basiques et intéressantes de Frida pour créer un script rapide :
### Appeler Frida depuis la ligne de commande
```bash
@ -82,92 +86,6 @@ frida -U --no-pause -l disableRoot.js -f owasp.mstg.uncrackable1
#continue execution with our modified code.
```
### Script Python de base
```python
import frida
# Attach to the target process
session = frida.attach("com.example.app")
# Define the JavaScript code to be injected
js_code = """
Java.perform(function () {
// Find the target method
var targetMethod = Java.use("com.example.app.TargetClass.targetMethod");
// Hook the target method
targetMethod.implementation = function () {
// Print the arguments passed to the method
console.log("Arguments: " + Array.prototype.slice.call(arguments));
// Call the original method
var result = this.targetMethod.apply(this, arguments);
// Print the result
console.log("Result: " + result);
// Modify the result
result += 100;
// Return the modified result
return result;
};
});
"""
# Create the script
script = session.create_script(js_code)
# Load the script into the target process
script.load()
# Detach from the target process
session.detach()
```
### Script Python de base
```python
import frida
# Se connecter au processus cible
session = frida.attach("com.example.app")
# Définir le code JavaScript à injecter
js_code = """
Java.perform(function () {
// Trouver la méthode cible
var targetMethod = Java.use("com.example.app.TargetClass.targetMethod");
// Accrocher la méthode cible
targetMethod.implementation = function () {
// Afficher les arguments passés à la méthode
console.log("Arguments : " + Array.prototype.slice.call(arguments));
// Appeler la méthode originale
var result = this.targetMethod.apply(this, arguments);
// Afficher le résultat
console.log("Résultat : " + result);
// Modifier le résultat
result += 100;
// Retourner le résultat modifié
return result;
};
});
"""
# Créer le script
script = session.create_script(js_code)
# Charger le script dans le processus cible
script.load()
# Se détacher du processus cible
session.detach()
```
```python
import frida, sys
@ -178,7 +96,7 @@ print('[ * ] Running Frida Demo application')
script.load()
sys.stdin.read()
```
### Accrocher des fonctions sans paramètres
### Accrochage de fonctions sans paramètres
Accrochez la fonction `a()` de la classe `sg.vantagepoint.a.c`
```javascript
@ -190,127 +108,14 @@ return false;
};
});
```
# Hook java `exit()`
In this tutorial, we will learn how to hook the `exit()` method in a Java application using Frida. By hooking this method, we can intercept the application's exit calls and perform additional actions before the application terminates.
## Prerequisites
Before getting started, make sure you have the following:
- A rooted Android device or an emulator
- Frida Server installed on the device/emulator
- Frida Python bindings installed on your computer
## Steps
1. Launch the target application on your device/emulator.
2. Open a terminal and start the Frida server:
```bash
frida-server -D
```
3. Create a new Python script, for example, `exit_hook.py`, and import the necessary Frida modules:
```python
import frida
import sys
```
4. Define a function that will be called when the `exit()` method is invoked:
```python
def on_exit_called(message, data):
print("[*] exit() called")
# Perform additional actions here
```
5. Connect to the target application using Frida:
```python
try:
session = frida.get_usb_device().attach('com.example.app')
except Exception as e:
print("Failed to attach to the target process:", e)
sys.exit(1)
```
6. Retrieve the `exit()` method from the Java Virtual Machine (JVM) and hook it:
```python
try:
script = session.create_script("""
Java.perform(function () {
var System = Java.use('java.lang.System');
System.exit.implementation = function () {
send("[*] exit() called");
// Perform additional actions here
this.exit();
};
});
""")
script.on('message', on_exit_called)
script.load()
sys.stdin.read()
except Exception as e:
print("Failed to create script:", e)
sys.exit(1)
```
7. Save and run the Python script:
```bash
python exit_hook.py
```
8. Trigger the `exit()` method in the target application, either by closing the application or by calling `System.exit()` explicitly.
9. Check the terminal output to see if the hook was successful. You should see the message `[*] exit() called` printed.
By hooking the `exit()` method, we can intercept and modify the behavior of the target application before it exits. This technique can be useful for various purposes, such as debugging, analyzing application flow, or implementing custom cleanup logic.
Accrocher java `exit()`
```javascript
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 :)");
};
```
# Hook MainActivity `.onStart()` & `.onCreate()`
To hook the `.onStart()` and `.onCreate()` methods of the MainActivity class in an Android app, we can use the Frida framework. Frida allows us to dynamically inject JavaScript code into the app's runtime and intercept method calls.
Here's an example of how to hook these methods using Frida:
```javascript
Java.perform(function () {
var MainActivity = Java.use('com.example.app.MainActivity');
MainActivity.onStart.implementation = function () {
console.log('Hooked MainActivity.onStart()');
// Your custom code here
// Call the original method
this.onStart();
};
MainActivity.onCreate.implementation = function (savedInstanceState) {
console.log('Hooked MainActivity.onCreate()');
// Your custom code here
// Call the original method
this.onCreate(savedInstanceState);
};
});
```
In the above code, we first use `Java.use()` to obtain a reference to the MainActivity class. Then, we override the `.onStart()` and `.onCreate()` methods using `.implementation`. Inside the overridden methods, we can add our custom code to be executed when these methods are called. Finally, we call the original methods using `this.onStart()` and `this.onCreate(savedInstanceState)`.
By hooking these methods, we can perform various actions such as logging method calls, modifying method behavior, or even bypassing certain checks. This can be useful for analyzing the app's behavior, identifying vulnerabilities, or testing security controls.
Remember to run this code using Frida on a rooted or jailbroken device, or with the appropriate permissions if running on a non-rooted device.
Happy hacking!
Accrocher MainActivity `.onStart()` & `.onCreate()`
```javascript
var mainactivity = Java.use("sg.vantagepoint.uncrackable1.MainActivity");
mainactivity.onStart.overload().implementation = function() {
@ -322,32 +127,7 @@ send("MainActivity.onCreate() HIT!!!");
var ret = this.onCreate.overload("android.os.Bundle").call(this,var_0);
};
```
# Hook android `.onCreate()`
To hook the `.onCreate()` method in an Android application, we can use the Frida framework. Frida allows us to dynamically inject JavaScript code into the target application and modify its behavior.
Here's an example of how to hook the `.onCreate()` method using Frida:
```javascript
Java.perform(function() {
var targetClass = Java.use('com.example.MyActivity');
targetClass.onCreate.implementation = function(savedInstanceState) {
// Your custom code here
console.log('Hooked .onCreate()');
// Call the original method
this.onCreate(savedInstanceState);
};
});
```
In the above code, we first use `Java.use()` to get a reference to the target class (`com.example.MyActivity`). Then, we override the `.onCreate()` method by assigning our custom implementation to `targetClass.onCreate.implementation`. Inside the custom implementation, we can add our own code to be executed when the `.onCreate()` method is called. Finally, we call the original method using `this.onCreate(savedInstanceState)`.
Remember to replace `com.example.MyActivity` with the actual class name you want to hook.
By hooking the `.onCreate()` method, we can intercept and modify the initialization logic of an Android application, allowing us to perform various actions such as logging, debugging, or even modifying the application's behavior.
Note: Make sure you have the necessary permissions and legal authorization before performing any security testing or penetration testing activities.
Accrocher `.onCreate()` Android
```javascript
var activity = Java.use("android.app.Activity");
activity.onCreate.overload("android.os.Bundle").implementation = function(var_0) {
@ -355,9 +135,9 @@ send("Activity HIT!!!");
var ret = this.onCreate.overload("android.os.Bundle").call(this,var_0);
};
```
### Accrocher des fonctions avec des paramètres et récupérer la valeur
### Accrochage de fonctions avec paramètres et récupération de la valeur
Accrocher une fonction de décryptage. Imprimer l'entrée, appeler la fonction originale pour décrypter l'entrée et enfin, imprimer les données en clair :
Accrochage d'une fonction de déchiffrement. Imprimer l'entrée, appeler la fonction originale pour déchiffrer l'entrée et enfin, imprimer les données en clair :
```javascript
function getString(data){
var ret = "";
@ -382,9 +162,9 @@ send("Decrypted flag: " + flag);
return ret; //[B
};
```
### Accrocher des fonctions et les appeler avec notre entrée
### Interception de fonctions et appel avec nos entrées
Accrochez une fonction qui reçoit une chaîne de caractères et appelez-la avec une autre chaîne de caractères (à partir de [ici](https://11x256.github.io/Frida-hooking-android-part-2/))
Intercepter une fonction qui reçoit une chaîne de caractères et l'appeler avec une autre chaîne (depuis [ici](https://11x256.github.io/Frida-hooking-android-part-2/))
```javascript
var string_class = Java.use("java.lang.String"); // get a JS wrapper for java's String class
@ -410,26 +190,29 @@ console.log("Result of secret func: " + instance.secret());
onComplete:function(){}
});
```
## Autres tutoriels sur Frida
## Autres tutoriels Frida
* [https://github.com/DERE-ad2001/Frida-Labs](https://github.com/DERE-ad2001/Frida-Labs)
* [Partie 1 de la série de blogs sur l'utilisation avancée de Frida : Bibliothèques de chiffrement IOS](https://8ksec.io/advanced-frida-usage-part-1-ios-encryption-libraries-8ksec-blogs/)
<figure><img src="../../../.gitbook/assets/i3.png" alt=""><figcaption></figcaption></figure>
**Astuce pour les primes de bug**: **inscrivez-vous** sur **Intigriti**, une plateforme premium de **prime de bug créée par des hackers, pour des hackers**! Rejoignez-nous sur [**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks) dès aujourd'hui et commencez à gagner des primes allant jusqu'à **100 000 $**!
**Conseil pour les bug bounty** : **inscrivez-vous** sur **Intigriti**, une plateforme de bug bounty premium créée par des hackers, pour des hackers ! Rejoignez-nous sur [**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks) dès aujourd'hui, et commencez à gagner des primes jusqu'à **100 000 $** !
{% embed url="https://go.intigriti.com/hacktricks" %}
<details>
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks Cloud ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
<summary><strong>Apprenez le hacking AWS de zéro à héros avec</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong> !</strong></summary>
* Travaillez-vous dans une **entreprise de cybersécurité**? Voulez-vous voir votre **entreprise annoncée dans HackTricks**? ou voulez-vous avoir accès à la **dernière version de PEASS ou télécharger HackTricks en PDF**? Consultez les [**PLANS D'ABONNEMENT**](https://github.com/sponsors/carlospolop)!
* Découvrez [**The PEASS Family**](https://opensea.io/collection/the-peass-family), notre collection exclusive de [**NFT**](https://opensea.io/collection/the-peass-family)
* Obtenez le [**swag officiel PEASS & HackTricks**](https://peass.creator-spring.com)
* **Rejoignez le** [**💬**](https://emojipedia.org/speech-balloon/) [**groupe Discord**](https://discord.gg/hRep4RUj7f) ou le [**groupe Telegram**](https://t.me/peass) ou **suivez** moi sur **Twitter** [**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
* **Partagez vos astuces de piratage en soumettant des PR au** [**repo hacktricks**](https://github.com/carlospolop/hacktricks) **et au** [**repo hacktricks-cloud**](https://github.com/carlospolop/hacktricks-cloud).
Autres moyens de soutenir HackTricks :
* Si vous souhaitez voir votre **entreprise annoncée dans HackTricks** ou **télécharger HackTricks en PDF**, consultez les [**PLANS D'ABONNEMENT**](https://github.com/sponsors/carlospolop) !
* Obtenez le [**merchandising officiel PEASS & HackTricks**](https://peass.creator-spring.com)
* Découvrez [**La Famille PEASS**](https://opensea.io/collection/the-peass-family), notre collection d'[**NFTs**](https://opensea.io/collection/the-peass-family) exclusifs
* **Rejoignez le** 💬 [**groupe Discord**](https://discord.gg/hRep4RUj7f) ou le [**groupe telegram**](https://t.me/peass) ou **suivez** moi sur **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**.**
* **Partagez vos astuces de hacking en soumettant des PR aux dépôts github** [**HackTricks**](https://github.com/carlospolop/hacktricks) et [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud).
</details>