<?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>usefulfor.com/security &#187; Java</title>
	<atom:link href="http://usefulfor.com/security/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://usefulfor.com/security</link>
	<description>security dojo</description>
	<lastBuildDate>Wed, 16 Jun 2010 15:47:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java Bytecode Injection</title>
		<link>http://usefulfor.com/security/2009/10/26/java-bytecode-injection/</link>
		<comments>http://usefulfor.com/security/2009/10/26/java-bytecode-injection/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 10:58:20 +0000</pubDate>
		<dc:creator>etd</dc:creator>
				<category><![CDATA[hack-fu]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://usefulfor.com/security/?p=149</guid>
		<description><![CDATA[When assessing the security posture of a Java thick application we can usually process the code through a decompiler (such as Jad) and have a proper look at the code. It may be the case that we need to bypass the SSL checks but that is usually it.
However, every now and then, we stumble upon [...]]]></description>
			<content:encoded><![CDATA[<p>When assessing the security posture of a Java thick application we can usually process the code through a decompiler (such as <a href="http://www.varaneckas.com/jad">Jad</a>) and have a proper look at the code. It may be the case that we need to <a href="http://usefulfor.com/security/2009/06/23/bypassing-java-thick-client-ssl-checks/">bypass the SSL checks</a> but that is usually it.</p>
<p>However, every now and then, we stumble upon more complex scenarios, for instance, an application whose code has been obfuscated causing decompilation errors. In that case it will no longer be enough to decompile, modify the code and compile again, we would need some other technique. Patching the .class file at the bytecode level sounds like a reasonable approach.<br />
<span id="more-149"></span><br />
Imagine that the class below (<code>Protected</code>) represents the original class we are dealing with. Remember that we would not have access to this code, instead, we would have been given a compiled an obfuscated version of it (different method names, class names and variable names). Also imagine that the class is substantially more complex and has a number of obscure routines that break the decompilation process.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Random</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #000000; font-weight: bold;">Protected</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> checkPassword<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> password<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> argv<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>argv.<span style="color: #006633;">length</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>quote<span style="color: #339933;">;</span>Please provide a password.<span style="color: #339933;">&amp;</span>quote<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> checkPassword<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Success&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Failure&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When compiled (<code>javac Protected.java</code>) and run, the result would be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ java Protected p4ssword
Failure</pre></div></div>

<p>In the obfuscated code scenario you would need to use debugging techniques to identify the relevant code (checkout <a href="http://code.google.com/p/jswat/">JSwat</a> for this). So, after hours of going through the decompiled obfuscated code, we have managed to identify that <code>Protected</code> is the class and <code>checkPassword()</code> is the method we want to focus our attention on.</p>
<p>In order to inject at the bytecode level we are going to use <a href="http://www.csg.is.titech.ac.jp/~chiba/javassist/">Javassist</a>, the Java Programming Assistant. From their website:</p>
<blockquote><p>
[...] it enables Java programs to define a new class at runtime and to modify a class file when the <acronym title="Java Virtual Machine">JVM</acronym> loads it.
</p></blockquote>
<p>Which is exactly what we want to do <img src='http://usefulfor.com/security/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So here is how you would bypass the <code>checkPassword</code> method by manipulating the bytecode of the class:</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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javassist.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Injector
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> argv<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Load the 'Protected' class representation</span>
    ClassPool pool <span style="color: #339933;">=</span> ClassPool.<span style="color: #006633;">getDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    CtClass cc <span style="color: #339933;">=</span> pool.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Protected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Find the method we want to patch and rename it </span>
    <span style="color: #666666; font-style: italic;">// (we will be creating a new method with the original name).</span>
    CtMethod m_old <span style="color: #339933;">=</span> cc.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;checkPassword&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    m_old.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;checkPassword$impl&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Create a new method with the same name as the old one</span>
    CtMethod m_new <span style="color: #339933;">=</span> CtNewMethod.<span style="color: #006633;">copy</span><span style="color: #009900;">&#40;</span>m_old, <span style="color: #0000ff;">&quot;checkPassword&quot;</span>, cc, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Provide the new method's implementation</span>
    StringBuilder sb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;{ return true; }&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    m_new.<span style="color: #006633;">setBody</span><span style="color: #009900;">&#40;</span> sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Add the new method to the class. Patch the .class file</span>
    cc.<span style="color: #006633;">addMethod</span><span style="color: #009900;">&#40;</span> m_new <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cc.<span style="color: #006633;">writeFile</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Injection  complete. List of methods:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    CtMethod<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> methods <span style="color: #339933;">=</span> cc.<span style="color: #006633;">getDeclaredMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>methods.<span style="color: #006633;">length</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #339933;">+</span> methods<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">getLongName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When run, the <code>Injector</code> application will patch the <code>Protected.class</code> file with a new implementation of the <code>checkPassword()</code> method.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ javac <span style="color: #660033;">-cp</span> .:javassist.jar Injector.java
$ java <span style="color: #660033;">-cp</span> .:javaassist.jar Injector
Injection complete. Methods <span style="color: #000000; font-weight: bold;">in</span> class Protected:
    Protected.checkPassword<span style="color: #007800;">$impl</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>java.lang.String<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    Protected.main<span style="color: #7a0874; font-weight: bold;">&#40;</span>java.lang.String<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    Protected.checkPassword<span style="color: #7a0874; font-weight: bold;">&#40;</span>java.lang.String<span style="color: #7a0874; font-weight: bold;">&#41;</span>
$ java Protected p4ssword
Success</pre></div></div>

<p>This is just a simple example of what can be accomplished with a framework such as Javassist. Check the <strong>References</strong> section below for additional information.</p>
<h3>References</h3>
<ul>
<li>Sample code at GitHub: <a href="http://github.com/usefulfor/usefulfor/tree/6c7afe339923b13a4ea71574c856edd6546010d1/security/java_injection">http://github.com/usefulfor/</a></li>
<li><a href="http://www.csg.is.titech.ac.jp/~chiba/javassist/">Javassist</a> (Java Programming Assistant)</li>
<li><a href="http://www.ibm.com/developerworks/java/library/j-dyn0916.html">Class transformation with Javassist</a></li>
</ul>
<img src="http://usefulfor.com/security/?ak_action=api_record_view&id=149&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://usefulfor.com/security/2009/10/26/java-bytecode-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
