Slide 1

Slide 1 text

Android hacking for dummies ‘if’ operator demolition Michael Pustovit Software Engineer @ Stanfy

Slide 2

Slide 2 text

#dfua WARNING #1 This presentation is for newbies in Android applications hacking. *If you are a pro or just know enough - pretend that you are interested in the topic. Please.

Slide 3

Slide 3 text

#dfua WARNING #2 Stay on the light side of the force! *Reverse engineering can be illegal.

Slide 4

Slide 4 text

#dfua Objectives Reverse Engineering methods APK build process Analysis tools Small demo Defence approaches

Slide 5

Slide 5 text

Reverse engineering What is this?

Slide 6

Slide 6 text

#dfua - extracting knowledge or design information from anything man-made - re-producing it based on the extracted information Reverse engineering

Slide 7

Slide 7 text

#dfua Reverse engineering aims Steal resources Analyze algorithms Repack ● Remove ads ● Piracy ● Add malware ● Algorithms ● API details ● Assets ● DB

Slide 8

Slide 8 text

#dfua Types of reverse engineering analysis Observation of information exchange Disassembly ● Logs ● Memory ● Storage ● Network ● get/view a program in raw machine language Decompilation ● get/view a program in high-level language

Slide 9

Slide 9 text

APK build process Only basic steps

Slide 10

Slide 10 text

#dfua Overview .java Files javac .class Files dx .dex file apkbuilder Resources .so APK jarsigner source aapt zipalign

Slide 11

Slide 11 text

#dfua APK structure Resources in binary form Executables in DEX form Manifest in binary form Str resources in bin form

Slide 12

Slide 12 text

#dfua Digital signature Certificate Jar manifest Signed hash list

Slide 13

Slide 13 text

#dfua .class - stack VM - registers VM VS .dex pop 20 pop 7 add 20, 7, result push result * result on a stack top mov r1, 20 mov r2, 7 add r1, r2, r3 * result in a register r3

Slide 14

Slide 14 text

#dfua .class - stack VM - registers VM VS .dex - a single file per java class - all java classes in a single file - better compression (because of single file)

Slide 15

Slide 15 text

Analysis tools Hacker toys!

Slide 16

Slide 16 text

#dfua Analysis tools Decompilers dex2jar apktools ● Smali/backsmali ● Resources unpacker ● Manifest unpacker ● “Unpack” - “Modify” - “Pack” scenario ● Transforms .dex back to set of .class files ● Transforms .class files back to java code

Slide 17

Slide 17 text

#dfua Smali/backsmali - smali/baksmali = assembler/disassembler (in Icelandic) - syntax is loosely based on Jasmin's/dedexer's syntax - supports the full functionality of the dex format

Slide 18

Slide 18 text

#dfua Smali format overview public static int sum( final int a, final int b) { return a + b; } java smali .method public static sum(II)I .locals 1 .param p0, "a" # I .param p1, "b" # I .prologue .line 8 add-int v0, p0, p1 return v0 .end method

Slide 19

Slide 19 text

#dfua public static int sum( final int a, final int b) { return a + b; } .method public static sum(II)I .locals 1 .param p0, "a" # I .param p1, "b" # I .prologue .line 8 add-int v0, p0, p1 return v0 .end method Local registers count Line num for debugger Main operation Method params java smali Smali format overview

Slide 20

Slide 20 text

#dfua Basic modification process Get APK > apktool d app.apk -o app Make modifications > apktool b app -o reapp.apk Sign & Install

Slide 21

Slide 21 text

Demo time!

Slide 22

Slide 22 text

Defense Armors?

Slide 23

Slide 23 text

#dfua Tool types Packers Obfuscators ● transform sources ● encode binaries

Slide 24

Slide 24 text

#dfua Obfuscation * Java compiler leaves names in bytecode Obfuscator ○ renames ○ shrink unused code/res ○ encodes names ○ adds junk-code ○ etc

Slide 25

Slide 25 text

#dfua Obfuscation position .java Files javac .class Files dx .dex file apkbuilder APK source Obfuscation

Slide 26

Slide 26 text

#dfua Code shrinking and name encoding +org |-+lampapos |-+crackmeapp |-BuildConfig.smali |-MainActivity$1.smali |-MainActivity.smali |-MainActivity_ViewBinder.smali |-MainActivity_ViewBinding.smali |-R$anim.smali |-R$attr.smali |-R$bool.smali |-R$color.smali |-R$dimen.smali |-R$drawable.smali |-R$id.smali |-R$integer.smali |-R$layout.smali |-R$mipmap.smali |-R$string.smali |-R$style.smali |-R$styleable.smali |-R.smali +a |-+a |-+a |-a.smali |-b$1.smali |-b.smali |-b_a.smali before after

Slide 27

Slide 27 text

#dfua String encryption - java representation public static final String P = "123"; byte[] P = new byte[] {5, 6, 7}; byte KEY = 52; unencrypted encrypted byte[] P = new byte[] {5, 6, 7}; byte KEY = 52; byte[] realPassBytes = new byte[P.length]; for (int i = 0; i < P.length; i++) { realPassBytes[i] = (byte)(P[i] ^ KEY); } String pass = new String(realPassBytes);

Slide 28

Slide 28 text

#dfua String encryption - smali representation .field public static final P:Ljava/lang/String; = "123" const/4 v0, 0x3 new-array v0, v0, [B fill-array-data v0, :array_0 sput-object v0, Lorg/lampapos/crackmeapp/MainActivi ty;->P:[B return-void nop .array-data 1 0x5t 0x6t 0x7t .end array-data unencrypted encrypted

Slide 29

Slide 29 text

#dfua Reflection System.exit(0); Class.forName("java.lang.System") .getMethod("exit", Integer.TYPE).invoke(null, Integer.valueOf("0")); Class.forName(e.d("amF2YS5sYW5nLlN5c3RlbQ==")) .getMethod(e.d("ZXhpdA=="), Integer.TYPE) .invoke(null, Integer.valueOf(e.d("MCA=")));

Slide 30

Slide 30 text

#dfua Native code * but remember that there are disassemblers for a native code binaries Want to hide an algorithm? Move it to a native (C/C++) part ** can be reused even without decompilation

Slide 31

Slide 31 text

#dfua Packers Wrapper APK Wrappers DEX file Native lib Mangled/encoded DEX Original APK DEX file Resources Resources Packer

Slide 32

Slide 32 text

#dfua Packers APK Original DEX file Native lib 1. anti-emulator 2. anti-debugging Decode/fix DEX

Slide 33

Slide 33 text

#dfua Tools Packers Obfuscators ● Proguard ● DexGuard ● Allatori ● SecNeo (Bangcle) ● Ijiami ● ApkProtect

Slide 34

Slide 34 text

Summary 4 more slides and coffee break, snacks, freedom... ;)

Slide 35

Slide 35 text

#dfua Summary Don’t store secrets in your code!

Slide 36

Slide 36 text

#dfua Summary ● Smartphone is not a “trusted environment” ● “Security through obscurity ” doesn’t stop but slows down a hacker ● Everything can be hacked, it’s question of cost and time

Slide 37

Slide 37 text

#dfua Sources ● Android hacker protection level 0 (video) ● Understanding the Dalvik bytecode with the Dedexer tool ● Code protection in Android ● Hacking APK for fun and for profit ● Reverse engineering android apps ● Android reverse engineering 101 ● Understanding the Android build process ● Stack based vs Register based VM Architecture, and the Dalvik VM

Slide 38

Slide 38 text

Michael Pustovit @pustovit https://github.com/lampapos Questions? Thank you!