<?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>Anti-debugging &#8211; Adwiteeya Agrawal | Home</title>
	<atom:link href="https://adwiteeya.com/tag/anti-debugging/feed/" rel="self" type="application/rss+xml" />
	<link>https://adwiteeya.com</link>
	<description>Security fanatic. Reverse Engineer and Web App Specialist.</description>
	<lastBuildDate>Fri, 12 Jul 2013 21:29:39 +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>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 fetchpriority="high" 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="(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 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="(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 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="(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>
		<item>
		<title>CheckRemoteDebuggerPresent API</title>
		<link>https://adwiteeya.com/checkremotedebuggerpresent-api/</link>
					<comments>https://adwiteeya.com/checkremotedebuggerpresent-api/#comments</comments>
		
		<dc:creator><![CDATA[Adwiteeya Agrawal]]></dc:creator>
		<pubDate>Thu, 11 Jul 2013 18:21:26 +0000</pubDate>
				<category><![CDATA[Information Security]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Anti-debugging]]></category>
		<category><![CDATA[RE]]></category>
		<category><![CDATA[summer project]]></category>
		<category><![CDATA[win32 api]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://adwiteeya.com/blog/?p=346</guid>

					<description><![CDATA[Hi, I am currently working on anti-debugging techniques which will also be my project for this year in college.Though i know for a fact  most basic anti-debug techniques are  API based techniques, but i am still doing this post because firstly i did not find any working code and whatever non-working code was present was [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Hi,</p>
<p>I am currently working on anti-debugging techniques which will also be my project for this year in college.Though i know for a fact  most basic anti-debug techniques are  API based techniques, but i am still doing this post because firstly i did not find any working code and whatever non-working code was present was also implemented locally&#8230; By the way I am really impressed by the work compiled by Tyler Shields in 2009 and he has put up some of the best resources out there!</p>
<p>This post is basically about one of those standard API&#8217;s &gt; CheckRemoteDebuggerPresent function. <span id="more-346"></span>Now there is enough stuff on msdn and other websites that you can find about its implementations and about the working of the function however for some reason they did not work for me! So here is what finally worked. And I have done a remote process example too.</p>
<p><strong>Implementation for process locally </strong></p>
<p><span style="color: #339966;">#ifndef _WIN32_WINNT</span><br />
<span style="color: #339966;">#define _WIN32_WINNT 0x0601</span><br />
<span style="color: #339966;">#endif</span><br />
<span style="color: #339966;">#include&lt;windows.h&gt;</span><br />
<span style="color: #339966;">#include&lt;stdio.h&gt;</span><br />
<span style="color: #339966;">#include&lt;conio.h&gt;</span></p>
<p>int main()<br />
{<br />
BOOL pblsPresent = FALSE;<br />
HANDLE hndle = NULL;</p>
<p>CheckRemoteDebuggerPresent(GetCurrentProcess(),&amp;pblsPresent);<br />
if(pblsPresent){<br />
printf(&#8220;<span style="color: #3366ff;">Being Debugged\n</span>&#8220;);<br />
}<br />
else{<br />
printf(&#8220;<span style="color: #3366ff;">Not being debugged\n</span>&#8220;);<br />
}<br />
getch();<br />
return 0;<br />
}</p>
<p><strong>Implementation For Remote Process (</strong>with PID 2800<strong>)</strong></p>
<p><span style="color: #339966;">#ifndef _WIN32_WINNT</span><br />
<span style="color: #339966;">#define _WIN32_WINNT 0x0601</span><br />
<span style="color: #339966;">#endif</span><br />
<span style="color: #339966;">#include&lt;windows.h&gt;</span><br />
<span style="color: #339966;">#include&lt;stdio.h&gt;</span><br />
<span style="color: #339966;">#include&lt;conio.h&gt;</span></p>
<p>int main()<br />
{<br />
BOOL pblsPresent = FALSE;<br />
HANDLE hndle = NULL;</p>
<p>hndle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,2800);</p>
<p>if(hndle){<br />
printf(&#8220;<span style="color: #3366ff;">Done getting a handle \n</span>&#8220;);<br />
}else {<br />
printf(&#8220;<span style="color: #3366ff;">Invalid PID \n</span>&#8220;);<br />
}<br />
CheckRemoteDebuggerPresent(hndle,&amp;pblsPresent);<br />
if(pblsPresent){<br />
printf(&#8220;<span style="color: #3366ff;">Being Debugged\n</span>&#8220;);<br />
}<br />
else{<br />
printf(&#8220;<span style="color: #3366ff;">Not being debugged\n</span>&#8220;);<br />
}<br />
getch();<br />
return 0;<br />
}</p>
]]></content:encoded>
					
					<wfw:commentRss>https://adwiteeya.com/checkremotedebuggerpresent-api/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
