ProGuard

Tomik 790 views 43 slides Nov 12, 2015
Slide 1
Slide 1 of 43
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43

About This Presentation

Lecture on ProGuard, primarily from the Android perspective. Presented at aDevMeetup #22.


Slide Content

ProGuard
Tomáš Kypta

ProGuard
•free tool
•shrinker, optimizer, obfuscator

ProGuard

Configuration

Configuration
•Empty configuration?
•You have to specify '-keep' options for the
shrinking step.

Configuration
•define entry points

Inputs & Outputs
-injars
-libraryjars
-outjars

Keep rules
-keep
•keep class and class members
-keepclassmembers
•keep class members if their class is kept
-keepclasseswithmembers
•keep class with members if all the class members
are present

Keep rules
-keepnames
•short for -keep,allowshrinking class_specification
-keepclassmembernames
-keepclasseswithmembernames

Keep Attributes
•-keepattributes Signature
•for generics (JDK 5.0 and higher)
•-keepattributes Exceptions
•for exceptions

Keep Attributes
-keepattributes *Annotation*
*Annotation* =
RuntimeVisibleAnnotations,
RuntimeInvisibleAnnotations,
RuntimeVisibleParameterAnnotations,
RuntimeInvisibleParameterAnnotations,
AnnotationDefault

Keep Attributes
-keepattributes EnclosingMethod
•specified the method in which the class was
defined
-keepattributes InnerClasses
•if you have inner class that can be reference from
outside of the library

Other
-keepparameternames
•keeps parameter names in LocalVariableTable
and LocalVariableTypeTable
•might be useful for IDEs

Keep Modifiers
allowshrinking
•Specifies whether the entry points specified in the keep tag may
be shrunk.
allowoptimization
•Specifies whether the entry points specified in the keep tag may
be optimized.
allowobfuscation
•Specifies whether the entry points specified in the keep tag may
be obfuscated.

Output Files
dump.txt
•internal structure of code
mapping.txt
•obfuscation mapping
seeds.txt
•unobfuscated code
usage.txt
•stripped code

Notes & Warnings
•Notes
•-dontnote <filter>
•Warnings
•-dontwarn <filter>

Problems
•Reflection!!!
•missing attributes

ProGuard & Android

Output files
•created in build/outputs/mapping

Gradle config

Gradle config
buildTypes {

release {

minifyEnabled true 

proguardFiles
getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro' 

}

}

Gradle config
buildTypes {

debug {

minifyEnabled true 

proguardFiles
getDefaultProguardFile('proguard-android.txt'),
‘proguard-rules.pro ’,
‘proguard-rules-debug.pro' 

}
release {

minifyEnabled true 

proguardFiles
getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro' 

}

}

Gradle config
productFlavors {
flavor1 {

proguardFile
‘proguard-rules-flavor1 .pro'

}

}

ProGuard & Android
Libraries

Gradle config - library
defaultConfig {

consumerProguardFiles ‘proguard-rules-lib.pro’ 

}
•packed into aar
•proguard.txt

Generated ProGuard config
•build/intermediates/proguard-rules
•components in AndroidManifest.xml
•custom views in layouts
•only when minifyEnabled true

Config merging
-printconfiguration configuration.txt
•merging is a bit stupid
-keepattributes
*Annotation*,SourceFile,LineNumberTable,Signature,Excepti
ons,*Annotation*,Exceptions,*Annotation*,Exceptions,*Anno
tation*,Signature,Exceptions,*Annotation*,Exceptions,Sign
ature,*Annotation*,Signature,Exceptions,*Annotation*,Exce
ptions,*Annotation*,Signature,Exceptions,*Annotation*,Sig
nature,Signature,Exceptions,*Annotation*,Signature

Apk build
•ProGuard output in apk build
•build/intermediates/classes-
proguard/{variant}/classes.jar

Deobfuscation
•ReTrace
•retrace.sh mapping.txt [<stacktrace_file>]
•completeness depends on presence of line
number tables
•-keepattributes SourceFile,LineNumberTable
•ambiguous without these attributes - it will list
all possible original method names
•-renamesourcefileattribute MyApp
•resolve unknown source

Deobfuscation

Frequent library
configs

Some library configs
•Retrofit
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions
•ButterKnife
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}

Some library configs
•Otto
-keepattributes *Annotation*
-keepclassmembers class ** {
@com.squareup.otto.Subscribe public *;
@com.squareup.otto.Produce public *;
}

Some library configs
•Dagger 2
•doesn’t require anything
•Rx
•dependency compile 'com.artemzin.rxjava:proguard-
rules:1.0.14.2'

Tips, Tricks & Traps

Tips, Tricks & Traps
•never use
-dontwarn **
-dontnote **

Tips, Tricks & Traps
•in library projects, in customerProguardFiles don’t
use:
•-printconfiguration configuration.txt
•-dontobfuscate, -dontoptimize, …
•-keepattributes
SourceFile,LineNumberTable, LocalVariableTable,L
ocalVariableTypeTable
•declare the bare minimum

Tips, Tricks & Traps
-applymapping <file>
•reuse previous mapping
-obfuscationdictionary <file >
•custom dictionary
•you can e.g. use Java keywords there (not that
helpful)

Tips, Tricks & Traps
-repackageclasses 'com.example.obfuscated '
•in Java there can be a problem when class tries
to load resource in the same directory

DexGuard
•comercial
•extra features
•resource obfuscation
•string encryption
•class encryption
•dex splitting
•native code obfuscation

Links
•http://proguard.sourceforge.net/
•https://www.guardsquare.com/dexguard

Q&A

THE END