From 32bf329fee9254175953f7b22e0c2ee2a919a026 Mon Sep 17 00:00:00 2001 From: kcharan Date: Thu, 27 Feb 2020 19:24:26 +0530 Subject: [PATCH 1/5] New configuration parameters to keep all new names unique (-dontresetmembernaming, -dontresetclassnaming, -dontresetpackagenaming) (cherry picked from commit dc09fab87e41379622de4608a37ca814f26ae43c) --- core/src/proguard/Configuration.java | 15 +++++++++++++ core/src/proguard/ConfigurationConstants.java | 3 +++ core/src/proguard/ConfigurationParser.java | 3 +++ core/src/proguard/ConfigurationWriter.java | 3 +++ .../proguard/obfuscate/ClassObfuscator.java | 22 ++++++++++++++----- .../proguard/obfuscate/MemberObfuscator.java | 10 ++++++++- core/src/proguard/obfuscate/Obfuscator.java | 7 ++++++ 7 files changed, 56 insertions(+), 7 deletions(-) diff --git a/core/src/proguard/Configuration.java b/core/src/proguard/Configuration.java index bc4aea2ec..8fa507748 100644 --- a/core/src/proguard/Configuration.java +++ b/core/src/proguard/Configuration.java @@ -196,6 +196,21 @@ public class Configuration */ public boolean obfuscate = true; + /** + * Specifies whether package naming factory should reset for each package or not. + */ + public boolean dontResetPackageNaming = false; + + /** + * Specifies whether class naming factory should reset for each package or not. + */ + public boolean dontResetClassNaming = false; + + /** + * Specifies whether class member naming factory should reset for each package or not. + */ + public boolean dontResetMemberNaming = false; + /** * An optional output file for listing the obfuscation mapping. * An empty file name means the standard output. diff --git a/core/src/proguard/ConfigurationConstants.java b/core/src/proguard/ConfigurationConstants.java index 244ebc0f6..c842c7439 100644 --- a/core/src/proguard/ConfigurationConstants.java +++ b/core/src/proguard/ConfigurationConstants.java @@ -69,6 +69,9 @@ class ConfigurationConstants public static final String DONT_OBFUSCATE_OPTION = "-dontobfuscate"; public static final String PRINT_MAPPING_OPTION = "-printmapping"; public static final String APPLY_MAPPING_OPTION = "-applymapping"; + public static final String DONT_RESET_PACKAGE_NAMING_OPTION = "-dontresetpackagenaming"; + public static final String DONT_RESET_CLASS_NAMING_OPTION = "-dontresetclassnaming"; + public static final String DONT_RESET_MEMBER_NAMING_OPTION = "-dontresetmembernaming"; public static final String OBFUSCATION_DICTIONARY_OPTION = "-obfuscationdictionary"; public static final String CLASS_OBFUSCATION_DICTIONARY_OPTION = "-classobfuscationdictionary"; public static final String PACKAGE_OBFUSCATION_DICTIONARY_OPTION = "-packageobfuscationdictionary"; diff --git a/core/src/proguard/ConfigurationParser.java b/core/src/proguard/ConfigurationParser.java index 4916b808b..38f2bf89d 100644 --- a/core/src/proguard/ConfigurationParser.java +++ b/core/src/proguard/ConfigurationParser.java @@ -191,6 +191,9 @@ public void parse(Configuration configuration) else if (ConfigurationConstants.DONT_OBFUSCATE_OPTION .startsWith(nextWord)) configuration.obfuscate = parseNoArgument(false); else if (ConfigurationConstants.PRINT_MAPPING_OPTION .startsWith(nextWord)) configuration.printMapping = parseOptionalFile(); else if (ConfigurationConstants.APPLY_MAPPING_OPTION .startsWith(nextWord)) configuration.applyMapping = parseFile(); + else if (ConfigurationConstants.DONT_RESET_PACKAGE_NAMING_OPTION .startsWith(nextWord)) configuration.dontResetPackageNaming = parseNoArgument(true); + else if (ConfigurationConstants.DONT_RESET_CLASS_NAMING_OPTION .startsWith(nextWord)) configuration.dontResetClassNaming = parseNoArgument(true); + else if (ConfigurationConstants.DONT_RESET_MEMBER_NAMING_OPTION .startsWith(nextWord)) configuration.dontResetMemberNaming = parseNoArgument(true); else if (ConfigurationConstants.OBFUSCATION_DICTIONARY_OPTION .startsWith(nextWord)) configuration.obfuscationDictionary = parseURL(); else if (ConfigurationConstants.CLASS_OBFUSCATION_DICTIONARY_OPTION .startsWith(nextWord)) configuration.classObfuscationDictionary = parseURL(); else if (ConfigurationConstants.PACKAGE_OBFUSCATION_DICTIONARY_OPTION .startsWith(nextWord)) configuration.packageObfuscationDictionary = parseURL(); diff --git a/core/src/proguard/ConfigurationWriter.java b/core/src/proguard/ConfigurationWriter.java index b1909162f..b4143fce6 100644 --- a/core/src/proguard/ConfigurationWriter.java +++ b/core/src/proguard/ConfigurationWriter.java @@ -114,6 +114,9 @@ public void write(Configuration configuration) throws IOException writeOption(ConfigurationConstants.DONT_OBFUSCATE_OPTION, !configuration.obfuscate); writeOption(ConfigurationConstants.PRINT_MAPPING_OPTION, configuration.printMapping); writeOption(ConfigurationConstants.APPLY_MAPPING_OPTION, configuration.applyMapping); + writeOption(ConfigurationConstants.DONT_RESET_PACKAGE_NAMING_OPTION, !configuration.dontResetPackageNaming); + writeOption(ConfigurationConstants.DONT_RESET_CLASS_NAMING_OPTION, !configuration.dontResetClassNaming); + writeOption(ConfigurationConstants.DONT_RESET_MEMBER_NAMING_OPTION, !configuration.dontResetMemberNaming); writeOption(ConfigurationConstants.OBFUSCATION_DICTIONARY_OPTION, configuration.obfuscationDictionary); writeOption(ConfigurationConstants.CLASS_OBFUSCATION_DICTIONARY_OPTION, configuration.classObfuscationDictionary); writeOption(ConfigurationConstants.PACKAGE_OBFUSCATION_DICTIONARY_OPTION, configuration.packageObfuscationDictionary); diff --git a/core/src/proguard/obfuscate/ClassObfuscator.java b/core/src/proguard/obfuscate/ClassObfuscator.java index 3394b750a..3f7aae238 100644 --- a/core/src/proguard/obfuscate/ClassObfuscator.java +++ b/core/src/proguard/obfuscate/ClassObfuscator.java @@ -50,6 +50,8 @@ public class ClassObfuscator private final DictionaryNameFactory classNameFactory; private final DictionaryNameFactory packageNameFactory; private final boolean useMixedCaseClassNames; + private final boolean dontResetPackageNaming; + private final boolean dontResetClassNaming; private final StringMatcher keepPackageNamesMatcher; private final String flattenPackageHierarchy; private final String repackageClasses; @@ -86,6 +88,10 @@ public class ClassObfuscator * dictionary. * @param useMixedCaseClassNames specifies whether obfuscated packages and * classes can get mixed-case names. + * @param dontResetPackageNaming specifies whether package naming factory should be + * package level or root level for all packages. + * @param dontResetClassNaming specifies whether class naming factory should be + * package level or root level for all classes. * @param keepPackageNames the optional filter for which matching * package names are kept. * @param flattenPackageHierarchy the base package if the obfuscated package @@ -100,6 +106,8 @@ public ClassObfuscator(ClassPool programClassPool, DictionaryNameFactory classNameFactory, DictionaryNameFactory packageNameFactory, boolean useMixedCaseClassNames, + boolean dontResetPackageNaming, + boolean dontResetClassNaming, List keepPackageNames, String flattenPackageHierarchy, String repackageClasses, @@ -123,6 +131,8 @@ public ClassObfuscator(ClassPool programClassPool, } this.useMixedCaseClassNames = useMixedCaseClassNames; + this.dontResetPackageNaming = dontResetPackageNaming; + this.dontResetClassNaming = dontResetClassNaming; this.keepPackageNamesMatcher = keepPackageNames == null ? null : new ListParser(new FileNameParser()).parse(keepPackageNames); this.flattenPackageHierarchy = flattenPackageHierarchy; @@ -403,7 +413,7 @@ private String generateUniquePackagePrefix(String newSuperPackagePrefix) { // Find the right name factory for this package. NameFactory packageNameFactory = - (NameFactory)packagePrefixPackageNameFactoryMap.get(newSuperPackagePrefix); + (NameFactory)packagePrefixPackageNameFactoryMap.get((dontResetPackageNaming) ? "_" : newSuperPackagePrefix); if (packageNameFactory == null) { // We haven't seen packages in this superpackage before. Create @@ -416,7 +426,7 @@ private String generateUniquePackagePrefix(String newSuperPackagePrefix) packageNameFactory); } - packagePrefixPackageNameFactoryMap.put(newSuperPackagePrefix, + packagePrefixPackageNameFactoryMap.put((dontResetPackageNaming) ? "_" : newSuperPackagePrefix, packageNameFactory); } @@ -453,7 +463,7 @@ private String generateUniqueClassName(String newPackagePrefix) { // Find the right name factory for this package. NameFactory classNameFactory = - (NameFactory)packagePrefixClassNameFactoryMap.get(newPackagePrefix); + (NameFactory)packagePrefixClassNameFactoryMap.get((dontResetClassNaming) ? "_" : newPackagePrefix); if (classNameFactory == null) { // We haven't seen classes in this package before. @@ -466,7 +476,7 @@ private String generateUniqueClassName(String newPackagePrefix) classNameFactory); } - packagePrefixClassNameFactoryMap.put(newPackagePrefix, + packagePrefixClassNameFactoryMap.put((dontResetClassNaming) ? "_" : newPackagePrefix, classNameFactory); } @@ -481,14 +491,14 @@ private String generateUniqueNumericClassName(String newPackagePrefix) { // Find the right name factory for this package. NameFactory classNameFactory = - (NameFactory)packagePrefixNumericClassNameFactoryMap.get(newPackagePrefix); + (NameFactory)packagePrefixNumericClassNameFactoryMap.get((dontResetClassNaming) ? "_" : newPackagePrefix); if (classNameFactory == null) { // We haven't seen classes in this package before. // Create a new name factory for them. classNameFactory = new NumericNameFactory(); - packagePrefixNumericClassNameFactoryMap.put(newPackagePrefix, + packagePrefixNumericClassNameFactoryMap.put((dontResetClassNaming) ? "_" : newPackagePrefix, classNameFactory); } diff --git a/core/src/proguard/obfuscate/MemberObfuscator.java b/core/src/proguard/obfuscate/MemberObfuscator.java index 48e8d296f..93c0eda67 100644 --- a/core/src/proguard/obfuscate/MemberObfuscator.java +++ b/core/src/proguard/obfuscate/MemberObfuscator.java @@ -42,6 +42,7 @@ public class MemberObfuscator implements MemberVisitor { private final boolean allowAggressiveOverloading; + private final boolean dontResetMemberNaming; private final NameFactory nameFactory; private final Map descriptorMap; @@ -50,16 +51,20 @@ public class MemberObfuscator * Creates a new MemberObfuscator. * @param allowAggressiveOverloading a flag that specifies whether class * members can be overloaded aggressively. + * @param dontResetMemberNaming a flag that specifies whether to reset + * naming factory for every new class or not. * @param nameFactory the factory that can produce * obfuscated member names. * @param descriptorMap the map of descriptors to * [new name - old name] maps. */ public MemberObfuscator(boolean allowAggressiveOverloading, + boolean dontResetMemberNaming, NameFactory nameFactory, Map descriptorMap) { this.allowAggressiveOverloading = allowAggressiveOverloading; + this.dontResetMemberNaming = dontResetMemberNaming; this.nameFactory = nameFactory; this.descriptorMap = descriptorMap; } @@ -98,7 +103,10 @@ public void visitAnyMember(Clazz clazz, Member member) if (newName == null) { // Find an acceptable new name. - nameFactory.reset(); + if(!dontResetMemberNaming) + { + nameFactory.reset(); + } do { diff --git a/core/src/proguard/obfuscate/Obfuscator.java b/core/src/proguard/obfuscate/Obfuscator.java index b8c3373b5..3fc690ee5 100644 --- a/core/src/proguard/obfuscate/Obfuscator.java +++ b/core/src/proguard/obfuscate/Obfuscator.java @@ -232,6 +232,8 @@ public void execute(ClassPool programClassPool, classNameFactory, packageNameFactory, configuration.useMixedCaseClassNames, + configuration.dontResetPackageNaming, + configuration.dontResetClassNaming, configuration.keepPackageNames, configuration.flattenPackageHierarchy, configuration.repackageClasses, @@ -264,6 +266,7 @@ public void execute(ClassPool programClassPool, programClassPool.classesAccept( new AllMemberVisitor( new MemberObfuscator(configuration.overloadAggressively, + configuration.dontResetMemberNaming, nameFactory, descriptorMap))); } @@ -292,6 +295,7 @@ public void execute(ClassPool programClassPool, new AllMemberVisitor( new MemberAccessFilter(0, ClassConstants.ACC_PRIVATE, new MemberObfuscator(configuration.overloadAggressively, + configuration.dontResetMemberNaming, nameFactory, descriptorMap))), @@ -343,6 +347,7 @@ public void execute(ClassPool programClassPool, new AllMemberVisitor( new MemberAccessFilter(ClassConstants.ACC_PRIVATE, 0, new MemberObfuscator(configuration.overloadAggressively, + configuration.dontResetMemberNaming, nameFactory, descriptorMap))), @@ -400,6 +405,7 @@ public void execute(ClassPool programClassPool, descriptorMap, warningPrinter, new MemberObfuscator(configuration.overloadAggressively, + configuration.dontResetMemberNaming, specialNameFactory, specialDescriptorMap))))), @@ -431,6 +437,7 @@ public void execute(ClassPool programClassPool, descriptorMap, warningPrinter, new MemberObfuscator(configuration.overloadAggressively, + configuration.dontResetMemberNaming, specialNameFactory, specialDescriptorMap)))), From 2700d02a99cb5a53a08fd26ce06c9428a5a05672 Mon Sep 17 00:00:00 2001 From: kcharan Date: Thu, 27 Feb 2020 18:34:06 +0530 Subject: [PATCH 2/5] Added test source & Jupiter dependencies (cherry picked from commit 2b41dcedb452480ce08dfbde8cae15ae1e59befd) --- core/build.gradle | 14 ++++++++++++++ core/gradle.properties | 1 + core/pom.xml | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/core/build.gradle b/core/build.gradle index 01461af00..8f9d720b6 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -22,6 +22,17 @@ sourceSets.main { } } +sourceSets.test { + java { + srcDir "$projectDir/test" + } +} + +test { + useJUnitPlatform() + maxHeapSize = '1G' +} + jar { manifest.from 'src/META-INF/MANIFEST.MF' } @@ -29,4 +40,7 @@ jar { dependencies { compile "com.google.code.gson:gson:${gsonVersion}" + testImplementation "org.junit.jupiter:junit-jupiter:${jupiterVersion}" + testImplementation "org.junit.jupiter:junit-jupiter-api:${jupiterVersion}" + testImplementation "org.junit.jupiter:junit-jupiter-engine:${jupiterVersion}" } diff --git a/core/gradle.properties b/core/gradle.properties index e8cc261c7..dfca54289 100644 --- a/core/gradle.properties +++ b/core/gradle.properties @@ -3,3 +3,4 @@ target = 1.8 gsonVersion = 2.8.5 +jupiterVersion = 5.6.0 \ No newline at end of file diff --git a/core/pom.xml b/core/pom.xml index 9bd90bbfb..b6e2b4e7d 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -13,9 +13,11 @@ proguard-base [${project.groupId}] ${project.artifactId} + 6.2.2.1 src + test maven-source-plugin @@ -33,9 +35,11 @@ + @@ -46,6 +50,25 @@ 2.8.5 provided + + + org.junit.jupiter + junit-jupiter + 5.6.0 + test + + + org.junit.jupiter + junit-jupiter-api + 5.6.0 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.6.0 + test + From 7bc1fdeb4575649633cbe2ab80f26733fecda6f5 Mon Sep 17 00:00:00 2001 From: kcharan Date: Thu, 27 Feb 2020 18:36:12 +0530 Subject: [PATCH 3/5] Added test case for ProGurad.main() (cherry picked from commit 4392e89386530f0aa8e938f6812f5dec39d54bc7) --- core/test/proguard/ProGuardTest.java | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 core/test/proguard/ProGuardTest.java diff --git a/core/test/proguard/ProGuardTest.java b/core/test/proguard/ProGuardTest.java new file mode 100644 index 000000000..47f57494d --- /dev/null +++ b/core/test/proguard/ProGuardTest.java @@ -0,0 +1,45 @@ +package proguard; + +import org.junit.jupiter.api.Test; + +class ProGuardTest { + + @Test + void main() { + + String targetLocation = "\\"; + String inputJar = "classes"; + String outputJar = "proguardClasses"; + String map_filename = "proguard_map.txt"; + String seed_filename = "proguard_seed.txt"; + String method_dir_filename = "method-dictionary.txt"; + String class_dir_filename = "class-dictionary.txt"; + String package_dir_filename = "package-dictionary.txt"; + + String [] arguments = { + "-dontshrink", + "-dontoptimize", + "-adaptclassstrings", + "-keepparameternames", + "-keep class com.study.BootWebApplication", + "-ignorewarnings", + "-injars '" + targetLocation + inputJar + "'", + "-outjars '" + targetLocation + outputJar + "'", + "-printmapping '" + targetLocation + map_filename + "'", + "-printseeds '" + targetLocation + seed_filename + "'", + + "-dontresetmembernaming", + "-dontresetclassnaming", + "-dontresetpackagenaming", + + "-obfuscationdictionary '" + targetLocation + method_dir_filename + "'", + "-classobfuscationdictionary '" + targetLocation + class_dir_filename + "'", + "-packageobfuscationdictionary '" + targetLocation + package_dir_filename + "'", + + "-dontwarn java.lang.**", + "-dontwarn java.util.**" + }; + + ProGuard.main(arguments); + } +} \ No newline at end of file From 76f961b57803e18423c5db70101ae4032532be70 Mon Sep 17 00:00:00 2001 From: kcharan Date: Fri, 28 Feb 2020 11:53:53 +0530 Subject: [PATCH 4/5] Changed main class name --- core/test/proguard/ProGuardTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/test/proguard/ProGuardTest.java b/core/test/proguard/ProGuardTest.java index 47f57494d..09825603f 100644 --- a/core/test/proguard/ProGuardTest.java +++ b/core/test/proguard/ProGuardTest.java @@ -21,7 +21,7 @@ void main() { "-dontoptimize", "-adaptclassstrings", "-keepparameternames", - "-keep class com.study.BootWebApplication", + "-keep class com.study.Application", "-ignorewarnings", "-injars '" + targetLocation + inputJar + "'", "-outjars '" + targetLocation + outputJar + "'", From a9af8a1b259de4f675eec9fdefb2e53b2181d21e Mon Sep 17 00:00:00 2001 From: kcharan Date: Fri, 28 Feb 2020 12:02:18 +0530 Subject: [PATCH 5/5] Renamed variables and added comments in test case --- core/test/proguard/ProGuardTest.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/core/test/proguard/ProGuardTest.java b/core/test/proguard/ProGuardTest.java index 09825603f..58cb9338a 100644 --- a/core/test/proguard/ProGuardTest.java +++ b/core/test/proguard/ProGuardTest.java @@ -7,14 +7,17 @@ class ProGuardTest { @Test void main() { + // NOTE: Change targetLocation value here to the location where + // inputJar and all below required items are available / expected String targetLocation = "\\"; + String inputJar = "classes"; String outputJar = "proguardClasses"; String map_filename = "proguard_map.txt"; String seed_filename = "proguard_seed.txt"; - String method_dir_filename = "method-dictionary.txt"; - String class_dir_filename = "class-dictionary.txt"; - String package_dir_filename = "package-dictionary.txt"; + String members_dir_filename = "members-dictionary.txt"; + String classes_dir_filename = "classes-dictionary.txt"; + String packages_dir_filename = "packages-dictionary.txt"; String [] arguments = { "-dontshrink", @@ -28,13 +31,16 @@ void main() { "-printmapping '" + targetLocation + map_filename + "'", "-printseeds '" + targetLocation + seed_filename + "'", + // This option makes sure that the obfuscation will use all unique names for members "-dontresetmembernaming", + // This option makes sure that the obfuscation will use all unique names for classes "-dontresetclassnaming", + // This option makes sure that the obfuscation will use all unique names for packages "-dontresetpackagenaming", - "-obfuscationdictionary '" + targetLocation + method_dir_filename + "'", - "-classobfuscationdictionary '" + targetLocation + class_dir_filename + "'", - "-packageobfuscationdictionary '" + targetLocation + package_dir_filename + "'", + "-obfuscationdictionary '" + targetLocation + members_dir_filename + "'", + "-classobfuscationdictionary '" + targetLocation + classes_dir_filename + "'", + "-packageobfuscationdictionary '" + targetLocation + packages_dir_filename + "'", "-dontwarn java.lang.**", "-dontwarn java.util.**"