如何使用 Ant 和 Ant 混淆我的 Web 应用程序 project.war守卫?
我们开发了一个 Web 应用程序(基于 struts 1.x/Hibernate),我为其构建了一个 使用 ANT 构建脚本生成 war 文件。现在,我的公司希望我混淆 .classes 文件 在产生战争之前将其分发给客户端。当我谷歌搜索时,我来了 一个使用的示例YGuard库来完成这个任务。然而,该链接非常有用,我只取得了部分成功,因为它混淆了所有 java 类,留下了未混淆的 hibernate 映射 (*.hbm.xml) 文件,其中引用了这些已混淆的类。
例如:混淆后,对 MenuGlobalBean.class 的引用将变成类似 ABHIN 的内容(其中 A、B.. 是包名称,N 是类名称)。 但我的 MenuGlobal.hbm.xml 仍然将其称为而
<class name="com.mycompany.myproduct.bean.MenuGlobalBean" table="MENU_GLOBAL">
不是
<class name="A.B.H.I.N" table="MENU_GLOBAL">
现在我的问题是如何以这样的方式混淆我的战争文件 混淆的类引用反映在我的 *.hbm.xml & 中。其他配置/属性文件(如果有)。
下面是我使用 YGuard 库进行混淆的完整 ANT 构建脚本
<!-- Build MyProject.war section -->
<project name="MyProject" default="dist" basedir=".">
<property name="proj-home" value="/home/simba/tomcat-7.0.19/webapps/MyProject" />
<!-- set global properties for this build -->
<property name="src" location="WEB-INF/src"/>
<property name="build" location="build"/>
<property name="lib" location="WEB-INF/lib"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<path id="project-classpath">
<fileset dir="${proj-home}/WEB-INF/lib" includes="*.jar" />
</path>
<target name="copy-non-java-files">
<copy todir="build" includeemptydirs="false">
<fileset dir=".">
<include name="*" />
<include name="css/**/*" />
<include name="help_files/**/*" />
<include name="images/**/*" />
<include name="js/**/*" />
<include name="jsp/**/*" />
<include name="schemas/**/*" />
<include name="Sounds/**/*" />
<include name="VideoImage/**/*" />
<exclude name="WEB-INF/src" />
<exclude name="yguard.jar" />
<exclude name="*.war" />
<exclude name="build.xml" />
</fileset>
<fileset dir=".">
<include name="WEB-INF/classes/**/*" />
<include name="WEB-INF/classes/*.xml" />
<include name="WEB-INF/lib/**/*" />
<include name="WEB-INF/*.xml" />
<include name="WEB-INF/*.properties"/>
<include name="WEB-INF/*.dtd" />
<include name="WEB-INF/*.tld" />
<include name="WEB-INF/*.txt" />
<include name="WEB-INF/*.ico" />
</fileset>
</copy>
</target>
<target name="compile" depends="clean,init,copy-non-java-files" description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}/WEB-INF/classes" classpathref="project-classpath"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<war jarfile="${dist}/lib/MyProject.war" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
<!-- Using Yguard to obfuscate my .war file -->
<!-- prepare a temporary directory in which the war file is expanded and obfuscated -->
<tempfile property="unwar.dir" destdir="${java.io.tmpdir}" deleteonexit="no"/>
<mkdir dir="${unwar.dir}"/>
<unwar src="${dist}/lib/MyProject.war" dest="${unwar.dir}"/>
<!-- create a jar of webapp classes (required by yguard) for obfuscation -->
<jar destfile="${unwar.dir}/WEB-INF/lib/MyProject.jar" whenempty="fail">
<zipfileset dir="${unwar.dir}/WEB-INF/classes" excludes="*.xml,*.properties"/>
</jar>
<delete dir="${unwar.dir}/WEB-INF/classes/*" excludes="*.xml,*.properties"/>
<!-- create a fileset of internal libraries to be obfuscated -->
<fileset dir="${unwar.dir}/WEB-INF/lib" id="internal.lib.set">
<include name="MyProject.jar"/>
</fileset>
<!-- move the internal libraries to a temporary directory and make a fileset out of them -->
<tempfile property="obfuscation.dir" destDir="${java.io.tmpdir}" deleteonexit="yes"/>
<mkdir dir="${obfuscation.dir}"/>
<move todir="${obfuscation.dir}">
<fileset refid="internal.lib.set"/>
</move>
<!-- create a jar of web.xml (required by yguard) for obfuscation -->
<jar destfile="${obfuscation.dir}/web.xml.jar" whenempty="fail">
<zipfileset dir="${unwar.dir}/WEB-INF" includes="*.xml"/>
</jar>
<!--<delete file="${unwar.dir}/WEB-INF/web.xml"/> -->
<!-- make a fileset of all jars to be obfuscated -->
<fileset dir="${obfuscation.dir}" includes="*.jar" id="in-out.set"/>
<!-- make a fileset of the remaining libraries, these are not obfuscated -->
<path id="external.lib.path">
<fileset dir="${unwar.dir}/WEB-INF/lib" includes="*.jar"/>
</path>
<taskdef name="yguard"
classname="com.yworks.yguard.YGuardTask"
classpath="../ref/yguard.jar"/>
<yguard>
<inoutpairs>
<!-- these filesets are inputs to be obfuscated -->
<fileset refid="in-out.set"/>
</inoutpairs>
<externalclasses refid="external.lib.path"/> <!-- external libs, not obfuscated -->
<rename>
<adjust replaceContent="true">
<include name="web.xml"/> <!-- modified to reference the obfuscated Servlet -->
<include name="struts-config.xml"/>
<include name="*.hbm.xml"/>
</adjust>
<keep>
<!-- classes, packages, methods, and fields which should not obfuscated are specified here -->
</keep>
</rename>
</yguard>
<!-- move our newly obfuscated classes back into the lib area -->
<move todir="${unwar.dir}/WEB-INF/lib">
<fileset dir="${obfuscation.dir}" includes="*_obf.jar"/>
</move>
<!-- unjar the adjusted web.xml -->
<unzip dest="${unwar.dir}/WEB-INF/" src="${unwar.dir}/WEB-INF/lib/web.xml_obf.jar">
<patternset includes="*.xml"/>
</unzip>
<!-- <delete>
<fileset dir="${unwar.dir}/WEB-INF/lib" includes="web.xml*.jar"/>
</delete> -->
<!-- rebuild the war file -->
<war destfile="MyProject_obf.war" basedir="${unwar.dir}"/>
</project>
We developed a web application (struts 1.x/Hibernate based) for which I built a
war file using ANT build script. Now, my company wants me to obfuscate the .classes files
before generating a war & distributing it to the client. When I googled, I came
across an example using YGuard library to accomplish this task. The link was pretty useful however, I only had a partial success, as it obfuscated all the java classes, leaving behind the hibernate mapping (*.hbm.xml) files un-obfuscated, which had references to these classes which were already obfuscated.
For example: After obfuscation, references to MenuGlobalBean.class would turn to something like say A.B.H.I.N(where A,B..are package names & N is the class name).
But my MenuGlobal.hbm.xml still refers to this as
<class name="com.mycompany.myproduct.bean.MenuGlobalBean" table="MENU_GLOBAL">
rather than
<class name="A.B.H.I.N" table="MENU_GLOBAL">
Now my question is how do I obfuscate my war file in such a way that
the obfuscated class references reflect in my *.hbm.xml & other config/property files if any.
Below is my complete ANT build script using YGuard library for obfuscation
<!-- Build MyProject.war section -->
<project name="MyProject" default="dist" basedir=".">
<property name="proj-home" value="/home/simba/tomcat-7.0.19/webapps/MyProject" />
<!-- set global properties for this build -->
<property name="src" location="WEB-INF/src"/>
<property name="build" location="build"/>
<property name="lib" location="WEB-INF/lib"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<path id="project-classpath">
<fileset dir="${proj-home}/WEB-INF/lib" includes="*.jar" />
</path>
<target name="copy-non-java-files">
<copy todir="build" includeemptydirs="false">
<fileset dir=".">
<include name="*" />
<include name="css/**/*" />
<include name="help_files/**/*" />
<include name="images/**/*" />
<include name="js/**/*" />
<include name="jsp/**/*" />
<include name="schemas/**/*" />
<include name="Sounds/**/*" />
<include name="VideoImage/**/*" />
<exclude name="WEB-INF/src" />
<exclude name="yguard.jar" />
<exclude name="*.war" />
<exclude name="build.xml" />
</fileset>
<fileset dir=".">
<include name="WEB-INF/classes/**/*" />
<include name="WEB-INF/classes/*.xml" />
<include name="WEB-INF/lib/**/*" />
<include name="WEB-INF/*.xml" />
<include name="WEB-INF/*.properties"/>
<include name="WEB-INF/*.dtd" />
<include name="WEB-INF/*.tld" />
<include name="WEB-INF/*.txt" />
<include name="WEB-INF/*.ico" />
</fileset>
</copy>
</target>
<target name="compile" depends="clean,init,copy-non-java-files" description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}/WEB-INF/classes" classpathref="project-classpath"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<war jarfile="${dist}/lib/MyProject.war" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
<!-- Using Yguard to obfuscate my .war file -->
<!-- prepare a temporary directory in which the war file is expanded and obfuscated -->
<tempfile property="unwar.dir" destdir="${java.io.tmpdir}" deleteonexit="no"/>
<mkdir dir="${unwar.dir}"/>
<unwar src="${dist}/lib/MyProject.war" dest="${unwar.dir}"/>
<!-- create a jar of webapp classes (required by yguard) for obfuscation -->
<jar destfile="${unwar.dir}/WEB-INF/lib/MyProject.jar" whenempty="fail">
<zipfileset dir="${unwar.dir}/WEB-INF/classes" excludes="*.xml,*.properties"/>
</jar>
<delete dir="${unwar.dir}/WEB-INF/classes/*" excludes="*.xml,*.properties"/>
<!-- create a fileset of internal libraries to be obfuscated -->
<fileset dir="${unwar.dir}/WEB-INF/lib" id="internal.lib.set">
<include name="MyProject.jar"/>
</fileset>
<!-- move the internal libraries to a temporary directory and make a fileset out of them -->
<tempfile property="obfuscation.dir" destDir="${java.io.tmpdir}" deleteonexit="yes"/>
<mkdir dir="${obfuscation.dir}"/>
<move todir="${obfuscation.dir}">
<fileset refid="internal.lib.set"/>
</move>
<!-- create a jar of web.xml (required by yguard) for obfuscation -->
<jar destfile="${obfuscation.dir}/web.xml.jar" whenempty="fail">
<zipfileset dir="${unwar.dir}/WEB-INF" includes="*.xml"/>
</jar>
<!--<delete file="${unwar.dir}/WEB-INF/web.xml"/> -->
<!-- make a fileset of all jars to be obfuscated -->
<fileset dir="${obfuscation.dir}" includes="*.jar" id="in-out.set"/>
<!-- make a fileset of the remaining libraries, these are not obfuscated -->
<path id="external.lib.path">
<fileset dir="${unwar.dir}/WEB-INF/lib" includes="*.jar"/>
</path>
<taskdef name="yguard"
classname="com.yworks.yguard.YGuardTask"
classpath="../ref/yguard.jar"/>
<yguard>
<inoutpairs>
<!-- these filesets are inputs to be obfuscated -->
<fileset refid="in-out.set"/>
</inoutpairs>
<externalclasses refid="external.lib.path"/> <!-- external libs, not obfuscated -->
<rename>
<adjust replaceContent="true">
<include name="web.xml"/> <!-- modified to reference the obfuscated Servlet -->
<include name="struts-config.xml"/>
<include name="*.hbm.xml"/>
</adjust>
<keep>
<!-- classes, packages, methods, and fields which should not obfuscated are specified here -->
</keep>
</rename>
</yguard>
<!-- move our newly obfuscated classes back into the lib area -->
<move todir="${unwar.dir}/WEB-INF/lib">
<fileset dir="${obfuscation.dir}" includes="*_obf.jar"/>
</move>
<!-- unjar the adjusted web.xml -->
<unzip dest="${unwar.dir}/WEB-INF/" src="${unwar.dir}/WEB-INF/lib/web.xml_obf.jar">
<patternset includes="*.xml"/>
</unzip>
<!-- <delete>
<fileset dir="${unwar.dir}/WEB-INF/lib" includes="web.xml*.jar"/>
</delete> -->
<!-- rebuild the war file -->
<war destfile="MyProject_obf.war" basedir="${unwar.dir}"/>
</project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用与加密 web.xml 中的引用相同的技巧——暂时将 Hibernate .xml 文件放入 jar 中。 (请参阅“创建一个 web.xml 的 jar(yguard 需要)进行混淆”注释的部分。)
Use the same trick I used to encrypt the references in web.xml -- temporarily put the Hibernate .xml files into a jar. (See the section commented by "create a jar of web.xml (required by yguard) for obfuscation".)