Android Application
• .apk - Android Package
• Uploaded to Play Store or sideloaded
• Can be dumped from the device
• A regular .zip file
• Always signed
@h4oxer
Build Process
@h4oxer
Source Code
Resource Files
Jar Libraries
AAR Libraries
Compilers
DEX Files
Compiled Res
Slide 10
Slide 10 text
DEX Bytecode
• Register-based bytecode
• Executed by Dalvik / ART Runtime
• Compiled to native code - dex2oat
• Can be reversed to Java source
• Smali bytecode – intermediate bytecode
@h4oxer
Slide 11
Slide 11 text
Smali Bytecode
@h4oxer
Slide 12
Slide 12 text
Attack Surfaces
@h4oxer
Slide 13
Slide 13 text
Attack Models
• Reverse engineering offline & online
• App on device – without root
• App on device – with root
• Man-in-the-Middle
• Physical access to device
@h4oxer
Slide 14
Slide 14 text
Research Questions
• How does the app handle authentication?
• Access tokens, storage credentials, proper session management
• How does the app store user data?
• Cryptography, storage location, API keys, exposed content providers
• How does the app communicate?
• HTTP, HTTPS, Certificate pinning
• How does the app protect itself?
• Obfuscation, dynamic checks
@h4oxer
Slide 15
Slide 15 text
@h4oxer
Slide 16
Slide 16 text
Attacking Applications
Statically
@h4oxer
Slide 17
Slide 17 text
Reverse Engineering Process
@h4oxer
APK
.so
libraries
Classes.de
x
Resources Xml resources
unzip
AAPT
Java Source
Smali bytecode
dex2jar
Baksmali/smal
i
X86/ARM/x86-
64
Radare2/Hopper/IDAPro
Slide 18
Slide 18 text
$ aapt d xmltree file.apk AndroidManifest.xml
@h4oxer
Slide 19
Slide 19 text
$ aapt d xmltree file.apk AndroidManifest.xml
@h4oxer
Cryptography 101
• Symmetric Crypto = one key for encryption/decryption
• AES, 3DES, Blowfish, …
• Public-key Crypto = private and public key
• Encrypt with private key and decrypt with public key = digital
signature
• Encrypt with public key and decrypt with private key =
confidentiality
@h4oxer
Slide 24
Slide 24 text
Locate Cryptography
• Which crypto library is used?
• Android, BouncyCastle, SpongyCastle, etc.
• Does it use a hardcoded crypto key?
• Does it use broken crypto?
• MD5, RC4, AES in EBC, etc.
• Where does it store cryptography keys?
@h4oxer
Frida
• Dynamic instrumentation toolkit
• Debug a live process
• Loads JavaScript (JS) Engine
• Write, inject and execute JS script to interact
• Server (on phone) and client (on pc) code
@h4oxer
Slide 33
Slide 33 text
Frida – frida_client.py
@h4oxer
scriptname = sys.argv[1] # get script
fd = open(scriptname, "r") # open script
procname = sys.argv[2] # get process
# define callback function
def on_message(message, data):
print(message)
print(data)
#Connect to Frida on the phone
session = frida.get_usb_device().attach(procname)
#Create script
script = session.create_script(fd.read())
fd.close()
script.on('message', on_message)
script.load()
Slide 34
Slide 34 text
Frida – hooks.js
@h4oxer
Dalvik.perform(function () {
var WebView = Dalvik.use("android.webkit.WebView");
WebView.loadUrl.overload("java.lang.String").implementation = function (s) {
send(s.toString());
this.loadUrl.overload("java.lang.String").call(this, s);
};
});
$ python frida_client.py hooks.js com.example.webview
Slide 35
Slide 35 text
xPosed Framework
• Framework for hooking method calls
• Implemented as hooking modules
• Separate APK file that hooks into other running
applications
• Disadvantage: need to build APK for hooking
• xPosed bridge jar is injected in every application
@h4oxer
Slide 36
Slide 36 text
xPosed Framework
@h4oxer
public class Example implements IXposedHookLoadPackage {
public void handleLoadPackage(final LoadPackageParam lpparam) {
if (!lpparam.packageName.equals("com.example.webview"))
return;
findAndHookMethod("android.webkit.WebView”, lpparam.classLoader,
"loadUrl","java.lang.String", new XC_MethodHook()
{
@Override
protected void beforeHookedMethod(MethodHookParam param){
String url = (String) param.args[0];
XposedBridge.log("URL:"+ url);
}
@Override
protected void afterHookedMethod(MethodHookParam param) {
// this will be called after
}
});
}
}
Slide 37
Slide 37 text
SSL 101
• SSL Certificate = cryptographically secured piece of
information
• Certificates are issued by CAs
• Android device trusts a set of root Cas
• SSL Validation = check if certificate from server is trusted
@h4oxer
Slide 38
Slide 38 text
Problem
@h4oxer
Applicatio
n
MiTM Server
Identify? Identify?
Returns
Cert
Returns
MiTM Cert
Slide 39
Slide 39 text
MiTM Attack
• Allows you to test/discover/fuzz APIs
• Install trusted user certificate
• Trusted by all applications on < Android 6.0
• Install trusted system certificate
• User certificates are not trusted by all apps on Android 6.0+
• Requires root
@h4oxer
SafetyNet API
• Google’s opinion on device
• Result - JWT – JSON Web Token
• Result: compatible or not
• Can be cryptographically verified
• Good protection against dynamic attacks
@h4oxer
Slide 45
Slide 45 text
Key Take Aways
• Think about a secure design
• Apply a layered approach to security
• Make sure you hack you application first
@h4oxer