<?xml version="1.0"?>
<rss version="2.0">
    <channel>
        <title>OpenRCE: Blog</title>
        <link>http://www.openrce.org/rss/feeds/blog</link>
        <description>OpenRCE: The Open Reverse Code Engineering Community</description>
                <item>
            <title>User-supplied Array Index Exploitation Simplified.. sort of.</title>
                            <pubDate>Thu, 04 Feb 2010 20:38:25 -0600</pubDate>
                                        <link>https://www.openrce.org/blog/view/1547/User-supplied_Array_Index_Exploitation_Simplified.._sort_of.</link>
                                        <author>lin0xx &lt;email-suppressed@example.com&gt;</author>
                                                    <description>Have you ever ran into a case where you controlled an index into an array, but then were tasked with the annoying prospect of massaging the overwrite to suit your needs?&lt;br /&gt;
&lt;br /&gt;
For example, from http://labs.idefense.com/intelligence/vulnerabilities/display.php?id=5 :&lt;br /&gt;
&lt;br /&gt;
#define RFC2231_MAX 64&lt;br /&gt;
...&lt;br /&gt;
char *pieces[RFC2231_MAX];&lt;br /&gt;
and indexed by the signed integer variable 'n':&lt;br /&gt;
if(n &amp;lt; RFC2231_MAX){&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pieces[n] = parms-&amp;gt;value;&lt;br /&gt;
&lt;br /&gt;
^^ Note: parms-&amp;gt;value is a pointer to user data.&lt;br /&gt;
&lt;br /&gt;
Say you do some reversing and find out that 'pieces' is 300 bytes away from the stored return address. From the array write, we can tell that overwriting the return address would totally be exploitable (assuming no N^X) since it's overwritten with a pointer to data supplied by you.&lt;br /&gt;
&lt;br /&gt;
We need the following: a negative integer that when multiplied by 4 (assuming a 32 bit architecture here), will yield 300. &lt;br /&gt;
&lt;br /&gt;
Well, I'm not the most k-awesome bithax0r in the world, so I find the prospect of finding a negative integer to exploit this bug to be quite annoying. I know what I want, I just don't want to waste time telling some stupid imperative language how to give it to me! What could possibly solve my quandary? Hmmm, STP.. a constraint solver.. that sounds like just the thing!&lt;br /&gt;
&lt;br /&gt;
Throw this into a file:&lt;br /&gt;
&lt;br /&gt;
dummy: BITVECTOR(32);&lt;br /&gt;
overflow : BITVECTOR(64);&lt;br /&gt;
multiplyOverflow : BITVECTOR(64);&lt;br /&gt;
notDummy : BITVECTOR(32);&lt;br /&gt;
evilIndex : BITVECTOR(32);&lt;br /&gt;
&lt;br /&gt;
% Make sure the number is negative via two's compliment ninjutsu&lt;br /&gt;
ASSERT(notDummy = ~dummy);&lt;br /&gt;
ASSERT(evilIndex = BVPLUS(32, notDummy, 0hex00000001));&lt;br /&gt;
&lt;br /&gt;
% Make sure it's signed too&lt;br /&gt;
ASSERT(evilIndex &amp;amp; 0hex80000000 = 0hex80000000);&lt;br /&gt;
&lt;br /&gt;
% Need a bigger bit width to express the overflow&lt;br /&gt;
ASSERT(overflow[31:0] = evilIndex);&lt;br /&gt;
ASSERT(multiplyOverflow = BVMULT(64, overflow, 0hex0000000000000004));&lt;br /&gt;
&lt;br /&gt;
% We want evilIndex*4 == 300&lt;br /&gt;
ASSERT(multiplyOverflow[31:0] = 0hex0000012C);&lt;br /&gt;
QUERY(FALSE);&lt;br /&gt;
&lt;br /&gt;
Now feed it to the solver...&lt;br /&gt;
&lt;br /&gt;
$ stp -p array.access.stp &lt;br /&gt;
ASSERT( v_solver_0&amp;nbsp;&amp;nbsp;= 0x00000000&amp;nbsp;&amp;nbsp;);&lt;br /&gt;
ASSERT( overflow&amp;nbsp;&amp;nbsp;= 0x00000000C000004B&amp;nbsp;&amp;nbsp;);&lt;br /&gt;
ASSERT( multiplyOverflow&amp;nbsp;&amp;nbsp;= 0x000000030000012C&amp;nbsp;&amp;nbsp;);&lt;br /&gt;
ASSERT( evilIndex&amp;nbsp;&amp;nbsp;= 0xC000004B&amp;nbsp;&amp;nbsp;);&lt;br /&gt;
ASSERT( notDummy&amp;nbsp;&amp;nbsp;= 0xC000004A&amp;nbsp;&amp;nbsp;);&lt;br /&gt;
ASSERT( dummy&amp;nbsp;&amp;nbsp;= 0x3FFFFFB5&amp;nbsp;&amp;nbsp;);&lt;br /&gt;
Invalid.&lt;br /&gt;
&lt;br /&gt;
Well, there you have it: 0xC000004B. Compare this to the alternative (from: http://milw0rm.org/exploits/99 ) :&lt;br /&gt;
&lt;br /&gt;
unsigned int num(int dist) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int tmp = -1-dist;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;unsigned int border = tmp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int neg = (border/4)*(-1);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;unsigned int res;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;res = 0xffffffff+neg;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return res;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
I checked the output of my formula against the return value for num() here - they matched.&lt;br /&gt;
&lt;br /&gt;
Well, I know which method looks more intuitive to me.&lt;br /&gt;
&lt;br /&gt;
Hope someone finds this useful!</description>
                    </item>
            </channel>
</rss>
