<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>And now here&#039;s something… &#187; yguard</title>
	<atom:link href="http://christiansons.net/mike/blog/tag/yguard/feed/" rel="self" type="application/rss+xml" />
	<link>http://christiansons.net/mike/blog</link>
	<description>Java, Ant, SVN, Jetty, cygwin and other stuff…</description>
	<lastBuildDate>Fri, 02 Dec 2011 16:53:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Obfuscating a webapp .war file with yGuard and Ant</title>
		<link>http://christiansons.net/mike/blog/2009/02/obfuscating-a-webapp-war-file-with-yguard-and-ant/</link>
		<comments>http://christiansons.net/mike/blog/2009/02/obfuscating-a-webapp-war-file-with-yguard-and-ant/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 19:07:03 +0000</pubDate>
		<dc:creator>Mike Christianson</dc:creator>
				<category><![CDATA[ant]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[yguard]]></category>

		<guid isPermaLink="false">http://blog.christiansons.net/?p=178</guid>
		<description><![CDATA[One way to protect Java software from casual reverse engineering is obfuscation. Essentially, an obfuscator renames classes, variables, and packages with random/meaningless names. The application still functions correctly; the decompiled code becomes difficult to read. There are quite a few obfuscators for Java &#8212; at least a dozen when I looked &#8212; most of which <a href='http://christiansons.net/mike/blog/2009/02/obfuscating-a-webapp-war-file-with-yguard-and-ant/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>One way to protect Java software from casual reverse engineering is obfuscation.  Essentially, an obfuscator renames classes, variables, and packages with random/meaningless names.  The application still functions correctly; the decompiled code becomes difficult to read.</p>
<p>There are quite a few obfuscators for Java &#8212; at least a dozen when I looked &#8212; most of which are easily used from an Ant script.  </p>
<p>For my use I selected one with a ready-to-use Ant task: <a href="http://www.yworks.com/en/products_yguard_about.html">yGuard</a>.  Unfortunately, yGuard operates on .jar files only, not individual .class files or webapp .war files.  Thus, I have written my own Ant script which uses yGuard and built-in Ant tasks to obfuscate .war files.<span id="more-178"></span></p>
<p style="border: thin dashed;"><em>Update 2009-12-11</em>: If I had to do it over again, I would obfuscate my classes/jars before creating the .war file.</p>
<p>Before you try using my Ant script, read <a href="http://www.yworks.com/products/yguard/yguard_ant_howto.html">yGuard&#8217;s documentation</a> and be sure you fully understand how it works.  You should pay special attention to the <a href="http://www.yworks.com/products/yguard/yguard_ant_howto.html#inoutpairs">inoutpairs</a>, <a href="http://www.yworks.com/products/yguard/yguard_ant_howto.html#keep">keep</a>, and <a href="http://www.yworks.com/products/yguard/yguard_ant_howto.html#hints">hints and troubleshooting</a> sections.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- prepare a temporary directory in which the war file is expanded and obfuscated --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tempfile</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;unwar.dir&quot;</span> <span style="color: #000066;">destdir</span>=<span style="color: #ff0000;">&quot;${java.io.tmpdir}&quot;</span> <span style="color: #000066;">deleteonexit</span>=<span style="color: #ff0000;">&quot;yes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${unwar.dir}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;unwar</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;webapp.war&quot;</span> <span style="color: #000066;">dest</span>=<span style="color: #ff0000;">&quot;${unwar.dir}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- create a jar of webapp classes (required by yguard) for obfuscation --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jar</span> <span style="color: #000066;">destfile</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/lib/webapp.jar&quot;</span> <span style="color: #000066;">whenempty</span>=<span style="color: #ff0000;">&quot;fail&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;zipfileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/classes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/classes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- create a fileset of internal libraries to be obfuscated --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/lib&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;internal.lib.set&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myLibrary.jar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myOtherLibrary.jar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- move the internal libraries to a temporary directory and make a fileset out of them --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tempfile</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;obfuscation.dir&quot;</span> <span style="color: #000066;">destDir</span>=<span style="color: #ff0000;">&quot;${java.io.tmpdir}&quot;</span> <span style="color: #000066;">deleteonexit</span>=<span style="color: #ff0000;">&quot;yes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${obfuscation.dir}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;move</span> <span style="color: #000066;">todir</span>=<span style="color: #ff0000;">&quot;${obfuscation.dir}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">&quot;internal.lib.set&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/move<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- create a jar of web.xml (required by yguard) for obfuscation --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jar</span> <span style="color: #000066;">destfile</span>=<span style="color: #ff0000;">&quot;${obfuscation.dir}/web.xml.jar&quot;</span> <span style="color: #000066;">whenempty</span>=<span style="color: #ff0000;">&quot;fail&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;zipfileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF&quot;</span> <span style="color: #000066;">includes</span>=<span style="color: #ff0000;">&quot;web.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/web.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- make a fileset of all jars to be obfuscated --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${obfuscation.dir}&quot;</span> <span style="color: #000066;">includes</span>=<span style="color: #ff0000;">&quot;*.jar&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;in-out.set&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- make a fileset of the remaining libraries, these are not obfuscated --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;path</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;external.lib.path&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/lib&quot;</span> <span style="color: #000066;">includes</span>=<span style="color: #ff0000;">&quot;*.jar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/path<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;yguard&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;com.yworks.yguard.YGuardTask&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">classpath</span>=<span style="color: #ff0000;">&quot;yguard.jar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;yguard<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;inoutpairs<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- these filesets are inputs to be obfuscated --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">&quot;in-out.set&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/inoutpairs<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;externalclasses</span> <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">&quot;external.lib.path&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>  <span style="color: #808080; font-style: italic;">&lt;!-- external libs, not obfuscated --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rename<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;adjust</span> <span style="color: #000066;">replaceContent</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;web.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>  <span style="color: #808080; font-style: italic;">&lt;!-- modified to reference the obfuscated Servlet --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/adjust<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;keep<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #808080; font-style: italic;">&lt;!-- classes, packages, methods, and fields which should not obfuscated are specified here --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/keep<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rename<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/yguard<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- move our newly obfuscated classes back into the lib area --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;move</span> <span style="color: #000066;">todir</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/lib&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${obfuscation.dir}&quot;</span> <span style="color: #000066;">includes</span>=<span style="color: #ff0000;">&quot;*_obf.jar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/move<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- unjar the adjusted web.xml --&gt;</span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;unzip</span> <span style="color: #000066;">dest</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/lib/web.xml_obf.jar&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;patternset</span> <span style="color: #000066;">includes</span>=<span style="color: #ff0000;">&quot;web.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/unzip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${unwar.dir}/WEB-INF/lib&quot;</span> <span style="color: #000066;">includes</span>=<span style="color: #ff0000;">&quot;web.xml*.jar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/delete<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- rebuild the war file --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;war</span> <span style="color: #000066;">destfile</span>=<span style="color: #ff0000;">&quot;webapp_obf.war&quot;</span> <span style="color: #000066;">basedir</span>=<span style="color: #ff0000;">&quot;${unwar.dir}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p><a href="http://www.yworks.com/">yWorks</a>, the creator of yGuard, is perhaps best known for <a href="http://www.yworks.com/en/products_ydoc.html">yDoc a UML diagram generator for JavaDocs</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://christiansons.net/mike/blog/2009/02/obfuscating-a-webapp-war-file-with-yguard-and-ant/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

