Android static code analysis
بسم الله الرحمن الرحيم
in the name of Allah the Most Gracious, Most Merciful
last time we scratched the surface of Android penetration testing, what is it, and the tools required to start.
also, we mentioned that Android penetration testing has 2 types, static and dynamic code analysis.
static code analysis is where you review the APK while it is in its static phase ( when it is not running ) reviewing the source code, looking for vulnerable implementations of methods and components, and insecure storage of the data.
in this blog, we will discuss static code analysis, its steps, and What to expect while performing static code analysis, so let's get started.
First off we need to have 2 Tools to start our analysis.
- ADB Shell
- Jadx-Gui
ADB Shell :
the Android OS is built on a Linux operating system meaning that it contains its own file system and list of commands, the way to interact with that command line is by using ADB shell tool.
it includes 3 components:
- A client, which sends commands ( installed on your Host machine )
- A daemon ( adbd ), which runs commands on a device
- A server that manages communication between the client and the ADBD
this tool is used both by penetration testers and Android developers as it can be used in debugging as well as interacting with the Android command line.
ADB is part of the Android SDK and is downloaded when you download an Android IDE like Android Studio and it is installed by default on Linux Machines.
Some of the Basic ADB commands are
└─$ adb devices
List of devices attached
192.168.140.102:5555 devicewhich lists all devices attached to your machine ( emulators and physical devices )
└─$ adb connect $ipconnects ADB shell to a device with the IP address you specified
└─$ adb shell
vbox86p:/ #gives you shell access to your Android device.
note that there are a lot of ADB commands which you can explore here
Android File system :
as we said Android has its own file system which consists of different directories each with its own purpose here are some of the common directories you will encounter.
/boot
contains files needed for your Android device to boot
/system
contains everything about your Android OS including the system files, binaries, Android GUI, and pre-installed system files like settings and file manager.
/data
as the name suggests, contains all your data including installed apps, settings, contacts, messages, and more.
one of the common directories in this path is the /data/data directory, which contains all the applications installed on the Android device
/sdcard
this directory represents the SD card in the Android, not to confuse it with the physical SD card, this directory represents a virtual SD card implemented by the Android OS to represent External storage that apps can use, we will dive more into the concept of Data storage later on.
Jadx-GUI :
the main aspect of static code analysis is source code reviewing where you reverse the APK to its original state and start reviewing the classes and methods implemented to hunt for vulnerabilities inside it, the main tool to do so is Jadx-Gui which is the fastest and easiest tool to use.
installing it is pretty straightforward using this repo, running its binaries, and then dragging the APK inside it to start reviewing the code.
throughout this blog, we will be using the Diva application which is an APK that is intentionally designed to be vulnerable to sharpen your Android penetration testing skills.
Get Anas Eladly ( 0x3adly )’s stories in your inbox
Join Medium for free to get updates from this writer.
let's install the application on our emulator to see what it does before reverse-engineering it, using this command.
adb install DivaApplicationas you can see the application includes multiple Vulnerabilities to train yourself on.
so let's unpack it using Jadx-Gui and that's what we get.
note that the first thing you want to look at when starting your testing is the android manifest file as it gives you a quick overview of what the APK contains
now at first sight you might get overwhelmed and confused, so let’s break down the file for more clarification ( you should open the application using the emulator for better clarification of what each tag does )
First, we know that the manifest file is written using XML markup language meaning that it is written using tags so this is the schema you are seeing
starting the first couple of lines.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="jakhar.aseem.diva" platformBuildVersionCode="23" platformBuildVersionName="6.0-2166767">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23"/>the first line just specifies the XML version and the encoding used.
the second line specifies the APK package name (which will be useful later ), API version ( in this case 23 ), and android version ( in this case it is android Marshemello 6.0 )
the third line specifies the minimum SDK the APK can run on and the targeted SDK the APK is meant to run on
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>the next couple of lines specify what permissions this APK has
in this APK it is allowed to read and write in the external storage ( /sdcard directory ) and access the internet.
we will discuss permissions in detail in other articles.
<application android:theme="@style/AppTheme" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:debuggable="true" android:allowBackup="true" android:supportsRtl="true">this line is very important, it actually contains 2 vulnerabilities on its own
the first being android:debuggable="true" which enables the attacker to debug the application and gain access to internal architecture he shouldn't be allowed to access.
the second one being android:allowBackup="true" which allows an attacker to take the backup of the application data via ADB even if the device is not rooted.
now getting to the fun part :D
<activity android:theme="@style/AppTheme.NoActionBar" android:label="@string/app_name" android:name="jakhar.aseem.diva.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:label="@string/d1" android:name="jakhar.aseem.diva.LogActivity"/>
<activity android:label="@string/d2" android:name="jakhar.aseem.diva.HardcodeActivity"/>
<activity android:label="@string/d3" android:name="jakhar.aseem.diva.InsecureDataStorage1Activity"/>
<activity android:label="@string/d4" android:name="jakhar.aseem.diva.InsecureDataStorage2Activity"/>
<activity android:label="@string/d5" android:name="jakhar.aseem.diva.InsecureDataStorage3Activity"/>
<activity android:label="@string/d6" android:name="jakhar.aseem.diva.InsecureDataStorage4Activity"/>
<activity android:label="@string/d7" android:name="jakhar.aseem.diva.SQLInjectionActivity"/>
<activity android:label="@string/d8" android:name="jakhar.aseem.diva.InputValidation2URISchemeActivity"/>
<activity android:label="@string/d9" android:name="jakhar.aseem.diva.AccessControl1Activity"/>
<activity android:label="@string/apic_label" android:name="jakhar.aseem.diva.APICredsActivity">
<intent-filter>
<action android:name="jakhar.aseem.diva.action.VIEW_CREDS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:label="@string/d10" android:name="jakhar.aseem.diva.AccessControl2Activity"/>
<activity android:label="@string/apic2_label" android:name="jakhar.aseem.diva.APICreds2Activity">
<intent-filter>
<action android:name="jakhar.aseem.diva.action.VIEW_CREDS2"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>as you can see through the tags above they are separated into 2 tags
- activity tag
- intent filters
simply put, activities in Android are the screens you see, each activity represents a screen in the application, and each tag represents an activity, you can even see similarities in the activity names and the level names in the application.
we will discuss what intent filters in another article ( because this one is getting way too long :D ) but for now, you could think of it as telling the Android OS what actions are your application capable of doing and what tasks are you capable of receiving.
<provider android:name="jakhar.aseem.diva.NotesProvider" android:enabled="true" android:exported="true" android:authorities="jakhar.aseem.diva.provider.notesprovider"/>this is the last tag we have the provider tag.
indicating the presence of a content provider, we will discuss what a content provider is in another article, simply put it is a way to provide other applications with the data your application has
want to look for low-hanging fruit through the manifest file?
- Look for
android:debuggable="true"andandroid:allowBackup="true"attributes - look for
android:exported="true”on any component ( having this flag on an important component means that this component can be accessed from an external application ) - Look for intent filters and how are they implemented
- look for interesting permissions especially signatureOrSystem ones
I know that was a lot to take in so let's break down what we discussed in this article :
- what is ADB shell
- android file system
- Jadx-Gui tool and reverse engineering the APK
- Contents of Android manifest file
- Low-hanging fruit to look for in the manifest file
there are a lot of tools that can automate this part including Mobsf and Drozer, but you must know the underlying work of those scanners before starting to use them right? :’D .








