<?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>time to bleed by Joe Damato &#187; security</title>
	<atom:link href="http://timetobleed.com/category/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://timetobleed.com</link>
	<description>technical ramblings from a wanna-be unix dinosaur</description>
	<lastBuildDate>Tue, 20 Jul 2010 21:03:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>WARNING: American Express fails miserably at basic security.</title>
		<link>http://timetobleed.com/warning-american-express-fails-miserably-at-basic-security/</link>
		<comments>http://timetobleed.com/warning-american-express-fails-miserably-at-basic-security/#comments</comments>
		<pubDate>Tue, 25 May 2010 18:54:55 +0000</pubDate>
		<dc:creator>Joe Damato</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[systems]]></category>
		<category><![CDATA[vulnerability]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=1711</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. As of 3:35pm PST on 5/25/2010 it seems to be fixed. wireshark shows only TLS traffic now, nothing in the clear. Pretty quick fix, since this was published at 11:54am. Good deal. This article is going to reveal a pretty [...]]]></description>
			<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/americanexpress.jpg" alt="" width="400" height="300" /></center><br />
If you enjoy this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>As of 3:35pm PST on 5/25/2010 it seems to be fixed. wireshark shows only TLS traffic now, nothing in the clear. Pretty quick fix, since this was published at 11:54am. Good deal.</h2>
<p>
<b>This article is going to reveal a pretty serious error in a web form on the American Express Network website. <u>I would strongly recommend NOT filling out the web form described below</u>.</b></p>
<h2>Daily Wish from the American Express Network</h2>
<p><a href="http://dailywish.amexnetwork.com">Daily wish</a> from the <a href="http://www1.amexnetwork.com/?issuerName=us_amexnetworkdefault">American Express Network</a> sent me an email this morning trying to get me to sign up for their deal of the day service where they offer a very limited quantity of products for a low price.</p>
<p>Sounds simple enough, right?</p>
<p>Well, the time of the sale is not released until the day the sale occurs, <b>unless</b> you are an American Express cardholder. If you are a card holder, you get a special landing page on their website telling you that if you sign up, you can get the sale times before the sale date.</p>
<p>The white arrow below points to the tab that only appears if you clicked through from an email from American Express. The red arrow below points to the sign up button. Take a look:<br />
<br />
<img src="http://timetobleed.com/images/amexlanding2.png" width="650"/>
</p>
<h2>Sign up page</h2>
<p>After clicking the sign up button (red arrow above), a lightbox appears asking for:</p>
<ul>
<li><b>First and last name</b></li>
<li><b>American Express credit card number</b></li>
<li><b>Security code</b></li>
<li><b>Expiration date</b></li>
<li><b>Billing zip</b></li>
</ul>
<p>Quite a bit of personal information, much of it sensitive. [sarcarsm]<b>Don&#8217;t worry the page is secure</b>[/sarcasm], see the form and the white arrow below:<br />
<br />
<img src="http://timetobleed.com/images/amexsignup.png">
</p>
<h2>The code from the form</h2>
<p>This form looked very suspicious to me, so I decided to take a look at the code to see if the <code>action</code> for this sign up form was over <code>HTTPS</code>. Check it:</p>
<pre>
&lt;form name="form1" method="post" action="preid2.aspx?ct=7" onsubmit="javascript:return WebForm_OnSubmit();" id="form1"&gt;
</pre>
<p>
<p>So the action is to a handler at <code>http://dailywish.amexnetwork.com/preid2.aspx?ct=7</code>. <u><b>The lack of <code>https</code> doesn&#8217;t make me feel very good.</b></u></p>
<p>Maybe the <code>WebForm_OnSubmit()</code> function is doing something that might make this secure?  Let&#8217;s take a look:<br />
</p>
<pre>
&lt;script type="text/javascript">
//&lt;![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" &#038;&#038; ValidatorOnSubmit() == false) return false;
return true;
}
//]]&gt;
&lt;/script&gt;
</pre>
<p>
<br />
So it looks like that function is just a validator. It is really starting to feel like this form is insecure.
</p>
<p>Let&#8217;s bring out wireshark and see what it has to say.</p>
<h2>Wireshark packet sniff</h2>
<p>So I filled out the form with <u>fake information</u> and sniffed the <code>POST</code> to the server.</p>
<p><b>The Daily Wish sign up form from the American Express Network is sending credit card numbers, expiration dates, and all the other personal information on the sign up form <u><i>in the clear</i></u> back to their server.</b></p>
<p><i>Holy. Fuck.</i></p>
<p>
<img src="http://timetobleed.com/images/amexfail.png">
</p>
<h2>Conclusion</h2>
<ul>
<li><b>Do NOT fill out the form until American Express fixes this issue.</b></li>
</ul>
<p>Thanks for reading and don&#8217;t forget to <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://timetobleed.com/warning-american-express-fails-miserably-at-basic-security/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Defeating the Matasano C++ Challenge with ASLR enabled</title>
		<link>http://timetobleed.com/defeating-the-matasano-c-challenge-with-aslr-enabled/</link>
		<comments>http://timetobleed.com/defeating-the-matasano-c-challenge-with-aslr-enabled/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 11:59:29 +0000</pubDate>
		<dc:creator>Joe Damato</dc:creator>
				<category><![CDATA[bugfix]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[systems]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[x86]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[vulnerability]]></category>
		<category><![CDATA[x86_64]]></category>

		<guid isPermaLink="false">http://timetobleed.com/?p=1152</guid>
		<description><![CDATA[If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter. Important note I am NOT a security researcher (I kinda want to be though). As such, there are probably way better ways to do everything in this article. This article is just illustrating my thought process when cracking this challenge. [...]]]></description>
			<content:encoded><![CDATA[<p><center><img src="http://timetobleed.com/images/computer_bug.jpg"  alt="" width="400" height="300"/></center><br />

<p>If you enjoy this article, <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>Important note</h2>
<p>I am <b>NOT</b> a security researcher (I kinda want to be though). As such, there are probably way better ways to do everything in this article. This article is just illustrating my thought process when cracking this challenge.</p>
<h2>The Challenge</h2>
<p>The <a href="http://chargen.matasano.com/chargen/2009/10/9/a-c-challenge.html">Matasano Security blog</a> recently posted an article titled <i>A C++ Challenge</i><sup>1</sup> which included a particularly ugly piece of C++ code that has a security vulnerability. The challenge is for the reader to find the vulnerability, use it execute arbitrary code, and submit the data to Matasano.</p>
<p>Sounds easy enough, let&#8217;s do this! <i>cue hacking music</i></p>
<h2>Making it harder</h2>
<p>Recent linux kernels have feature called Address Space Layout Randomization (ASLR) which can be set in <code>/proc/sys/kernel/randomize_va_space</code>. ASLR is a security feature which randomizes the start address of various parts of a process image. Doing this makes exploiting a security bug more difficult because the exploit cannot use any hard coded addresses.</p>
<p>The options you can set are:</p>
<ul>
<li>0 &#8211; ASLR off</li>
<li>1 &#8211; Randomize the addresses of the stack, mmap area, and VDSO page. <b>This is the default.</b></li>
<li>2 &#8211; Everything in option 1, but also randomize the <code>brk</code> area so the heap is randomized.</li>
</ul>
<p>Just for fun I decided to set it to <b>2</b> to make exploiting the challenge more difficult.</p>
<h2>Got the code, but now what?</h2>
<p>I decided to start attacking this problem by looking for a few common errors, in this order:</p>
<ol>
<li><code>strcpy()/strncpy()</code> bugs <b>No calls</b></li>
<li><code>memcpy()</code> bugs <b>A few calls</b></li>
<li>Off by one bugs <b>None obvious</b></li>
</ol>
<p>It turned out from a quick look that all calls to <code>memcpy()</code> included sane, hard-coded values. So, it had to be something more complex.</p>
<h2>Digging deeper &#8211; finding input streams the user can control</h2>
<p>Next, I decided to actually <b>read</b> the code and see what it was doing at a high level and what inputs could be controlled. Turns out that the program reads data from a file and uses the data from the file to determine how many objects to allocate.</p>
<p>Obviously, this portion of the code caught my interest so let&#8217;s take a quick look:</p>
<pre class="prettyprint">
/* ... */

fd.read(file_in_mem, MAX_FILE_SIZE-1);

/* ... */

struct _stream_hdr *s = (struct _stream_hdr *) file_in_mem;

if(s->num_of_streams >= INT_MAX / (int)sizeof(int)) {
    safe_count = MAX_STREAMS;
} else {
    safe_count = s->num_of_streams;
}

Obj *o = new Obj[safe_count];
</pre>
<p>
<p>OK, so clearly that <code>if</code> statement is suspect. At the <i>very least</i> it doesn&#8217;t check for negative values, so you could end up with <code>safe_count = -1</code> which might do something interesting when passed to the <code>new</code> operator. Moreover, it appears this <code>if</code> statement will allow values as large as 536870910 ([INT_MAX / sizeof(int)] &#8211; 1).</p>
<p>Maybe the exploit has something to do with values this <code>if</code> statement is allowing through?</p>
<h2>A closer look at the integer overflow in <code>new</code></h2>
<p>Let&#8217;s use GDB to take a closer look at what the compiler does before calling new. I&#8217;ve added a few comments in line to explain the assembly code:</p>
<pre class="prettyprint">
mov    %edx,%eax   ;  %edx and %eax store s->num_of_streams
add    %eax,%eax   ;  add %eax to itself (s->num_of_streams * 2)
add    %edx,%eax   ;  add  s->num_of_streams + %eax (s->num_of_streams*3)
shl    $0x2,%eax   ;  multiply (s->num_of_streams * 3) by 4  (s->num_of_streams * 12)
mov    %eax,(%esp) ;  move it into position to pass to new
call   0x8048a7c <_Znaj@plt> ; call new
</pre>
<p>
<p>The compiler has generated code to calculate: <code>s->num_of_streams * sizeof(Obj)</code>. <code>sizeof(Obj)</code> is 12 bytes. For large values of <code>s->num_of_streams</code> multiplying it by 12, causes an <b>integer overflow</b> and the value passed to new will actually be <i>less than</i> what was intended.</p>
<p>For my exploit, I ended up using the value 357913943. This value causes an overflow, because 357913943 * 12 is <i>greater than</i> the biggest possible value for an integer by 20. So the value passed to new is 20. Which is, of course, significantly less than what we actually wanted to allocate. Other people have written about integer overflow in <code>new</code> in other compilers<sup>2</sup> before.</p>
<p>Let&#8217;s see how this can be used to cause arbitrary code to execute. <b>Remember</b>, for arbitrary code execution to occur there <i>must</i> be a way to <i>cause the target program to write some data to a memory address that can be controlled</i>.</p>
<h2>Find the (possible) hand-off(s) to arbitrary code</h2>
<p>To find any hand-off locations, I looked for places where memory writes were occurring in the program. I found a few memory writes:</p>
<ul>
<li>2 calls to <code>memset()</code></li>
<li>2 calls to <code>memcpy()</code></li>
<li><code>parse_stream()</code> of <code>class Obj</code></li>
</ul>
<p>Unfortunately (from the attacker&#8217;s perspective) the calls to <code>memcpy()</code> and <code>memset()</code> <i>looked</i> pretty sane. The <code>parse_stream()</code> function caught my interest, though.</p>
<p>Take a look:</p>
<pre class="prettyprint">
class Obj {
    public:
    int parse_stream(int t, char *stream)
    {
      type = t;
      // ... do something with stream here ...
      return 0;
    }

    int length;
    int type;
/* ... */
</pre>
<p>
<p><b>REMEMBER:</b> In C++, member functions of <code>class</code>es have a <b>sekrit parameter</b> which is a pointer to the object the function is being called on. In the function itself, this parameter is accessed using <code>this</code>. So the line writing to the <code>type</code> variable is actually doing <code>this->type = t;</code> where <code>this</code> is supplied to the function <b>sektrily</b> by the compiler.</p>
<p><b>This is important</b> because this piece of code could be our hand-off! We need to find a way to control the value of <code>this</code> so we can cause a memory write to a location of our choice.</p>
<h2>Controlling <code>this</code> to cause arbitrary code to execute</h2>
<p>Take a look at an important piece of code in the challenge:</p>
<pre class="prettyprint">
struct imetad {
  int msg_length;
  int (*callback)(int, struct imetad *);
/* ... */
</pre>
<p>
<p>Nice! The <code>callback</code> field of <code>struct imetad</code> is offset by 4 bytes into the structure. The <code>type</code> field of <code>class Obj</code> is also offset by 4 bytes. See where I&#8217;m going?</p>
<p>If we can control the <code>this</code> pointer to point at the <code>struct imetad</code> on the heap when <code>parse_stream</code> is called, it will overwrite the <code>callback</code> pointer. We&#8217;ll then be able to set the pointer to any address we want and hand-off execution to arbitrary code!</p>
<p>But how can we manipulate <code>this</code>?</p>
<p>Take a look at this piece of code that calls <code>callback</code>:</p>
<pre class="prettyprint">
o[i].parse_stream(dword, stream_temp);
imd->callback(o[i].type, imd);
</pre>
<p>
<p>Since it is possible to overflow <code>new</code> and allocate fewer objects than <code>safe_count</code> is counting, that means that for some values of i, <i><code>o[i]</code> will be pointing at data that isn&#8217;t actually an <code>Obj</code> object, but just other data on the heap</i>. Infact, when <code>i = 2</code>, <b><code>o[i]</code> will be pointing at the <code>struct imetad</code> object on the heap</b>. The call to <code>parse_stream</code> will pass in a corrupted <code>this</code> pointer, that points at <code>struct imetad</code>. The write to <code>type</code> will actually overwrite <code>callback</code> since they are both offset equal amounts into their respective structures.</p>
<p>And with that, we&#8217;ve successfully exploited the challenge causing arbitrary code to execute.</p>
<p>Let&#8217;s now figure out how to beat ASLR!</p>
<h2>How to defeat address space layout randomization</h2>
<p>I <b>did NOT</b> invent this technique, but I read about it and thought it was cool. You can read a more verbose explanation of this technique <a href="http://sophsec.com/research/aslr_research.html">here</a>. The idea behind the technique is pretty simple:
</p>
<ul>
<li>When you call <code>exec</code>, the PID remains the same, but the image of the process in memory is changed.</li>
<li>The kernel uses the PID and the number of jiffies (jiffies is a fine-grained time measurement in the kernel) to pull data from the entropy pool.</li>
<li>If you can run a program which records stack, heap, and other addresses and then quickly call <code>exec</code> to start the vulnerable program, you can end up with the <b>same memory layout</b>.</li>
</ul>
<p>My exploit program is actually a <i>wrapper</i> which records an approximate location of the heap (by just calling <code>malloc()</code>), generates the exploit file, and then executes the challenge binary.</p>
<p>Take a look at the relevant pieces of my exploit to get an idea of how it works:
<pre class="prettyprint">
/* ... */

/* do a malloc to get an idea of where the heap lives */
void *dummy = malloc(10);

/* ... */

unsigned int shell_addr = reinterpret_void_ptr_as_uint(dummy);

/*
 * XXX TODO FIXME - on my platform, execl'ing from here to the challenge binary
 * incurs a constant offset of 0x3160, probably for changes in the environment
 * (libs linked for c++ and whatnot).
 */
shell_addr += 0x3160;

/*
 * a guess as to how far off the heap the shellcode lives.
 *
 * luckily we have a large NOP sled, so we should only fail when we miss
 * the current entropy cycle (see below).
 */
shell_addr += 700;

/* ... build exploit file in memory ... */

/* copy in our best guess as to the address of the shellcode, pray NOPs
 * take care of the rest! */
memcpy(entire_file+88, &#038;shell_addr, sizeof(shell_addr));

/* ... write exploit out to disk ... */

/* launch program with the generated exploit file!
*
* calling execl here inherits the PID of this process, and IF we get lucky
* ~85%+ of the time, we'll execute before the next entropy cycle and hit
* the shellcode, even with ASLR=2.
*/
execl("./cpp_challenge", "cpp_challenge", "exploit", (char *)0);
</pre>
<h2>My exploit for the C++ challenge</h2>
<p>My exploit comes with the following caveats:</p>
<ul>
<li>i386 system</li>
<li>The challenge binary is called &#8220;cpp_challenge&#8221; and lives in the same directory as the exploit binary.</li>
<li>The exploit binary can write to the directory and create a file called &#8220;exploit&#8221; which will be handed off to &#8220;cpp_challenge&#8221;</li>
</ul>
<p>Get the full code of my exploit <a href="http://timetobleed.com/files/exploit_gen.c">here</a>.</p>
<h2>Results</h2>
<p>Results on my i386 Ubuntu 8.04 VM running in VMWare fusion, for each level of randomize_va_space:</p>
<ul>
<li>0 &#8211; <b>100%</b> exploit hit rate</li>
<li>1 &#8211; <b>100%</b> exploit hit rate</li>
<li>2 &#8211; <b>~85%</b> exploit hit rate. Sometimes, my exploit code falls out of the time window and the address map changes before the challenge binary is run</li>
</ul>
<p>I could probably boost the hit rate for 2 a bit, but then I&#8217;d probably re-write the entire exploit in assembly to make it run as fast as possible. I didn&#8217;t think there was really a point to going to such an extreme, though. So, an 85% hit rate is good enough.</p>
<h2>Conclusion</h2>
<ol>
<li>Security challenges are fun.</li>
<li>More emphasis and more freely available information on secure coding would be very useful.</li>
<li>Like it or not developers need to be security conscious when writing code in C and C++.</li>
<li>As C and C++ change, developers need to carefully consider security implications of new features.</li>
</ol>
<p>
Thanks for reading and don&#8217;t forget to <a rel="alternate" type="application/rss+xml" href="http://feeds.feedburner.com/TimeToBleed">subscribe (via RSS or e-mail)</a> and <a href="http://twitter.com/joedamato">follow me on twitter.</a></p>
<h2>References</h2>
<ol class="footnotes"><li id="footnote_0_1152" class="footnote"><a href="http://chargen.matasano.com/chargen/2009/10/9/a-c-challenge.html">Matasano Security LLC &#8211; Chargen &#8211; A C++ Challenge</a></li><li id="footnote_1_1152" class="footnote"><a href="http://blogs.msdn.com/oldnewthing/archive/2004/01/29/64389.aspx">Integer overflow in the new[] operator</a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://timetobleed.com/defeating-the-matasano-c-challenge-with-aslr-enabled/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
