yGuard icon indicating copy to clipboard operation
yGuard copied to clipboard

Class Tag Is Keeping All public methods.

Open LB45440078L opened this issue 1 year ago • 1 comments


<class methods="none" classes="public" fields="none">
  <patternset>
    <include name="top.cmarco.lightlogin.LightLoginPlugin"/>
  </patternset>
</class>

adding this tag inside the keep element does not result in my public methods inside LightLoginPlugin java class being obfuscated.

I needed to keep the classname and its package without keeping the fields or methods, however this does not seem to work as indicated by the documentation.

LB45440078L avatar Mar 07 '24 23:03 LB45440078L

Cannot reproduce, works for me:

<project
 name="yguard-github-issues"
 basedir=".">
  <target name="-init">
    <property
     name="src.dir"
     value="${basedir}/tgt/generated-sources"/>
    <property
     name="tgt.dir"
     value="${basedir}/tgt"/>
    <property
     name="bin.dir"
     value="${tgt.dir}/classes"/>
    <property
     name="test.jar.unobf"
     value="${tgt.dir}/test_unobf.jar"/>
    <property
     name="test.jar.obf"
     value="${tgt.dir}/test_obf.jar"/>
  </target>

  <target name="issue_152" depends="-init">
    <mkdir dir="${src.dir}/github/issue_152"/>
    <echo file="${src.dir}/github/issue_152/KeepClassNameOnly.java">package github.issue_152;
      
/**
 * Test case for obfuscating all methods while keeping the class name.
 */
public class KeepClassNameOnly {
  public int aPublicField;

  public void aPublicMethod() {}

  protected void aProtectedMethod() {}

  void aPackagePrivateMethod() {}

  private void aPrivateMethod() {}
}
</echo>

    <mkdir dir="${bin.dir}"/>
    <javac
     debug="true"
     destdir="${bin.dir}"
     includeantruntime="false"
     srcdir="${src.dir}">
      <include name="github/issue_152/**.java"/>
    </javac>

    <jar destfile="${test.jar.unobf}">
      <fileset dir="${bin.dir}">
        <include name="github/issue_152/**"/>
      </fileset>
    </jar>

    <property
     name="yguard.home"
     value="PATH_TO_YGUARD_INSTALLATION_DIRECTORY"/>
    <property
     name="yguard.jar"
     value="${yguard.home}/lib/yguard-4.1.0.jar"/>
    <property
     name="asm.jar"
     value="${yguard.home}/lib/asm-9.6.jar"/>
    <path id="yguard.classpath">
      <pathelement location="${yguard.jar}"/>
      <pathelement location="${asm.jar}"/>
    </path>
    <taskdef
     name="yguard"
     classname="com.yworks.yguard.YGuardTask"
     classpathref="yguard.classpath"/>

    <yguard>
      <inoutpair
       in="${test.jar.unobf}"
       out="${test.jar.obf}"/>

      <rename logfile="${tgt.dir}/test_rename_log.xml">
        <keep>
          <class methods="none" classes="public" fields="none">
            <patternset>
              <include name="github.issue_152.KeepClassNameOnly"/>
            </patternset>
          </class>
        </keep>
      </rename>
    </yguard>
  </target>

  <target name="clean-all" depends="-init">
    <delete dir="${tgt.dir}"/>
  </target>
</project>

Please provide a short, self-contained, example if you still think this is a yGuard problem.

thomasbehr avatar Mar 08 '24 09:03 thomasbehr

Click this to collapse/fold my entire build XML.

This pom.xml is causing the main class in my software to have public method left as original.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.cmarco</groupId>
    <artifactId>LightLogin</artifactId>
    <version>1.0.7-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>LightLogin</name>

    <description>Optimised and Safe SpigotMC Software for Authentication</description>
    <properties>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <dependencies>
                    <dependency>
                        <groupId>com.yworks</groupId>
                        <artifactId>yguard</artifactId>
                        <version>4.1.0</version>
                        <scope>compile</scope>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <id>obfuscate</id>
                        <configuration>
                            <target>
                                <property name="runtime_classpath" refid="maven.runtime.classpath"/>
                                <!--suppress UnresolvedMavenProperty -->
                                <taskdef name="yguard" classname="com.yworks.yguard.YGuardTask"
                                         classpath="${runtime_classpath}"/>
                                <yguard>
                                    <inoutpair in="${project.build.directory}/${project.build.finalName}.jar"
                                               out="${project.build.directory}/${project.build.finalName}.jar"/>

                                    <rename logfile="${project.build.directory}/yguard.log.xml"
                                            replaceClassNameStrings="false">

                                        <property name="overload-enabled" value="true"/>
                                        <property name="naming-scheme" value="simple"/>
                                        <property name="language-conformity" value="legal"/>
                                        <!--<property name="obfuscation-prefix" value="top.cmarco.lightlogin.LightLoginPlugin"/> -->

                                        <keep>
                                            <class methods="private" classes="public" fields="none">
                                                <patternset>
                                                    <include name="com.alessiodp.libby.**"/>
                                                    <exclude name="top.cmarco.lightlogin.**"/>
                                                </patternset>
                                            </class>

                                            <class methods="none" classes="public" fields="none">
                                                <patternset>
                                                    <include name="top.cmarco.lightlogin.LightLoginPlugin"/>
                                                </patternset>
                                            </class>

                                            <method name="org.bukkit.event.HandlerList getHandlerList()">
                                                <patternset>
                                                    <include name="top.cmarco.lightlogin.api.**"/>
                                                </patternset>
                                            </method>

                                            <method name="org.bukkit.event.HandlerList getHandlers()">
                                                <patternset>
                                                    <include name="top.cmarco.lightlogin.api.**"/>
                                                </patternset>
                                            </method>

                                            <field name="HANDLERS_LIST">
                                                <patternset>
                                                    <include name="top.cmarco.lightlogin.api.**"/>
                                                </patternset>
                                            </field>

                                        </keep>
                                    </rename>

                                    <externalclasses>
                                        <!--suppress UnresolvedMavenProperty -->
                                        <pathelement path="${mvn.classpath}"/>
                                    </externalclasses>

                                </yguard>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.5.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>

                        <configuration>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                            <relocations>
                                <relocation>
                                    <pattern>com.alessiodp.libby</pattern>
                                    <shadedPattern>top.cmarco.lightlogin.libs.com.alessiodp.libby</shadedPattern>
                                </relocation>

                                <relocation>
                                    <pattern>com.zaxxer.hikari</pattern>
                                    <shadedPattern>top.cmarco.lightlogin.libs.com.zaxxer.hikari</shadedPattern>
                                </relocation>

                                <relocation>
                                    <pattern>org.bouncycastle</pattern>
                                    <shadedPattern>top.cmarco.lightlogin.libs.org.bouncycastle</shadedPattern>
                                </relocation>

                                <relocation>
                                    <pattern>org.postgresql</pattern>
                                    <shadedPattern>top.cmarco.lightlogin.libs.org.postgresql</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>

                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <repositories>
        <repository>
            <id>spigotmc-repo</id>
            <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/groups/public/</url>
        </repository>
        <repository>
            <id>maven-snapshots</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.7.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>24.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.spigotmc</groupId>
            <artifactId>spigot-api</artifactId>
            <version>1.17.1-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk18on</artifactId>
            <version>1.77</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>5.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.19.0</version> <!-- This is the version Spigot 1.20.1 uses -->
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.alessiodp.libby</groupId>
            <artifactId>libby-bukkit</artifactId> <!-- Replace bukkit if you're using another platform -->
            <version>2.0.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>



<class methods="none" classes="public" fields="none">
  <patternset>
    <include name="top.cmarco.lightlogin.LightLoginPlugin"/>
  </patternset>
</class>

I am asking for clarification about this. LightLoginPlugin class is being kept, but I specified methods="none" inside it, meaning my public methods shouldn't be kept.

Recaf tool, with Procyon decompiler, still shows all public methods as untouched. image

I build my project with mvn clean package and I am using Java 17 by Eclipse Temurin.

LB45440078L avatar Mar 10 '24 13:03 LB45440078L

Thank you very much for the additional information. Unfortunately, I still cannot reproduce the problem with the information at hand. (When I run your pom.xml, Maven creates a jar with only top.cmarco.lightlogin.libs classes.)

Regarding your request for clarification: Yes, <class methods="none" classes="public" fields="none"> does work - i.e. it forces yGuard to keep class names of public classes but lets yGuard rename methods and fields of those classes. You can easily verify that this element indeed works as expected with the sample ANT project I posted in my previous reply.

Since the <class> element works in my proof-of-concept sample, but not in your project, there has to be a configuration problem specific to your project. However, I do not see any obvious mistake in your configuration. (Your LightLoginPlugin is a public class, right?)

Since I cannot reproduce the problem, you need to narrow down its cause. E.g. remove all the <class> and <method> children of the <rename> element and check if you get the intended result (i.e. everything is renamed). Then add the

<class methods="none" classes="public" fields="none">
  <patternset>
    <include name="top.cmarco.lightlogin.LightLoginPlugin"/>
  </patternset>
</class>

and check if it work if there are no other exclusion rules. If it does, add one rule after the other to find which rules interfere with each other. If it does not, well, double-check class name spelling, class modifiers, etc.

thomasbehr avatar Mar 11 '24 13:03 thomasbehr

Thanks.

LB45440078L avatar Mar 12 '24 23:03 LB45440078L