Android Pentesting Handbook
Android Pentesting Handbook
ANDROID
PENTESTING
101
"A Novice’s Handbook to Getting Started"
By Saumya Kasthuri
www.viehgroup.com
Disclaimer
p
professional advice. While we have made every
u
effort to ensure the accuracy and reliability of
the information presented, VIEH Group
o
disclaims any warranties or representations,
r
express or implied, regarding the completeness,
g
accuracy, or usefulness of this document. Any
h
reliance you place on the information contained
e
in this document is strictly at your own risk.
i
VIEH Group shall not be liable for any damages
v
arising from the use of or reliance on this
document. also we highly appreciate the source
@
person for this document.
Happy reading !
Note: This document is not created by a professional content writer so any mistake and error is a
part of great design
p
code, binary files, and network traffic to find
u
security flaws. There are mainly 2 parts Static
and Dynamic analysis
ro
Static analysis involves examining an
g
application’s code and configuration files
h
without executing it, while dynamic analysis
involves testing the application in a running
ie
state to observe its behaviour and interactions.
v
Both approaches are important for testing the
security of an Android application.
Table of contents
p
d. Extracting the APK from the device
u
2. Static Analysis
o
a. Reverse Engineering
r
i. DEX → JAR → JAVA
g
ii. APK → JAVA
iii. What to check in the code
h
b. Decompiling and Recompiling
e
i. APK Tool
i
c. Manifest file analysis
v
3. Dynamic Analysis
a. Setting up the proxy
@
i. Configure the Burp Proxy listener
ii. Configure your device to use the
proxy
iii. Install a CA certificate on your
Android device
iv. Test the configuration / Intercept the
traffic
4. Local Data Storage Enumeration
5. Open-Source Frameworks
6. References
Emulators
p
Genymotion, QEMU,Memu, NOX_Player etc.
u
Compared to others the setp-up is more easy in
o
Memu and Nox. So, I personally prefer Memu or
r
Nox_Player for testing.
hg
An emulator provides a complete virtual environment that mimics the actual
device, while a simulator only simulates the behavior of the device.
ie
v
@
up
ro
hg
ie
v
@
3. adb install filename.apk
up
Here are some common ADB commands:
adb devices: Lists all connected Android devices and their
o
status.
r
adb shell: Opens a shell on the Android device, allowing
the user to execute commands directly on the device.
g
adb install [path to APK]: Installs an Android application
on the connected device.
h
adb uninstall [package name]: Uninstalls an application
e
from the connected device.
i
adb pull [remote file path] [local file path]: Copies a file
from the Android device to the local computer.
v
adb push [local file path] [remote file path]: Copies a file
from the local computer to the Android device.
@
adb bugreport: Generates a detailed bug report of the
Android device, including system logs, application data,
and device information.
adb screenrecord: Records the Android device’s screen in
real-time and saves it as a video file on the local
computer.
Start/ Stop ADB server: If a device is connected start the
adb server to be able to interact with the device.
adb start-server
adb kill-server
p
adb logcat --pid 15236 (displays log of that app's pid
u
only) you can also append -f <filename> to adb logcat
command
o
adb logcat packagename:[priority level: V, D, I, W, E, F,
r
S]Filter log files by priority e.g. adb logcat
com.myapp:E which prints all error logs
g
adb shell Allow you to interact with an Android device’s
command-line interface directly from your computer
h
adb shell pm list packages: Lists all installed packages
e
on the Android device.
i
adb shell am start [intent]: Starts an activity on the
Android device using an intent.
v
adb shell am force-stop com.android.settings: Stops an
activity on the Android device using an intent.
@
adb shell input text [text]: Simulates typing text on the
Android device’s keyboard.
p
part of the package name.
3. Once you have the package name, use the command adb
u
shell pm path com.example.sampleappto get the full path
o
of the APK file.
4. Finally, retrieve the base.apk file using adb pull
r
/data/app/com.example.sampleapp-1/base.apk.
g
Static Analysis
h
Static analysis involves analyzing an application’s code,
resources, and configuration files without executing the
ie
application. This type of analysis is typically performed by
analyzing the application’s source code or its compiled binary
v
file (APK) using tools like APKtool, dex2jar. The goal of static
analysis is to identify potential vulnerabilities in the code,
such as insecure coding practices, data leakage, or hard-
@
coded credentials.
p
such as strings, images, and layouts that are used by
the application.
u
4. lib/: This folder contains compiled native code
o
libraries for specific device architectures, such as ARM
r
or x86.
5. META-INF/: This folder contains the manifest file, the
g
certificate of the APK signature, and a list of all the
files in the APK, along with their checksums.
h
6. assets/: This folder contains additional application
e
data files, such as sound and video files, that are not
i
compiled into the APK.
v
7. res/: This folder contains the application resources,
such as layouts, strings, and images, in their original
format before being compiled into the Resources.arsc
@
file.
8. Android System Files: This folder contains system-level
files such as the Android runtime, framework libraries,
and system components that the application may use.
Dex2Jar
The dex files are Dalvik executable files format, and are not
human readable. So, we need to convert it back to some
human eye friendly language.
up
https://sourceforge.net/projects/dex2jar/
o
How to get the .dex file?
r
UNZIP, WINRAR
g
Convert the .apk file into .zip file then Extract the zipped file
and under extracted folder we find classes.dex file along with
h
other files
ie
v
@
https://github.com/pxb1988/dex2jar
up
ro
hg
ie
v
jd-gui
@
Now in order to open the classes.dex2jar file we need a tool
called jdgui.
For that we just open the tool and add the classes.dex2jar file
in it.
up
ro
hg
ie
v
What to check in the code?
Cryptography: Look for any use of encryption algorithms
and verify that they are implemented correctly. Check for
@
any hardcoded keys, weak encryption methods, or use of
insecure cryptographic algorithms.
Code Obfuscation: Check for obfuscation techniques used
to hide the code and make it difficult to understand.
Obfuscation techniques make it harder for reverse
engineering, but it can also hide any malicious code.
API Usage: Verify that the application does not use any
insecure APIs or APIs with known vulnerabilities. Look for
any APIs that allow unauthorized access or data leakage.
Reflection: Check for reflection, a feature that allows
dynamic code execution. Verify that reflection is not used
in a way that can allow an attacker to inject malicious code.
p
credentials, or personal information. Search for harcoded
u
database(SQL queries), password, keys, sensitive
information,URLs.
o
External Libraries: Verify that the application does not use
r
any insecure third-party libraries or libraries with known
vulnerabilities.
g
Integrity Checks: Look for any integrity checks that the
application performs to ensure that the code has not been
h
tampered with.
e
Native Code: If the application uses native code, verify
i
that the native code is compiled securely and does not
contain any vulnerabilities.
v
Web view related checks:
setJavaScriptEnabled(): This method enables or
@
disables the use of JavaScript in a web view. If this
method is set to true, the web view can execute
JavaScript code, which can be used to manipulate the
web page or to communicate with the native Android
code. However, if the application does not properly
validate the input data sent to the web view, it could
allow an attacker to inject malicious JavaScript code.
p
JavaScript code in the web view to access the native
u
Android code by exposing a Java object to JavaScript.
This feature can be used to provide additional
o
functionality or to interact with the native Android
r
code. However, if the application does not properly
validate the input data sent to the web view, it could
g
allow an attacker to execute arbitrary Java code.
runtime.exec(): This method is used to execute shell
h
commands on the device. If an attacker can inject
e
malicious input data into a web view and exploit an
i
application vulnerability, it could allow the attacker to
execute arbitrary shell commands on the device.
v
Root Detection Implementation details
SSL Pinning Implementation details
@
Decompiling and Recompiling
Apk Tool
APKTool is an application which decompiles android APKs. It
also allows you to debug the smali code step by step. It will
enable us to build a language pack by translating the .xml
strings inside APK files.
up
ro
hg
ie
Decompile an APK
v
Run command (for example consider dvba.apk) apktool d
dvba.apk
up
ro
g
Files obtained after decompiling the dvba.apk
h
Re-compiling and Signing
1. Smali file before modification
ie
v
@
up
ro
hg
3. Recompile / re-build the apk : apktool d <folder>
ie
v
@
4. Sign the APK jarsigner.
p
If you get the Please specify alias name use the following
u
command instead:
ro
$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-
key.keystore <app_modified.apk> alias_name
g
The modified APK should now be signed for 10,000 days and
h
ready to be installed onto the Android device. To do this,
e
ensure the device has USB debugging enabled then attach the
i
device to the computer’s USB port and run:
v
$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-
key.keystore PayGoWithSSL.apk alias_name
@
Manifest File Analysis
p
Check if the app is using any implicit intents that could be
u
intercepted or manipulated by attackers.
Allow debugable: true — Without a rooted phone it is
o
possible to extract the data or run an arbitrary code using
r
application permission (Should be false) The default value
is “false”
g
Allow backup: true — The default value of this attribute is
true. This setting defines whether application data can be
h
backed up and restored by a user who has enabled usb
e
debugging.(Should be false)
i
Application information: Check if the application has any
hard-coded credentials, sensitive information, or
v
debugging features that could be exploited by attackers.
Malware signatures: Check if the application has any
@
malware signatures that could indicate that the app is
malicious or potentially harmful.
Target SDK version: Check if the app is targeting an older
version of the Android SDK. If the app is not targeting the
latest version, it could be vulnerable to known security
vulnerabilities.
p
port that is not in use).
u
5. Select All interfaces and click OK.
6. At the prompt, click Yes.
ro
hg
ie
v
@ Configuring the proxy listener
p
5. Set Proxy port to the port value that you configured for the
u
Burp Proxy listener, in this example 8083.
6. Touch Save
ro
hg
ie
v
@
p
In order to interact with HTTPS traffic, you need to install a CA
certificate from Burp Suite Professional on your Android
u
device.
To download the CA certificate
ro
1. In your Android device, Open any browser and Enter
g
http://burp
2. Click on the CA certificate
h
3. cacert.der is available for download
4. rename the file as cacert.cer
e
5. Click on Download
vi
To install the CA certificate
@
2. Click on the 3 dots in the upper right corner and select
Advanced
3. Select Install Certificates from Advanced Wi-Fi
4. Select the downloaded cacert.cer file
5. Enter a name to the certificate and click on OK
6. Certificate installed successfully.
p
2. Go to Proxy > Intercept and click Intercept is off to switch
u
intercept on.
3. Open the browser on your Android device and go to an
o
HTTPS web page.
r
The page should load without any security warnings. You
g
should see the corresponding requests within Burp Suite
Professional.
eh
vi
@ Intercepted the browser traffic in proxy tool
p
Enable the USB Debugging option on the device
u
Settings, then About Device or About Phone, depending on the
device, and then we scroll down and tap on the Build Number
o
seven times until the message “You are now a developer!”
r
pops up.
Next, tap the back button one time and navigate to Developer
g
Options.
eh
vi
@ Enabling USB debuggubg
IP details
up
This command will also start an adb server locally, waiting for
o
new connections. According to the official ADB
r
documentation , port 5555 is used by the adb server, and it is
the first port in a sequence that the emulator will attempt to
g
connect to. Once the connection is established, we can use
adb devices to list the connected devices.
eh
vi
Important directories
Below are some important directories to be aware of on an
@
Android device:
/data/data: This directory contains all user-installed
applications.
/data/user/0: This directory contains data that is private
to a specific app and cannot be accessed by other apps.
/data/app: This directory contains APK files of user-
installed applications.
/system/app: This directory contains pre-installed system
applications.
/system/bin: This directory contains binary files.
/data/local/tmp: This directory is world-writable, which
can be a potential security issue.
/data/system: This directory contains system
configuration files.
p
/sdcard: This directory contains a symbolic lin
u
In Android, the directory /data/data/ contains the installation
o
directories of every application that is installed on the device.
r
This directory is private and cannot be accessed by other
apps or by non-root users. Having root access via ADB, we can
g
list the contents of this directory.
h
Applications that are stored in the internal memory of the
e
device reside in the directory /data/data/.
i
Directories like databases and shared_prefs are created as
v
part of the installation process.
@
The databases directory is usually populated when the app is
first run, storing structured data in a private database.
up
ro
hg
ie
Databases are not the only place where data is stored. In the
v
source code, SharedPreferences are objects that point to XML
files in order to read and write on them. These XML files
eventually will contain a collection of key-value pairs, and will
@
be stored in a directory called shared_prefs.
References
up
https://www.maketecheasier.com/run-android-on-desktop-
o
with-genymotion-android-emulator/
r
https://azurecloudai.blog/2020/09/22/setting-up-an-android-
g
emulator-for-testing-intune-features/
h
https://www.memuplay.com/
ie
v
@
up
ro
hg
ie
v
@