proguard

43
ProGuard Tomáš Kypta

Upload: tomas-kypta

Post on 25-Jan-2017

342 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: ProGuard

ProGuardTomáš Kypta

Page 2: ProGuard

ProGuard

• free tool

• shrinker, optimizer, obfuscator

Page 3: ProGuard

ProGuard

Page 4: ProGuard

Configuration

Page 5: ProGuard

Configuration

• Empty configuration?

• You have to specify '-keep' options for the shrinking step.

Page 6: ProGuard

Configuration

• define entry points

Page 7: ProGuard

Inputs & Outputs

-injars

-libraryjars

-outjars

Page 8: ProGuard

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

Page 9: ProGuard

Keep rules

-keepnames

• short for -keep,allowshrinking class_specification

-keepclassmembernames

-keepclasseswithmembernames

Page 10: ProGuard

Keep Attributes

• -keepattributes Signature

• for generics (JDK 5.0 and higher)

• -keepattributes Exceptions

• for exceptions

Page 11: ProGuard

Keep Attributes

-keepattributes *Annotation*

*Annotation* = RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations, RuntimeVisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations, AnnotationDefault

Page 12: ProGuard

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

Page 13: ProGuard

Other

-keepparameternames

• keeps parameter names in LocalVariableTable and LocalVariableTypeTable

• might be useful for IDEs

Page 14: ProGuard

Keep Modifiersallowshrinking

• 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.

Page 15: ProGuard

Output Filesdump.txt

• internal structure of code

mapping.txt

• obfuscation mapping

seeds.txt

• unobfuscated code

usage.txt

• stripped code

Page 16: ProGuard

Notes & Warnings

• Notes

• -dontnote <filter>

• Warnings

• -dontwarn <filter>

Page 17: ProGuard

Problems

• Reflection!!! • missing attributes

Page 18: ProGuard

ProGuard & Android

Page 19: ProGuard

Output files

• created in build/outputs/mapping

Page 20: ProGuard

Gradle config

Page 21: ProGuard

Gradle config

buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }

Page 22: ProGuard

Gradle configbuildTypes { 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' } }

Page 23: ProGuard

Gradle config

productFlavors { flavor1 { proguardFile ‘proguard-rules-flavor1.pro' } }

Page 24: ProGuard

ProGuard & Android Libraries

Page 25: ProGuard

Gradle config - library

defaultConfig { consumerProguardFiles ‘proguard-rules-lib.pro’}

• packed into aar

• proguard.txt

Page 26: ProGuard

Generated ProGuard config

• build/intermediates/proguard-rules

• components in AndroidManifest.xml

• custom views in layouts

• only when minifyEnabled true

Page 27: ProGuard

Config merging

-printconfiguration configuration.txt

• merging is a bit stupid

-keepattributes *Annotation*,SourceFile,LineNumberTable,Signature,Exceptions,*Annotation*,Exceptions,*Annotation*,Exceptions,*Annotation*,Signature,Exceptions,*Annotation*,Exceptions,Signature,*Annotation*,Signature,Exceptions,*Annotation*,Exceptions,*Annotation*,Signature,Exceptions,*Annotation*,Signature,Signature,Exceptions,*Annotation*,Signature

Page 28: ProGuard

Apk build

• ProGuard output in apk build

• build/intermediates/classes-proguard/{variant}/classes.jar

Page 29: ProGuard

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

Page 30: ProGuard

Deobfuscation

Page 31: ProGuard

Frequent library configs

Page 32: ProGuard

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>; }

Page 33: ProGuard

Some library configs

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

Page 34: ProGuard

Some library configs

• Dagger 2

• doesn’t require anything

• Rx

• dependency compile 'com.artemzin.rxjava:proguard-rules:1.0.14.2'

Page 35: ProGuard

Tips, Tricks & Traps

Page 36: ProGuard

Tips, Tricks & Traps

• never use

-dontwarn **

-dontnote **

Page 37: ProGuard

Tips, Tricks & Traps• in library projects, in customerProguardFiles don’t

use: • -printconfiguration configuration.txt

• -dontobfuscate, -dontoptimize, …

• -keepattributes SourceFile,LineNumberTable,LocalVariableTable,LocalVariableTypeTable

• declare the bare minimum

Page 38: ProGuard

Tips, Tricks & Traps-applymapping <file>

• reuse previous mapping

-obfuscationdictionary <file>

• custom dictionary

• you can e.g. use Java keywords there (not that helpful)

Page 39: ProGuard

Tips, Tricks & Traps

-repackageclasses 'com.example.obfuscated'

• in Java there can be a problem when class tries to load resource in the same directory

Page 40: ProGuard

DexGuard• comercial

• extra features

• resource obfuscation

• string encryption

• class encryption

• dex splitting

• native code obfuscation

Page 41: ProGuard

Links

• http://proguard.sourceforge.net/

• https://www.guardsquare.com/dexguard

Page 42: ProGuard

Q&A

Page 43: ProGuard

THE END