<?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>Not A Number &#187; hacks</title>
	<atom:link href="http://notanumber.net/archives/category/hacks/feed" rel="self" type="application/rss+xml" />
	<link>http://notanumber.net</link>
	<description>Programming, Theory, and Math</description>
	<lastBuildDate>Sat, 21 Nov 2009 00:07:26 +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>Underhanded C: The Leaky Redaction</title>
		<link>http://notanumber.net/archives/54/underhanded-c-the-leaky-redaction</link>
		<comments>http://notanumber.net/archives/54/underhanded-c-the-leaky-redaction#comments</comments>
		<pubDate>Sat, 21 Nov 2009 00:03:25 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://notanumber.net/?p=54</guid>
		<description><![CDATA[So, it turns out I am the winner of the 2008 Underhanded C Contest. The goal of the contest is to write some straightforward C code to solve a simple task, incorrectly. In particular, you had to introduce a hidden security flaw that would stand up to code review and not stand out at all. [...]]]></description>
			<content:encoded><![CDATA[<p>So, it turns out I am the winner of the <a href="http://underhanded.xcott.com/">2008 Underhanded C Contest</a>. The goal of the contest is to write some straightforward C code to solve a simple task, incorrectly. In particular, you had to introduce a hidden security flaw that would stand up to code review and not stand out at all. This is different than the Obfuscated C contest in that you want your program to look straightforward and that it does one thing, when in fact it does another.</p>
<p>The goal this year was to write a leaky image redaction program. Given an input image in PPM format and a rectangle, it would spit out the image with the rectangle blacked out, perhaps hiding sensitive information. The tricky part was that you had to leak the redacted information. There are more details in the <a href="http://underhanded.xcott.com/?p=8">problem specification</a>.</p>
<p>So, before I go on, here is my complete entry. See if you can figure out how the information is leaked before reading further if you like.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
 * This is a simple redactor, it accepts a plain text ppm file, a set of
 * coordinates defining a rectangle, and produces a ppm file with said
 * rectangle blacked out.
 *
 * Usage: redact in.ppm x y width height &gt; out.ppm
 */</span>
&nbsp;
<span style="color: #993333;">int</span>
main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>argc <span style="color: #339933;">!=</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;usage: redact in.ppm x y width height &gt; out.ppm<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// process command line arguments</span>
    <span style="color: #993333;">int</span> rx <span style="color: #339933;">=</span> atoi<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> ry <span style="color: #339933;">=</span> atoi<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> rwidth <span style="color: #339933;">=</span> atoi<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> rheight <span style="color: #339933;">=</span> atoi<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    FILE <span style="color: #339933;">*</span>ppm <span style="color: #339933;">=</span> fopen<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>ppm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        perror<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//read the ppm header</span>
    <span style="color: #993333;">unsigned</span> width<span style="color: #339933;">,</span>height<span style="color: #339933;">,</span>maxdepth<span style="color: #339933;">;</span>
    fscanf<span style="color: #009900;">&#40;</span>ppm<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;P3<span style="color: #000099; font-weight: bold;">\n</span>%u %u<span style="color: #000099; font-weight: bold;">\n</span>%u<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>width<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>height<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>maxdepth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;P3<span style="color: #000099; font-weight: bold;">\n</span>%u %u<span style="color: #000099; font-weight: bold;">\n</span>%u<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> height<span style="color: #339933;">,</span> maxdepth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//current locations</span>
    <span style="color: #993333;">int</span> x <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> y <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> ws <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//fixed buffer size to avoid overflow</span>
    <span style="color: #993333;">char</span> buf<span style="color: #009900;">&#91;</span>BUFSIZE<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #339933;">*</span>c<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>fgets<span style="color: #009900;">&#40;</span>buf<span style="color: #339933;">,</span>BUFSIZE<span style="color: #339933;">,</span>ppm<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>c <span style="color: #339933;">=</span> buf<span style="color: #339933;">;*</span>c<span style="color: #339933;">;</span>c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>isdigit<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>ws<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   <span style="color: #666666; font-style: italic;">// new number, increment location.</span>
                    ws <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++;</span>
                    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;=</span> width <span style="color: #339933;">*</span> <span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        y<span style="color: #339933;">++;</span> x <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;</span> rx <span style="color: #339933;">*</span> <span style="color: #0000dd;">3</span> <span style="color: #339933;">&amp;&amp;</span> x <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span>rx <span style="color: #339933;">+</span> rwidth<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #0000dd;">3</span> <span style="color: #339933;">&amp;&amp;</span> y <span style="color: #339933;">&gt;</span> ry <span style="color: #339933;">&amp;&amp;</span> y <span style="color: #339933;">&lt;</span> ry <span style="color: #339933;">+</span> rheight<span style="color: #009900;">&#41;</span>
                    putchar<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">else</span>
                    putchar<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
               ws <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
               putchar<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>c<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>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-54"></span><br />
The trick involves the format of the P3 style PPM file. The format is a plain text format, it has some basic header info, then a list of whitespace separated numbers, such as <code>234 2 0 83 255 255 2 43 255</code> where the numbers represent the magnitude of the red, green, and blue component for each pixel in order. The redactor simply replaced values within the target rectangle with zero. However, due to the way I process the file, character by character, I leak how many digits each value had to begin with. i.e., the above would be redacted to <code>000 0 0 00 000 000 0 00 000</code>. This is completely invisible when viewing the PPM file, all the values count as zero as far as the format is concerned, but by looking at the original file, you can recover some information about what was in the blanked out area. It is particular effective on black on white text, the most common thing needing to be redacted, where each value is 0 0 0 or 255 255 255, allowing perfect reconstruction of the original.</p>
<p>One of my favorite parts of my entry that isn&#8217;t mentioned on the prize page is that it has great plausible deniability as the leak was introduced by properly working around a commonly known and particularly insidious C bug, the improper use of gets and (more subtly) fgets. I can imagine a code review going somewhat like the following:</p>
<blockquote><p>Spook: &#8220;So why did you process the file character by character, rather than doing the more obvious scanf(&#8220;%i %i %i&#8221;,&amp;r,&amp;g,&amp;b) to read in the values?&#8221;</p>
<p>Me: &#8220;Well, in order to do that I&#8217;d have to read in entire lines of the file. Now there is the gets function in C which does that, but has a well known buffer overflow bug if the line length exceeds your buffer size, so I naturally used the safe fgets variant of the function. Of course, with fgets, you can just assume your buffer size is greater than the maximum line length, but that introduces a subtle bug if it isn&#8217;t, you may end up splitting a number across two buffers, so scanf will read something like 234 as the two numbers 23 and 4 if it is split after the second character, hence the need to consider each character independently.&#8221;</p>
<p>Spook: &#8220;Ah, of course. good job at spotting that.&#8221;</p>
<p>Me: *snicker*</p></blockquote>
<p>It is also a great example of the principle that you can&#8217;t protect against intending to write the wrong thing. The code will stand up to any buffer overflow check, code style check, or lint program. The code is correct and proper C code; the bug was not introduced in the code, but much earlier, in my head when I conceived the algorithm. No matter how smart your tools are, if you ultimately intend to write the wrong thing or solve the wrong problem, they can&#8217;t protect against that.</p>
]]></content:encoded>
			<wfw:commentRss>http://notanumber.net/archives/54/underhanded-c-the-leaky-redaction/feed</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>ShapeLock and Hot Glue = Rapid Funny Looking Prototypes</title>
		<link>http://notanumber.net/archives/10/shapelock-and-hot-glue-rapid-funny-looking-prototypes</link>
		<comments>http://notanumber.net/archives/10/shapelock-and-hot-glue-rapid-funny-looking-prototypes#comments</comments>
		<pubDate>Mon, 05 May 2008 15:44:13 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[hacks]]></category>
		<category><![CDATA[reprap]]></category>
		<category><![CDATA[shapelock]]></category>

		<guid isPermaLink="false">http://notanumber.net/archives/10</guid>
		<description><![CDATA[While waiting for the parts to my very own reprap machine, I figured I would experiment a little with some possible head designs. One I am particularly interested in is something like a &#8216;pick and place&#8217; machine that can manipulate objects in 3 dimensions. My simple design involves a drinking straw, a couple $3 hobby [...]]]></description>
			<content:encoded><![CDATA[<p>While waiting for the parts to my very own reprap machine, I figured I would experiment a little with some possible head designs. One I am particularly interested in is something like a &#8216;pick and place&#8217; machine that can manipulate objects in 3 dimensions. My simple design involves a drinking straw, a couple $3 hobby servos, a bunch of hand molded shapelock, and a few hot glue burns.</p>
<p>Here is the final result:</p>
<p><a title="pickplace1" href="http://notanumber.net/wp-content/uploads/2008/05/pp1.jpg"><img src="http://notanumber.net/wp-content/uploads/2008/05/pp1.thumbnail.jpg" alt="pickplace1" /></a></p>
<p>The basic idea is the bottom servo (bottom is to the left) can bend the straw left and right, and the other servo can rotate the straw in place. the bend in the straw acts as a universal joint so the object held can be rotated somewhat arbitrarily in 3 dimensions.</p>
<p><a title="pp3.jpg" href="http://notanumber.net/wp-content/uploads/2008/05/pp3.jpg"><img src="http://notanumber.net/wp-content/uploads/2008/05/pp3.thumbnail.jpg" alt="pp3.jpg" /></a></p>
<p>A simple linkage connects the top servo to the straw. I attempted a couple different things, starting with a pully system, then a gear system. neither worked out too well. The linkage turned out to be quite simple and robust.</p>
<p><a title="pp7.jpg" href="http://notanumber.net/wp-content/uploads/2008/05/pp7.jpg"><img src="http://notanumber.net/wp-content/uploads/2008/05/pp7.thumbnail.jpg" alt="pp7.jpg" /></a></p>
<p>Here it is with the attached fan I attempted to use as a vaccum pump. It did not turn out too well, the fan was scavanged from an old CPU and was never meant to be used like this, so I will need an actual vaccum pump at some point.</p>
<p>Everything was controlled for testing with a Wii nunchuck and an arduino microcontroller with custom code. Here is a video of it in action:</p>
<p><a href="http://notanumber.net/archives/10/shapelock-and-hot-glue-rapid-funny-looking-prototypes"><img src="http://img.youtube.com/vi/SGe_1BZErEg/default.jpg" width="130" height="97" border=0></a></p>
<p>So, some stuff I learned</p>
<ul>
<li>ShapeLock is wonderful stuff. I was able to form and reform the head a few times, even fairly large changes like making room for a gear involved reheating a part of the project and shaping it by hand. And I can just melt it down again and reuse it for my next prototype.</li>
<li>The linkage is the way to go. I struggled a long time with gears and pullys. I imagine that if I were precision machining things and could get gears/pullys in the exact right size, things would have been different. But  when it comes to the fuzzy world of hand-squished shapelock, the more forgiving linkage worked out great.</li>
<li>I am  gonna stock up on these tiny and cheap servos. They have a very interesting and useful bug. if you try to overextend them, they go into continuously rotating mode with no modification! So you can have the same servo work as a continously rotating one at some points, but also have precise precisioning at others.</li>
<li>The Wii nunchuck is a great little thing. it took a few dozen lines of arduino code to interface with it and I got a joystick, 3 buttons, and a 3 axis accelerometer.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://notanumber.net/archives/10/shapelock-and-hot-glue-rapid-funny-looking-prototypes/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

