# Useful commands

![](https://3928478158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhjMjdRXwO33Lfo7uCpl6%2Fuploads%2Fgit-blob-8c5d6ef24ae54759175052b237351dccf0432770%2Fmapt.png?alt=media)

All the following commands are integrated in [HacknDroid](https://github.com/RaffaDNDM/HacknDroid).

## ADB shell

```bash
adb shell
```

On a specific device ID:

```bash
adb shell devices
```

![](https://3928478158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhjMjdRXwO33Lfo7uCpl6%2Fuploads%2Fgit-blob-198a31e5c13ba8e0ee769917c222e43e0d24a992%2Fadb_devices.png?alt=media)

```bash
adb shell -s <device_serial_number>
```

The option `-s <device_serial_number>` can be added to every ADB command.

## APK from AAB

```bash
java -jar bundletool.jar build-apks --bundle=<aab_file> --output=<output_path>.apks --ks=<keystore_path>.jks --ks-pass=pass:<keystore_password> --ks-key-alias=<key_alias> --key-pass=pass:<key_password>
```

## App data

Retrieve app data (storage):

```bash
adb shell
su root
cp -r /data/data/<app_id> /sdcard/temp
chmod 666 /sdcard/temp
exit
```

```
adb pull /sdcard/temp <local_path>
```

Reset app data

```bash
adb shell pm clear <app_id>
```

## App Information

If the APK file was decompiled, look at AndroidManifest.xml.

Otherwise:

```bash
aapt dump badging <apk_path>
```

## Backup

**Create a backup**

| **Description**      | **Command**                                        |
| -------------------- | -------------------------------------------------- |
| All the applications | `adb backup -apk -f -shared -all <backup_name>.ab` |
| Specific Application | `adb backup -apk -f <backup_name>.ab <app_id>`     |

The device will ask you to insert a `<password>` that you are going to use in the following command to create a TAR archive from the AB file.

```bash
java -jar abe.jar unpack <backup_name>.ab <backup_name>.tar <password>
```

The you can extract the TAR file as you want.

**Restore a backup**

```bash
adb restore <backup_name>.ab
```

## Compile APK

```bash
java -jar apktool.jar b <code_folder> -o <compiled_apk_filepath>
zipalign -v 4 <compiled_apk_filepath> <zipaligned_apk_filepath>
```

The next step is the signing of the apk.

## Connectivity & Battery

| **Option**          | **Command**                                            |
| ------------------- | ------------------------------------------------------ |
| Airplane Mode ON    | `adb shell settings put global airplane_mode_on 1`     |
| Airplane Mode OFF   | `adb shell settings put global airplane_mode_on 0`     |
| Battery Status      | `adb shell dumpsys battery`                            |
| Network information | `adb shell dumpsys connectivity`                       |
| Wifi OFF            | `adb shell svc wifi disable`                           |
| Wifi ON             | `adb shell svc wifi enable`                            |
| Wifi Settings       | `adb shell am start -a android.settings.WIFI_SETTINGS` |

## CPU information

```bash
adb shell cat /proc/cpuinfo
```

## Decompile APK

```bash
java -jar apktool.jar d <apk_filepath> -o <destination_folder>
```

## Device information

```bash
adb shell getprop <property>
```

| Property                   | Description          |
| -------------------------- | -------------------- |
| `ro.product.model`         | Device Model         |
| `ro.product.brand`         | Brand                |
| `ro.product.manufacturer`  | Manufacturer         |
| `ro.build.version.release` | Android Version      |
| `ro.build.id`              | Build ID             |
| `ro.build.version.sdk`     | SDK Version          |
| `ro.build.date`            | Build Date           |
| `ro.serialno`              | Device Serial Number |

## External storage

```bash
adb shell 'echo $EXTERNAL_STORAGE'
```

## File transfer

From mobile device to PC:

```bash
adb pull <mobile_path> <local_path>
```

From PC to mobile device:

```bash
adb push <local_path> <mobile_path>
```

## Install/Uninstall application

Uninstall an application from the mobile device:

```bash
adb uninstall <app_id>
```

Install from the APK file path:

```bash
adb install <path.apk>
```

Open the Play Store page of a specific application:

```bash
adb shell am start -a android.intent.action.VIEW -d "market://details?id=<app_id>"
```

*Example:*

```bash
adb shell am start -a android.intent.action.VIEW -d "market://details?id=com.mcdonalds.mobileapp"
```

![](https://3928478158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhjMjdRXwO33Lfo7uCpl6%2Fuploads%2Fgit-blob-353de1ce8a36a322b5e3d0e6b52a74355f631c61%2Fmcdonalds.png?alt=media)

## Keystore creation

```bash
keytool -genkeypair -v -keystore <keystore_path>.jks -alias <my_app_alias> -keyalg RSA -keysize 2048 -validity 10000 -storepass "my_secure_password" -keypass "my_secure_password" -dname "CN=John Doe, OU=Development, O=MyCompany, L=New York, ST=NY, C=US"
```

## Launch an application

```bash
adb shell monkey -p <app_id> -v <num_pseudo_random_events>
```

*Example:*

```bash
adb shell monkey -p com.example.app -v 1
```

## List applications on the mobile device

| **Description**        | **Command**                             |
| ---------------------- | --------------------------------------- |
| All the applications   | `adb shell pm list packages`            |
| Running applications   | `adb shell "ps -A \| awk '{print $9}'"` |
| System applications    | `adb shell pm list packages -s`         |
| 3rd-party applications | `adb shell pm list packages -3`         |

## Logs

Retrieve the PID of the application

```bash
adb shell pidof <app_id>
```

| Action       | Command                      |
| ------------ | ---------------------------- |
| Default logs | `adb logcat --pid=<pid>`     |
| Crash logs   | `adb logcat --pid=<pid> *:E` |

## Memory dump

```bash
adb shell dumpsys meminfo <app_id>
```

## Merge APK

```bash
java -jar apkeditor.jar m -f -i <dir_with_apks> -o <merged_apk_name>.apk
```

## Mirroring

On default command port (27183)

```bash
scrcpy
```

If errors on binding like the following Open

![](https://3928478158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhjMjdRXwO33Lfo7uCpl6%2Fuploads%2Fgit-blob-b9f811f92a51f9d72b0048dd065f934c379877a2%2Fscrcpy_error.png?alt=media)

* mirror on a different port:

  ```bash
  scrcpy --port=<port>
  ```
* or restart *Host Network Service (HNS)* if on Windows and then run scrcpy:

  ```bash
  net stop hns
  net start hns
  scrcpy
  ```

## Owner UID of the application

```bash
adb shell "pm list packages -U | grep <app_id> | cut -d: -f3"
```

## Package name from the Owner UID of the application

```bash
adb shell "pm list packages -U | grep <owner_uid> | cut -d: -f2 | cut -d' ' -f1"
```

## Print APK certificates information

```bash
apksigner verify --print-certs <apk_filepath>
```

## RAM information

```bash
adb shell cat /proc/meminfo
```

## Reboot/Shutdown

| Action                                          | Command                 |
| ----------------------------------------------- | ----------------------- |
| Reboot the device                               | `adb reboot`            |
| Reboot the device into bootloader/fastboot mode | `adb reboot bootloader` |
| Reboot the device into recovery mode            | `adb reboot recovery`   |
| Shutdown the device                             | `adb shell reboot -p`   |

## Screen recording

```bash
adb shell screenrecord /sdcard/video.mp4
```

## Screenshot

```bash
adb shell screencap -p /sdcard/screenshot.png
```

## Sign APK

```bash
apksigner sign --ks <keystore_file> --ks-pass pass:<keystore_password> --key-pass pass:<key_password> --v1-signing-enabled true --v2-signing-enabled true <apk_path>
```

## Stop an application

```bash
adb shell am force-stop <app_id>
```

## Storage info

```bash
adb shell df
```

## Verify APK signature

```bash
apksigner verify --verbose <apk_filepath>
```
