penTest HackFest AndroidCodeInspection

Presented by: Christopher Crowley

Presentation
Resource Thumbnail

The "PenTest HackFest AndroidCodeInspection" focuses on the technical concepts of Android application assessment, mobile threats, and code analysis. It emphasizes the importance of penetration testing to identify vulnerabilities and weaknesses in Android applications, utilizing tools like FindBugs and Eclipse for static and behavioral code analysis. The segment also covers APK disassembly, customization of FindBugs, and identifying suspect methods in Java code, highlighting the need for downloading and analyzing APK files to inspect the code and identify potential security issues, with a focus on preventing data theft and financial loss from malware and other threats.

Full Transcript / Content

--- Page 1 --- Android Application Assessment © 2013 Christopher Crowley 1 Android Application Assessment Inspecting Android Applications for Known Issues --- Page 2 --- Android Application Assessment © 2013 Christopher Crowley 2 Mobile - Ubiquitous - Present new challenges to organizations - Challenge for Attackers (and the pen testers who emulate them) – Almost purely “client side” attack surface - Opportunities for Attackers – Data and service theft opportunity outside the traditional perimeter --- Page 3 --- Android Application Assessment © 2013 Christopher Crowley 3 Android (1) - Serious mobile malware problem – “Android is responsible for 92% of all known mobile malware…” - Juniper Networks Third Annual Mobile Threats Report - March 2012 through March 2013 - Android Update Problem – 4% of deployed Android devices have latest OS - Majority of devices in the World are Android – “Platform-wise, Android grew the fastest during the quarter, by 79% year-on-year. It powered 190 million, or 80%, of the smart phones shipped in Q2 [2013].” --- Page 4 --- Android Application Assessment © 2013 Christopher Crowley 4 Android (2) - Questionable apps use the same behaviors as malware – “FireEye researchers have discovered a rapidly- growing class of mobile threats represented by a popular ad library affecting apps with over 200 million downloads in total. This ad library, anonymized as “Vulna,” is aggressive at collecting sensitive data and is able to perform dangerous operations such as downloading and running new components on demand.” --- Page 5 --- Android Application Assessment © 2013 Christopher Crowley 5 Android (3) - The methodology I will talk about is applicable to apps on any other mobile platform: iOS, Windows Phone, Blackberry… - The tools will be different, though - Android uses Dalvik Executable Apps – Bytecode compiled Java – Not Oracle™ Java Runtime – Open Handset Alliance Dalvik Runtime --- Page 6 --- Android Application Assessment © 2013 Christopher Crowley 6 Threats - Primary threat to Android devices is mobile malware – SMS premium rate messages cost people money – Private Data Theft - Pen test objective is to model the threats associated with running the apps - Non-malware threats use the same methods as malware --- Page 7 --- Android Application Assessment © 2013 Christopher Crowley 7 Threat Characteristics - Financial Theft - Privilege Escalation - Command and Control - Code updates without user awareness - Data Exfiltration - Code Obfuscation - Anti-security and Anti-awareness code --- Page 8 --- Android Application Assessment © 2013 Christopher Crowley 8 Assessing Mobile Apps (1) - Risk from mobile applications – Low – app poses little or no threat to device and data on device – Moderate – app poses some threat to device protective measures and data stored on device – High – app is likely to circumvent device protective measures and/or exfiltrate private data - All address book data to a server - Intent with poor filtering --- Page 9 --- Android Application Assessment © 2013 Christopher Crowley 9 Assessing Mobile Apps (2) - Checklist of actions to perform for mobile app assessment - Checklists are effective in this context - Helps to build in house knowledge for ongoing assessment, once the org starts to assess apps, you are responsible to do so for evermore… --- Page 10 --- Android Application Assessment © 2013 Christopher Crowley 10 Checklist - Checklists are not sexy (at all) - But checklists have proven to be extremely effective at preventing common mistakes – Pre-flight checklist “On the other hand, other entries (read: FUEL QUANTITY-- VISUALLY EXAMINE) have literally hundreds upon hundreds of examples in the NTSB database.” – Medical Checklist “…the checklist has evolved from being perceived as an assault on the practitioners' integrity to being welcomed as an important tool in reducing complications and preventing medical errors.” --- Page 11 --- Android Application Assessment © 2013 Christopher Crowley 11 Two Types of Assessments - Behavioral – Use a sandbox environment – Watch file access, network traffic, SMS messages, Calls – Easier when dealing with obfuscated code – App may not misbehave while you’re watching - Static Code Analysis – More challenging – More thorough (if done properly) – Can detect latent behaviors, long running timers and triggered events --- Page 12 --- Android Application Assessment © 2013 Christopher Crowley 12 Android Behavioral Analysis - 1 - Android Behavioral Analysis – Install APK and run it - Need devices or virtual devices and instrumented network to capture traffic – Track - Read & write file system access - Network traffic – Servers contacted – Data sent – Code downloaded --- Page 13 --- Android Application Assessment © 2013 Christopher Crowley 13 Automated Behavioral Analysis - 1 - Android Behavioral Analysis – DroidBox – Has switched to APIMonitor to overcome latency of TaintDroid updates – Reporting still sucks (just text), but information provided is very good – APIMonitor doesn’t adequately deal with all code, and is still Beta --- Page 14 --- Android Application Assessment © 2013 Christopher Crowley 14 Automated Behavioral Analysis - 2 - Anubis – Upload .apk File – Report with actions – static & behavioral - Permissions requested & used - Potentially malicious functions - Files accessed - Network Traffic – Anubis App to automate upload http://anubis.iseclab.org/ --- Page 15 --- Android Application Assessment © 2013 Christopher Crowley 15 Android Static Analysis - 1 - Android Static Analysis – Applications for Android come in the form of Android Packages (APK) – APKs contain a Dalvik Executable (DEX), a Manifest, and other files – DEX is bit code compiled Java code - We can use two different approaches to extract Java code from DEX files --- Page 16 --- Android Application Assessment © 2013 Christopher Crowley 16 Android Static Analysis - 2 - Android Static Analysis – This is a challenging thing to do! – There are some tools that are available to help with automation – Look at a few of these, then dig into a manual method --- Page 17 --- Android Application Assessment © 2013 Christopher Crowley 17 Automated Static Analysis - 1 - Androwarn – Local static assessment – Produces HTML report of potentially sensitive actions based on the function calls present in the code – Available from: https://github.com/maaaaz/androwarn --- Page 18 --- Android Application Assessment © 2013 Christopher Crowley 18 Automated Static Analysis - 2 - Mobile Sandbox – Upload File – Produces HTML report online with potentially sensitive actions based on the function calls present in the code - Permissions requested & used - Potentially malicious functions - Intents --- Page 19 --- Android Application Assessment © 2013 Christopher Crowley 19 Automated Static Analysis - 3 - Badger Application Analysis http://davidson-www.cs.wisc.edu/baa/ – Upload File – App available to automate upload – Online report also gallery of previously uploaded apps - Permissions requested - Potentially malicious functions - Ad libraries utilized --- Page 20 --- Android Application Assessment © 2013 Christopher Crowley 20 Automated Analysis - 4 - Stowaway – Online assessment, requires upload of the apk file to the server at http://www.android- permissions.org/ – Gives a listing of the permissions requested, and if the permissions are actually used – Currently offline, unknown return date --- Page 21 --- Android Application Assessment © 2013 Christopher Crowley 21 Manual Analysis - 1 - BOOT (Boot Completed) – BOOT COMPLETED SMS - (SMS/MMS) SMS RECEIVED – WAP PUSH RECEIVED - NET (Network) – CONNECTIVITY CHANGE – PICK WIFI WORK – CALL - (Phone Events) PHONE STATE – NEW OUTGOING CALL - USB (USB Storage) – UMS CONNECTED – UMS DISCONNECTED - MAIN (Main Activity) – ACTION MAIN - PKG (Package) – PACKAGE ADDED – PACKAGE REMOVED – PACKAGE CHANGED – PACKAGE REPLACED – PACKAGE RESTARTED – PACKAGE INSTALL - BATT (Power/Battery) – ACTION POWER CONNECTED – ACTION POWER DISCONNECTED – BATTERY LOW – BATTERY OKAY – BATTERY CHANGED ACTION - SYS (System Events) – USER PRESENT – INPUT METHOD CHANGED – SIG STR – SIM FULL - System calls we’re looking for --- Page 22 --- Android Application Assessment © 2013 Christopher Crowley 22 Manual Analysis - 2 - Manual code inspection provides the most thorough assessment - Requires the most skill - Objective is to help you develop a methodology for quickly scouring code for known suspicious functions - Could use Eclipse + FindBugs™ - Could decompile and do string search --- Page 23 --- Android Application Assessment © 2013 Christopher Crowley 23 Eclipse - Eclipse is a development environment - Provides opportunity for code review – But this is manual – Need to know java fairly well to know what to look for – We can help resolve this with FindBugs™ --- Page 24 --- Android Application Assessment © 2013 Christopher Crowley 24 FindBugs™ - FindBugs™ is a suite of checks for known programming flaws - It is intended to help programmers to avoid known, common errors - We are going to adapt it to our purpose of looking for code that is suspicious and warrants review --- Page 25 --- Android Application Assessment © 2013 Christopher Crowley 25 Install Eclipse - http://www.eclipse.org/downloads - Download eclipse classic - Install default – Next, next, finish ;-) --- Page 26 --- Android Application Assessment © 2013 Christopher Crowley 26 Install FindBugs™ - 1 - Eclipse: Help->“Install New Software…” --- Page 27 --- Android Application Assessment © 2013 Christopher Crowley 27 Install FindBugs™ - 2 - Click Add… - Enter “FindBugs update site” (no quotes) for the name - Enter “http://findbugs.cs.u md.edu/eclipse” (no quotes) as the Location - Click “OK” --- Page 28 --- Android Application Assessment © 2013 Christopher Crowley 28 Install FindBugs™ - 3 - Click “Select All” - Click “Next” --- Page 29 --- Android Application Assessment © 2013 Christopher Crowley 29 Install FindBugs™ - 4 - Accept license - Ugh. Accept the warning --- Page 30 --- Android Application Assessment © 2013 Christopher Crowley 30 Sidebar – Android Package downloads - Where can you get the code to assess? – Download .apk file from internet – Move application to removable sdcard, then remove sdcard and copy – Copy .apk file from rooted android device - Malware – http://contagiominidump.blogspot.com is a repository of known android malware - Good material to practice with, but be careful with malware! - See me for password scheme --- Page 31 --- Android Application Assessment © 2013 Christopher Crowley 31 Dalvik to Java - 1 - Now we need to have java code to import into eclipse - We will use the APK files to inspect the code. - There are two main ways to do this – Apktool/smali – Dex2jar and Procyon --- Page 32 --- Android Application Assessment © 2013 Christopher Crowley 32 Dalvik to Java - 2 - Use apktool/smali to get executable, but difficult to read java code - Use dex2jar and Procyon to get readable, but non-executable java code - You will end up doing both - You want this code to view for matching --- Page 33 --- Android Application Assessment © 2013 Christopher Crowley 33 APK disassembly – smali - Smali and apktool method - Results in code that can be modified and recompiled Apktool d Package.apk (edit class files if you like) Apktool r Package.apk --- Page 34 --- Android Application Assessment © 2013 Christopher Crowley 34 APK disassembly – Procyon - Procyon method – can be used on class files or jar files /home/bin/dex2jar/dex2jar.sh the_app.apk java -jar procyon_decompiler.jar /path/to/the_app_dex2jar.jar Import Code into Eclipse (instructions follow) or use grep --- Page 35 --- Android Application Assessment © 2013 Christopher Crowley 35 Quick and Dirty Search - At this point, with Java files, do a grep for permissions and function calls of interest – Rudimentary, subject to evasion, but a good litmus test unzip the_app.apk java –jar /path/to/AXMLprinter2.jar AndroidManifest.xml > AndroidManifest.txt more AndroidManifest.txt ## review perms cd /path/to/procyon_output_dir grep –Rf file_of_suspect_functions ./ --- Page 36 --- Android Application Assessment © 2013 Christopher Crowley 36 Quick and Dirty Search Example - Use of an Intent grep –Ri “putextra.*android\.intent” /path/to/procyon_output_dir/* --- Page 37 --- Android Application Assessment © 2013 Christopher Crowley 37 Import Code to Eclipse for FindBugs - 1) Create new project --- Page 38 --- Android Application Assessment © 2013 Christopher Crowley 38 Import Code to Eclipse - 2) Create new project --- Page 39 --- Android Application Assessment © 2013 Christopher Crowley 39 Import Code to Eclipse - 3) Create new project --- Page 40 --- Android Application Assessment © 2013 Christopher Crowley 40 Import Code to Eclipse - 4) Create new project --- Page 41 --- Android Application Assessment © 2013 Christopher Crowley 41 Import Code to Eclipse - 1) Import external archive --- Page 42 --- Android Application Assessment © 2013 Christopher Crowley 42 Customize FindBugs - I’m not a java programmer - It is actually easier in grep - This is opportunity for sharing checks Edit java file with patterns and compile Update findbugs.xml and messages.xml Build .jar file Incorporate into findbugs --- Page 43 --- Android Application Assessment © 2013 Christopher Crowley 43 Edit Java and Compile - 1 - I worked within the findbugs-contrib framework, so I can eventually contribute to that - I emulated the “MoreDumbMethods” code, identifying offending methods, creating “AndroidSuspectMethods” - Not yet ready to contrib - Needs more work in detection and more samples --- Page 44 --- Android Application Assessment © 2013 Christopher Crowley 44 Edit Java and Compile - 2 - Compiled .java into .class javac -classpath /home/cc/.eclipse/org.eclipse.platform_3.7.0_793567567 /plugins/edu.umd.cs.findbugs.plugin.eclipse_2.0.2.20121 210/lib/annotations.jar:/home/cc/.eclipse/org.eclipse.plat form_3.7.0_793567567/plugins/edu.umd.cs.findbugs.plu gin.eclipse_2.0.2.20121210/lib/asm- 3.3.jar:/home/cc/.eclipse/org.eclipse.platform_3.7.0_793 567567/plugins/edu.umd.cs.findbugs.plugin.eclipse_2.0.2 .20121210/lib/asm-commons-3.3.jar [classpath_trimmed] AndroidSuspectMethods.java --- Page 45 --- Android Application Assessment © 2013 Christopher Crowley 45 Update FindBugs Contrib files - Copy new file .class into detect sub-directory - Update findbugs.xml – Definition of the detector class – Definition of the detector pattern - messages.xml – Details of the detector classes - Create new jar file – jar –cvf ../fb-contrib_updated.jar ./* - Copy jar file into eclipse plugin/ directory - Install additional plugin in eclipse --- Page 46 --- Android Application Assessment © 2013 Christopher Crowley 46 Assure Checks are Enabled --- Page 47 --- Android Application Assessment © 2013 Christopher Crowley 47 Run FindBugs --- Page 48 --- Android Application Assessment © 2013 Christopher Crowley 48 FindBugs Results --- Page 49 --- Android Application Assessment © 2013 Christopher Crowley 49 Conclusion - App Assessment is a valuable task to protect individuals and organizations – Behavioral – Automated code analysis – Static code analysis - Android has substantial threats from malware - Malware exhibits all the bad behaviors, good for a pen tester to comprehend and emulate --- Page 50 --- Android Application Assessment © 2013 Christopher Crowley 50 Contact Info - chris@montance.com – LinkedIn / e-mail - +ChrisCrowley – G+ - @CCrowMontance – #Twitter - http://bit.ly/1epx2Tl – slide deck & checklist

Related Q&A

  • Q: What is the main topic of the Android Code Inspection presentation?
    A: Static and dynamic analysis of Android applications for security vulnerabilities.
  • Q: What tool is recommended for decompiling Android APKs?
    A: JADX (or dex2jar + JD-GUI).
  • Q: What is the 'AndroidManifest.xml' file?
    A: A critical file that defines the app's structure, permissions, and components.