# Android

### Links

* [HackTricks :: Android Application Pentesting](https://book.hacktricks.xyz/mobile-pentesting/android-app-pentesting)

### Commands

```bash
# Download a file from sd
adb pull /sdcard/RKStorage ./RkStorage

# Install an APK
adb install mercado-pago-apk/app-release-universal.apk

# Copy file to device
adb push ./my-file /sdcard/Download/

# Trigger deeplink
adb shell am start -d "DEEP_LINK_HERE"

# Set port on device to localhost
adb reverse tcp:8082 tcp:8082
```

### Intercept Requests Burp

**Requisitos:**

* Ter um dispositivo com ROOT habilitado.
* Ter o burp suite instalado e rodando.

Download de certificado do burp:

```bash
curl localhost:8082/cert -o cert.der
```

Gerar PEM:

```bash
openssl x509 -inform der -in cert.der -out burp.pem
```

Ver o certificado:

```bash
openssl x509 -inform PEM -subject_hash_old -in burp.pem
```

Você verá um resultado semelhante à este:

```
9a5ba575
-----BEGIN CERTIFICATE-----
MIIDqDCCApCgAwIB...54kowV8HLAvWVdBl3ea1B4uyXSkUCv0Nt1NAgL/vqGH
teE156MEW/NvqA5vtr8qScGXGAVpWR8TH2HW0A==
-----END CERTIFICATE-----
```

Está vendo este código aqui? `9a5ba575`

O que temos que fazer com ele é anotar e colocar um `.0` ao final.

Ou seja, vamos utilizar ele com este valor:

```
9a5ba575.0
```

Memorize ele, vamos chamar ele de `$CERT_CODE`.

Gere um certificado com o nome igual ao $CERT\_CODE:

```bash
cp burp.pem $CERT_CODE
# exemplo: cp burp.pem 9a5ba575.0
```

Verifique se está tudo correto vendo o certificado:

```bash
openssl x509 -inform PEM -subject -in $CERT_CODE
```

```
subject= /C=PortSwigger/ST=PortSwigger/L=PortSwigger/O=PortSwigger/OU=PortSwigger CA/CN=PortSwigger CA
-----BEGIN CERTIFICATE-----
MIIDqDCCApCgAwIBAgIFA...owV8HLAvWVdBl3ea1B4uyXSkUCv0Nt1NAgL/vqGH
teE156MEW/NvqA5vtr8qScGXGAVpWR8TH2HW0A==
-----END CERTIFICATE-----
```

Copie o certificado para o storage interno do dispositivo, pode ser em qualquer pasta que você tenha acesso, como a de downloads:

```bash
adb push $CERT_CODE /sdcard/Download/
```

Agora nos conectar no dispositivo:

```
adb shell
```

Você vai ver algo assim:

```
guam:/ $
```

Vamos logar como root:

```
su
```

Agora vai ficar assim:

```
guam:/ #
```

Perceba a mudança de `$` para `#`.

Agora vamos copiar o certificado para a pasta de certificados do device:

```bash
cp /sdcard/Download/$CERT_CODE /system/etc/security/cacerts
```

Veja se o certificado foi instalado corretamente:

```bash
ls -la /system/etc/security/cacerts | grep $CERT_CODE
```

```
-rw-r--r-- 1 root root 1330 2023-08-15 10:41 9a5ba575.0
```

Agora vamos mudar as permissões do arquivo:

```bash
chmod 644 /system/etc/security/cacerts/$CERT_CODE
```

Agora só habilitar o proxy no celular com o IP e Porta do Burp.

Não esqueça que o burp tem que estar com o proxy habilitado para escutar em todas as interfaces:

<figure><img src="/files/txKrhGfDMDOeleNuNeaE" alt=""><figcaption></figcaption></figure>

```bash
adb shell settings put global http_proxy localhost:8080
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ben-hurs-organization.gitbook.io/guia-de-appsec/cheat-sheets/android.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
