> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/android-appsec/adb.md).

# ADB

ADB (Android Debug Bridge) is a command-line tool that enables communication between a computer and an Android device. ADB provides a variety of commands that allow developers and security testers to interact with an Android device, transfer files, install or remove applications, and collect information for debugging or security testing purposes.

***

### **How ADB Works**

ADB works as a **client–server system** with three components:

1. **Client**
   * Sends commands to the device.
   * Runs on the developer’s machine.
   * Invoked via terminal or Android Studio by typing the `adb` command.
2. **Daemon (`adbd`)**
   * Executes commands on the Android device.
   * Runs as a background process on the device or emulator.
3. **Server**
   * Manages communication between the **client** and **daemon**.
   * Runs in the background on the development machine.

***

#### **Install ADB**

```bash
sudo apt update
sudo apt install adb
```

#### General Device Interaction

* Lists all connected Android devices and their status.

```
adb devices
```

* Opens a shell on the Android device, allowing the user to execute commands directly on the device.

```
adb shell
```

#### Application Management

* Installs an Android application on the connected device.

```
adb install [path to APK]
```

* Uninstalls an application from the connected device. (only works for **user-installed** apps — not system apps.)

```
adb uninstall [package name]
```

* Disable system apps for the current user.

```
adb shell pm uninstall --user 0 com.mipay.wallet.in
```

#### File Management

* Copies a file from the Android device to the local computer.

```
adb pull [remote file path] [local file path]
```

* Copies a file from the local computer to the Android device.

```
adb push [local file path] [remote file path]
```

#### Bug Reporting

* ```bash
  adb bugreport
  ```

  Generates a detailed bug report of the Android device, including system logs, application data, and device information.

#### Starting/Stopping ADB Server

* Starts the ADB server.

```
adb start-server
```

* Stops the ADB server.

```
adb kill-server
```

#### Logcat (System Logs)

* Displays the Android device’s system log in real-time.

```
adb logcat
```

* Saves the logcat output to a file on the local system.

```
adb logcat -d > [path_to_file]
```

* Clears the current logs on the device.

```
adb logcat -c
```

**Capturing Logs of a Specific App**

1. Get the process ID (PID) of the app:

   ```bash
   adb shell pidof com.example.app
   ```
2. Display logs for the specific app's PID:

   ```bash
   adb logcat --pid 15236
   ```
3. Filter log files by priority level:

   ```bash
   adb logcat packagename:[priority level]
   ```

#### Shell Commands

* Interacts with the Android device’s command-line interface.

```
adb shell
```

* Lists all installed packages on the Android device.

```
adb shell pm list packages
```

* Starts an activity on the Android device using an intent.

```
adb shell am start [intent]
```

* Stops an activity on the Android device using an intent.

```
adb shell am force-stop com.android.settings
```

* Removes the app **only for the current user**, doesn’t delete from the system.

```bash
adb shell pm uninstall -k --user 0 <package_name>
```

* Re-enables the app if its APK still exists in `/system`.

```bash
adb shell cmd package install-existing <package_name>
```

* Simulates typing text on the Android device’s keyboard.

```
adb shell input text [text]
```

#### Enable and Disable Proxy

* Enable Proxy

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

* Disable Proxy

```bash
adb shell settings put global http_proxy :0
```

#### Network and Port Forwarding

* **Port Forwarding** (PC → Device)

  ```
  adb forward tcp:8080 tcp:8080
  ```
* **Reverse Port Forwarding** (Device → PC)

  ```
  adb reverse tcp:8080 tcp:8080
  ```
