<?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>AwghBlog &#187; code audit</title>
	<atom:link href="http://www.awgh.org/archives/tag/code-audit/feed" rel="self" type="application/rss+xml" />
	<link>http://www.awgh.org</link>
	<description>Shaking Your Tree</description>
	<lastBuildDate>Sun, 04 Dec 2011 06:30:37 +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>How To Win At Java Code Audit</title>
		<link>http://www.awgh.org/archives/4</link>
		<comments>http://www.awgh.org/archives/4#comments</comments>
		<pubDate>Mon, 02 Feb 2009 22:14:57 +0000</pubDate>
		<dc:creator>awgh</dc:creator>
				<category><![CDATA[Java Security]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[code audit]]></category>
		<category><![CDATA[exploit]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.awgh.org/?p=4</guid>
		<description><![CDATA[Reviewing Java source code can pose a challenge for a security auditor, as methods used to exploit programs in C or C++, namely memory corruption bugs, are mitigated by Java itself, which hides the details of memory management from the programmer.  This same tendency to hide implementation details with a layer of abstraction leads to [...]]]></description>
			<content:encoded><![CDATA[<p>Reviewing Java source code can pose a challenge for a security auditor, as methods used to exploit programs in C or C++, namely memory corruption bugs, are mitigated by Java itself, which hides the details of memory management from the programmer.  This same tendency to hide implementation details with a layer of abstraction leads to an entire class of common Java programming errors which can have a critical impact on the security of the application.</p>
<p>Java vulnerabilities are most commonly found in places where unsanitized user input is passed, directly or indirectly, on to an underlying library or service.  To put it another way, vulnerabilities aren&#8217;t found in the Java code itself, they are found by following user input through the Java source and out the other side.</p>
<p>The tendency of Java to hide implementation details from the developer actually creates these vulnerabilities in places where it might not otherwise exist.  Java developers use wrapper libraries for backend services, such as SQL or LDAP, and assume that they automatically sanitize their inputs, when usually they do not.  In most cases, Java wrapper libraries themselves are simply classes that store and manipulate strings which are just passed directly on to the wrapped service.  In many of these implementations, such as the ORM library Hibernate, there are architectural reasons why this behavior can not be changed.</p>
<p>In this post, I will describe a class of extremely common Java vulnerabilities, specifically these &#8220;pass-through&#8221; bugs, characterized by user input passing directly through Java unexamined.</p>
<p><span id="more-4"></span></p>
<p>For our first example, we&#8217;ll look at one of the most commonly used (and misused) constructs in the Java programming language:</p>
<p><strong>The File Class Constructor</strong></p>
<p>The File class has several constructors, but the most common takes a single string argument, which is the full path to a file.  The second most commonly used constructor takes two string arguments, which are effectively appended together and treated the same as the single string argument.</p>
<p>The Java documentation uses the word &#8216;Canonicalization&#8217; all over the place.  All paths fed in to the File constructor are canonicalized.  Many people understand this as &#8220;All the dot-dot-slashes are removed.&#8221;</p>
<p>While this is technically true, a canonicalized path has no path meta-characters, canonicalization doesn&#8217;t simply remove them &#8211; it resolves them correctly!</p>
<p>For example, the path &#8220;/www/hosts/mydomain.org/docs/../../../../etc/&#8221; would be &#8220;/etc/&#8221; after canonicalization.</p>
<p>This confusion over what canonicalization means commonly leads to directory traversal vulnerabilities in Java-based services.</p>
<p>Imagine a simple web server in Java, which does the following:</p>
<ol>
<li>Accept an HTTP request for a particular URL:  &#8220;http://www.mydomain.org/PATH&#8221;</li>
<li>Calls the File constructor with the web root and path:  File f = new File( &#8220;/www/hosts/mydomain.org/docs&#8221;, PATH );</li>
<li>Simply opens the file and returns it to the requester as an HTTP response.</li>
</ol>
<p>Perhaps the assumption is that somehow the File constructor filters out path meta-characters such as &#8220;../&#8221;, which it doesn&#8217;t.  Some developers assume that the first argument to the file constructor will somehow act like a chroot and prevent &#8220;../&#8221; in the second argument from traversing to a higher directory.  This is not the case, as both arguments are simply appended and treated as one big path string.</p>
<p>Whatever the developer assumptions, this error appears in different variations across a surprisingly large percentage of Java code.</p>
<p>This category of errors comes from the fact that Java can&#8217;t interact with the file system directly &#8211; it has to pass path information through to the operating system.  In fact, the specific path meta-characters that can lead to injection will vary from platform to platform &#8211; even though Java tries to be &#8220;platform independent&#8221;!  An obvious example:  &#8220;../&#8221; on a Linux system is the same as &#8220;..\&#8221; on Windows.</p>
<p>To find these errors, simply search for places where user-controlled input is passed directly in to the File class constructor, without any additional logic to remove path meta-characters such as &#8220;../&#8221; or &#8220;..\&#8221;.</p>
<p><!--more--></p>
<p>Our next example of a &#8220;pass-through&#8221; bug is in the use of a common Java logging library:</p>
<p><strong>Log4J Javascript Injection</strong></p>
<p>The most commonly used Java logging library is Log4J from Apache.  Log4J provides a number of different methods that write data to a log file,  for example:<br />
<code><br />
Logger log = Logger.getLogger("mylogger");<br />
log.error("This is an error message");<br />
log.warn("This is a warning message");<br />
log.debug("This is a debug message");</code></p>
<p>Log4J does not do any sanitization of strings passed in to it by the various logging methods, it simply takes the string it is given and writes this directly to the log file.</p>
<p>Most web applications that use Log4J will commonly include user-supplied values in at least some of their logging messages, for example:</p>
<p><code><br />
protected void doGet(HttpServletRequest request, HttpServletResponse response)<br />
throws ServletException, IOException<br />
{<br />
// do stuff, then on error something like:<br />
Logger logger = Logger.getLogger("GetLogger");<br />
logger.error( "Invalid value for parameter fnord: "+ request.getParameter("fnord"));<br />
}</code></p>
<p>This makes some kind of sense &#8211; an error condition has been caused by invalid input, so the developer wants to see what the bad input was.</p>
<p>Web developers are also in the habit of viewing their web application log files directly from the web server, sometimes they even include HTML formatting tags in with their calls to Log4J methods so that the logs will be formatted nicely in the browser.</p>
<p>Imagine the following common scenario:</p>
<ul>
<li>The production web server is on the domain <strong>http://somesite.com</strong> and the QA server is on <strong>http://qa.somesite.com</strong>.</li>
<li>Developers working on the QA server routinely view the Log4J logs through the browser by visiting <strong>http://qa.somesite.com/logs/mylog</strong>.</li>
<li>The subdomain <strong>www.somesite.com</strong> redirects to <strong>somesite.com</strong>, so all of the regular sites domain cookies are for <strong>somesite.com</strong>.</li>
</ul>
<p>Now we construct a standard Cross-Site Scripting cookie-stealing attack by injecting some Javascript into the &#8220;fnord&#8221; parameter mentioned above.  The program will then save this Javascript into the Log4J logs.</p>
<p>When the developer opens these logs in the browser, the Javascript will execute, and have access to the cookies for the domain <strong>qa.somesite.com</strong>.</p>
<p>However, the situation is much worse than this.  There is a <a href="https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript">single exception</a> to the Same Origin Policy which happens to apply to this situation.</p>
<p>A Javascript can set its effective domain by manipulating the &#8220;document.domain&#8221; variable.  The exception to the Same Origin Policy says that a script can only set its domain to a suffix of its current domain.</p>
<p>This means that an attack script that runs from <strong>qa.somesite.com </strong>can reset its domain to <strong>somesite.com</strong>, and then access all of <strong>somesite.com</strong>&#8216;s cookies!</p>
<p>A successful log-file script injection on a QA, development, or staging server which is a subdomain can result in stealing saved credentials from developers for the main domain!</p>
<p>This category of attacks passes directly through Java into a log file, which is then loaded by a browser.</p>
<p>To find these types of errors, look for calls to Log4J or similar logging functions which include HTML formatting tags and/or unsanitized user input.</p>
<p><!--more--><strong></strong></p>
<p><strong>Stay tuned</strong> for the second exciting installment of &#8220;How to Win at Java Code Audit&#8221;, including LDAP injection, Null Byte Injection, and ORM injection!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.awgh.org/archives/4/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dehydra-GCC: Static Analysis for Poor People</title>
		<link>http://www.awgh.org/archives/73</link>
		<comments>http://www.awgh.org/archives/73#comments</comments>
		<pubDate>Thu, 25 Dec 2008 06:15:39 +0000</pubDate>
		<dc:creator>awgh</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[code audit]]></category>
		<category><![CDATA[computer security]]></category>
		<category><![CDATA[exploit]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[infosec]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pen test]]></category>

		<guid isPermaLink="false">http://www.awgh.org/?p=73</guid>
		<description><![CDATA[Over the past few months, I&#8217;ve been playing with a new static analysis tool from Mozilla called Dehydra. Dehydra is a GCC plugin that allows you to write Javascript that can perform queries on the Abstract Syntax Tree (AST) that GCC generates from source files.  This lets you write a script that can notify you [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past few months, I&#8217;ve been playing with a new static analysis tool from Mozilla called <a href="https://developer.mozilla.org/En/Dehydra_GCC">Dehydra</a>.</p>
<p>Dehydra is a GCC plugin that allows you to write Javascript that can perform queries on the Abstract Syntax Tree (AST) that GCC generates from source files.  This lets you write a script that can notify you when it sees any type of code construct that you can describe in script.</p>
<p>There are a number of code constructs that might be interesting to a code auditor, for example:</p>
<ul>
<li>Calls to asnprintf, malloc, or calloc with unchecked return values.</li>
<li>Assignment operations where the datatype of the Left Hand Side is signed and the Right Hand Side is unsigned, or vice versa.</li>
<li>Assignment operations where the datatypes of both sides have different bit-lengths.</li>
</ul>
<p>The possibilities are much greater than my short list of examples!</p>
<p>I will be the first to admit that static analysis has its faults.  For one thing, it has been proven that static analysis cannot discover all possible bugs in any given program.  Commercial static analysis tools, such as Coverity, are expensive and have not proven to be a particularly effective method of finding bugs by themselves.  I have heard many accounts of nasty bugs discovered by code auditors when looking through source code routinely scanned by Coverity.</p>
<p>That said, on Day One of a code audit, 4 out of 5 code auditors find themselves reaching for Grep.</p>
<p>Grep is great, it lets you search for regular expressions across many files very quickly, but Grep has no awareness of the syntax of the C++ programming language.  I&#8217;m really more interested in searching for specific code constructs and less interested in searching for substrings, which is Grep&#8217;s purpose.</p>
<p>When looking for vulnerabilities, I&#8217;m not interested in searching for the string &#8220;malloc&#8221;.  What I <em>really</em> want to know is more along the lines of &#8220;Where are all the calls to malloc where the return value is not checked&#8221;.  I don&#8217;t want to know all the locations of the string &#8220;int&#8221; as much as I want to know every location that a variable of type <em>int</em> is implicitly cast to an <em>unsigned int</em> when passed in as a function argument.</p>
<p>This is the great thing about Dehydra.  It lets you query the parsed syntax tree of C++ source code and ask the kinds of questions that can&#8217;t be easily answered by Grep.</p>
<p>Scripts for Dehydra are written in Javascript by way of the SpiderMonkey engine.  Javascript is a nice, small language that is good for operations on tree-like data structures.  In a browser, this would mean the DOM, but in GCC this means the AST!</p>
<p>Dehydra is still in development, but the developers have been extremely responsive to feature requests from security auditors ( well, mine anyway&#8230; *grin* ).</p>
<p>It would be great to see a bunch of people contribute scripts and build a big set of security scanning scripts to replace the venerable regular-expression-based <a href="http://www.dwheeler.com/flawfinder/">FlawFinder</a> as the king of no-budget security-oriented static analysis.</p>
<p>Try it out and get back to me.</p>
<p><a href="https://developer.mozilla.org/En/Dehydra_GCC">Setup and Installation Instructions for Dehydra on Linux or OSX</a></p>
<p>I&#8217;ve included a sample Dehydra script below that logs a message anytime it sees certain assignment operations.</p>
<p>The full sample script, along with a test file, is available <a href="http://awgh.org/files/dehydra-example.tgz">here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;color: #FCFFBA;"><span style="color: #003366; font-weight: bold;color: #577A61;">function</span> assignVisitor<span style="color: #009900;color: #CCC;">&#40;</span>node<span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #009900;color: #CCC;">&#123;</span>
   <span style="color: #000066; font-weight: bold;color: #B83A24;">for</span><span style="color: #009900;color: #CCC;">&#40;</span><span style="color: #003366; font-weight: bold;color: #577A61;">var</span> i <span style="color: #000066; font-weight: bold;color: #B83A24;">in</span> node.<span style="color: #660066;">statements</span><span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #009900;color: #CCC;">&#123;</span>
      <span style="color: #003366; font-weight: bold;color: #577A61;">var</span> loc <span style="color: #339933;color: #CCC;">=</span> node.<span style="color: #660066;">loc</span>
      <span style="color: #003366; font-weight: bold;color: #577A61;">var</span> lhs <span style="color: #339933;color: #CCC;">=</span> node.<span style="color: #660066;">statements</span><span style="color: #009900;color: #CCC;">&#91;</span>i<span style="color: #009900;color: #CCC;">&#93;</span>.<span style="color: #660066;">type</span>
      <span style="color: #003366; font-weight: bold;color: #577A61;">var</span> rhs <span style="color: #339933;color: #CCC;">=</span> node.<span style="color: #660066;">statements</span><span style="color: #009900;color: #CCC;">&#91;</span>i<span style="color: #009900;color: #CCC;">&#93;</span>.<span style="color: #660066;">assign</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;color: #B83A24;">if</span><span style="color: #009900;color: #CCC;">&#40;</span> rhs <span style="color: #339933;color: #CCC;">&amp;</span>amp<span style="color: #339933;color: #CCC;">;&amp;</span>amp<span style="color: #339933;color: #CCC;">;</span> lhs <span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #009900;color: #CCC;">&#123;</span>
         <span style="color: #000066; font-weight: bold;color: #B83A24;">if</span><span style="color: #009900;color: #CCC;">&#40;</span> lhs.<span style="color: #660066;">unsigned</span> <span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #009900;color: #CCC;">&#123;</span>
            <span style="color: #000066; font-weight: bold;color: #B83A24;">if</span><span style="color: #009900;color: #CCC;">&#40;</span>parseInt<span style="color: #009900;color: #CCC;">&#40;</span>rhs<span style="color: #009900;color: #CCC;">&#91;</span><span style="color: #CC0000;color: #DDD;">0</span><span style="color: #009900;color: #CCC;">&#93;</span>.<span style="color: #660066;">value</span><span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #339933;color: #CCC;">&amp;</span>gt<span style="color: #339933;color: #CCC;">;</span> <span style="color: #CC0000;color: #DDD;">0</span><span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #009900;color: #CCC;">&#123;</span>
               <span style="color: #000066;color: #8FB394;">print</span><span style="color: #009900;color: #CCC;">&#40;</span> <span style="color: #3366CC;color: #666666;">&quot;ASSIGN: negative to unsigned at:&quot;</span><span style="color: #339933;color: #CCC;">+</span>loc<span style="color: #339933;color: #CCC;">+</span><span style="color: #3366CC;color: #666666;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;color: #CCC;">&#41;</span>
            <span style="color: #009900;color: #CCC;">&#125;</span>
            <span style="color: #000066; font-weight: bold;color: #B83A24;">else</span> <span style="color: #000066; font-weight: bold;color: #B83A24;">if</span><span style="color: #009900;color: #CCC;">&#40;</span>rhs<span style="color: #009900;color: #CCC;">&#91;</span><span style="color: #CC0000;color: #DDD;">0</span><span style="color: #009900;color: #CCC;">&#93;</span>.<span style="color: #660066;">type</span> <span style="color: #339933;color: #CCC;">&amp;</span>amp<span style="color: #339933;color: #CCC;">;&amp;</span>amp<span style="color: #339933;color: #CCC;">;</span> <span style="color: #339933;color: #CCC;">!</span>rhs<span style="color: #009900;color: #CCC;">&#91;</span><span style="color: #CC0000;color: #DDD;">0</span><span style="color: #009900;color: #CCC;">&#93;</span>.<span style="color: #660066;">type</span>.<span style="color: #660066;">unsigned</span><span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #009900;color: #CCC;">&#123;</span>
               <span style="color: #000066;color: #8FB394;">print</span><span style="color: #009900;color: #CCC;">&#40;</span> <span style="color: #3366CC;color: #666666;">&quot;ASSIGN: signed to unsigned at:&quot;</span><span style="color: #339933;color: #CCC;">+</span>loc<span style="color: #339933;color: #CCC;">+</span><span style="color: #3366CC;color: #666666;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;color: #CCC;">&#41;</span>
            <span style="color: #009900;color: #CCC;">&#125;</span>
         <span style="color: #009900;color: #CCC;">&#125;</span>
         <span style="color: #000066; font-weight: bold;color: #B83A24;">else</span> <span style="color: #000066; font-weight: bold;color: #B83A24;">if</span><span style="color: #009900;color: #CCC;">&#40;</span>rhs<span style="color: #009900;color: #CCC;">&#91;</span><span style="color: #CC0000;color: #DDD;">0</span><span style="color: #009900;color: #CCC;">&#93;</span>.<span style="color: #660066;">type</span><span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #009900;color: #CCC;">&#123;</span><span style="color: #006600; font-style: italic;color: #CDC;">// lhs is signed</span>
            <span style="color: #000066; font-weight: bold;color: #B83A24;">if</span><span style="color: #009900;color: #CCC;">&#40;</span> rhs<span style="color: #009900;color: #CCC;">&#91;</span><span style="color: #CC0000;color: #DDD;">0</span><span style="color: #009900;color: #CCC;">&#93;</span>.<span style="color: #660066;">type</span>.<span style="color: #660066;">unsigned</span> <span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #009900;color: #CCC;">&#123;</span>
               <span style="color: #000066;color: #8FB394;">print</span><span style="color: #009900;color: #CCC;">&#40;</span> <span style="color: #3366CC;color: #666666;">&quot;ASSIGN: unsigned to signed at:&quot;</span><span style="color: #339933;color: #CCC;">+</span>loc<span style="color: #339933;color: #CCC;">+</span><span style="color: #3366CC;color: #666666;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;color: #CCC;">&#41;</span><span style="color: #339933;color: #CCC;">;</span>
            <span style="color: #009900;color: #CCC;">&#125;</span>
         <span style="color: #009900;color: #CCC;">&#125;</span>
      <span style="color: #009900;color: #CCC;">&#125;</span>
   <span style="color: #009900;color: #CCC;">&#125;</span>
<span style="color: #009900;color: #CCC;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;color: #577A61;">function</span> process_function<span style="color: #009900;color: #CCC;">&#40;</span>decl<span style="color: #339933;color: #CCC;">,</span>body<span style="color: #009900;color: #CCC;">&#41;</span> <span style="color: #009900;color: #CCC;">&#123;</span>
   iter<span style="color: #009900;color: #CCC;">&#40;</span>assignVisitor<span style="color: #339933;color: #CCC;">,</span> body<span style="color: #009900;color: #CCC;">&#41;</span>
<span style="color: #009900;color: #CCC;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.awgh.org/archives/73/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: basic
Database Caching 1/18 queries in 0.140 seconds using disk: basic
Object Caching 309/329 objects using disk: basic

Served from: www.awgh.org @ 2012-02-05 19:27:16 -->
