2024-02-08 04:35:32 +00:00
# Tutorial do Frida
2022-04-28 16:01:33 +00:00
< details >
2024-02-09 01:28:17 +00:00
< summary > < strong > Aprenda hacking AWS do zero ao herói com< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > !< / strong > < / summary >
2022-04-28 16:01:33 +00:00
2024-02-08 04:35:32 +00:00
Outras maneiras de apoiar o HackTricks:
2024-01-03 13:53:35 +00:00
2024-02-08 04:35:32 +00:00
* Se você quiser ver sua **empresa anunciada no HackTricks** ou **baixar o HackTricks em PDF** Confira os [**PLANOS DE ASSINATURA** ](https://github.com/sponsors/carlospolop )!
* Adquira o [**swag oficial do PEASS & HackTricks** ](https://peass.creator-spring.com )
* Descubra [**A Família PEASS** ](https://opensea.io/collection/the-peass-family ), nossa coleção exclusiva de [**NFTs** ](https://opensea.io/collection/the-peass-family )
2024-02-09 01:28:17 +00:00
* **Junte-se ao** 💬 [**grupo Discord** ](https://discord.gg/hRep4RUj7f ) ou ao [**grupo telegram** ](https://t.me/peass ) ou **siga-nos** no **Twitter** 🐦 [**@carlospolopm** ](https://twitter.com/hacktricks_live )**.**
2024-02-08 04:35:32 +00:00
* **Compartilhe seus truques de hacking enviando PRs para os** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) e [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) repositórios do github.
2022-04-28 16:01:33 +00:00
2022-10-27 23:22:18 +00:00
< / details >
2022-04-28 16:01:33 +00:00
2023-10-09 20:03:21 +00:00
< figure > < img src = "../../../.gitbook/assets/i3.png" alt = "" > < figcaption > < / figcaption > < / figure >
2022-04-28 16:01:33 +00:00
2024-02-08 04:35:32 +00:00
**Dica de recompensa por bugs**: **inscreva-se** no **Intigriti** , uma plataforma de **bug bounty premium criada por hackers, para hackers** ! Junte-se a nós em [**https://go.intigriti.com/hacktricks** ](https://go.intigriti.com/hacktricks ) hoje e comece a ganhar recompensas de até ** $100,000**!
2022-04-28 16:01:33 +00:00
2022-10-27 23:22:18 +00:00
{% embed url="https://go.intigriti.com/hacktricks" %}
2022-04-28 16:01:33 +00:00
2023-06-06 18:56:34 +00:00
## Instalação
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
Instale as **ferramentas do frida** :
2020-09-09 15:07:39 +00:00
```bash
2020-07-15 15:43:14 +00:00
pip install frida-tools
pip install frida
```
2024-02-08 04:35:32 +00:00
**Baixe e instale** no Android o **servidor frida** ([Baixe a última versão](https://github.com/frida/frida/releases)).\
2024-02-09 01:28:17 +00:00
Um comando para reiniciar o adb em modo root, conectar a ele, fazer upload do frida-server, dar permissões de execução e executá-lo em segundo plano:
2023-08-24 16:55:43 +00:00
{% code overflow="wrap" %}
2020-09-09 15:07:39 +00:00
```bash
2023-08-24 16:55:43 +00:00
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 & "
2020-07-15 15:43:14 +00:00
```
2024-01-13 22:18:07 +00:00
{% endcode %}
2023-06-06 18:56:34 +00:00
**Verifique** se está **funcionando** :
2020-09-09 15:07:39 +00:00
```bash
2020-07-15 15:43:14 +00:00
frida-ps -U #List packages and processes
frida-ps -U | grep -i < part_of_the_package_name > #Get all the package name
```
2023-06-06 18:56:34 +00:00
## Tutoriais
2020-07-15 15:43:14 +00:00
2022-10-27 23:22:18 +00:00
### [Tutorial 1](frida-tutorial-1.md)
2020-07-15 15:43:14 +00:00
2023-06-06 18:56:34 +00:00
**De**: [https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1 ](https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1 )\
2021-10-18 11:21:18 +00:00
**APK**: [https://github.com/t0thkr1s/frida-demo/releases ](https://github.com/t0thkr1s/frida-demo/releases )\
2024-01-03 13:53:35 +00:00
**Código Fonte**: [https://github.com/t0thkr1s/frida-demo ](https://github.com/t0thkr1s/frida-demo )
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
**Siga o [link para ler ](frida-tutorial-1.md ).**
2020-07-15 15:43:14 +00:00
2022-10-27 23:22:18 +00:00
### [Tutorial 2](frida-tutorial-2.md)
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
**De**: [https://11x256.github.io/Frida-hooking-android-part-2/ ](https://11x256.github.io/Frida-hooking-android-part-2/ ) (Partes 2, 3 e 4)\
2024-01-03 13:53:35 +00:00
**APKs e Código Fonte**: [https://github.com/11x256/frida-android-examples ](https://github.com/11x256/frida-android-examples )
2020-07-15 15:43:14 +00:00
2024-02-09 01:28:17 +00:00
**Siga o [link para ler ](frida-tutorial-2.md ).**
2020-07-15 15:43:14 +00:00
2022-10-27 23:22:18 +00:00
### [Tutorial 3](owaspuncrackable-1.md)
2020-07-15 15:43:14 +00:00
2023-06-06 18:56:34 +00:00
**De**: [https://joshspicer.com/android-frida-1 ](https://joshspicer.com/android-frida-1 )\
2021-10-18 11:21:18 +00:00
**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 )
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
**Siga o [link para ler ](owaspuncrackable-1.md ).**
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
**Você pode encontrar mais scripts incríveis do Frida aqui:** [**https://codeshare.frida.re/** ](https://codeshare.frida.re )
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
## Exemplos Rápidos
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
### Chamando o Frida a partir da linha de comando
2020-07-15 15:43:14 +00:00
```bash
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.
```
2024-02-08 04:35:32 +00:00
### Script Python Básico
2020-07-15 15:43:14 +00:00
```python
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()
```
2024-02-08 04:35:32 +00:00
### Enganando funções sem parâmetros
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
Engane a função `a()` da classe `sg.vantagepoint.a.c`
2020-07-15 15:43:14 +00:00
```javascript
Java.perform(function () {
; rootcheck1.a.overload().implementation = function() {
2023-08-24 16:55:43 +00:00
rootcheck1.a.overload().implementation = function() {
send("sg.vantagepoint.a.c.a()Z Root check 1 HIT! su.exists()");
return false;
};
2020-07-15 15:43:14 +00:00
});
```
2024-02-08 04:35:32 +00:00
Hook java `exit()`
Gancho em `exit()` do java
2020-07-15 15:43:14 +00:00
```javascript
var sysexit = Java.use("java.lang.System");
2023-08-24 16:55:43 +00:00
sysexit.exit.overload("int").implementation = function(var_0) {
send("java.lang.System.exit(I)V // We avoid exiting the application :)");
};
2020-07-15 15:43:14 +00:00
```
2024-02-08 04:35:32 +00:00
### Hook MainActivity `.onStart()` & `.onCreate()`
2024-02-09 01:28:17 +00:00
Para interceptar os métodos `.onStart()` e `.onCreate()` da MainActivity, você pode usar o seguinte script Frida:
2024-02-08 04:35:32 +00:00
```javascript
Java.perform(function() {
var MainActivity = Java.use('com.example.MainActivity');
MainActivity.onStart.implementation = function() {
console.log('onStart() foi chamado');
this.onStart();
};
MainActivity.onCreate.implementation = function() {
console.log('onCreate() foi chamado');
this.onCreate();
};
});
```
2020-07-15 15:43:14 +00:00
```javascript
var mainactivity = Java.use("sg.vantagepoint.uncrackable1.MainActivity");
2023-08-24 16:55:43 +00:00
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);
};
2020-07-15 15:43:14 +00:00
```
2024-02-08 04:35:32 +00:00
### Hook android `.onCreate()`
2024-02-09 01:28:17 +00:00
Para interceptar a chamada do método `.onCreate()` em um aplicativo Android, você pode usar o Frida. Abaixo está um exemplo de script Frida que pode ser usado para realizar esse tipo de intercepção:
2024-02-08 04:35:32 +00:00
```javascript
Java.perform(function() {
var Activity = Java.use('android.app.Activity');
Activity.onCreate.implementation = function() {
console.log('onCreate() foi chamado');
this.onCreate();
};
});
```
2020-07-15 15:43:14 +00:00
```javascript
2023-08-24 16:55:43 +00:00
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);
};
2020-07-15 15:43:14 +00:00
```
2024-02-08 04:35:32 +00:00
### Enganchando funções com parâmetros e recuperando o valor
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
Enganchando uma função de descriptografia. Imprimir a entrada, chamar a função original para descriptografar a entrada e, finalmente, imprimir os dados em texto simples:
2020-07-15 15:43:14 +00:00
```javascript
2023-08-24 16:55:43 +00:00
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
};
2020-07-15 15:43:14 +00:00
```
2024-02-08 04:35:32 +00:00
### Enganando funções e chamando-as com nossa entrada
2020-07-15 15:43:14 +00:00
2024-02-08 04:35:32 +00:00
Engane uma função que recebe uma string e a chame com outra string (de [aqui ](https://11x256.github.io/Frida-hooking-android-part-2/ ))
2020-07-15 15:43:14 +00:00
```javascript
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
2023-08-24 16:55:43 +00:00
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;
2020-07-15 15:43:14 +00:00
};
```
2024-02-08 04:35:32 +00:00
### Obter um objeto de uma classe já criado
2020-07-15 15:43:14 +00:00
2024-02-09 01:28:17 +00:00
Se deseja extrair algum atributo de um objeto criado, pode usar este método.
2020-07-15 15:43:14 +00:00
2023-08-24 16:55:43 +00:00
Neste exemplo, você verá como obter o objeto da classe my\_activity e como chamar a função .secret() que imprimirá um atributo privado do objeto:
2020-07-15 15:43:14 +00:00
```javascript
Java.choose("com.example.a11x256.frida_test.my_activity" , {
2023-08-24 16:55:43 +00:00
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(){}
2020-07-15 15:43:14 +00:00
});
```
2024-02-08 04:35:32 +00:00
## Outros tutoriais do Frida
2023-12-11 09:52:48 +00:00
* [https://github.com/DERE-ad2001/Frida-Labs ](https://github.com/DERE-ad2001/Frida-Labs )
2024-02-08 04:35:32 +00:00
* [Parte 1 da série de blogs sobre o uso avançado do Frida: Bibliotecas de Criptografia do IOS ](https://8ksec.io/advanced-frida-usage-part-1-ios-encryption-libraries-8ksec-blogs/ )
2024-01-13 18:45:43 +00:00
2023-12-11 09:52:48 +00:00
2023-10-09 20:03:21 +00:00
< figure > < img src = "../../../.gitbook/assets/i3.png" alt = "" > < figcaption > < / figcaption > < / figure >
2022-04-28 16:01:33 +00:00
2024-02-09 01:28:17 +00:00
**Dica de recompensa por bugs**: **inscreva-se** no **Intigriti** , uma plataforma premium de **recompensas por bugs criada por hackers, para hackers** ! Junte-se a nós em [**https://go.intigriti.com/hacktricks** ](https://go.intigriti.com/hacktricks ) hoje e comece a ganhar recompensas de até ** $100,000**!
2022-04-28 16:01:33 +00:00
2022-10-27 23:22:18 +00:00
{% embed url="https://go.intigriti.com/hacktricks" %}
2022-04-28 16:01:33 +00:00
2022-10-27 23:22:18 +00:00
< details >
2022-04-28 16:01:33 +00:00
2024-02-08 04:35:32 +00:00
< summary > < strong > Aprenda hacking na AWS do zero ao herói com< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > !< / strong > < / summary >
2024-01-03 13:53:35 +00:00
2024-02-08 04:35:32 +00:00
Outras maneiras de apoiar o HackTricks:
2022-04-28 16:01:33 +00:00
2024-02-08 04:35:32 +00:00
* Se você deseja ver sua **empresa anunciada no HackTricks** ou **baixar o HackTricks em PDF** , verifique os [**PLANOS DE ASSINATURA** ](https://github.com/sponsors/carlospolop )!
* Adquira o [**swag oficial do PEASS & HackTricks** ](https://peass.creator-spring.com )
* Descubra [**A Família PEASS** ](https://opensea.io/collection/the-peass-family ), nossa coleção exclusiva de [**NFTs** ](https://opensea.io/collection/the-peass-family )
2024-02-09 01:28:17 +00:00
* **Junte-se ao** 💬 [**grupo do Discord** ](https://discord.gg/hRep4RUj7f ) ou ao [**grupo do telegram** ](https://t.me/peass ) ou **siga-nos** no **Twitter** 🐦 [**@carlospolopm** ](https://twitter.com/hacktricks_live )**.**
2024-02-08 04:35:32 +00:00
* **Compartilhe seus truques de hacking enviando PRs para os repositórios do** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) e [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ).
2022-04-28 16:01:33 +00:00
< / details >