Browse Source

Remember how I implemented highlighting...

master
Chris Smith 4 years ago
parent
commit
da62a4bac7
Signed by: Chris Smith <chris@chameth.com> GPG Key ID: 3A2D4BBDC4A3C9A9
1 changed files with 24 additions and 24 deletions
  1. 24
    24
      site/content/post/2019-10-21-obfuscating-kotlin-proguard.md

+ 24
- 24
site/content/post/2019-10-21-obfuscating-kotlin-proguard.md View File

27
 At first it seemed like ProGuard was refusing to obfuscate any class with a
27
 At first it seemed like ProGuard was refusing to obfuscate any class with a
28
 `keep` rule. With a simple test class:
28
 `keep` rule. With a simple test class:
29
 
29
 
30
-```kotlin
30
+{{< highlight kotlin >}}
31
 class Test {
31
 class Test {
32
 
32
 
33
     private val secret = 123
33
     private val secret = 123
47
     )
47
     )
48
 
48
 
49
 }
49
 }
50
-```
50
+{{< / highlight >}}
51
 
51
 
52
 And a ProGuard rule of:
52
 And a ProGuard rule of:
53
 
53
 
54
-```proguard
54
+{{< highlight proguard >}}
55
 -keep public class Test {
55
 -keep public class Test {
56
     public void greet();
56
     public void greet();
57
 }
57
 }
58
-```
58
+{{< / highlight >}}
59
 
59
 
60
 I expected that the `Test` class and the `greet` method would remain, but both
60
 I expected that the `Test` class and the `greet` method would remain, but both
61
 fields and the `guess` method would be obfuscated. When I built the project
61
 fields and the `guess` method would be obfuscated. When I built the project
62
 and opened the class from Android Studio's APK inspector I was disappointed:
62
 and opened the class from Android Studio's APK inspector I was disappointed:
63
 
63
 
64
-```kotlin
64
+{{< highlight kotlin >}}
65
 public final class Test public constructor() {
65
 public final class Test public constructor() {
66
     public final var name: kotlin.String /* compiled code */
66
     public final var name: kotlin.String /* compiled code */
67
 
67
 
71
 
71
 
72
     private final fun guess(attempt: kotlin.Int): kotlin.Unit { /* compiled code */ }
72
     private final fun guess(attempt: kotlin.Int): kotlin.Unit { /* compiled code */ }
73
 }
73
 }
74
-```
74
+{{< / highlight >}}
75
 
75
 
76
 Having your secret sauce in a field labelled "secret" isn't exactly the level of
76
 Having your secret sauce in a field labelled "secret" isn't exactly the level of
77
 obfuscation I was hoping for. ProGuard has lots of knobs that you can twist to
77
 obfuscation I was hoping for. ProGuard has lots of knobs that you can twist to
93
 output for me. Looking at the mapping file it seems that ProGuard has
93
 output for me. Looking at the mapping file it seems that ProGuard has
94
 indeed decided to rename some things:
94
 indeed decided to rename some things:
95
 
95
 
96
-```
96
+{{< highlight proguardmapping >}}
97
 Test -> Test:
97
 Test -> Test:
98
     int secret -> a
98
     int secret -> a
99
     java.lang.String name -> b
99
     java.lang.String name -> b
100
     void greet() -> greet
100
     void greet() -> greet
101
     void <init>() -> <init>
101
     void <init>() -> <init>
102
-```
102
+{{< / highlight >}}
103
 
103
 
104
 The obvious solution is to look at the class file in something less smart
104
 The obvious solution is to look at the class file in something less smart
105
 than Android Studio. A couple of unzips later and I could do a quick test
105
 than Android Studio. A couple of unzips later and I could do a quick test
106
 to see if the original names were still present:
106
 to see if the original names were still present:
107
 
107
 
108
-```console
108
+{{< highlight console >}}
109
 $ strings Test.class | grep secret
109
 $ strings Test.class | grep secret
110
 secret
110
 secret
111
-```
111
+{{< / highlight >}}
112
 
112
 
113
 The problem is evidently not with Android Studio, as `secret` shouldn't end
113
 The problem is evidently not with Android Studio, as `secret` shouldn't end
114
 up in the class file at all: it should have been entirely replaced with `a` like
114
 up in the class file at all: it should have been entirely replaced with `a` like
115
 the mapping file says. The output from `javap -p` doesn't show any hint of the
115
 the mapping file says. The output from `javap -p` doesn't show any hint of the
116
 original names, however:
116
 original names, however:
117
 
117
 
118
-```console
118
+{{< highlight console >}}
119
 $ javap -p Test
119
 $ javap -p Test
120
 public final class Test {
120
 public final class Test {
121
   private final int a;
121
   private final int a;
123
   public final void greet();
123
   public final void greet();
124
   public Test();
124
   public Test();
125
 }
125
 }
126
-```
126
+{{< / highlight >}}
127
 
127
 
128
 But, given the names show up in `strings`, they must be kicking around
128
 But, given the names show up in `strings`, they must be kicking around
129
 somewhere. None of the various outputs from `javap` helped until I hit
129
 somewhere. None of the various outputs from `javap` helped until I hit
130
 `-verbose`. Right at the end of the class is:
130
 `-verbose`. Right at the end of the class is:
131
 
131
 
132
-```
132
+{{< highlight javap >}}
133
 RuntimeVisibleAnnotations:
133
 RuntimeVisibleAnnotations:
134
   0: #58(#84=[I#2,I#2,I#4],#69=[I#2,I#1,I#3],#81=I#2,#70=[s#40],#71=[s#55,s#39,s#43,s#85,s#39,s#72,s#42,s#91,s#47,s#90,s#39,s#73,s#39,s#74,s#67,s#83])
134
   0: #58(#84=[I#2,I#2,I#4],#69=[I#2,I#1,I#3],#81=I#2,#70=[s#40],#71=[s#55,s#39,s#43,s#85,s#39,s#72,s#42,s#91,s#47,s#90,s#39,s#73,s#39,s#74,s#67,s#83])
135
     kotlin.Metadata(
135
     kotlin.Metadata(
139
       d1=["\u0000\"\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0005\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0003\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\u0006\u0010\u000b\u001a\u00020\fJ\u0010\u0010\r\u001a\u00020\f2\u0006\u0010\u000e\u001a\u00020\nH\u0002R\u001a\u0010\u0003\u001a\u00020\u0004X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0005\u0010\u0006\"\u0004\b\u0007\u0010\bR\u000e\u0010\t\u001a\u00020\nX\u0082D¢\u0006\u0002\n\u0000¨\u0006\u000f"]
139
       d1=["\u0000\"\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0005\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0003\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\u0006\u0010\u000b\u001a\u00020\fJ\u0010\u0010\r\u001a\u00020\f2\u0006\u0010\u000e\u001a\u00020\nH\u0002R\u001a\u0010\u0003\u001a\u00020\u0004X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0005\u0010\u0006\"\u0004\b\u0007\u0010\bR\u000e\u0010\t\u001a\u00020\nX\u0082D¢\u0006\u0002\n\u0000¨\u0006\u000f"]
140
       d2=["LTest;","","()V","name","","getName","()Ljava/lang/String;","setName","(Ljava/lang/String;)V","secret","","greet","","guess","attempt","lib_release"]
140
       d2=["LTest;","","()V","name","","getName","()Ljava/lang/String;","setName","(Ljava/lang/String;)V","secret","","greet","","guess","attempt","lib_release"]
141
     )
141
     )
142
-```
142
+{{< / highlight >}}
143
 
143
 
144
 There's a Kotlin *annotation* containing all of the symbols we were trying to
144
 There's a Kotlin *annotation* containing all of the symbols we were trying to
145
 obfuscate away! Kotlin apparently uses this annotation for reflection and for
145
 obfuscate away! Kotlin apparently uses this annotation for reflection and for
148
 back to Android Studio and making it "decompile" the Kotlin code into Java
148
 back to Android Studio and making it "decompile" the Kotlin code into Java
149
 shows the annotation:
149
 shows the annotation:
150
 
150
 
151
-```java
151
+{{< highlight java >}}
152
 @Metadata(
152
 @Metadata(
153
    mv = {1, 1, 15},
153
    mv = {1, 1, 15},
154
    bv = {1, 0, 3},
154
    bv = {1, 0, 3},
157
    d2 = {"LTest;", "", "()V", "name", "", "getName", "()Ljava/lang/String;", "setName", "(Ljava/lang/String;)V", "secret", "", "greet", "", "guess", "attempt", "lib_release"}
157
    d2 = {"LTest;", "", "()V", "name", "", "getName", "()Ljava/lang/String;", "setName", "(Ljava/lang/String;)V", "secret", "", "greet", "", "guess", "attempt", "lib_release"}
158
 )
158
 )
159
 public final class Test {
159
 public final class Test {
160
-```
160
+{{< / highlight >}}
161
 
161
 
162
 ## Solving the right problem
162
 ## Solving the right problem
163
 
163
 
177
 the full configuration being passed to ProGuard, and the reason for keeping
177
 the full configuration being passed to ProGuard, and the reason for keeping
178
 quickly becomes obvious:
178
 quickly becomes obvious:
179
 
179
 
180
-```proguard
180
+{{< highlight proguard >}}
181
 -keepattributes *Annotation*,*Annotation*
181
 -keepattributes *Annotation*,*Annotation*
182
-```
182
+{{< / highlight >}}
183
 
183
 
184
 This appears to be added by the Android build plugin before it invokes
184
 This appears to be added by the Android build plugin before it invokes
185
 ProGuard, and there's no obvious way to disable it. ProGuard doesn't offer
185
 ProGuard, and there's no obvious way to disable it. ProGuard doesn't offer
187
 seems to concatenate all of the `-keepattribute` values together and puts our
187
 seems to concatenate all of the `-keepattribute` values together and puts our
188
 user-supplied ones first. Adding a negative filter:
188
 user-supplied ones first. Adding a negative filter:
189
 
189
 
190
-```proguard
190
+{{< highlight proguard >}}
191
 -keepattributes !*Annotation*
191
 -keepattributes !*Annotation*
192
-```
192
+{{< / highlight >}}
193
 
193
 
194
 Results in the following in the printed configuration:
194
 Results in the following in the printed configuration:
195
 
195
 
196
-```proguard
196
+{{< highlight proguard >}}
197
 -keepattributes !*Annotation*,*Annotation*,*Annotation*
197
 -keepattributes !*Annotation*,*Annotation*,*Annotation*
198
-```
198
+{{< / highlight >}}
199
 
199
 
200
 The negative filter prevents any subsequent filters from matching. Recompiling
200
 The negative filter prevents any subsequent filters from matching. Recompiling
201
 and looking at the class file again looks a lot more sensible:
201
 and looking at the class file again looks a lot more sensible:
202
 
202
 
203
-```java
203
+{{< highlight java >}}
204
 import kotlin.io.ConsoleKt;
204
 import kotlin.io.ConsoleKt;
205
 
205
 
206
 public final class Test {
206
 public final class Test {
219
       }
219
       }
220
    }
220
    }
221
 }
221
 }
222
-```
222
+{{< / highlight >}}
223
 
223
 
224
 And that's the story of how I spent half a day making a one line change.
224
 And that's the story of how I spent half a day making a one line change.

Loading…
Cancel
Save