# 4. Android Static Analysis

## Injured Android

> 🔗 [InjuredAndroid](https://github.com/B3nac/InjuredAndroid)
>
> 🔗 [InjuredAndroid walk-throughs](https://github.com/B3nac/InjuredAndroid/blob/master/InjuredAndroid-FlagWalkthroughs.md)
>
> 1. Download the latest release [injuredandroid.apk](https://github.com/B3nac/InjuredAndroid/releases/tag/v1.0.12) from Github
> 2. Enable USB debugging on your Android test phone.
> 3. Connect your phone and your pc with a USB cable.
> 4. Install via `adb` if installing from releases. (You need to use the absolute path to the .apk file or be in the same directory)
>
> 📌 Packages name: `b3nac.injuredandroid`

`InjuredAndroid` is a *a vulnerable Android application that shows simple examples of vulnerabilities in a ctf style*.

```bash
mkdir ~/apks
cd ~/apks

wget -O InjuredAndroid.apk https://github.com/B3nac/InjuredAndroid/releases/download/v1.0.12/InjuredAndroid-1.0.12-release.apk

adb install InjuredAndroid.apk
```

> **EXTRA**
>
> * Check for app's path (and pull `base.apk` if necessary)
>
> ```bash
> adb shell
> pm list packages | grep injured
> 	package:b3nac.injuredandroid
> pm path b3nac.injuredandroid
> 	package:/data/app/b3nac.injuredandroid-Ms4WCz1i9EefZuncV6Xnpw==/base.apk
>
> # Pull the apk from the path into the host OS
> exit # the adb shell
> adb pull /data/app/b3nac.injuredandroid-Ms4WCz1i9EefZuncV6Xnpw==/base.apk InjuredAndroid_base.apk
> ```

🔬 Open `InjuredAndroid.apk` with `jadx-gui` for analysis.

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-ee061de5ef1d10f4fbdd664a208a3d945eec39ee%2F2024-01-05_20-20-52_292.png?alt=media)

***

## [AndroidManifest.xml](https://developer.android.com/guide/topics/manifest/manifest-intro)

The `AndroidManifest.xml` file contains essential information about the app, declaring the components of the app like minSDKVersion, Permissions, Activities, Services, Content Providers, Intent Filters, Debugging Info, etc.

* [Permissions](https://developer.android.com/reference/android/Manifest.permission) (Network, Internet, Phone, RW External Storage, etc)
* [Activities](https://developer.android.com/guide/components/activities/intro-activities) (UI elements for user's interaction)
  * hidden screens protected with *intent-filters*
  * outside exposed activity - `android:exported="true"`
* [Content Providers](https://developer.android.com/guide/topics/providers/content-providers) (sharing data between apps)
  * dangerous if exported

![AndroidManifest.xml](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-1988a81fe93841e11f9495459183dfe185dd216c%2F2024-01-05_19-44-52_291.png?alt=media)

* Look for
  * `minSdkVersion`
  * `android.permission`
  * `activity` & `provider`
  * `android:exported` flags
  * various normal strings
  * backup options

```xml
<!-- # Min and Target SDK  -->
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29"/>

<!-- # Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<!-- # Activities -->
<activity android:theme="@style/AppTheme.NoActionBar" android:label="@string/title_activity_flag_eighteen" android:name="b3nac.injuredandroid.FlagEighteenActivity" android:exported="true"/>
<provider android:name="androidx.core.content.FileProvider" android:exported="false" android:authorities="b3nac.injuredandroid.fileprovider" android:grantUriPermissions="true">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/>
</provider>
<activity android:theme="@style/AppTheme.NoActionBar" android:label="@string/title_activity_flag_seventeen" android:name="b3nac.injuredandroid.FlagSeventeenActivity"/>
```

***

## Manual Static Analysis

> [Apktool CLI Parameters](https://apktool.org/docs/cli-parameters)

🔬Decompile the app using [`apktool`](https://apktool.org/)

```bash
apktool d InjuredAndroid.apk
```

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-2ec0bca41cb504609c3ff6df5984b279d8f6e322%2F2024-01-05_20-24-11_293.png?alt=media)

* `lib` - directory where source code is changed for injection (`.so` files = shared objects)
* `original` - check for sensitive information in the files, `AndroidManifest.xml`
* `res` - resources directory, check the `values/strings.xml` file
* `smali` - directory where the app's source code is stored (Smali is not human readable, use `dex2jar` converter or `jadx-gui` that decompile the files directly into Java)
* `AndroidManifest.xml` - important for static analysis

```bash
# Read .so files' strings, e.g.
strings ~/apks/InjuredAndroid/lib/x86_64/libencrypt.so

# Read strings.xml, AndroidManifest.xml files
cat ~/apks/InjuredAndroid/res/values/strings.xml

cat ~/apks/InjuredAndroid/AndroidManifest.xml
```

***

## Hardcoded Strings

**Hardcoded strings** refer to strings or text values that are directly written into the source code of a program, typically without being stored in a separate configuration file or resource file.

* Found in `Resources/strings.xml` and in Activity source code

Threat vector can be login bypass with hardcoded credentials, exposed URLs and API Keys, Firebase URLs, etc.

🔬 Open `InjuredAndroid.apk` with `jadx-gui` for analysis, open `Resources/resources.arsc/res/values/*.xml` files and search for hardcoded strings.

![strings.xml](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-31f9c65a33287b1c49156684db3e30453a3e6f38%2F2024-01-06_11-24-45_295.png?alt=media)

```bash
# strings.xml

<string name="AWS_ID" />
<string name="AWS_SECRET" />

<string name="firebase_database_url">https://injuredandroid.firebaseio.com</string>

<string name="google_api_key">AIzaSyCUImEIOSvqAswLqFak75xhskkB6illd7A</string>
<string name="google_app_id">1:430943006316:android:d97db57e11e42a1a037249</string>
<string name="google_crash_reporting_api_key">AIzaSyCUImEIOSvqAswLqFak75xhskkB6illd7A</string>
<string name="google_storage_bucket">injuredandroid.appspot.com</string>

```

Use the `Text search` tool to search the source code. Search for useful info like API, URLs, ids, passwords, SQL, Firebase, HTTP/HTTPS, secrets, sensitive data, etc.

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-0043b4b2a4faeb7a9edcac1d723a76a6d48e5720%2F2024-01-06_11-36-39_296.png?alt=media)

***

## Injured Android Flags

### Flag1

Using `jadx-gui` open `Source code\b3nac.injuredandroid\FlagOneLoginActivity`

![FlagOneLoginActivity](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-068b441a0777a600e3a1180cdd5b3e1d033c171b%2F2024-01-06_11-45-43_297.png?alt=media)

> The `submitFlag` method verifies user input against the string "**F1ag\_0n3**" and, if matched, navigates to the `FlagOneSuccess` activity, with associated actions related to flags and UI.

> 🚩 `F1ag_0n3` - Flag found in the `submitFlag` method.

### Flag2

*There is a way to bypass the main activity and invoke other activities that are exported*. Activities can be accessed with `adb`.

In `jadx-guid`, search for `android:exported="true"` in the `AndroidManifest.xml` file.

* Take the `<activity android:name="b3nac.injuredandroid.b25lActivity" android:exported="true"/>` activity.
* `b3nac.injuredandroid.b25lActivity` can be accessed from anywhere on the phone

```bash
# Open terminal
adb shell
su

# Start the activity in the app
am start b3nac.injuredandroid/.b25lActivity
```

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-7cd89c2f6844edd23a17910bcdf24631191fb222%2F2024-01-06_12-03-09_299.png?alt=media)

> 🚩 `S3c0nd_F1ag`

### Flag3

*R stands for Resources. Check for `xml` files.*

Using `jadx-gui` open `Source code\b3nac.injuredandroid\FlagThreeActivity`

![FlagThreeActivity](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-0226c3985ff15610d5414cf5c26ff0039d73c758%2F2024-01-06_12-08-00_300.png?alt=media)

> The `submitFlag` method checks if the user input matches the string resource `cmVzb3VyY2VzX3lv`. If true, it directs to the `FlagOneSuccess` activity, with related flag handling and UI.
>
> * The hardcoded string (containing the flag) is stored in the `strings.xml` file - easy to reverse engineer.

Search for the `cmVzb3VyY2VzX3lv` string in the `strings.xml` file.

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-debef5ad8cce5ac49356f1185962f35d1059ebad%2F2024-01-06_12-10-03_301.png?alt=media)

> 🚩 `F1ag_thr33`

### Flag4

*Classes and imports.*

Using `jadx-gui` open `Source code\b3nac.injuredandroid\FlagFourActivity`

![FlagFourActivity](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-fb04a354c24a1468b6270684c8f30366198ce722%2F2024-01-06_12-13-32_302.png?alt=media)

> The `submitFlag` method compares the user input with a string decoded from a byte array obtained by the `decoder.getData()` method. If there's a match, it launches the `FlagOneSuccess` activity, updating flag status and UI.

Search the obfuscated `g` class.

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-2fb67ac025ddac269981a3db69c9dd5491ccdb44%2F2024-01-06_12-25-25_303.png?alt=media)

* [CyberChef](https://gchq.github.io/CyberChef/#recipe=From_Base64\('A-Za-z0-9%2B/%3D',true,false\)\&input=TkY5dmRtVnlaRzl1WlY5dmJXVnNaWFJ6) online tool can be used to decode the string or Terminal `base64 -d` command.

```bash
echo "NF9vdmVyZG9uZV9vbWVsZXRz" | base64 -d
	4_overdone_omelets
```

> The `g` class provides a byte array obtained by decoding a Base64-encoded string. The decoded byte array represents the value "**4\_overdone\_omelets**" and is accessible through the `a()` method.

> 🚩 `4_overdone_omelets`

### Flag8

*AWS Cli, Profiles and Credentials.*

> 🔗 Tools
>
> * [cloud\_enum](https://github.com/initstring/cloud_enum) - *Multi-cloud OSINT tool. Enumerate public resources in AWS, Azure, and Google Cloud.*
> * [AWS CLI](https://aws.amazon.com/cli/)

```bash
# cloud_enum

cd ~/repo
git clone https://github.com/initstring/cloud_enum.git
cd ~/repo/cloud_enum
pip3 install -r requirements.txt
```

* Run `cloudenum.py`

```bash
python3 cloud_enum.py -k injuredandroid
```

Check the <http://injuredandroid.s3.amazonaws.com/> bucket, the enumeration reveals the flag. Instead of attempting to crack the AWS login secret, one of the discovered URLs will get the flag.

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-f36fd59cfa9eda769342a375a76c3591b48578f0%2F2024-01-06_12-49-23_304.png?alt=media)

> 🚩 `C10ud_S3cur1ty_lol`

> EXTRA - Previous AWS with ID & SECRET strings.
>
> ```bash
> # If bucket exists and access is necessary with ID & SECRET, AWS CLI can be used
> sudo apt install -y awscli
>
> aws configure --profile injuredandroid
> aws s3 ls s3://injuredandroid --profile injuredandroid
> ```

### Flag9

*Use .json trick with database URL.*

> 🔗 Tools
>
> * [firebaseEnum](https://github.com/Sambal0x/firebaseEnum) - *Enumerate exposed firebase databases*

Open `strings.xml` and search for `firebase`.

Found `<string name="firebase_database_url">https://injuredandroid.firebaseio.com</string>`.

Open <https://injuredandroid.firebaseio.com> in a browser - Google login.

> * Enumerating Firebase database (if not found in the strings)
>
> ```bash
> # firebaseEnum
>
> cd ~/repo
> git clone https://github.com/Sambal0x/firebaseEnum.git
> cd ~/repo/firebaseEnum
> pip3 install -r requirements.txt
> ```
>
> * Run `firebaseEnum.py`
>
> ```bash
> python3 firebaseEnum.py -k injuredandroid
> # nothing found
> ```
>
> <img src="https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-c86b017c8f8f32ac85337392fa6f5344666f5cb8%2F2024-01-06_13-17-35_308.png?alt=media" alt="" data-size="original">

Using `jadx-gui` open `Source code\b3nac.injuredandroid\FlagNineFirebaseActivity`

![FlagNineFirebaseActivity](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-ae6a798838fa52f159231e549171ade43e7b3f07%2F2024-01-06_13-11-56_305.png?alt=media)

![FlagNineFirebaseActivity](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-f5fe429cb32e63c4d097a1e0d8110f889e9b80fd%2F2024-01-06_13-12-40_306.png?alt=media)

```bash
# Decode the string found in the FlagNineFirebaseActivity method
echo "ZmxhZ3Mv" | base64 -d
	flags/
```

Open <https://injuredandroid.firebaseio.com/flags/.json> in the browser and find the flag

* this Firebase directory is not protected.

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-d91bcb9e004cf3dbf8dc24817f7a7082be37a068%2F2024-01-06_13-14-56_307.png?alt=media)

Base64 encode the found flag `[nine!_flag]` and input in the app.

```bash
echo -n '[nine!_flag]' | base64
	W25pbmUhX2ZsYWdd
```

> 🚩 `W25pbmUhX2ZsYWdd`

***

## MobSF Automated Analysis

> 🔗 [MobSF](https://github.com/MobSF/Mobile-Security-Framework-MobSF) - *a security research platform for mobile applications in Android, iOS and Windows Mobile*

Run `MobSF` (with Docker) and import the `InjuredAndroid.apk` into it for Static Analysis.

```bash
docker run -it --rm --name mobsf -p 8000:8000 -v ~/docker/mobsf:/home/mobsf/.MobSF opensecurity/mobile-security-framework-mobsf:latest
```

![MobSF - InjuredAndroid.apk](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-4aa658d046d1793a0476b5cd3f67fb8ea92ceded%2F2024-01-06_14-53-44_321.png?alt=media)

![MobSF Application Security Scorecard - InjuredAndroid 1.0.9](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-efa776543464449c4fd6c2f8d6e1816199d939fc%2F2024-01-06_14-37-31_311.png?alt=media)

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-b8563272a2428398a5278346474996d42a52be7d%2F2024-01-06_14-38-13_312.png?alt=media)

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-eed310a2c7f70f97e787b86cc18034226824d3b0%2F2024-01-06_14-38-34_313.png?alt=media)

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-76db1c45016d83de26cc3a9c8dc57eb72e21629e%2F2024-01-06_14-39-52_314.png?alt=media)

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-397164a028f5e1d98ecdf7c00121fc078d89b3b0%2F2024-01-06_14-47-50_318.png?alt=media)

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-681d0fbb93706d072f741a068006168fc700687d%2F2024-01-06_14-49-11_319.png?alt=media)

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-fda0486e66e66337448744cbf5301660ae4d9659%2F2024-01-06_14-40-58_315.png?alt=media)

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-5dc4ed67a0e5d09c5b82ebd2d607092113f42c77%2F2024-01-06_14-50-11_320.png?alt=media)

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-1a078102a511a50faa1555ac3234a3b798f968b6%2F2024-01-06_14-41-44_316.png?alt=media)

![](https://1178537843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2KUxfxUFmy000PDT7MtM%2Fuploads%2Fgit-blob-6db33baa725be8cef4c924d6b19f821cd80ca8e0%2F2024-01-06_14-42-41_317.png?alt=media)

***
