<?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>Reverse Engineering &#8211; Adwiteeya Agrawal | Home</title>
	<atom:link href="https://adwiteeya.com/tag/reverse-engineering/feed/" rel="self" type="application/rss+xml" />
	<link>https://adwiteeya.com</link>
	<description>Security fanatic. Reverse Engineer and Web App Specialist.</description>
	<lastBuildDate>Tue, 03 Dec 2013 07:53:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.7</generator>
	<item>
		<title>Code Obfusction &#124; Writing a VM</title>
		<link>https://adwiteeya.com/code-obfusction-writing-a-vm/</link>
					<comments>https://adwiteeya.com/code-obfusction-writing-a-vm/#comments</comments>
		
		<dc:creator><![CDATA[Adwiteeya Agrawal]]></dc:creator>
		<pubDate>Tue, 03 Dec 2013 07:18:19 +0000</pubDate>
				<category><![CDATA[At the College]]></category>
		<category><![CDATA[Information Security]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[adwiteeya agrawal]]></category>
		<category><![CDATA[Code Obfusction]]></category>
		<category><![CDATA[OIVM]]></category>
		<category><![CDATA[Protection Against Reversing]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Virtual Machine in C]]></category>
		<guid isPermaLink="false">http://adwiteeya.com/blog/?p=444</guid>

					<description><![CDATA[What is this Blog About ? It is about a minor project that i recently completed. That is writing a virtual machine to slow down the process of reverse engineering. This is not a tutorial to do the same however I have included the presentation that i used during the final presentation, the PDFs for [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><strong>What is this Blog About ?</strong></p>
<p>It is about a minor project that i recently completed. That is writing a virtual machine to slow down the process of reverse engineering. This is not a tutorial to do the same however I have included the presentation that i used during the final presentation, the PDFs for System Design and Sample Runs and one step by step sample run.<span id="more-444"></span></p>
<p><strong>#Presentation</strong></p>
<p><center><br />
<iframe style="border: 1px solid #CCC; border-width: 1px 1px 0; margin-bottom: 5px;" src="http://www.slideshare.net/slideshow/embed_code/28832154" height="356" width="427" allowfullscreen="" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></center></p>
<div style="margin-bottom: 5px; text-align: center;"><strong> <a title="OIVM" href="https://www.slideshare.net/adwiteeyaagrawal/oivm" target="_blank">OIVM</a> </strong> from <strong><a href="http://www.slideshare.net/adwiteeyaagrawal" target="_blank">Adwiteeya Agrawal</a></strong></div>
<div style="margin-bottom: 5px;"></div>
<div style="margin-bottom: 5px;"></div>
<div style="margin-bottom: 5px;"><strong>#Design</strong></div>
<div style="margin-bottom: 5px;"></div>
<p><iframe style="border: 1px solid #CCC; border-width: 1px 1px 0; margin-bottom: 5px;" src="http://www.slideshare.net/slideshow/embed_code/28832223" height="613" width="574" allowfullscreen="" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<div style="margin-bottom: 5px;"></div>
<div style="margin-bottom: 5px;"><strong>#Implementation &#8211; Sample Runs</strong></div>
<p><iframe style="border: 1px solid #CCC; border-width: 1px 1px 0; margin-bottom: 5px;" src="http://www.slideshare.net/slideshow/embed_code/28832221" height="613" width="574" allowfullscreen="" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>#Code</strong></p>
<p><script src="https://gist.github.com/adwiteeya/7765256.js"></script></p>
<p><strong>#Sample Run</strong></p>
<p>In this sample run I am going to implement the POC expansion algorithm that was the final program that was written for OIVM.1. Make sure bootrom.txt and OIVM.exe are in the same folder.<br />
2. Open the Bootrom.txt using a Hex Editor (example : Hex Workshop) and write the following hex stream to it.</p>
<p><strong>3F <span style="color: #000080;">11</span> 25 <span style="color: #333399;">00</span> 2F <span style="color: #333399;">08</span> 3F <span style="color: #333399;">38</span> 3F <span style="color: #333399;">38</span> 41 <span style="color: #333399;">3D</span> 09 <span style="color: #333399;">1B</span> 3C<span style="color: #333399;"> 4C</span> <span style="color: #000000;">21<span style="color: #000080;"> 25</span> 00 <span style="color: #000080;">2F</span> 14 <span style="color: #000080;">7C</span> 67 <span style="color: #000080;">63</span> 55 <span style="color: #000080;">64</span> 61 <span style="color: #000080;">38</span> 67 <span style="color: #000080;">7C</span> 63</span> <span style="color: #000080;">38</span> 55 <span style="color: #000080;">64</span> 61 <span style="color: #000080;">38</span> 37 <span style="color: #000080;">41</span> 3D <span style="color: #000080;">15</span> C9 <span style="color: #000080;">45</span> 3F <span style="color: #000080;">11</span> 3F 12 18 <span style="color: #000080;">68</span> 3C <span style="color: #000080;">25</span> 00 <span style="color: #000080;">2F</span> 08 <span style="color: #000080;">12</span> 86 <span style="color: #000080;">C5</span> 38 <span style="color: #000080;">41</span> 3D <span style="color: #000080;">09</span> 00 <span style="color: #000080;">02</span> 04 <span style="color: #000080;">06</span> 08 </strong></p>
<p>3. Save the file. This is basically in the format of [STUB][Compressed Code]. The stub is 60 Bytes long and extends till the 0x09 in the second line of the hexadecimal stream. The parallel <strong>assembly</strong> for this code is as follows :</p>
<p><strong>[EXPANSION STUB]</strong><br />
scanf(&#8220;%d&#8221;,&amp;STACK[SP])<br />
MOV A,STACK[SP]<br />
FLAG=A-ROM[++IP]<br />
JE 8 BYTES<br />
scanf(&#8220;%d&#8221;,&amp;STACK[SP])<br />
++SP<br />
scanf(&#8220;%d&#8221;,&amp;STACK[SP])<br />
++SP<br />
&#8211;A<br />
JMP -9 BYTES<br />
MOV B,ROM[++IP] //STUB LENGTH<br />
A = fileLen<br />
A=A-B<br />
FLAG=A-ROM[++IP]<br />
JE 14 BYTES<br />
MOV C ,ROM[B]<br />
MOV D,SP<br />
MOV SP,C<br />
MOV C,STACK[SP]<br />
MOV SP,D<br />
MOV STACK[SP],C<br />
++SP<br />
MOV D,SP<br />
MOV C,ROM[B]<br />
MOV SP,C<br />
++SP<br />
MOV C,STACK[SP]<br />
MOV SP,D<br />
MOV STACK[SP],C<br />
++SP<br />
++B<br />
&#8211;A<br />
JMP -21 BYTES<br />
ROM=(char *)realloc(ROM,ROM[++IP]) //FILE EXPANDED<br />
scanf(&#8220;%d&#8221;,&amp;STACK[SP])<br />
MOV A,STACK[SP]<br />
scanf(&#8220;%d&#8221;,&amp;STACK[SP])<br />
MOV B,STACK[SP]<br />
MOV SP,B<br />
MOV C,ROM[++IP]<br />
FLAG=A-ROM[++IP]<br />
JE 8 BYTES<br />
MOV B,STACK[SP]<br />
MOV ROM[C],B<br />
++C<br />
++SP<br />
&#8211;A<br />
JMP -9 BYTES<br />
<strong>[COMPRESSED FORM]</strong><br />
00 02 04 06 08</p>
<p>4. Now run the program.If however you dont speak assembly that doesnt matter here is the <strong>algorithm</strong> for it :</p>
<p>1. Enter the Number of elements in the dictionary.<br />
2. Enter the dictionary.<br />
(Dictionary is stored on the stack)<br />
(The dictionary is looked up for values corresponding to compressed code)<br />
(The new Values are updated over the stack)<br />
3. Enter the New Size.<br />
4. Enter the SP (which could have been hardcoded as well)<br />
(The code expands)<br />
(The execution is now passed on to the expanded program)<br />
5. Enter the first element. Hit Enter.<br />
6. Enter the second element. Hit Enter.<br />
7. The sum is Displayed.<br />
8. OIVM halts.</p>
<p><strong>Results/Output</strong></p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/12/output.png"><img loading="lazy" decoding="async" class="size-full wp-image-453 aligncenter" alt="Output" src="http://adwiteeya.com/wp-content/uploads/2013/12/output.png" width="438" height="325" srcset="https://adwiteeya.com/wp-content/uploads/2013/12/output.png 438w, https://adwiteeya.com/wp-content/uploads/2013/12/output-300x222.png 300w" sizes="auto, (max-width: 438px) 100vw, 438px" /></a>#Thanks</p>
<p><strong>Special thanks</strong> to Mr. Atul Alex Cherian, Ms. Nishtha Jatana and Ms. Kritika Sobti.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://adwiteeya.com/code-obfusction-writing-a-vm/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Trolling func1()</title>
		<link>https://adwiteeya.com/trolling-func1/</link>
					<comments>https://adwiteeya.com/trolling-func1/#comments</comments>
		
		<dc:creator><![CDATA[Adwiteeya Agrawal]]></dc:creator>
		<pubDate>Wed, 18 Sep 2013 05:44:28 +0000</pubDate>
				<category><![CDATA[Information Security]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[breakpoint]]></category>
		<category><![CDATA[Loader]]></category>
		<category><![CDATA[OllyDbg]]></category>
		<category><![CDATA[openprocess]]></category>
		<category><![CDATA[Return Address]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Reversing]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[waitfordebugevent]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows debugging]]></category>
		<guid isPermaLink="false">http://adwiteeya.com/blog/?p=418</guid>

					<description><![CDATA[A note to the technical reader : This is basically a custom loader I wrote so that i could edit the return address of functions on the stack. Hi, non technical reader. Introduction So basically this is the small program I wrote, which we are going to play with. In this program there are two [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><strong>A note to the technical reader</strong> : This is basically a custom loader I wrote so that i could edit the return address of functions on the stack.</p>
<p>Hi, non technical reader.<span id="more-418"></span></p>
<p><span style="text-decoration: underline;"><strong>Introduction</strong></span></p>
<p>So basically this is the small program I wrote, which we are going to play with. In this program there are two functions : func1() and func2(). func1() is called from main, but func2() is never called.</p>
<p><script type="text/javascript" src="https://gist.github.com/adwiteeya/6605006.js"></script>So our aim is to execute func2 at run time.Here is the first run :</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/09/first-run.png"><img loading="lazy" decoding="async" class="size-full wp-image-423 aligncenter" alt="first run" src="http://adwiteeya.com/wp-content/uploads/2013/09/first-run.png" width="401" height="111" srcset="https://adwiteeya.com/wp-content/uploads/2013/09/first-run.png 401w, https://adwiteeya.com/wp-content/uploads/2013/09/first-run-300x83.png 300w" sizes="auto, (max-width: 401px) 100vw, 401px" /></a></p>
<p>As you can see we do not go inside func2.</p>
<p><span style="text-decoration: underline;"><strong>Reverse Engineering solution</strong></span> The first and the lot easier approach to this is, a RE solution. In this solution i just change some return addresses on the stack and we are done. Lets go step by step.</p>
<p><strong>Step1</strong> : Loading the program in OllyDbg</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/09/print2-in-olly.png"><img loading="lazy" decoding="async" class="size-full wp-image-424 aligncenter" alt="print2 in olly" src="http://adwiteeya.com/wp-content/uploads/2013/09/print2-in-olly.png" width="663" height="328" srcset="https://adwiteeya.com/wp-content/uploads/2013/09/print2-in-olly.png 663w, https://adwiteeya.com/wp-content/uploads/2013/09/print2-in-olly-300x148.png 300w, https://adwiteeya.com/wp-content/uploads/2013/09/print2-in-olly-624x308.png 624w" sizes="auto, (max-width: 663px) 100vw, 663px" /></a></p>
<p>As can be seen there are three functions main, func1 and func2. These are indicated by the square brackets. The order of these functions is func1, func2 and lastly main.</p>
<p><strong>Step2</strong> : Now I am going to put a breakpoint on func1 and then observe the stack.</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/09/print2-hit-breakpoint.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-425" alt="print2 hit breakpoint" src="http://adwiteeya.com/wp-content/uploads/2013/09/print2-hit-breakpoint.png" width="734" height="528" srcset="https://adwiteeya.com/wp-content/uploads/2013/09/print2-hit-breakpoint.png 734w, https://adwiteeya.com/wp-content/uploads/2013/09/print2-hit-breakpoint-300x215.png 300w, https://adwiteeya.com/wp-content/uploads/2013/09/print2-hit-breakpoint-624x448.png 624w" sizes="auto, (max-width: 734px) 100vw, 734px" /></a></p>
<p>We have two images here the first one represents the assembly view of print2.cpp when i hitthe breakpoint and the second one represents the stack when I hit the breakpoint. In figure 1 it can be seen that the func1 starts at 401334 and it(the address) is in read with black background. Which indicates that we have hit the breakpoint. In figure two we can see that the return address for func1() is 0040139A. If we look up this address in the assembly dump, it is the one just after func1() is called that is 401395. Hence, things are fine here, the function always returns to the immediate expression after a call to it. So we have two deductions from here. First that the return address is 40139A which can be changed to make the func1() jump somewhere else. and secondly that this address is placed on the memory address starting 28FF0C. <strong> </strong></p>
<p><strong>Step3</strong> : Now we are going to edit 40139A to 401359 so that we can jump over to func2().</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/09/stck-modified.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-427" alt="stck modified" src="http://adwiteeya.com/wp-content/uploads/2013/09/stck-modified.png" width="457" height="148" srcset="https://adwiteeya.com/wp-content/uploads/2013/09/stck-modified.png 457w, https://adwiteeya.com/wp-content/uploads/2013/09/stck-modified-300x97.png 300w" sizes="auto, (max-width: 457px) 100vw, 457px" /></a></p>
<p>After the modification even the comment changes to print2.00401359, which suggests that it is call. And as expected we enter the func2().</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/09/func2-entered.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-428" alt="func2 entered" src="http://adwiteeya.com/wp-content/uploads/2013/09/func2-entered.png" width="712" height="328" srcset="https://adwiteeya.com/wp-content/uploads/2013/09/func2-entered.png 712w, https://adwiteeya.com/wp-content/uploads/2013/09/func2-entered-300x138.png 300w, https://adwiteeya.com/wp-content/uploads/2013/09/func2-entered-624x287.png 624w" sizes="auto, (max-width: 712px) 100vw, 712px" /></a></p>
<p>However this is not enough. In a similar way we have to edit the return address of func2 to 40139A which was the original return address of func1. Now to be able to identify where is the return address of func2, we single step through func2 till we reach RETN.</p>
<p><strong>Step4</strong> : Here the top address on the stack is the return address for the stack. We need to change this address. In order to return and exit normally.</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/09/func2-return.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-431" alt="func2 return" src="http://adwiteeya.com/wp-content/uploads/2013/09/func2-return.png" width="570" height="296" srcset="https://adwiteeya.com/wp-content/uploads/2013/09/func2-return.png 570w, https://adwiteeya.com/wp-content/uploads/2013/09/func2-return-300x155.png 300w" sizes="auto, (max-width: 570px) 100vw, 570px" /></a></p>
<p>As can be seen on the stack 0028FF10 is the address where the return address 00403050 is stored. We modify this and then run the program.</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/09/re-success.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-432" alt="re success" src="http://adwiteeya.com/wp-content/uploads/2013/09/re-success.png" width="457" height="161" srcset="https://adwiteeya.com/wp-content/uploads/2013/09/re-success.png 457w, https://adwiteeya.com/wp-content/uploads/2013/09/re-success-300x105.png 300w" sizes="auto, (max-width: 457px) 100vw, 457px" /></a></p>
<p>We are able to do it neatly. :)</p>
<p><span style="text-decoration: underline;"><strong>Program Solution</strong></span></p>
<p>Now the RE solution was though easy, it wasn&#8217;t automated, and i had no fuckin idea about how do i make a permanent modification. What i mean here is like if you want to completely change an instruction you can do that and simply save changes to the new executable. But the stack is a dynamic memory region, it is not a part of the exe file that you save. So there was something more, some workaround that was needed. And thanks to Atul Alex Cherian, who told me about loader. So a loader is basically a program that lets the entire exe load in memory and then plays around with it. I first thought about using a tool for this, but then why not just code this shit out. So here is the algorithm that i followed for this. <strong></strong></p>
<p><strong>Step1</strong>. Use fread to read the bytes of print2.exe to an array.</p>
<p><strong>Step2</strong>. In this array modify the byte that represents beginning of func1 to CC (breakpoint).</p>
<p><strong>Step3.</strong> Save this array to a new myf.exe</p>
<p><strong>Step4</strong>. Detach pointer to myf.exe and open process myf.exe as a DEBUG_PROCESS <strong></strong></p>
<p><strong>Step5</strong>. Waitforadebugevent and use a simple condition to check that it orignates from the one that you forced.</p>
<p><strong>Step6</strong>. On receiving the EXCEPTION_BREAKPOINT DEBUG_EVENT suspend the thread using process information of myf.exe</p>
<p><strong>Step7</strong>, Now change the CC that you placed to the original byte</p>
<p><strong>Step8</strong>. Getthreadcontext of this thread and access the context structure to decrement EIP.</p>
<p><strong>Step9</strong>. Now change return addresses of func1 and func2. For func1 only one byte is edited and for func2 one has to edit 2 bytes.</p>
<p><strong>Step10</strong>. Resume thread. Myf.exe should exit after you press the first enter, then loader.exe should exit after you press the second enter. Here is the code :<script type="text/javascript" src="https://gist.github.com/adwiteeya/6605021.js"></script></p>
<p>Here is the screen shot showing its execution.</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/09/program-success.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-434" alt="program success" src="http://adwiteeya.com/wp-content/uploads/2013/09/program-success.png" width="671" height="338" srcset="https://adwiteeya.com/wp-content/uploads/2013/09/program-success.png 671w, https://adwiteeya.com/wp-content/uploads/2013/09/program-success-300x151.png 300w, https://adwiteeya.com/wp-content/uploads/2013/09/program-success-624x314.png 624w" sizes="auto, (max-width: 671px) 100vw, 671px" /></a></p>
<p>And special thanks to Dhanesh sir. :)</p>
<p>H3LL Y34H! :)</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://adwiteeya.com/trolling-func1/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>Debbuging Code by Modifying an EXE</title>
		<link>https://adwiteeya.com/one-hell-of-a-debug/</link>
					<comments>https://adwiteeya.com/one-hell-of-a-debug/#respond</comments>
		
		<dc:creator><![CDATA[Adwiteeya Agrawal]]></dc:creator>
		<pubDate>Fri, 12 Jul 2013 17:20:20 +0000</pubDate>
				<category><![CDATA[Information Security]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Anti-debugging]]></category>
		<category><![CDATA[GetCurrentThread]]></category>
		<category><![CDATA[GetProcAddress]]></category>
		<category><![CDATA[Load Library]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Windows API]]></category>
		<category><![CDATA[ZwGetInformationThread]]></category>
		<guid isPermaLink="false">http://adwiteeya.com/blog/?p=351</guid>

					<description><![CDATA[Hey, So moving on to a relatively advanced API based debugger detection or anti-debugging techniques, i am going to today discuss the implmentation of ZwSetInformationThread routine. Now this is an undocumented win32 API and we need to get a pointer to the function inside a library and then make a call to the function. The [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Hey,<br />
So moving on to a relatively advanced API based debugger detection or anti-debugging techniques, i am going to today discuss the implmentation of ZwSetInformationThread routine. Now this is an undocumented win32 API and we need to get a pointer to the function inside a library and then make a call to the function. The very first implementation is as follows (by the way I code on Code:Blocks 12.11): <span id="more-351"></span></p>
<p style="padding-left: 30px;"><span style="color: #339966;">#include &#8220;stdafx.h&#8221;</span><br />
<span style="color: #339966;">#include &#8220;windows.h&#8221;</span></p>
<p><strong><span style="color: #003366;">void</span></strong> antidebug<span style="color: #ff0000;">()</span>;<br />
<span style="color: #003366;"><strong>void</strong></span> antidebug<span style="color: #ff0000;">()</span><br />
<span style="color: #ff0000;">{</span><br />
HMODULE lib<span style="color: #ff0000;">;</span><br />
FARPROC _ZwSetInformationThread<span style="color: #ff0000;">;</span><br />
lib = LoadLibrary<span style="color: #ff0000;">(&#8220;</span><span style="color: #3366ff;">ntdll.dll</span><span style="color: #ff0000;">&#8220;)</span><span style="color: #ff0000;">;</span><br />
_ZwSetInformationThread <span style="color: #ff0000;">=</span> GetProcAddress<span style="color: #ff0000;">(</span>lib<span style="color: #ff0000;">, &#8220;</span><span style="color: #3366ff;">ZwSetInformationThread</span><span style="color: #ff0000;">&#8220;);</span><br />
<span style="color: #ff0000;">(</span>_ZwSetInformationThread<span style="color: #ff0000;">)(</span>GetCurrentThread<span style="color: #ff0000;">(),</span>0x11<span style="color: #ff0000;">,</span>0<span style="color: #ff0000;">,</span>0<span style="color: #ff0000;">);</span><br />
MessageBox<span style="color: #ff0000;">(</span>NULL<span style="color: #ff0000;">, &#8220;</span><span style="color: #3366ff;">Debugger Detached</span><span style="color: #ff0000;">&#8220;, &#8220;</span><span style="color: #3366ff;">Debugger Detached</span><span style="color: #ff0000;">&#8220;,</span> MB_OK<span style="color: #ff0000;">);</span><br />
<span style="color: #ff0000;">}</span></p>
<p><strong><span style="color: #003366;">int</span></strong> _tmain<span style="color: #ff0000;">(</span><strong><span style="color: #003366;">int</span></strong> argc<span style="color: #ff0000;">,</span> _TCHAR* argv<span style="color: #ff0000;">[])</span><br />
<span style="color: #ff0000;">{</span><br />
antidebug<span style="color: #ff0000;">();</span><br />
MessageBox<span style="color: #ff0000;">(</span>NULL<span style="color: #ff0000;">, &#8220;</span><span style="color: #3366ff;">Double Check&#8230;</span><span style="color: #ff0000;">&#8220;, &#8220;</span><span style="color: #3366ff;">Double Check</span><span style="color: #ff0000;">&#8220;,</span> MB_OK<span style="color: #ff0000;">);</span><br />
<span style="color: #003366;"><strong>return</strong></span> 0<span style="color: #ff0000;">;</span><br />
<span style="color: #ff0000;">}</span></p>
<p>Before I get into the BUGs lemme just try and explain what is happening here, so we are basically loading a handle to ntdll.dll by calling the LoadLibrary process and then we load the callback to _ZwSetInformationThread which is of datatype FARPROC[<a title="More on FARPROC" href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms633571%28v=vs.85%29.aspx" target="_blank">more on FARPROC</a>] then we call ZwSetInformationThread routine which has four parameters as per http://msdn.microsoft.com/en-us/library/windows/hardware/ff567101%28v=vs.85%29.aspx .</p>
<p>The firstparameter is the handle to the current thread and since we want to run this function within our program we use the GetCurrentThread() function call which returns a handle to the current thread. Next is a value 11. This is basically the value because of which the debugger would get detached from the process. Now how did i get this value. As per documentation on msdn the second parameter is of the enumeration type _THREADINFOCLASS which is as follows <a title="THREADINFOCLASS enum" href="http://www.reversing.be/forum/viewtopic.php?t=520" target="_blank">[source]</a> :</p>
<p style="padding-left: 30px;">typedef enum _THREADINFOCLASS {<br />
ThreadBasicInformation,<br />
ThreadTimes,<br />
ThreadPriority,<br />
ThreadBasePriority,<br />
ThreadAffinityMask,<br />
ThreadImpersonationToken,<br />
ThreadDescriptorTableEntry,<br />
ThreadEnableAlignmentFaultFixup,<br />
ThreadEventPair_Reusable,<br />
ThreadQuerySetWin32StartAddress,<br />
ThreadZeroTlsCell,<br />
ThreadPerformanceCount,<br />
ThreadAmILastThread,<br />
ThreadIdealProcessor,<br />
ThreadPriorityBoost,<br />
ThreadSetTlsArrayAddress,<br />
ThreadIsIoPending,<br />
<strong>ThreadHideFromDebugger,</strong><br />
ThreadBreakOnTermination,<br />
MaxThreadInfoClass<br />
} THREADINFOCLASS;</p>
<p>So ThreadHideFromDebugger is 17 which is basically 11 in hex. And the remaining two parameters are obviously not of importance to us. :D</p>
<p>Now coming to the bugs. &#8220;|17|error: too many arguments to function|&#8221; But MSDN said that it needed four :O and now i started removing one parameter at a time and the function ran when i did not pass any arguments. After having searched a lot! The only solution that i could think of was to use the asm function and pass these arguments manually.</p>
<p>So i made the following changes.</p>
<p>(_ZwSetInformationThread)();<br />
GetCurrentProcess();<br />
<strong><span style="color: #000080;">asm</span></strong><span style="color: #ff0000;">(&#8220;</span><span style="color: #339966;">pushl 0</span><span style="color: #ff0000;">&#8220;);</span><br />
<span style="color: #000080;"><strong>asm</strong></span><span style="color: #ff0000;">(&#8220;</span><span style="color: #339966;">pushl 0</span><span style="color: #ff0000;">&#8220;);</span><br />
<span style="color: #000080;"><strong>asm</strong></span><span style="color: #ff0000;">(&#8220;</span><span style="color: #339966;">pushl 0x11<span style="color: #ff0000;">&#8220;</span></span><span style="color: #ff0000;">);</span><br />
<span style="color: #000080;"><strong>asm</strong></span><span style="color: #ff0000;">(&#8220;</span><span style="color: #339966;">pushl %eax</span><span style="color: #ff0000;"><span style="color: #339966;">&#8220;</span>);</span></p>
<p>Oh and by the way the assembly syntax utilized on GCC is the AT&amp;T asm. So yeah! I had to first read that because i have being doing the intel format. I read [<a title="ASM AT&amp;T on code::blocks" href="http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html" target="_blank">this</a>]</p>
<p>So this was some success because atleast the exe compiled, i had already though that now i would easily debug everything on OllyDbg and get it through. But this is what GCC(or i) had done to the code. :</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/07/asm.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-352" alt="asm" src="http://adwiteeya.com/wp-content/uploads/2013/07/asm.png" width="631" height="324" srcset="https://adwiteeya.com/wp-content/uploads/2013/07/asm.png 631w, https://adwiteeya.com/wp-content/uploads/2013/07/asm-300x154.png 300w" sizes="auto, (max-width: 631px) 100vw, 631px" /></a></p>
<p>So this wasnt the best solution.But it was some success since i was atleast able to compile the exe now and look it up in Olly (Somehow i am more comfortable with that.)</p>
<p>I decided that since the value passed as the handle to the process is the only thing that i do not know lemme pass other values by modifying the assembly code manually in Olly and changed the code to the following :</p>
<p>HANDLE hndle = GetCurrentThread();<br />
(_NtSetInformationThread)();</p>
<p>The exe did compile and i was getting the pop up messages too.</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/07/popup.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-353" alt="popup" src="http://adwiteeya.com/wp-content/uploads/2013/07/popup.png" width="675" height="318" srcset="https://adwiteeya.com/wp-content/uploads/2013/07/popup.png 675w, https://adwiteeya.com/wp-content/uploads/2013/07/popup-300x141.png 300w" sizes="auto, (max-width: 675px) 100vw, 675px" /></a></p>
<p>Next thing was to immediately load this exe into my dearest OllyDbg and get started. Here is the very first look.</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/07/asm2.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-354" alt="asm2" src="http://adwiteeya.com/wp-content/uploads/2013/07/asm2.png" width="600" height="407" srcset="https://adwiteeya.com/wp-content/uploads/2013/07/asm2.png 600w, https://adwiteeya.com/wp-content/uploads/2013/07/asm2-300x203.png 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /></a></p>
<p>You can see that a call gets made to GetCurrentThread at 00401365 and the value is returned into EAX which is them moved to our variable &#8220;handle&#8221; (EBP-14). So after a couple of runs i realized that this value was always the same and always returning FFFFFFFE i am not sure if this is a fixed constant for a each system or is it just the local handle to every thread. But this is what it was in the case that I had in front of me. Then later you can see that a different value is moved into EAX and at 00401370 we make a call to that address (call eax) hence this is the handle to the ZwSetInformationThread.</p>
<p>So far so good. Next i had to modify the code so that i could push values to the stack before the call eax is executed. If you at the address 00401362 you would see that MOV EBP-10,EAX instruction is executed just after GetProcAddress so basically this is the place where the handle to ZwGetInformationThread is placed on the stack hence we can only modify the code after this. That is from 401365 to 40136D where the value is pushed back into EAX. (Or we could NOP both of these so that the value remains unchanged)</p>
<p>So here are the modifications that i did, wait and after this modification i clicked analyse code. So you&#8217;d see that now Olly recognizes this as a function ^_^</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/07/asm3.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-355" alt="asm3" src="http://adwiteeya.com/wp-content/uploads/2013/07/asm3.png" width="618" height="259" srcset="https://adwiteeya.com/wp-content/uploads/2013/07/asm3.png 618w, https://adwiteeya.com/wp-content/uploads/2013/07/asm3-300x125.png 300w" sizes="auto, (max-width: 618px) 100vw, 618px" /></a></p>
<p>so we have obviously overwritten the call to GetCurrentThread but the code seems to fit just fine! :)</p>
<p>Now do you see a push -2 at 0040136B i had assembled PUSH 0xFFFFFFFE. :) And also since this is windows you would have to push the last argument first&#8230; just in case you did not know this.<br />
Now before you run the code remember to save all modifications to exe because if the code is successful you would be detached from the debugger.<br />
And finally we run the program. We Get the pop ups. And next we are detached from the debugger. :) (The code screen goes blank. Try reloading with &lt;&lt; button.<br />
The Blank Screen :</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/07/asm4.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-356" alt="asm4" src="http://adwiteeya.com/wp-content/uploads/2013/07/asm4.png" width="607" height="256" srcset="https://adwiteeya.com/wp-content/uploads/2013/07/asm4.png 607w, https://adwiteeya.com/wp-content/uploads/2013/07/asm4-300x126.png 300w" sizes="auto, (max-width: 607px) 100vw, 607px" /></a></p>
<p>The Error :</p>
<p><a href="http://adwiteeya.com/wp-content/uploads/2013/07/error.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-357" alt="error" src="http://adwiteeya.com/wp-content/uploads/2013/07/error.png" width="460" height="178" srcset="https://adwiteeya.com/wp-content/uploads/2013/07/error.png 460w, https://adwiteeya.com/wp-content/uploads/2013/07/error-300x116.png 300w" sizes="auto, (max-width: 460px) 100vw, 460px" /></a></p>
<p>H3LL Y34H! B)</p>
]]></content:encoded>
					
					<wfw:commentRss>https://adwiteeya.com/one-hell-of-a-debug/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
