free page hit counter

Mã hóa Proguard dành cho người lười

Giới thiệu

Đã bao giờ bạn gặp hoặc tự mình hỏi những câu hỏi như thế này:

Ứng dụng của mình vừa lên Store đã bị clone thế nhở?

Làm sao để che giấu mã nguồn của ứng dụng vô cùng xịn xò này?

Làm thế nào để làm cho việc thực hiện mã hóa ứng dụng trở nên đơn giản?

Nếu tôi chỉ muốn bảo vệ APK ở mức cơ bản nhất thì có cách nào nhanh hơn không?

Mở đầu

Để trả lời những câu trả lời ở trên thì mình sẽ phải trả lời câu hỏi: “Vậy thì, mã hóa mã nguồn để làm cái gì nhỉ?”

Khi bạn mã hóa, code bên trong APK sẽ được đảo lộn theo quy luật mà bạn đặt ra. Điều này khiến các Cracker/Hacker sẽ mất nhiều thời gian hơn để “luộc” được ứng dụng của bạn. Mã hóa chỉ có tác dụng kéo dài thời gian, làm nản chí các đại ca Crack/Decompile thôi nhá. Chứ chả có quả quýt nào lại không có người “thịt” được cả 😆.

Để làm được điều này, Android Studio đã cung cấp sẵn cho chúng ta một công cụ là Proguard – một công cụ hoàn toàn miễn phí. Ngoài ra, để có thể có thể bá đạo hơn về việc này thì có thêm một lựa chọn nữa là dùng bản trả phí như DexGuard.

Ngày hôm nay, mình sẽ giới thiệu cho các bạn cách mã hóa Proguard nhanh chóng, không cần nỗ nực. Công việc chỉ cần Copy + Paste, chỉnh sửa cho phù hợp với dự án và bắt đầu build app.

-repackageclasses ''

#Optimization Option
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizationpasses 5

#Annotation
-keepattributes *Annotation*
-keep @interface com.google.gson.annotations.*

-renamesourcefileattribute SourceFile

-keepattributes SourceFile,LineNumberTable

-keepattributes Exceptions, InnerClasses
#JNI
-keepclasseswithmembernames class * {
    native <methods>;
}

-dontwarn javax.annotation.**
# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*
# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
#
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

######################### Google Play Services specific rules ##########################

-keepattributes Signature
-keepattributes MethodParameters
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-dontwarn android.webkit.JavascriptInterface

-dontnote org.apache.http.**
-dontnote android.net.http.**

# PROGUARD FIREBASE
# Keep SafeParcelable value, needed for reflection. This is required to support backwards
# compatibility of some classes.
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

# Needed for Parcelable/SafeParcelable classes & their creators to not get renamed, as they are
# found via reflection.
-keepnames class * implements android.os.Parcelable
-keepclassmembers class * implements android.os.Parcelable {
  public static final *** CREATOR;
}

## Keep the classes/members we need for client functionality.
#-keep @interface android.support.annotation.Keep
#-keep @android.support.annotation.Keep class *
#-keepclasseswithmembers class * {
#  @android.support.annotation.Keep <fields>;
#}
#-keepclasseswithmembers class * {
#  @android.support.annotation.Keep <methods>;
#}

# Keep the names of classes/members we need for client functionality.
-keep @interface com.google.android.gms.common.annotation.KeepName
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
  @com.google.android.gms.common.annotation.KeepName *;
}

-dontwarn com.android.installreferrer.api.**

# OkHttp
-keepattributes Signature
-keepattributes *Annotation*

# Needed when building against pre-Marshmallow SDK.
-dontwarn android.security.NetworkSecurityPolicy
# Needed when building against Marshmallow SDK.
#-dontwarn android.app.Notification

#for js and webview interface
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

#Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep class * extends com.bumptech.glide.module.AppGlideModule {
 <init>(...);
}
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}
-keep class com.bumptech.glide.load.data.ParcelFileDescriptorRewinder$InternalRewinder {
  *** rewind();
}


#eventbus
-keepattributes *Annotation*
-keepclassmembers class * {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

#crashlytics
-keepattributes SourceFile,LineNumberTable
-keep public class * extends java.lang.Exception


#GGDrive
-keep interface com.google.** { *; }
-keep class com.google.** { *;}
buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
}

Chú ý khi sử dụng

Bạn phải bật option minifyEnabled để kích hoạt cơ chế mã hóa.

Bạn cần keep những class/package được sử dụng bằng phương pháp reflection. Vì sau khi mã hóa, tên của class sẽ bị thay đổi, khiến cho logic tìm class theo tên không hoạt động.

Tips

Để kiểm tra file APK sau khi mã hóa, bạn hãy sử dụng công cụ JADX – một công cụ Decompile APK rất là bá đạo.

Tổng kết

Ở bài viết này, mình đã giới thiệu cho các bạn những điều sau:

  • Cách sử dụng trình mã hóa Proguard của Android Studio
  • Kinh nghiệm khi mã hóa Proguard
  • Công cụ Decompile APK và check mã hóa

Ở các bài viết sau mình sẽ chia sẻ sâu hơn về các dạng mã hóa khác nha.

Mọi ý kiến đóng góp rất mong các bạn sẽ comment ngay bên dưới bài viết này.

Loading

Là một người thích chia sẻ, tôi tạo ra blog này để mọi người - đặc biệt là các bạn mới vào nghề biết thêm được những kiến thức hữu ích. Rất mong nó sẽ có ích với bạn.
Back To Top