automated analysis and deobfuscation of android …jbremer.org/wp-posts/athcon.pdf · automated...

84
Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance Security Researcher June 10, 2013 June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 1 / 44

Upload: lythuy

Post on 27-May-2018

264 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Automated Analysis and Deobfuscation ofAndroid Apps & Malware

Jurriaan Bremer@skier t

Freelance Security Researcher

June 10, 2013

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 1 / 44

Page 2: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction

I Who am I?

I Student (University of Amsterdam)I Freelance Security ResearcherI Cuckoo Sandbox Developer (Malware Analysis System)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 2 / 44

Page 3: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction

I Who am I?I Student (University of Amsterdam)I Freelance Security ResearcherI Cuckoo Sandbox Developer (Malware Analysis System)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 2 / 44

Page 4: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction

Android?

I Smartphones

I Runs custom Linux

I Millions of Devices

I Hundreds of thousands of applications

I etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 3 / 44

Page 5: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction

Android?

I Smartphones

I Runs custom Linux

I Millions of Devices

I Hundreds of thousands of applications

I etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 3 / 44

Page 6: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Android Applications

Android Applications?

I Application Package File (APK)I Download from Google PlayI Zip fileI Some Metadata (Manifest, Images, ..)I classes.dex

I All your code are belong to classes.dexI More on this later.

I ResourcesI ImagesI Data filesI Native libraries

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 4 / 44

Page 7: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Android Applications

Android Applications?

I Application Package File (APK)I Download from Google PlayI Zip fileI Some Metadata (Manifest, Images, ..)I classes.dex

I All your code are belong to classes.dexI More on this later.

I ResourcesI ImagesI Data filesI Native libraries

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 4 / 44

Page 8: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Android Applications

Android Applications?

I Application Package File (APK)I Download from Google PlayI Zip fileI Some Metadata (Manifest, Images, ..)I classes.dex

I All your code are belong to classes.dexI More on this later.

I ResourcesI ImagesI Data filesI Native libraries

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 4 / 44

Page 9: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Android Applications

Android Applications?

I Application Package File (APK)I Download from Google PlayI Zip fileI Some Metadata (Manifest, Images, ..)I classes.dex

I All your code are belong to classes.dexI More on this later.

I ResourcesI ImagesI Data filesI Native libraries

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 4 / 44

Page 10: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Running Code on Android

There are two ways.

I Running native librariesI Extremely awesomeI This talk does not focus on native

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 5 / 44

Page 11: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Running Code on Android

There are two ways.

I Running native librariesI Extremely awesomeI This talk does not focus on native

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 5 / 44

Page 12: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Running Code on Android

There are two ways.

I Running native librariesI Extremely awesomeI This talk does not focus on native

I Running Dalvik BytecodeI Dalvik is Compiled JavaI Dalvik != JavaI classes.dexI (More on this later)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 6 / 44

Page 13: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dex File Format (I)

I Dalvik Executable FormatI classes.dex

I Container format to store Dalvik Bytecode with Metadata

I Various Data PoolsI Strings ”Hello World”I Classes Ljava/lang/String;I Fields Ljava/lang/String;->valueI Prototypes (I)Ljava/lang/String;

I Lots of headersI Complex Cross-references between fields and headersI The Classname is a StringI A Prototype has a String as return valueI A method links to a Prototype, etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 7 / 44

Page 14: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dex File Format (I)

I Dalvik Executable FormatI classes.dex

I Container format to store Dalvik Bytecode with MetadataI Various Data Pools

I Strings ”Hello World”I Classes Ljava/lang/String;I Fields Ljava/lang/String;->valueI Prototypes (I)Ljava/lang/String;

I Lots of headersI Complex Cross-references between fields and headersI The Classname is a StringI A Prototype has a String as return valueI A method links to a Prototype, etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 7 / 44

Page 15: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dex File Format (I)

I Dalvik Executable FormatI classes.dex

I Container format to store Dalvik Bytecode with MetadataI Various Data Pools

I Strings ”Hello World”I Classes Ljava/lang/String;I Fields Ljava/lang/String;->valueI Prototypes (I)Ljava/lang/String;

I Lots of headersI Complex Cross-references between fields and headersI The Classname is a StringI A Prototype has a String as return valueI A method links to a Prototype, etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 7 / 44

Page 16: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dex File Format (II)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 8 / 44

Page 17: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dalvik Bytecode Example

public static void hello() {System.out.println(”Hello AthCon”);

}

->

sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;const-string v1, ”Hello AthCon”invoke-virtual v0, v1,

Ljava/io/PrintStream;->println(Ljava/lang/String;)Vreturn-void

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 9 / 44

Page 18: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dalvik Bytecode Example

public static void hello() {System.out.println(”Hello AthCon”);

}

->

sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;

const-string v1, ”Hello AthCon”invoke-virtual v0, v1,

Ljava/io/PrintStream;->println(Ljava/lang/String;)Vreturn-void

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 9 / 44

Page 19: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dalvik Bytecode Example

public static void hello() {System.out.println(”Hello AthCon”);

}

->

sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;const-string v1, ”Hello AthCon”

invoke-virtual v0, v1,Ljava/io/PrintStream;->println(Ljava/lang/String;)V

return-void

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 9 / 44

Page 20: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dalvik Bytecode Example

public static void hello() {System.out.println(”Hello AthCon”);

}

->

sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;const-string v1, ”Hello AthCon”invoke-virtual v0, v1,

Ljava/io/PrintStream;->println(Ljava/lang/String;)V

return-void

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 9 / 44

Page 21: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dalvik Bytecode Example

public static void hello() {System.out.println(”Hello AthCon”);

}

->

sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;const-string v1, ”Hello AthCon”invoke-virtual v0, v1,

Ljava/io/PrintStream;->println(Ljava/lang/String;)Vreturn-void

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 9 / 44

Page 22: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

What’s your point?

I Decompiling is mostly trivial

I JEB - http://android-decompiler.com/

I Smali/Baksmali allows you to quickly modify code

I Based on .smali files, a wrapper around Dalvik bytecode

I Free and Open Sourcehttps://code.google.com/p/smali/

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 10 / 44

Page 23: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

What’s your point?

I Decompiling is mostly trivial

I JEB - http://android-decompiler.com/

I Smali/Baksmali allows you to quickly modify code

I Based on .smali files, a wrapper around Dalvik bytecode

I Free and Open Sourcehttps://code.google.com/p/smali/

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 10 / 44

Page 24: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Let’s welcome Obfuscators

I Commercial solutions

I Make Reverse Engineering harder

I Make automated analysis harder (what to look at?)

I What can we do..?

I Deobfuscate the obfuscated code!

But first..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 11 / 44

Page 25: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Let’s welcome Obfuscators

I Commercial solutions

I Make Reverse Engineering harder

I Make automated analysis harder (what to look at?)

I What can we do..?

I Deobfuscate the obfuscated code!

But first..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 11 / 44

Page 26: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Let’s welcome Obfuscators

I Commercial solutions

I Make Reverse Engineering harder

I Make automated analysis harder (what to look at?)

I What can we do..?

I Deobfuscate the obfuscated code!

But first..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 11 / 44

Page 27: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Let’s welcome Obfuscators

I Commercial solutions

I Make Reverse Engineering harder

I Make automated analysis harder (what to look at?)

I What can we do..?

I Deobfuscate the obfuscated code!

But first..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 11 / 44

Page 28: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction to Our Tools

readdex(1)

I Custom utility to read .dex files

I Not very strict

I Works in cases where traditional tools fail

I E.g., dexdump, dex2jar, sometimes even JEB

I (Will report JEB bugs later)

I Handles the following cases correctlyI Invalid checksum hashes (fails dexdump)I Unused opcodes (fails dex2jar/dexdump)I Invalid Data Pool Indices (dexdump/dex2jar)I Unicode function names (IDA Pro?!)I Etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 12 / 44

Page 29: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction to Our Tools

readdex(1)

I Custom utility to read .dex files

I Not very strict

I Works in cases where traditional tools fail

I E.g., dexdump, dex2jar, sometimes even JEB

I (Will report JEB bugs later)I Handles the following cases correctly

I Invalid checksum hashes (fails dexdump)I Unused opcodes (fails dex2jar/dexdump)I Invalid Data Pool Indices (dexdump/dex2jar)I Unicode function names (IDA Pro?!)I Etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 12 / 44

Page 30: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction to Our Libraries

I Dalvik Disassembler

I Basic Dalvik EmulatorI Supports most Dalvik InstructionsI Supports simple Java Classes (Strings, etc.)

I Dex File ParserI Dex File Creator is Work in Progress

I Totalling more than 5kloc C (including readdex)

I Not to mention basic Python wrappers

I All of it will be Open Source soon (TM)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 13 / 44

Page 31: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction to Our Libraries

I Dalvik DisassemblerI Basic Dalvik Emulator

I Supports most Dalvik InstructionsI Supports simple Java Classes (Strings, etc.)

I Dex File ParserI Dex File Creator is Work in Progress

I Totalling more than 5kloc C (including readdex)

I Not to mention basic Python wrappers

I All of it will be Open Source soon (TM)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 13 / 44

Page 32: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction to Our Libraries

I Dalvik DisassemblerI Basic Dalvik Emulator

I Supports most Dalvik InstructionsI Supports simple Java Classes (Strings, etc.)

I Dex File ParserI Dex File Creator is Work in Progress

I Totalling more than 5kloc C (including readdex)

I Not to mention basic Python wrappers

I All of it will be Open Source soon (TM)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 13 / 44

Page 33: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction to Our Libraries

I Dalvik DisassemblerI Basic Dalvik Emulator

I Supports most Dalvik InstructionsI Supports simple Java Classes (Strings, etc.)

I Dex File ParserI Dex File Creator is Work in Progress

I Totalling more than 5kloc C (including readdex)

I Not to mention basic Python wrappers

I All of it will be Open Source soon (TM)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 13 / 44

Page 34: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction to Our Libraries

I Dalvik DisassemblerI Basic Dalvik Emulator

I Supports most Dalvik InstructionsI Supports simple Java Classes (Strings, etc.)

I Dex File ParserI Dex File Creator is Work in Progress

I Totalling more than 5kloc C (including readdex)

I Not to mention basic Python wrappers

I All of it will be Open Source soon (TM)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 13 / 44

Page 35: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Introduction to Our Libraries

I Dalvik DisassemblerI Basic Dalvik Emulator

I Supports most Dalvik InstructionsI Supports simple Java Classes (Strings, etc.)

I Dex File ParserI Dex File Creator is Work in Progress

I Totalling more than 5kloc C (including readdex)

I Not to mention basic Python wrappers

I All of it will be Open Source soon (TM)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 13 / 44

Page 36: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

What’s next? This stuff is actually useful?

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 14 / 44

Page 37: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

What’s next? This stuff is actually useful?

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 14 / 44

Page 38: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Class & Function Name Obfuscation

Used by for example Dexguard & Freedom.apk..

Welcome to China..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 15 / 44

Page 39: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Class & Function Name Obfuscation

Used by for example Dexguard & Freedom.apk..Welcome to China..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 15 / 44

Page 40: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Class & Function Name Obfuscation

Used by for example Dexguard & Freedom.apk..Welcome to China..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 15 / 44

Page 41: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Class & Function Name Obfuscation

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 16 / 44

Page 42: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

China?

I Unreadable identifiers

I Problematic when Modifying Dalvik Code (.smali)

I unchina.py to the rescue!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 17 / 44

Page 43: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

China?

I Unreadable identifiers

I Problematic when Modifying Dalvik Code (.smali)

I unchina.py to the rescue!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 17 / 44

Page 44: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

unchina.py

I Walks the Dex file

I Enumerates all classes and methods

I Renames Chinese names with something readable

I ”zmagic ” + number

I (For now, can be changed of course..)

I Simple Python script using some hacky functionality

I Rewrites parts of the Dex file as needed

I Writes a new Dex file (still kind of experimental)

I Sounds easier than it is!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 18 / 44

Page 45: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

unchina.py

I Walks the Dex file

I Enumerates all classes and methods

I Renames Chinese names with something readable

I ”zmagic ” + number

I (For now, can be changed of course..)

I Simple Python script using some hacky functionality

I Rewrites parts of the Dex file as needed

I Writes a new Dex file (still kind of experimental)

I Sounds easier than it is!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 18 / 44

Page 46: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

unchina.py Demo

Demo of Unchina.py..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 19 / 44

Page 47: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Obfuscated Strings (I)

Used by for example Dexguard, Whatsapp.apk, Freedom.apk

I Instead of using Hardcoded Strings

I Build strings up at runtimeI Makes it harder to analyze

I Strings usually have meaningful informationI (Function names, Debug information, URLs, etc.)

I More code in the binaryI Normally one stringI Now entire functions for decoding, function calls, etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 20 / 44

Page 48: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Obfuscated Strings (I)

Used by for example Dexguard, Whatsapp.apk, Freedom.apk

I Instead of using Hardcoded Strings

I Build strings up at runtime

I Makes it harder to analyzeI Strings usually have meaningful informationI (Function names, Debug information, URLs, etc.)

I More code in the binaryI Normally one stringI Now entire functions for decoding, function calls, etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 20 / 44

Page 49: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Obfuscated Strings (I)

Used by for example Dexguard, Whatsapp.apk, Freedom.apk

I Instead of using Hardcoded Strings

I Build strings up at runtimeI Makes it harder to analyze

I Strings usually have meaningful informationI (Function names, Debug information, URLs, etc.)

I More code in the binaryI Normally one stringI Now entire functions for decoding, function calls, etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 20 / 44

Page 50: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Obfuscated Strings (I)

Used by for example Dexguard, Whatsapp.apk, Freedom.apk

I Instead of using Hardcoded Strings

I Build strings up at runtimeI Makes it harder to analyze

I Strings usually have meaningful informationI (Function names, Debug information, URLs, etc.)

I More code in the binaryI Normally one stringI Now entire functions for decoding, function calls, etc..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 20 / 44

Page 51: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Obfuscated Strings (II)

We want to reconstruct the obfuscated strings

I Use our Simple Dalvik Emulator

I Combined with some heuristics (in the future)

I For now a bit hardcoded..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 21 / 44

Page 52: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Obfuscated Strings (II)

We want to reconstruct the obfuscated strings

I Use our Simple Dalvik Emulator

I Combined with some heuristics (in the future)

I For now a bit hardcoded..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 21 / 44

Page 53: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Three different String Obfuscation examples

I Whatsapp.apk

I Freedom.apk

I A Dexguarded binary

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 22 / 44

Page 54: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Whatsapp (I)

#1 - Whatsapp.apk

I Defines <clinit>for lots of classesI Class Initialization functionI Called when the class is being loaded

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 23 / 44

Page 55: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Whatsapp (II)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 24 / 44

Page 56: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Whatsapp (III)

I We emulate the methodI Intercept the sput-object instruction

I sput-object v0, mb->z:Ljava/lang/String;

I ”Assign Static Class Variable”

I We now have the deobfuscated string

I (or multiple strings, in some cases)

I Roughly 5000 strings deobfuscated!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 25 / 44

Page 57: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Whatsapp (III)

I We emulate the methodI Intercept the sput-object instruction

I sput-object v0, mb->z:Ljava/lang/String;

I ”Assign Static Class Variable”

I We now have the deobfuscated string

I (or multiple strings, in some cases)

I Roughly 5000 strings deobfuscated!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 25 / 44

Page 58: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Whatsapp (III)

I We emulate the methodI Intercept the sput-object instruction

I sput-object v0, mb->z:Ljava/lang/String;

I ”Assign Static Class Variable”

I We now have the deobfuscated string

I (or multiple strings, in some cases)

I Roughly 5000 strings deobfuscated!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 25 / 44

Page 59: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Freedom (I)

#2 - Freedom.apk

I Has xor decryption methods

I Calls functions with magic decoding value

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 26 / 44

Page 60: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Freedom (II)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 27 / 44

Page 61: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Freedom (III)

I The xor decryption methods have a specific signature

I Their prototype is always (B)Ljava/lang/String;

I (Accepts an 8bit integer, returns a String.)

I We scan every method in the Dex file

I Function Call to Decryption Method ->Decrypt the String

I Roughly 600 strings deobfuscated!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 28 / 44

Page 62: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Freedom (III)

I The xor decryption methods have a specific signature

I Their prototype is always (B)Ljava/lang/String;

I (Accepts an 8bit integer, returns a String.)

I We scan every method in the Dex file

I Function Call to Decryption Method ->Decrypt the String

I Roughly 600 strings deobfuscated!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 28 / 44

Page 63: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Freedom (III)

I The xor decryption methods have a specific signature

I Their prototype is always (B)Ljava/lang/String;

I (Accepts an 8bit integer, returns a String.)

I We scan every method in the Dex file

I Function Call to Decryption Method ->Decrypt the String

I Roughly 600 strings deobfuscated!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 28 / 44

Page 64: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dexguard (I)

#3 - Dexguard is a Commercial ObfuscatorAs example we use an obfuscated Cyanide.apk

I Root exploit for some Motorala device

I (Thanks to Justin Case for the sample)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 29 / 44

Page 65: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dexguard (II)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 30 / 44

Page 66: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dexguard (III)

I Dexguard initializes a lookup table on <clinit >

I Decrypts strings using this lookup table

I One dedicated decryption method

I Signature (III)Ljava/lang/String;

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 31 / 44

Page 67: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dexguard (IV)

I Dexguard is a combination of Whatsapp and Freedom

I (With regards to techniques)

I First emulate <clinit >

I To obtain the lookup table

I Then scan every method in the Dex file

I Find function calls to the decryption method

I Decrypt strings!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 32 / 44

Page 68: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dexguard (IV)

I Dexguard is a combination of Whatsapp and Freedom

I (With regards to techniques)

I First emulate <clinit >

I To obtain the lookup table

I Then scan every method in the Dex file

I Find function calls to the decryption method

I Decrypt strings!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 32 / 44

Page 69: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Dexguard (IV)

Original Dexguarded Cyanide.apk

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 33 / 44

Page 70: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Rewriting the Dex file (I)

Rewriting Whatsapp, Freedom and Dexguarded Cyanide.apk

I We have the decrypted strings

I Obfuscated code always takes more instructions thandeobfuscated code

I Patching time..!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 34 / 44

Page 71: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Rewriting the Dex file (II)

Some problems..

I We have to introduce new stringsI Extend the String Data PoolI Shuffle around half the Dex..

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 35 / 44

Page 72: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Rewriting the Dex file (II)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 36 / 44

Page 73: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Rewriting the Dex file (III)

Some problems..

I We have to introduce new stringsI Extend the String Data PoolI Shuffle around half the Dex..

I Patch Dalvik instructions (straightforward)I Remove obsolete functions

I String Decryption Methods are now unusedI Quite painful.. Dex file-wiseI *Work in Progress*

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 37 / 44

Page 74: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Rewriting the Dex file (III)

Some problems..

I We have to introduce new stringsI Extend the String Data PoolI Shuffle around half the Dex..

I Patch Dalvik instructions (straightforward)I Remove obsolete functions

I String Decryption Methods are now unusedI Quite painful.. Dex file-wiseI *Work in Progress*

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 37 / 44

Page 75: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Rewriting the Dex file (IV)

I We move all strings to EOF

I We fixup other data structures

I Demo time

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 38 / 44

Page 76: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Rewriting the Dex file (V)

Demo of reconstructing Dexguarded Cyanide.apk

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 39 / 44

Page 77: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

How do we go from here?

I Generic DeobfuscationI Based on Heuristics with Prototypes etc

I Classification based on stripped down binariesI One binary can have many obfuscated representationsI Deobfuscate to something like the original binaryI Allows more accurate classification

I Did I mention plaintext strings?

I Plaintext Strings!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 40 / 44

Page 78: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

How do we go from here?

I Generic DeobfuscationI Based on Heuristics with Prototypes etc

I Classification based on stripped down binariesI One binary can have many obfuscated representationsI Deobfuscate to something like the original binaryI Allows more accurate classification

I Did I mention plaintext strings?

I Plaintext Strings!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 40 / 44

Page 79: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

How do we go from here?

I Generic DeobfuscationI Based on Heuristics with Prototypes etc

I Classification based on stripped down binariesI One binary can have many obfuscated representationsI Deobfuscate to something like the original binaryI Allows more accurate classification

I Did I mention plaintext strings?

I Plaintext Strings!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 40 / 44

Page 80: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

How do we go from here?

I Generic DeobfuscationI Based on Heuristics with Prototypes etc

I Classification based on stripped down binariesI One binary can have many obfuscated representationsI Deobfuscate to something like the original binaryI Allows more accurate classification

I Did I mention plaintext strings?

I Plaintext Strings!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 40 / 44

Page 81: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Automated Malware Analysis!

Yesterday a new malware was found in the wild..

http://www.securelist.com/en/blog/8106/The_most_

sophisticated_Android_Trojan

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 41 / 44

Page 82: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

High Expectations Asian Dad strikes again!

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 42 / 44

Page 83: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Backdoor.AndroidOS.Obad.a

I Seems like a pretty advanced android malwareI Multiple obfuscation layers (for strings)

I Got a start, but far from complete..I *Quick Demo*

I Some Plaintext Strings..I Tries to enable BluetoothI getSimSerialNumberI ..I (I need some more time)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 43 / 44

Page 84: Automated Analysis and Deobfuscation of Android …jbremer.org/wp-posts/athcon.pdf · Automated Analysis and Deobfuscation of Android Apps & Malware Jurriaan Bremer @skier t Freelance

Questions?

Any questions?

Cheers to..p1ra, nex‘, rep, blasty, thuxnder, diff-, jcase, George, jduck, ..

Interested in Android Security?Join #droidsec on irc.freenode.org (thanks jduck!)

June 10, 2013 Jurriaan Bremer @skier t Analysis and Deobfuscation of Android Apps 44 / 44