<?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>Coding UndeRealM</title>
	<atom:link href="http://code.underealm.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.underealm.com</link>
	<description>Software &#38; Sources</description>
	<lastBuildDate>Thu, 04 Jun 2009 20:32:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Array Initialization &amp; Split() Bug. Solution born from a WTF?!?</title>
		<link>http://code.underealm.com/2009/06/04/vb6-array-initialization-born-from-a-wtf/</link>
		<comments>http://code.underealm.com/2009/06/04/vb6-array-initialization-born-from-a-wtf/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 20:21:44 +0000</pubDate>
		<dc:creator>Skizo</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tricks & Magics]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[Arrays Initialization]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[Split]]></category>
		<category><![CDATA[VB6]]></category>

		<guid isPermaLink="false">http://code.underealm.com/?p=28</guid>
		<description><![CDATA[Everybody faced problems with array initialization, error handling for dinamics and so on. Today, first time in my life (and that&#8217;s almost 20 year worth of coding) I found a bug out of the blue. Or what I think to be so. Typical example incoming: On Local Error Resume Next Dim sArray() As String, C [...]]]></description>
			<content:encoded><![CDATA[<p>Everybody faced problems with array initialization, error handling for dinamics and so on. Today, first time in my life (and that&#8217;s almost 20 year worth of coding) I found a bug out of the blue. Or what I think to be so. Typical example incoming:</p>
<p><code>
<pre><span style="color: #0000ff;">On Local Error Resume Next
  Dim</span> sArray() <span style="color: #0000ff;">As String</span>, C <span style="color: #0000ff;">As Long</span>, D <span style="color: #0000ff;">As Long</span>
  D = <span style="color: #ff0000;">[102 or 99]</span>
<span style="color: #0000ff;">  For</span> C = 100 <span style="color: #0000ff;">To</span> D
<span style="color: #0000ff;">     ReDim</span> sArray(<span style="color: #0000ff;">UBound(</span>sArray<span style="color: #0000ff;">)</span> + 1)
<span style="color: #0000ff;">     If</span> Err.Number &gt; 0 <span style="color: #0000ff;">Then ReDim</span> sArray(0): Err.Clear
     sArray(<span style="color: #0000ff;">UBound(</span>sArray<span style="color: #0000ff;">)</span>) = <span style="color: #0000ff;">CStr(</span>C<span style="color: #0000ff;">)
  Next
  Debug</span>.<span style="color: #0000ff;">Print UBound(</span>sArray<span style="color: #0000ff;">)
On Local Error GoTo 0</span>

<span style="color: #339966;">' D = 102  -  Ubound(sArray) = 2
' D =  99  -  UBound(sArray) = Error</span></pre>
<p></code></p>
<p>This example is stupid on purpose, but gets us to the point. If <span style="color: #0000ff;">D</span> is set to <span style="color: #0000ff;">99</span>, then <span style="color: #0000ff;">sArray</span> is gonna be uninitialized. Which means that we have to set another error handling to check wether or not the array contains something. And this generally brings us pain, it&#8217;s always very, very, very ugly to do, but until now it was the only way, aside setting up a base 0 array with data starting from 1, like this:</p>
<p><code></p>
<pre><span style="color: #0000ff;">ReDim</span> sArray(0) <span style="color: #0000ff;">As String
ReDim</span> sArray(<span style="color: #0000ff;">UBound(</span>sArray<span style="color: #0000ff;">) </span>+ 1)
sArray(<span style="color: #0000ff;">UBound(</span>sArray<span style="color: #0000ff;">)</span>) = 1
<span style="color: #0000ff;">For</span> C = 1 <span style="color: #0000ff;">To</span> <span style="color: #0000ff;">UBound(</span>sArray<span style="color: #0000ff;">)</span>
<span style="color: #0000ff;">   Debug</span>.<span style="color: #0000ff;">Print</span> sArray(C)
<span style="color: #0000ff;">Next</span></pre>
<p></code></p>
<p>Another ugly way to do the job, but as I said a little above, there weren&#8217;t many ways to do it. Until I found something so stupid to be genial. Let&#8217;s see a slightly modified version of the same code.</p>
<p><code></p>
<pre><span style="color: #0000ff;">Dim</span> sArray() <span style="color: #0000ff;">As String</span>, C <span style="color: #0000ff;">As Long</span>, D <span style="color: #0000ff;">As Long</span>
sArray = Split(<span style="color: #a52a2a;">""</span>,<span style="color: #a52a2a;">" "</span>)
D = <span style="color: #ff0000;">[102 or 99]</span>
<span style="color: #0000ff;">For</span> C = 100 <span style="color: #0000ff;">To</span> D
<span style="color: #0000ff;">   ReDim</span> sArray(<span style="color: #0000ff;">UBound(</span>sArray<span style="color: #0000ff;">)</span> + 1)
   sArray(<span style="color: #0000ff;">UBound(</span>sArray<span style="color: #0000ff;">)</span>) = <span style="color: #0000ff;">CStr(</span>C<span style="color: #0000ff;">)
Next
Debug.Print UBound(</span>sArray<span style="color: #0000ff;">)</span>

<span style="color: #339966;">' D = 102  -  Ubound(sArray) = 2
' D =  99  -  UBound(sArray) = -1</span></pre>
<p></code></p>
<p>You probably won&#8217;t understand that, so I&#8217;ll explain: the <span style="color: #0000ff;">Split()</span> of a 0 length string with a non-0 length string, brings us an amazing<span style="color: #0000ff;"> Array(</span>0 <span style="color: #0000ff;">To</span> -1<span style="color: #0000ff;">)</span>. You heard it right, 0 to -1. What does this mean?</p>
<ol>
<li>Empty arrays can checked with a (<span style="color: #0000ff;">UBound(</span>Array<span style="color: #0000ff;">)</span> = -1).</li>
<li>Incremental arrays available from scratch, from index 0.</li>
<li>Possibility to return safely arrays without the need to define a count variable to set the array lenght.</li>
<li>&#8230;</li>
<li>Profit!</li>
</ol>
<p>This is one of the most <span style="text-decoration: line-through;">stupid</span> amazing things I ever seen, and it caught me so off-guard I totally didn&#8217;t expect it. Could even make it to <a title="The Daily WTF?!?" href=" http://thedailywtf.com/" target="_blank">The Daily WTF?!?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://code.underealm.com/2009/06/04/vb6-array-initialization-born-from-a-wtf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>1 * N = 27</title>
		<link>http://code.underealm.com/2008/12/21/27/</link>
		<comments>http://code.underealm.com/2008/12/21/27/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 10:20:48 +0000</pubDate>
		<dc:creator>Skizo</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://code.underealm.com/?p=18</guid>
		<description><![CDATA[#define CURRENT_YEAR 2008 UINT TimeSpan(const UINT *thisYear) { UINT span = 0; for (UINT C = 1981; C &#60; thisYear; C++) span++; return span; } int main() { printf("%ui: I'm screwed!!", TimeSpan(CURRENT_YEAR)); } This is cryptic, isn&#8217;t it? No wait, probably it&#8217;s not ;)]]></description>
			<content:encoded><![CDATA[<p><code></p>
<pre><span style="color: #0000ff;">#define</span> CURRENT_YEAR 2008

UINT TimeSpan(<span style="color: #0000ff;">const</span> UINT *thisYear)
{
    UINT span = 0;
<span style="color: #0000ff;">    for</span> (UINT C = 1981; C &lt; thisYear; C++)
       span++;
<span style="color: #0000ff;">    return</span> span;
}

<span style="color: #0000ff;">int</span> main()
{
    printf(<span style="color: #aa2200;">"%ui: I'm screwed!!"</span>, TimeSpan(CURRENT_YEAR));
}</pre>
<p></code></p>
<p>This is cryptic, isn&#8217;t it?</p>
<p>No wait, probably it&#8217;s not ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://code.underealm.com/2008/12/21/27/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SEH Jump</title>
		<link>http://code.underealm.com/2008/12/09/seh-jump/</link>
		<comments>http://code.underealm.com/2008/12/09/seh-jump/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 08:48:50 +0000</pubDate>
		<dc:creator>Skizo</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tricks & Magics]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[ASM]]></category>
		<category><![CDATA[SEH]]></category>
		<category><![CDATA[SEH Jump]]></category>
		<category><![CDATA[Structured Exception Handler]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://code.underealm.com/?p=11</guid>
		<description><![CDATA[I know what most of the people will think viewing future content or taglines around here: &#8220;pfft, vb kiddie&#8221;. So, just to preemptively shut them up, here&#8217;s some quite advanced asm trick. A little intro before the code. It is possible, working the way through a SEH, to jump to another location instead of using [...]]]></description>
			<content:encoded><![CDATA[<p>I know what most of the people will think viewing future content or taglines around here: &#8220;pfft, vb kiddie&#8221;. So, just to preemptively shut them up, here&#8217;s some quite advanced <abbr title="Assembly">asm</abbr> trick.</p>
<p>A little intro before the code. It is possible, working the way through a <abbr title="Structured Exception Handler">SEH</abbr>, to jump to another location instead of using a standard JMP. The advantage of this is the (eventual) obfuscation of the jump (if done properly), and although I&#8217;ve seen plenty of references around the web, I just couldn&#8217;t find anything on this matter, especially in the way I wanted it.</p>
<p>What you are going to find about <abbr title="Structured Exception Handler">SEH</abbr> around the web (including dirty tricks) are most likely:</p>
<ol>
<li>Standard way (no tricks at all, just standard behaviour)</li>
<li>Jump &#038; Clean (jump to the <abbr title="Structured Exception Handler">SEH</abbr>, never get back)</li>
<li>Clean jump based on <abbr title="Structured Exception Handler">SEH</abbr> hack (only here, searched a lot, and never found)</li>
</ol>
<p>About #1, I will let you seek through the <a href="http://www.google.com/search?q=SEH+handler">vast ASM/C++ references</a>, no need to talk about it here.</p>
<p>About #2, code snippet:</p>
<p><code>
<pre>
.code
assume fs:nothing

start:
   push @@SEH_Handler           ; Address of SEH Handler
   push FS:[0]                  ; Original SEH Handler
   mov  FS:[0],   ESP           ; Install SEH Handler
   xor  eax,      eax
   idiv eax                     ; Generate exception
   int  3                       ; Will never reach this
   jmp  @@Exit

@@SEH_Handler:
   mov  esp,  [esp+8]           ; Restore pre-jump stack
   pop  FS:[0]                  ; Reset Original Handler
   add  esp,      4             ; Remove handler address from stack
   [...]                        ; Code goes on from here as normal
</pre>
<p></code></p>
<p>This code works perfectly. But there&#8217;s a pretty grievous problem with it. That is, it <em>assumes</em> and <em>requires</em> that you are both the <strong>author of both codes</strong> (pre and post jump) and that <strong>you can modify both</strong>. Which wasn&#8217;t my case.</p>
<p>Example, while writing a packer, or a loader, or whatever you may want to, <em>you just can&#8217;t clean <abbr title="Structured Exception Handler">SEH</abbr> handler and the stack</em> after the jump. Because, since the code isn&#8217;t yours and can&#8217;t be recoded, it won&#8217;t do it. And that&#8217;s bad. Immagine it doesn&#8217;t have an exception handler, and at every unhandled exception it &#8220;returns&#8221; to your OEP. Nasty, huh? And that&#8217;s the least of the problems actually.</p>
<p>Nastier problem is having a &#8220;dirty&#8221; stack, and god only knows what else could happen. But there is a workaround, which is cleaning and resetting everything <strong>inside the <abbr title="Structured Exception Handler">SEH</abbr> handler</strong>, with a bit of hacking and eventually some heuristic approach. At least mine had to be, I knew nothing about it.</p>
<p><code>
<pre>
.code
assume fs:nothing

start:
   push @@SEH_Handler           ; Address of SEH Handler
   push FS:[0]                  ; Original SEH Handler
   mov  FS:[0],   ESP           ; Install SEH Handler
   xor  eax,      eax
   idiv eax                     ; Generate exception
   int  3                       ; Will never reach this
   jmp  @@Exit

@@SEH_Handler:
   mov  eax,      [esp+C]       ; Pointer to SEH Data
   add  [eax+C4], 8             ; [1]
   mov  [eax+B8], @@JustGoOn    ; [2]
   mov  eax,      [esp+8]
   mov  eax,      [eax]         ; Pointer to original Stack
   mov  FS:[0],   eax           ; Reset Original Handler
   xor  eax,      eax           ; Set to 0 to "return"
   ret
</pre>
<p></code></p>
<p>Pointer to <abbr title="Structured Exception Handler">SEH</abbr> data (<strong>[ESP+C]</strong>) points to a zone in the stack with lot of infos, including hardware breakpoints and (between the other things) the exact stack address returned to you after the jump. And, of course, the address it will return to.</p>
<p><strong>[0] &#8211; [ESP+C]</strong>: <abbr title="Structured Exception Handler">SEH</abbr> data<br />
<strong>[1] &#8211; [[ESP+C]+C4]</strong>: Original Stack Address (pre-jump)<br />
<strong>[2] &#8211; [[ESP+C]+B8]</strong>: Return Point</p>
<p>Adding 8 in point [1], makes the kernel return to the address with the stack pointer set to the original one (pre-jump) minus the two push instructions (first two instructions after the <em>start:</em>). Which also basically means, it will return from the <abbr title="Structured Exception Handler">SEH</abbr> handler with the <strong>original stack pointer</strong>, just as if the code started from the return point.</p>
<p>The point [2] should be fairly readable and intuitive, hacks the stack to &#8220;hack&#8221; the return point. Another approach, in case you wanted to continue right from the next instruction, would have been to add the size of the operand to the address (in this case the size of <strong>IDIV EAX</strong>). Just to remind you, the default <abbr title="Structured Exception Handler">SEH</abbr> way is to return to the very same instruction that caused the exception.</p>
<p>With this piece of code you achieve two things: the first and most important, you perfectly and seemlessly jump from one context to another, just as there were two programs in the same executable; secondly you have the ability to easily obfuscate the jump, which in this case is pretty obvious, but that&#8217;s up to you, not me.</p>
<p>This is it. Guess this article makes quite a good start, doesn&#8217;t it?</p>
<p>We&#8217;ll be seeing again.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.underealm.com/2008/12/09/seh-jump/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hallo! Was?</title>
		<link>http://code.underealm.com/2008/12/02/hallo-was/</link>
		<comments>http://code.underealm.com/2008/12/02/hallo-was/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 00:12:08 +0000</pubDate>
		<dc:creator>Skizo</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://code.underealm.com/?p=1</guid>
		<description><![CDATA[Coding UndeRealM has just been launched. There is a lot to come in the next few days, stay tuned!]]></description>
			<content:encoded><![CDATA[<p>Coding UndeRealM has just been launched.</p>
<p>There is a lot to come in the next few days, stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.underealm.com/2008/12/02/hallo-was/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

