<?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>Casaba Security &#187; Jordan Tigani</title>
	<atom:link href="http://www.casaba.com/blog/author/jordan/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.casaba.com/blog</link>
	<description>Building and breaking software and robots</description>
	<lastBuildDate>Wed, 11 Jan 2012 18:08:45 +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 safe is the safecrt handling of formatting strings?</title>
		<link>http://www.casaba.com/blog/2008/04/how-safe-is-the-safecrt-handling-of-formatting-strings/</link>
		<comments>http://www.casaba.com/blog/2008/04/how-safe-is-the-safecrt-handling-of-formatting-strings/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 22:33:37 +0000</pubDate>
		<dc:creator>Jordan Tigani</dc:creator>
				<category><![CDATA[Security Testing]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[One rule of thumb in c/c++ is that you should never let the user be in control of a formatting string. This has been recognized as a security bug for years, and one that has been mostly cleaned up since it is so easy to identify and fix. With visual studio 2005, Microsoft released a [...]]]></description>
			<content:encoded><![CDATA[<p>One rule of thumb in c/c++ is that you should never let the user be in control of a formatting string. This has been recognized as a security bug for years, and one that has been mostly cleaned up since it is so easy to identify and fix. With visual studio 2005, Microsoft released a safer version of the crt &#8212; functions that end with _s to tell you that they are  security enhanced. So let&#039;s say you are being a good security citizen by using the safe-crt &#8230;. can a format string vulnerability (where the user controls the format string) still be exploited?</p>
<p>The MSDN docs don&#039;t really have the answer. A cursory reading of the &#8220;security enhancements in the CRT&#8221; page as well as others may lead you to believe that format string vulnerabilities are a thing of the past. One example shows a call to &#039;sprintf_s(buf,_countof(buf), &#8220;%s&#8221;,NULL)&#039; and remarks that this results in a runtime error.  Looks like they do some kind runtime-validation. However, unless they added magic pixie dust to their compiler that sends cosmic rays from outer space to fix up malicious format strings at runtime, it isn&#039;t really possible to have strongly-typed printf-style format strings in C. </p>
<p>So let&#039;s investigate how far the parameter validation will get you. Here is a little sample program I wrote to send nasty format strings to sprintf_s:</p>
<p><code><br />
#include<br />
#define OUT_SIZE 0x1000<br />
int main(int argc, char** argv) {<br />
    char * out = new char[OUT_SIZE];<br />
    sprintf_s(out, OUT_SIZE, OUT_SIZE, argv[1]);<br />
    printf("%s\n", out);<br />
    return 0;<br />
}<br />
</code></p>
<p>So let&#039;s try this with a couple of format strings:<br />
<code><br />
Input: "%s"<br />
Output: Error: ("Buffer too small", 0)<br />
</code><br />
So far so good&#8230; but buffer too small?<br />
What about just dumping stack variables?<br />
<code><br />
Input:  "%p %p %p %p %p %p"<br />
Output: 00344FD0 00344FD0 0012FFB8 004019D3 00000002 00343728<br />
</code><br />
Interesting&#8230; so looks like this type checking is not so robust after all. We&#039;ve just dumped the stack.<br />
Let&#039;s see if we can crash the program. Looks like there is a 0000002 on the stack&#8230; that probably won&#039;t appreciate being dereferenced.<br />
<code><br />
Input:  "%p %p %p %p %s"</p>
<p></code><br />
Ok so <strong>we can crash the program</strong>. Can we do anything more interesting?<br />
Let&#039;s say there was some interesting data somewhere in the program. To simulate this, I&#039;ll put my bank account number on the stack with the following line of code at the beginning of &#8220;main&#8221;   &#8220;volatile char * bankAccount = &#8220;Account#123-456-7890&#8243;;&#8221; (the volatile helps convince the compiler not to throw it away since I don&#039;t use it).</p>
<p>Now when I call the function with the right input, I can dump my bank account number:</p>
<p><code>Input: test.exe "%p %s"<br />
Output: 00344FD0 Account#123-456-7890 00344FD0<br />
</code></p>
<p>Ok but nobody really cares about Denial-of-service and Information-disclosure. Those are sooooo pri-3. Can we use take over the machine? As everyone knows, the hacker&#039;s favorite format string character is &#039;%n&#039;. &#039;%n&#039; writes the number of bytes written so far to the param from the stack. Let&#039;s try a &#039;%n&#039;:<br />
<code><br />
Input: test.exe "%p %n"<br />
Output: Error: (state != ST_INVALID)<br />
</code></p>
<p>Blast! Foiled! It turns out Microsoft decided that %n was too much power, and that we mere mortals couldn&#039;t handle it. Good for them. There is an override, but it turns out to not be available using the Safe CRT. <strong>The moral of the story? </strong>The safe crt is a wonderful and powerful tool to help prevent buffer overruns. But there is no excuse for letting a user control a format string.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2008/04/how-safe-is-the-safecrt-handling-of-formatting-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

