<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2" -->
<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/"
	>

<channel>
	<title>Caloni.com.br</title>
	<link>http://www.caloni.com.br/blog/en</link>
	<description>What you C is What you Get!</description>
	<pubDate>Mon, 17 Aug 2009 11:03:27 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2</generator>
	<language>en</language>
			<item>
		<title>What I&#8217;ve been doing in the last 10 years</title>
		<link>http://www.caloni.com.br/blog/en/archives/what-ive-been-doing-in-the-last-10-years</link>
		<comments>http://www.caloni.com.br/blog/en/archives/what-ive-been-doing-in-the-last-10-years#comments</comments>
		<pubDate>Mon, 17 Aug 2009 10:00:26 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[Nothing]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/archives/what-ive-been-doing-in-the-last-10-years</guid>
		<description><![CDATA[This week I dedicate myself to update my resumè and I have the brilliant idea of put into it my technical historical, what resuming is a list of things I did or was involved with during my brief ten years stay in the programming world.
So I thought: &#8220;this could be useful to the people read [...]]]></description>
			<content:encoded><![CDATA[<p>This week I dedicate myself to update my resumè and I have the brilliant idea of put into it my technical historical, what resuming is a list of things I did or was involved with during my brief ten years stay in the programming world.</p>
<p>So I thought: &#8220;this could be useful to the people read me&#8221;. Why not? Perhaps you got some doubt waiting to be solved and is unable to find a guy who knows something about this. Perhaps this f* guy even exists and has a blog where he could share some knowledge that is stuck in that empty programmer head.</p>
<p>In this case, it follows bellow a brief description of my professional life, with the things I could remember I did since December 2000. What I haven&#8217;t remember probably is not worth of.</p>
<ul>
<li>Software and hardware inventory</li>
<li>Clipboard and PrintScreen protection using windows hooks and global messages manipulation</li>
<li>Driver writing system event log</li>
<li>DeviceIoControl user/kernel communication</li>
<li>Desktop remote control using VNC technique</li>
<li>Remote execution tool PsExec (SysInternals) like</li>
<li>Print control using regex (Boost) and shell hook</li>
<li>Access policies management during user logon/logoff (register and hooks)</li>
<li>Datgabase migration CTree -&gt; SQL (OLE classes)</li>
<li>Windows authentication using custom GINA and DCOM; Credential Provider (Vista)</li>
<li>CTree database synchronism using custom DCOM service</li>
<li>Bootable Linux CD with bash scripts and disk cryptography tools using C language</li>
<li>Hard disk encryption and PenDrive (USB) storage control</li>
<li>Blue Screen analysis using memory dumps and WinDbg live (Gflags)</li>
<li>System account execution using custom COM service</li>
<li>MBR (Master Boot Record) customization library</li>
<li>Blowfish/SHA-1 encryption library using C++ and 16 bits Assembly</li>
<li>Log access driver using shared memory between user and kernel mode</li>
<li>Kernel mode API hook for 9X and NT platforms</li>
<li>16 bits Assembly loader; debugging using debug.com tool</li>
<li>Executable protection using embedded domain authentication recorded inside files resources</li>
<li>Internet Explorer 6/7 and Firefox 1/2 browsing protection using Assembly 32 bits code injection</li>
<li>Code, strings and execution protection library (using Win32 interruptions)</li>
<li>Centralized log generation library using shared memory and global events</li>
<li>Internet Explorer 6/7 BHO (Broser Helper Object) and ActiveX; Mozilla/Firefox XPI plugin</li>
<li>Projects management using Source Safe, Bazaar and Batch (Win) scripts</li>
<li>Kernel mode debugging using SoftIce and WinDbg for NT platform, SoftIce and WDeb98 for 9X platform</li>
<li>Trojans reverse engineering (C++, Visual Basic, Delphi) using WinDbg and IDA</li>
<li>Diagnostic tool listing files, services, drivers, register, disk partitions, processes, etc</li>
<li>Jobs monitoring in Win2000+ to installation and update control</li>
<li>Application use monitoring using noninvasive and invasive windows hooks</li>
<li>Houaiss reverse engineering and Babylon importation (dictionaries)</li>
<li>Build control with Cruise Control .NET, symbol server with Debugging Tools</li>
<li>Projects documentation using Doxygen and Wiki (Trac)</li>
<li>Management interfaces using C++ Builder 5/6 and Visual C++ custom libraries</li>
<li>E-mails analyzer using regular expressions (ATL classes)</li>
<li>Configuration interfaces using Visual C++ (MFC /ATL/WTL)</li>
<li>Project and tracing analysis using regular expressions (Vim and Grep)</li>
<li>Articles development using technical blog and Code Project community.</li>
</ul>
<p>Perhaps I update this list frequently. Although I guess the rightest choice would be to update the list with articles about my every day &#8220;brushing bits&#8221; life . After all, I got a technical blog already!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/what-ive-been-doing-in-the-last-10-years/feed</wfw:commentRss>
		</item>
		<item>
		<title>Static Polymorphism</title>
		<link>http://www.caloni.com.br/blog/en/archives/static-polymorphism</link>
		<comments>http://www.caloni.com.br/blog/en/archives/static-polymorphism#comments</comments>
		<pubDate>Fri, 10 Jul 2009 18:16:32 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/archives/static-polymorphism</guid>
		<description><![CDATA[

To explain the polymorphism nothing is better than see how stuff used to be. If you were a twenty old C programmer in the past and created the following functions:
int sum&#40;int x, int y&#41;;
double sum&#40;double x, double y&#41;;
 
int main&#40;&#41;
&#123;
    int zi = sum&#40;2, 3&#41;;
    double zd = sum&#40;2.5, [...]]]></description>
			<content:encoded><![CDATA[

<p>To explain the polymorphism nothing is better than see how stuff used to be. If you were a twenty old C programmer in the past and created the following functions:</p>
<p><pre><span style="color: #0000ff;">int</span> sum<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #0000ff;">double</span> sum<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">double</span> x, <span style="color: #0000ff;">double</span> y<span style="color: #000000; font-weight: bold;">&#41;</span>;
 
<span style="color: #0000ff;">int</span> main<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
    <span style="color: #0000ff;">int</span> zi = sum<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span>, <span style="color: #000000;">3</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
    <span style="color: #0000ff;">double</span> zd = sum<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span>.<span style="color: #000000;">5</span>, <span style="color: #000000;">3</span>.<span style="color: #000000;">4</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
    <span style="color: #0000ff;">return</span> <span style="color: #000000;">0</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;</pre></p>
<p>Immediately the compiler would blame you about the following errors:</p>
<pre>overload.c</pre>
<pre>overload.c(2) : warning C4028: formal parameter 1 different from declaration
overload.c(2) : warning C4028: formal parameter 2 different from declaration
overload.c(2) : error C2371: 'sum' : redefinition; different basic types
        overload.c(1) : see declaration of 'sum'</pre>
<p>This happens because in C <strong>the identifiers are unique into the scope.</strong> This is the reason why the following code is wrong also:</p>
<p><pre><span style="color: #0000ff;">int</span> main<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
    <span style="color: #0000ff;">int</span> x = <span style="color: #000000;">0</span>;
    <span style="color: #0000ff;">int</span> x = <span style="color: #000000;">1</span>;
    <span style="color: #0000ff;">return</span> <span style="color: #000000;">0</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;</pre></p>
<pre>overload.c
overload.c(5) : error C2374: 'x' : redefinition; multiple initialization
        overload.c(4) : see declaration of 'x'</pre>
<p>Back to the 90&#8217;s, this is also wrong in C++. Even for a logic issue: how the compiler can pick a variable if we&#8217;re using the same name for both of them?</p>
<p>Even though, there&#8217;s a little trick to stop the ambiguity when we talk about functions: the parameters that they receives.</p>
<p><pre><span style="color: #0000ff;">int</span> sum<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #0000ff;">double</span> sum<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">double</span> x, <span style="color: #0000ff;">double</span> y<span style="color: #000000; font-weight: bold;">&#41;</span>;
 
<span style="color: #0000ff;">int</span> main<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
    <span style="color: #0000ff;">int</span> zi = sum<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span>, <span style="color: #000000;">3</span><span style="color: #000000; font-weight: bold;">&#41;</span>; <span style="color: #006600;">// two int types: call sum(int, int)</span>
    <span style="color: #0000ff;">double</span> zd = sum<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span>.<span style="color: #000000;">5</span>, <span style="color: #000000;">3</span>.<span style="color: #000000;">4</span><span style="color: #000000; font-weight: bold;">&#41;</span>; <span style="color: #006600;">// two double types: it just can be sum(double, double)</span>
    <span style="color: #0000ff;">return</span> <span style="color: #000000;">0</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;</pre></p>
<pre>C:\Tests&gt;cl /c overload.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.6030 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

overload.cpp

C:\Tests&gt;</pre>
<p>This allowed in C++ the creation of static overload, that is exactly this: to call a function not just by its name, but also to match its signature, the number and the type of the received parameters. We call static because this is done just by the compiler, not creating any overhead during the execution.</p>
<p>Among the most common uses some are as it follows:</p>
<ul>
<li>Functions with the same name treating different parameters;
<ul>
<li>sum(int, int);</li>
<li>sum(double, double);</li>
<li>Obs.: This ignores, of course, the templates usefulness.</li>
</ul>
</li>
<li>New version of the same fuction with addictional parameters;
<ul>
<li>export_data(void* buffer, int size);</li>
<li>export_data(void* buffer, int size, unsigned long options);</li>
</ul>
</li>
<li>Same method name to set and get the value of a class property;
<ul>
<li>Class::Property(int x); // setter</li>
<li>int x Class::Property() const; // getter</li>
</ul>
</li>
<li>Well, whatever your imagination and needs demand =)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/static-polymorphism/feed</wfw:commentRss>
		</item>
		<item>
		<title>Conteúdo em português</title>
		<link>http://www.caloni.com.br/blog/en/archives/conteudo-em-portugues</link>
		<comments>http://www.caloni.com.br/blog/en/archives/conteudo-em-portugues#comments</comments>
		<pubDate>Sat, 09 May 2009 04:35:33 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[Português]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/archives/conteudo-em-portugues</guid>
		<description><![CDATA[Se você está procurando pelo conteúdo do blogue em português, acesse o endereço original.
]]></description>
			<content:encoded><![CDATA[<p>Se você está procurando pelo conteúdo do blogue em português, acesse o <a href="http://caloni.com.br" title="Caloni.com.br">endereço original</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/conteudo-em-portugues/feed</wfw:commentRss>
		</item>
		<item>
		<title>Antidebugging during the process attach</title>
		<link>http://www.caloni.com.br/blog/en/archives/antidebugging-during-the-process-attach</link>
		<comments>http://www.caloni.com.br/blog/en/archives/antidebugging-during-the-process-attach#comments</comments>
		<pubDate>Tue, 05 Aug 2008 09:00:21 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[Reversing]]></category>

		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/?p=19</guid>
		<description><![CDATA[



Today was a great day for reverse engineering and protection analysis. I've found two great programs to to these things: a API call monitor and a COM call monitor. Besides that, in the first program site - from a enthusiastic of the good for all Win32 Assembly - I've found the source code for one [...]]]></description>
			<content:encoded><![CDATA[



<p>Today was a great day for reverse engineering and protection analysis. I've found two great programs to to these things: a <a href="http://www.kakeeware.com/">API call monitor</a> and a <a href="http://www.blunck.info/comtrace.html">COM call monitor</a>. Besides that, in the first program site - from a enthusiastic of the good for all Win32 Assembly - I've found the source code for one more antidebugging technique, what bring us back to our series of <a href="http://www.caloni.com.br/blog/?s=antidebug%3A">antidebugging techniques</a>.</p>
<h4>The antiattaching technique</h4>
<p>The purpose of this protection is to detect if some debugger tries to attach into our running process. The attach to process operation is pretty common in all known debugger, as WinDbg and Visual Studio. Different from the DebugPort protection, this solution avoids the attach action from the debuggee program. In this case the protection can make choices about what to do on the event of attach (terminate the process, send an e-mail, etc).</p>
<p>The code I've found does nothing more than to make use of the attach process function that's always called: the <strong>ntdll!DbgUiRemoteBreakin</strong>. Being always called, we can just to put our code there, what is relatively easy to do:</p>
<p><pre><span style="color: #006600;">#include &lt;windows.h&gt;</span>
<span style="color: #006600;">#include &lt;iostream&gt;</span>
<span style="color: #006600;">#include &lt;assert.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std;
&nbsp;
&nbsp;
<span style="color: #006600;">/** This function is triggered when a debugger try to attach into our process.
*/</span>
<span style="color: #0000ff;">void</span> AntiAttachAbort<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #006600;">// this is a test application, remember?</span>
	MessageBox<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">NULL</span>, <span style="color: #666666;">"Espertinho, hein?"</span>, <span style="color: #666666;">"AntiAttachDetector"</span>, MB_OK | MB_ICONERROR<span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
	<span style="color: #006600;">// this is the end</span>
	TerminateProcess<span style="color: #000000; font-weight: bold;">&#40;</span>GetCurrentProcess<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>, -<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #006600;">/** This function installs  a trigger that is activated when a debugger try to attach.
@see AntiAttachAbort.
*/</span>
<span style="color: #0000ff;">void</span> InstallAntiAttach<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	PVOID attachBreak = GetProcAddress<span style="color: #000000; font-weight: bold;">&#40;</span>
		GetModuleHandle<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #666666;">"ntdll"</span><span style="color: #000000; font-weight: bold;">&#41;</span>, <span style="color: #006600;">// this dll is ALWAYS loaded</span>
		<span style="color: #666666;">"DbgUiRemoteBreakin"</span><span style="color: #000000; font-weight: bold;">&#41;</span>; <span style="color: #006600;">// this function is ALWAYS called on the attach event</span>
&nbsp;
	<span style="color: #0000ff;">assert</span><span style="color: #000000; font-weight: bold;">&#40;</span>attachBreak<span style="color: #000000; font-weight: bold;">&#41;</span>; <span style="color: #006600;">// attachBreak NEVER can be null</span>
&nbsp;
	<span style="color: #006600;">// opcodes to run a jump to the function AntiAttachAbort</span>
	BYTE jmpToAntiAttachAbort<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">&#93;</span> =
	<span style="color: #000000; font-weight: bold;">&#123;</span> 0xB8, 0xCC, 0xCC, 0xCC, 0xCC,   <span style="color: #006600;">// mov eax, 0xCCCCCCCC</span>
	0xFF, 0xE0 <span style="color: #000000; font-weight: bold;">&#125;</span>;                     <span style="color: #006600;">// jmp eax</span>
&nbsp;
	<span style="color: #006600;">// we change 0xCCCCCCCC using a more useful address</span>
	*reinterpret_cast&lt;PVOID*&gt;<span style="color: #000000; font-weight: bold;">&#40;</span>&amp;jmpToAntiAttachAbort<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">&#41;</span> = AntiAttachAbort;
&nbsp;
	DWORD oldProtect = <span style="color: #000000;">0</span>;
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> VirtualProtect<span style="color: #000000; font-weight: bold;">&#40;</span>attachBreak, <span style="color: #0000ff;">sizeof</span><span style="color: #000000; font-weight: bold;">&#40;</span>jmpToAntiAttachAbort<span style="color: #000000; font-weight: bold;">&#41;</span>, 
		PAGE_EXECUTE_READWRITE, &amp;oldProtect<span style="color: #000000; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #006600;">// if we can change the code page protection we put a jump to our code</span>
		CopyMemory<span style="color: #000000; font-weight: bold;">&#40;</span>attachBreak, 
			jmpToAntiAttachAbort, <span style="color: #0000ff;">sizeof</span><span style="color: #000000; font-weight: bold;">&#40;</span>jmpToAntiAttachAbort<span style="color: #000000; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
		<span style="color: #006600;">// restore old protection</span>
		VirtualProtect<span style="color: #000000; font-weight: bold;">&#40;</span>attachBreak, <span style="color: #0000ff;">sizeof</span><span style="color: #000000; font-weight: bold;">&#40;</span>jmpToAntiAttachAbort<span style="color: #000000; font-weight: bold;">&#41;</span>, 
			oldProtect, &amp;oldProtect<span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #006600;">/** In the beginning, God said: 'int main!'
*/</span>
<span style="color: #0000ff;">int</span> main<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	InstallAntiAttach<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #0000ff;">cout</span> &lt;&lt; <span style="color: #666666;">"Try to attach, if you can..."</span>;
	<span style="color: #0000ff;">cin</span>.<span style="color: #000000;">get</span><span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span></pre></p>
<p>To compile the code above, just call the compiler and linker normally. Obs.: We need the user32.lib in order to call MessageBox API:</p>
<pre>cl /c antiattach.cpp
link antiattach.obj user32.lib</pre>
<pre>
antiattach.exe
Try to attach, if you can...</pre>
<p>After the program has been running, every try to attach will show a detection message and program termination.</p>
<pre>windbg -pn antiattach.exe</pre>
<pre><a href="http://www.caloni.com.br/blog/wp-content/uploads/antiattach.png" title="Detecção de attach"><img src="http://www.caloni.com.br/blog/wp-content/uploads/antiattach.png" alt="Detecção de attach" /></a></pre>
<h4>Code peculiarities</h4>
<p>Yes, I know. Sometimes we have to use "brute force codding" and make obscure codes, like this:</p>
<p><pre><span style="color: #006600;">// opcodes to run a jump to the function AntiAttachAbort</span>
BYTE jmpToAntiAttachAbort<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">&#93;</span> =
<span style="color: #000000; font-weight: bold;">&#123;</span> 0xB8, 0xCC, 0xCC, 0xCC, 0xCC,   <span style="color: #006600;">// mov eax, 0xCCCCCCCC</span>
0xFF, 0xE0 <span style="color: #000000; font-weight: bold;">&#125;</span>;                     <span style="color: #006600;">// jmp eax</span>
&nbsp;
<span style="color: #006600;">// we change 0xCCCCCCCC using a more useful address</span>
*reinterpret_cast&lt;PVOID*&gt;<span style="color: #000000; font-weight: bold;">&#40;</span>&amp;jmpToAntiAttachAbort<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">&#41;</span> = AntiAttachAbort;</pre></p>
<p>There are a lot of ways to do the same thing. The example above is what is normally called in the crackers community as a <a href="http://shellcode.org/Shellcode/"><strong>shellcode</strong></a>, what is a pretty name for "byte array that is really the assembly code that does interesting things". <em>Shellcode for short</em> =).</p>
<p>Alternative ways to do this are:</p>
<ol>
<li>To declare a naked function in Visual Studio, to create an empty function just after, do some math to calculate the size of the function to be copied into another place (aware of Edit and Continue option).</li>
<li>To create a structure whose members are masked opcodes. This way, is possible in the constructor to receive the values and use it as a "mobile function".</li>
</ol>
<p>Both have pros and cons. The cons are related with the environment dependency. In the first alternative is necessary to configure the project to disable "Edit and Continue" option, whilst in the second one is necessary to align 1 byte the structure.</p>
<p>Anyway, given the implementation, the main advantage is to isolate the code in only two functions - AntiAttachAbort and InstallAntiAttach - an API local hook (in the same process) that should never be called in production code. Besides, there are C++ ways to do such thing like "live assembly". But this is matter for other future and exciting articles.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/antidebugging-during-the-process-attach/feed</wfw:commentRss>
		</item>
		<item>
		<title>Antidebugging using the DebugPort</title>
		<link>http://www.caloni.com.br/blog/en/archives/antidebugging-using-the-debugport</link>
		<comments>http://www.caloni.com.br/blog/en/archives/antidebugging-using-the-debugport#comments</comments>
		<pubDate>Fri, 01 Aug 2008 09:00:12 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[Reversing]]></category>

		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/?p=18</guid>
		<description><![CDATA[



When a debugger starts a process to be debugged or, the article case, connects to a already created process, the communication between these processes is made through an internal resource inside Windows called LPC (Local Procedure Call). The system creates a "magic" communication port for debugging and the debugging events pass throw it.

Among these events [...]]]></description>
			<content:encoded><![CDATA[



<p>When a debugger starts a process to be debugged or, the article case, connects to a already created process, the communication between these processes is made through an internal resource inside Windows called LPC (Local Procedure Call). The system creates a "magic" communication port for debugging and the debugging events pass throw it.</p>
<p><span id="more-116"></span></p>
<p>Among these events we can tell the most frequent:</p>
<ul>
<li>Activated breakpoints</li>
<li>Thrown exceptions</li>
<li>Threads creation/termination</li>
<li>DLLs load/unload</li>
<li>Process exit</li>
</ul>
<p>In the case of connecting into a existent process, the API <a href="http://www.google.com/url?sa=t&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fmsdn2.microsoft.com%2Fen-us%2Flibrary%2Fms679295.aspx&amp;ei=cqDERvWoA4GKerippJ0M&amp;usg=AFQjCNFzrdQ83SQzTQxBiT9iEauTFyUPcA&amp;sig2=4p-HOh1Wk6uhDYD0ceEMDw" title="DebugActiveProcess no MSDN">DebugActiveProcess</a> is called. Since this call, if successful, the caller program is free now to call the API <a href="http://www.google.com/url?sa=t&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fmsdn2.microsoft.com%2Fen-us%2Flibrary%2Fms679295.aspx&amp;ei=cqDERvWoA4GKerippJ0M&amp;usg=AFQjCNFzrdQ83SQzTQxBiT9iEauTFyUPcA&amp;sig2=4p-HOh1Wk6uhDYD0ceEMDw" title="DebugActiveProcess no MSDN">DebugActiveProcess</a>, looking for debugging events. The main loop for a debugger is, so, pretty simple:</p>
<p><pre><span style="color: #0000ff;">void</span> DebugLoop<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #0000ff;">bool</span> exitLoop = <span style="color: #0000ff;">false</span>;
&nbsp;
	<span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> ! exitLoop <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		DEBUG_EVENT debugEvt;
&nbsp;
		<span style="color: #006600;">// Wait for some debug event.</span>
		WaitForDebugEvent<span style="color: #000000; font-weight: bold;">&#40;</span>&amp;debugEvt, INFINITE<span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
		<span style="color: #006600;">// Let us see what it is about.</span>
		<span style="color: #0000ff;">switch</span><span style="color: #000000; font-weight: bold;">&#40;</span> debugEvt.<span style="color: #000000;">dwDebugEventCode</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">&#123;</span>
			<span style="color: #006600;">// This one...</span>
&nbsp;
			<span style="color: #006600;">// That one...</span>
&nbsp;
			<span style="color: #006600;">// Process is going out. We get out the loop and go away.</span>
			<span style="color: #0000ff;">case</span> EXIT_PROCESS_DEBUG_EVENT:
			exitLoop = <span style="color: #0000ff;">true</span>;
			<span style="color: #0000ff;">break</span>;
		<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
		<span style="color: #006600;">// We need to unfreeze the thread who sent the debug event.</span>
		<span style="color: #006600;">// Otherwise, it stays frozen forever!</span>
		ContinueDebugEvent<span style="color: #000000; font-weight: bold;">&#40;</span>debugEvt.<span style="color: #000000;">dwProcessId</span>, debugEvt.<span style="color: #000000;">dwThreadId</span>, DBG_EXCEPTION_NOT_HANDLED<span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">&#125;</span></pre></p>
<p>The interesting detail about this communication process is that a program can be debugged actively only for ONE debugger. In other words, while there's a process A debugging process B, no one besides A can debug and break B.Using this principle, we can imagine a debugging protection based on this exclusivity, creating a protector process that connects to the protected process and "debugs" it:</p>
<p><pre><span style="color: #006600;">/** @brief Antidebug protection based on DebugPort aquisition.
* @author Wanderley Caloni (wanderley@caloni.com.br)
* @date 2007-08
*/</span>
<span style="color: #006600;">#include &lt;windows.h&gt;</span>
&nbsp;
<span style="color: #006600;">/* Every debugger needs a debugging loop. In this loop it catches
debugging events sent by the operating system.
*/</span>
DWORD DebugLoop<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	DWORD ret = ERROR_SUCCESS;
	<span style="color: #0000ff;">bool</span> exitLoop = <span style="color: #0000ff;">false</span>;
&nbsp;
	<span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> ! exitLoop <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		DEBUG_EVENT debugEvt;
&nbsp;
		WaitForDebugEvent<span style="color: #000000; font-weight: bold;">&#40;</span>&amp;debugEvt, INFINITE<span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
		<span style="color: #0000ff;">switch</span><span style="color: #000000; font-weight: bold;">&#40;</span> debugEvt.<span style="color: #000000;">dwDebugEventCode</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">&#123;</span>
			<span style="color: #006600;">// Process going out. We get out the loop and leave.</span>
			<span style="color: #0000ff;">case</span> EXIT_PROCESS_DEBUG_EVENT:
			exitLoop = <span style="color: #0000ff;">true</span>;
&nbsp;
			<span style="color: #0000ff;">break</span>;
		<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
		<span style="color: #006600;">// Necessary, since the current thread is frozen.</span>
		ContinueDebugEvent<span style="color: #000000; font-weight: bold;">&#40;</span>debugEvt.<span style="color: #000000;">dwProcessId</span>, debugEvt.<span style="color: #000000;">dwThreadId</span>, DBG_EXCEPTION_NOT_HANDLED<span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> ret;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">/* Attachs to the protected process againt debugging. Actually, we protect it
againt debugging being its debugger.
*/</span>
DWORD AntiAttach<span style="color: #000000; font-weight: bold;">&#40;</span>DWORD pid<span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	DWORD ret = ERROR_SUCCESS;
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> pid <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #0000ff;">BOOL</span> dbgActProc;
&nbsp;
		dbgActProc = DebugActiveProcess<span style="color: #000000; font-weight: bold;">&#40;</span>pid<span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
		<span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> dbgActProc <span style="color: #000000; font-weight: bold;">&#41;</span>
			DebugLoop<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
		<span style="color: #0000ff;">else</span>
			ret = GetLastError<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
	<span style="color: #0000ff;">else</span>
		ret = ERROR_INVALID_HANDLE;
&nbsp;
	<span style="color: #0000ff;">return</span> ret;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">/* In the beginning, God said: 'int main!'
*/</span>
<span style="color: #0000ff;">int</span> main<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span>* argv<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	DWORD ret = ERROR_SUCCESS;
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> argc &gt; <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		DWORD pid = <span style="color: #0000ff;">atoi</span><span style="color: #000000; font-weight: bold;">&#40;</span>argv<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
		ret = AntiAttach<span style="color: #000000; font-weight: bold;">&#40;</span>pid<span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000000; font-weight: bold;">&#41;</span> ret;
<span style="color: #000000; font-weight: bold;">&#125;</span></pre></p>
<p>The needed steps to test the code above are:</p>
<ol>
<li>Compile the code</li>
<li>Run notepad (or another victim)</li>
<li>Get its PID (Process ID)</li>
<li>Run the protector process passing the notepad PID as the argument</li>
<li>Try to attach to the notepad using a debugger (e.g. Visual C++)</li>
</ol>
<p>After the attach process, the debug port is occupied, and the communication between the debugger and debuggee is made throug LPC. Bellow we can see a little illustration of how things work:</p>
<p><a href="http://www.caloni.com.br/blog/wp-content/uploads/debug-port2.gif" title="debug-port2.gif"><img src="http://www.caloni.com.br/blog/wp-content/uploads/debug-port2.gif" alt="debug-port2.gif" /></a></p>
<p>Basically the process stay receiving debugging events (through the LPC message queue) until the final event, the process exit. Notice that if someone try to terminate the protector process the debuggee process will be terminated, too.</p>
<h4>Flawless? OK...</h4>
<p>The strength in this protection is that it doesn't affect the code understanding and readability. In fact the code that protects is in another process. The weakness, I would say, it is your visibility. Everyone that will try to attack the solution will se two processes being created, what gives him/her something to think about...</p>
<p>That's why thinking about the implementation is vital. Particularly the main point to be thought is the debugger/debuggee union. As much as better these two pieces were packed, harder to the attacker will be to separate them. An additional idea is to use the same technique in the opposite way, in other words, the debuggee process to attach into the debugger.</p>
<p>This time I'm not going to say that there's a easy solution. Maybe because I haven't though enough about the problem. Ideas?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/antidebugging-using-the-debugport/feed</wfw:commentRss>
		</item>
		<item>
		<title>Antidebugging using exceptions (part two)</title>
		<link>http://www.caloni.com.br/blog/en/archives/antidebugging-using-exceptions-part-two</link>
		<comments>http://www.caloni.com.br/blog/en/archives/antidebugging-using-exceptions-part-two#comments</comments>
		<pubDate>Wed, 30 Jul 2008 09:00:30 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[Reversing]]></category>

		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/?p=17</guid>
		<description><![CDATA[







In the first article we saw how it's possible to spoof the debugger through exceptions and let the attacker lose some considerable time trying to unbind the program from the fake breakpoints. However, we saw also that this is a difficult solution to keep in the source code, besides its main weakness to be easily [...]]]></description>
			<content:encoded><![CDATA[







<p>In the first article we saw how it's possible to spoof the debugger through exceptions and let the attacker lose some considerable time trying to unbind the program from the fake breakpoints. However, we saw also that this is a difficult solution to keep in the source code, besides its main weakness to be easily bypassed if discovered. Now it's time to put things easier to support and at the same time to guarantee tough times even if the attacker discover what is going on.</p>
<p>The upgrade showed here still uses the exception throwing intrinsically, but now it doesn't depends on the code division in minifunctions and minicalls. Instead, we just need to get code traces and put them inside a miraculous macro that will do everything we want. This, of course, after some "hammer work" that will be explained here.</p>
<p><pre><span style="color: #006600;">// Go back to place pre-defined by the restoration point.</span>
<span style="color: #0000ff;">void</span> <span style="color: #0000ff;">LongJmp</span><span style="color: #000000; font-weight: bold;">&#40;</span>restorePoint<span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #006600;">// Here we will generate an exception to make things difficult.</span>
	<span style="color: #006600;">// @todo Make a breakpoint exception and catch it.</span>
&nbsp;
	<span style="color: #006600;">// 3. We return to the if without using the stack, but from the restoration point.</span>
	GoBackToTheStartFunction<span style="color: #000000; font-weight: bold;">&#40;</span>restorePoint<span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">// Here everything begins.</span>
<span style="color: #0000ff;">int</span> Start<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #006600;">// Obs.: follow the agreement flow according to the numbers.</span>
&nbsp;
	<span style="color: #006600;">// 1. First pass: we define a restoration point to the return of LongJmp.</span>
	<span style="color: #006600;">// 4. Second pass: we go back from the LongJmp function, but this time we get into the else.</span>
	<span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> RestorePointDefined<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span> == Defined <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #006600;">// 2. We call the function that will return to the if.</span>
		<span style="color: #0000ff;">LongJmp</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">if</span> <span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #006600;">// 5. Call the real function, our true target.</span>
		CallTheUsefulFunction<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #006600;">// 6. End of execution.</span>
	<span style="color: #0000ff;">return</span> <span style="color: #000000;">0</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span></pre></p>
<p>The solution above is explained in pseudocode to make things clearer.  Notice that exist some kind of invisible return, not stack based. To handle it, however, we can use the good for all C ANSI standard, using the setjmp (step one) and longjmp (step 3). To understand the implementation for theses functions running on the 8086 platform we need to get the basic vision of the function calls in a stack based environment (the C and Pascal way).</p>
<h4>Registers, stack frame and call/ret</h4>
<p>Registers are reserved variables in the processor that can be used by the assembly code. Stack frame is the function calling hierarchy, the "who called who" in a given execution state. Call and ret are assembly instructions to call and return from a function, respectively. Both change the stack frame.</p>
<p>Imagine you have a function, CallFunc, and another function, Func, and one calls the other. In order to analyse just the function call, and just that, let's consider Func doesn't receive any argument and doesn't return any value. The C code, would be like bellow:</p>
<pre>void Func()
{
   return;
}</pre>
<pre>void CallFunc()
{
   Func();
}</pre>
<p>Simple, huh? Being simple, the generated assembly will be simple as well. In CallFunc it should have the function call, and inside Func the return from the call. The rest of the code is related with Debug version stuff.</p>
<pre>Func:
00411F73 prev_instruction ; ESP = 0012FD38 (four bytes stacked up)
00411F74 ret ; *ESP = 00411FA3 (return address)</pre>
<pre>CallFunc:
00411F9C prev_instruction
00411F9E call Func (411424h) ; ESP = 0012FD3C
00411FA3 next_instruction</pre>
<p>From the assembly above we can conclude two things: 1. The stack grows down, since its value decremented four bytes (0012FD3C minus 0012FD38 equal four) and 2. The return value from the calling is the address of the very next instruction after the call instruction, in the case 00411FA3.</p>
<p>Well, in the same way we can follow this simple execution, the attacker will do as well. That's why in the middle of this call we will throw an exception and, in the return, we will not do the return in the conventional way, but using another technique that, instead using the ret instruction, sets manually the esp value (stack state) and jumps to the next instruction in CallFunc.</p>
<pre>Func:
00411F60 throw_exception
00411F61 ...
00411F73 catch_exception
00411F74 mov ESP, 0012FD3C ; ESP = 0012FD3C, just like CallFunc
00411F75 jmp 00411FA3 ; jumps to CallFunc::next_instruction</pre>
<h4>Back to the Middle Earth</h4>
<p>All this assembly stuff doesn't need to be written in assembly level. It was just a way I found to illustrate the differences between the stack return and the jump return. As it was said, to the luck and well being for all, this same technique can be implemented using ANSI C functions:</p>
<p><pre><span style="color: #0000ff;">jmp_buf</span> env; <span style="color: #006600;">// Contains the next instruction (stack state).</span>
&nbsp;
<span style="color: #0000ff;">void</span> Func<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #006600;">// 3. Return using the &quot;nonconventional&quot; way</span>
	<span style="color: #0000ff;">longjmp</span><span style="color: #000000; font-weight: bold;">&#40;</span>env, <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> CallFunc<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #006600;">// 1. If we're setting, returns 0.</span>
	<span style="color: #006600;">// 2. If we're returning, returns a value different from 0.</span>
	<span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">setjmp</span><span style="color: #000000; font-weight: bold;">&#40;</span>env<span style="color: #000000; font-weight: bold;">&#41;</span> == <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
		Func<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
	<span style="color: #0000ff;">int</span> x = <span style="color: #000000;">10</span>; <span style="color: #006600;">// 4. Next instruction.</span>
<span style="color: #000000; font-weight: bold;">&#125;</span></pre></p>
<p>That was the new trick for the trowing of exceptions. The final code is clearer, now:</p>
<p><pre><span style="color: #006600;">/** The only purpose of this function is to generate an exception.
*/</span>
DWORD <span style="color: #0000ff;">LongJmp</span><span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">jmp_buf</span>* env<span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	__try
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		__asm <span style="color: #0000ff;">int</span> <span style="color: #000000;">3</span>
	<span style="color: #000000; font-weight: bold;">&#125;</span>
		__except<span style="color: #000000; font-weight: bold;">&#40;</span> EXCEPTION_EXECUTE_HANDLER <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #0000ff;">longjmp</span><span style="color: #000000; font-weight: bold;">&#40;</span>*env, <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> ERROR_SUCCESS;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #006600;">/** And God said: 'int main!'
*/</span>
<span style="color: #0000ff;">int</span> main<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	DWORD ret = ERROR_SUCCESS;
&nbsp;
	<span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">cin</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		string line;
&nbsp;
		<span style="color: #0000ff;">cout</span> &lt;&lt; <span style="color: #666666;">"Type something<span style="color: #666666;">\n</span>"</span>;
		getline<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">cin</span>, line<span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
		<span style="color: #0000ff;">jmp_buf</span> env;
&nbsp;
		<span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">setjmp</span><span style="color: #000000; font-weight: bold;">&#40;</span>env<span style="color: #000000; font-weight: bold;">&#41;</span> == <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">&#123;</span>
			<span style="color: #0000ff;">LongJmp</span><span style="color: #000000; font-weight: bold;">&#40;</span>&amp;env<span style="color: #000000; font-weight: bold;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">&#125;</span>
		<span style="color: #0000ff;">else</span>
		<span style="color: #000000; font-weight: bold;">&#123;</span>
			<span style="color: #0000ff;">cout</span> &lt;&lt; line &lt;&lt; endl;
		<span style="color: #000000; font-weight: bold;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000000; font-weight: bold;">&#41;</span> ret;
<span style="color: #000000; font-weight: bold;">&#125;</span></pre></p>
<p>At first sight, it seems a waste the if being directly in the code (remember we gonna use the same conditional structure in several parts in the code).  To turn things clearer, resume the protected call and allows the protection to be disabled in debug version code, let's create a macro:</p>
<p><pre><span style="color: #006600;">/** Use this macro instead LongJmp
*/</span>
<span style="color: #006600;">#define ANTIDEBUG(code)</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #0000ff;">jmp_buf</span> env;
&nbsp;
	<span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">setjmp</span><span style="color: #000000; font-weight: bold;">&#40;</span>env<span style="color: #000000; font-weight: bold;">&#41;</span> == <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #0000ff;">LongJmp</span><span style="color: #000000; font-weight: bold;">&#40;</span>&amp;env<span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		code;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">/** And God said: 'int main!'
*/</span>
<span style="color: #0000ff;">int</span> main<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	DWORD ret = ERROR_SUCCESS;
&nbsp;
	<span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">cin</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		string line;
&nbsp;
		<span style="color: #0000ff;">cout</span> &lt;&lt; <span style="color: #666666;">"Type something<span style="color: #666666;">\n</span>"</span>;
		getline<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">cin</span>, line<span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
		ANTIDEBUG<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">cout</span> &lt;&lt; line &lt;&lt; endl <span style="color: #000000; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000000; font-weight: bold;">&#41;</span> ret;
<span style="color: #000000; font-weight: bold;">&#125;</span></pre></p>
<p>Now we allow the antidebugging selection by call, what turns things much easier than to choose the protected points inside the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/antidebugging-using-exceptions-part-two/feed</wfw:commentRss>
		</item>
		<item>
		<title>Antidebugging using exceptions (part one)</title>
		<link>http://www.caloni.com.br/blog/en/archives/antidebugging-using-exceptions-part-one</link>
		<comments>http://www.caloni.com.br/blog/en/archives/antidebugging-using-exceptions-part-one#comments</comments>
		<pubDate>Mon, 28 Jul 2008 09:00:50 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[Reversing]]></category>

		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/?p=16</guid>
		<description><![CDATA[





A debugger puts breakpoints to stop for a moment the debuggee execution. In order to do this it makes use of a well known instruction: int 3. This instruction throws an exception - the breakpoint exception - that is caught by the operating system and bypassed to the handling code for this exception. For debuggee [...]]]></description>
			<content:encoded><![CDATA[





<p>A debugger puts breakpoints to stop for a moment the debuggee execution. In order to do this it makes use of a well known instruction: <strong>int 3</strong>. This instruction throws an exception - the breakpoint exception - that is caught by the operating system and bypassed to the handling code for this exception. For debuggee processes this code is inside the debugger. For free processes this code normally doesn't exist and the application simply crashs.</p>
<p>The main idea in this protection is to take care these exceptions during the application execution. Doing this, we can make use of this fact and, in the handling code, <strong>run the protected code</strong>. The solution here looks like a <strong>script interpreter</strong>. It consists basically of two threads: The first one read an instructions sequence and tells the second thread to <strong>run it step to step</strong>. In order to do this the second thread uses a <strong>small functions</strong> set with well defined code blocks. Here's the example in pseudocode:</p>
<p><span class="fullpost"></span></p>
<p><pre><span style="color: #006600;">// the well-defined functions are functional blocks of code and have</span>
<span style="color: #006600;">// the same signature, allowing the creation of a pointer array to them</span>
<span style="color: #0000ff;">void</span> WellDefinedFunction1<span style="color: #000000; font-weight: bold;">&#40;</span> args <span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #0000ff;">void</span> WellDefinedFunction2<span style="color: #000000; font-weight: bold;">&#40;</span> args <span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #0000ff;">void</span> WellDefinedFunction3<span style="color: #000000; font-weight: bold;">&#40;</span> args <span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #006600;">//...</span>
<span style="color: #0000ff;">void</span> WellDefinedFunctionN<span style="color: #000000; font-weight: bold;">&#40;</span> args <span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
<span style="color: #006600;">// this thread stays forever waiting execution commands from some</span>
<span style="color: #006600;">// well-defined function. the parameter that it receives is the function number</span>
<span style="color: #0000ff;">void</span> ExecutionThread<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #006600;">// 2. ad aeternum</span>
	<span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">true</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #006600;">// 5. it runs some well-defined function by number</span>
		ExecuteWellDefinedFunction<span style="color: #000000; font-weight: bold;">&#40;</span> functionNumber <span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">// the well-defined functions script is an integer array indicating </span>
<span style="color: #006600;">// the number for the next function that is going to be called</span>
<span style="color: #0000ff;">int</span> FunctionsToBeCalled<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">&#93;</span> = <span style="color: #000000; font-weight: bold;">&#123;</span> <span style="color: #000000;">3</span>, <span style="color: #000000;">4</span>, <span style="color: #000000;">1</span>, <span style="color: #000000;">2</span>, <span style="color: #000000;">34</span>, <span style="color: #000000;">66</span>, <span style="color: #000000;">982</span>, n <span style="color: #000000; font-weight: bold;">&#125;</span>;
&nbsp;
<span style="color: #0000ff;">int</span> Start<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #006600;">// 1. we create the thread that is going to run commands</span>
	CreateThread<span style="color: #000000; font-weight: bold;">&#40;</span> ExecutionThread <span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
	<span style="color: #006600;">// 3. for each script item (each function number)</span>
	<span style="color: #0000ff;">for</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">int</span> i = <span style="color: #000000;">0</span>; i &lt; <span style="color: #0000ff;">sizeof</span><span style="color: #000000; font-weight: bold;">&#40;</span>FunctionsToBeCalled<span style="color: #000000; font-weight: bold;">&#41;</span>; ++i <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #006600;">// 4. tells the thread to run the function number N</span>
		TellExecutionThreadToExecuteWellDefinedFunction<span style="color: #000000; font-weight: bold;">&#40;</span> FunctionToBeCalled<span style="color: #000000; font-weight: bold;">&#91;</span>i<span style="color: #000000; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #006600;">// 6. end of execution.</span>
	<span style="color: #0000ff;">return</span> <span style="color: #000000;">0</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span></pre><span class="fullpost"></span></p>
<p>The protection isn't there yet. But it will as intrinsic part of the execution thread. All we need to do is to add a exception handling and to throw lots of int 3. The thrown exceptions are caught by a second function that runs the instruction before to returning:</p>
<p><pre><span style="color: #006600;">// filter exceptions that were thrown by the thread below</span>
DWORD ExceptionFilterButExecuteWellDefinedFunction<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #006600;">// 5. run some well-defined function by number</span>
	ExecuteWellDefinedFunction<span style="color: #000000; font-weight: bold;">&#40;</span> number <span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
	<span style="color: #0000ff;">return</span> EXCEPTION_EXECUTE_HANDLER; <span style="color: #006600;">// goes to except code</span>
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">// this thread stays forever waiting execution commands from a </span>
<span style="color: #006600;">// well-defined function. its &quot;parameter&quot; is the function number</span>
<span style="color: #0000ff;">void</span> ExecutionThread<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
	<span style="color: #006600;">// 2. ad aeternum</span>
	<span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">true</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		__try
		<span style="color: #000000; font-weight: bold;">&#123;</span>
			__asm <span style="color: #0000ff;">int</span> <span style="color: #000000;">3</span> <span style="color: #006600;">// breakpoint exception</span>
&nbsp;
			<span style="color: #006600;">// it stops the debugger if we have an attached debugger in</span>
			<span style="color: #006600;">// the process, or throws an exception if there is no one</span>
		<span style="color: #000000; font-weight: bold;">&#125;</span>
		__except<span style="color: #000000; font-weight: bold;">&#40;</span> ExceptionFilterButExecuteWellDefinedFunction<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">&#123;</span>
			<span style="color: #006600;">// it does nothing. here is NOT where is the code (obvious, huh?)</span>
		<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
		Sleep<span style="color: #000000; font-weight: bold;">&#40;</span> someTime <span style="color: #000000; font-weight: bold;">&#41;</span>; <span style="color: #006600;">// give some time</span>
	<span style="color: #000000; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">&#125;</span></pre></p>
<p>The execution thread algorithm is the same. Just the point where each instruction is executed depends to the exception throw system. Note that this exception has to be thrown in order to the next instruction run. This is fundamental, since this way nobody can just rip of the int 3 code to avoid the exception. If one does that, so no instruction will be executed at all.</p>
<p>In practice, if one tries to debug such a program one will have to deal with tons of exceptions until find out what's happening. Of course, as in every software protection, is's not definitive; it has as a purpose to <strong>make hard</strong> the reverse engineering understanding. That's not going to stop those who are <a href="http://www.codebreakers-journal.com/">really good</a> doing that stuff.</p>
<p><strong>Nothing is for free</strong></p>
<p>The price paid for this protection stays on the source code visibility and understanding, compromised by the use of this technique. The programming is state machine based, and the functions are limited to some kind of behavior standard. So much smaller the code blocks inside the minifunctions, so much hard the code understanding will be.</p>
<p>The example bellow receives input through a command prompt and maps the first word typed to the function that must be called. The rest of the typed line is passed as arguments to the functions. The interpreter thread reads the user input and writes into a global string variable, at the same time the executor thread waits the string to be completed to starts the action. It was used the variable pool to let the code simpler, but the ideal would be some kind of synchronise, just like <a href="http://msdn.microsoft.com/library/en-us/dllproc/base/createevent.asp">events</a>, by example. You can download the source code <a href="http://www.caloni.com.br/blog/wp-content/uploads/antidebug.cpp" title="antidebug.cpp">here</a>.</p>
<p><pre><span style="color: #006600;">/** @brief Sample demonstrating how to implemente antidebug in a code exception based.
@date jul-2007
@author Wanderley Caloni
*/</span>
<span style="color: #006600;">#include &lt;windows.h&gt;</span>
&nbsp;
<span style="color: #006600;">#include &lt;iostream&gt;</span>
<span style="color: #006600;">#include &lt;map&gt;</span>
<span style="color: #006600;">#include &lt;sstream&gt;</span>
&nbsp;
<span style="color: #006600;">#include &lt;string&gt;</span>
<span style="color: #006600;">#include &lt;stdlib.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std;
&nbsp;
<span style="color: #006600;">// show available commands</span>
<span style="color: #0000ff;">bool</span> Help<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">const</span> string&amp;<span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
&nbsp;
   <span style="color: #0000ff;">cout</span> &lt;&lt; <span style="color: #666666;">"AntiDebug Test Program<span style="color: #666666;">\n</span>"</span>
      &lt;&lt; <span style="color: #666666;">" Echo string to be printed<span style="color: #666666;">\n</span>"</span>
      &lt;&lt; <span style="color: #666666;">" System command [params]<span style="color: #666666;">\n</span>"</span>
      &lt;&lt; <span style="color: #666666;">" Quit<span style="color: #666666;">\n</span><span style="color: #666666;">\n</span>"</span>;
   <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">// run system/shell command</span>
<span style="color: #0000ff;">bool</span> <span style="color: #0000ff;">System</span><span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">const</span> string&amp; cmd<span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
   <span style="color: #0000ff;">system</span><span style="color: #000000; font-weight: bold;">&#40;</span>cmd.<span style="color: #000000;">c_str</span><span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
   <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">// print string to output</span>
<span style="color: #0000ff;">bool</span> Echo<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">const</span> string&amp; str<span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
   <span style="color: #0000ff;">cout</span> &lt;&lt; str &lt;&lt; endl;
   <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">// quit program</span>
<span style="color: #0000ff;">bool</span> Quit<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">const</span> string&amp;<span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
   <span style="color: #0000ff;">exit</span><span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
   <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">// minifunctions array</span>
<span style="color: #0000ff;">bool</span> <span style="color: #000000; font-weight: bold;">&#40;</span>* <span style="color: #000000; font-weight: bold;">&#40;</span>g_miniFuncs<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">const</span> string&amp;<span style="color: #000000; font-weight: bold;">&#41;</span> = <span style="color: #000000; font-weight: bold;">&#123;</span> Help, <span style="color: #0000ff;">System</span>, Echo, Quit <span style="color: #000000; font-weight: bold;">&#125;</span>;
&nbsp;
<span style="color: #006600;">// &quot;minifunction -&gt; index&quot; mapping</span>
map&lt;string, int&gt; g_miniFuncIdx;
&nbsp;
<span style="color: #006600;">// start minifunctions mapping</span>
<span style="color: #0000ff;">void</span> InitializeMiniFuncIdx<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
   g_miniFuncIdx<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #666666;">"Help"</span><span style="color: #000000; font-weight: bold;">&#93;</span> = <span style="color: #000000;">0</span>;
   g_miniFuncIdx<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #666666;">"System"</span><span style="color: #000000; font-weight: bold;">&#93;</span> = <span style="color: #000000;">1</span>;
   g_miniFuncIdx<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #666666;">"Echo"</span><span style="color: #000000; font-weight: bold;">&#93;</span> = <span style="color: #000000;">2</span>;
   g_miniFuncIdx<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #666666;">"Quit"</span><span style="color: #000000; font-weight: bold;">&#93;</span> = <span style="color: #000000;">3</span>;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">// last line read from input</span>
string g_currentLine;
&nbsp;
<span style="color: #006600;">// how much time are we going to wait for the next line?</span>
<span style="color: #0000ff;">const</span> DWORD g_waitTime = <span style="color: #000000;">1000</span>;
&nbsp;
<span style="color: #006600;">// run minifunctions</span>
DWORD FilterException<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
   DWORD ret = EXCEPTION_CONTINUE_EXECUTION;
&nbsp;
   <span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> ! g_currentLine.<span style="color: #000000;">empty</span><span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
   <span style="color: #000000; font-weight: bold;">&#123;</span>
      istringstream line<span style="color: #000000; font-weight: bold;">&#40;</span>g_currentLine<span style="color: #000000; font-weight: bold;">&#41;</span>;
      g_currentLine.<span style="color: #000000;">clear</span><span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
      string function;
      string params;
&nbsp;
      line &gt;&gt; function;
&nbsp;
      getline<span style="color: #000000; font-weight: bold;">&#40;</span>line, params<span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
      <span style="color: #006600;">// 5. run some well-defined function by number</span>
      <span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> ! g_miniFuncs<span style="color: #000000; font-weight: bold;">&#91;</span>g_miniFuncIdx<span style="color: #000000; font-weight: bold;">&#91;</span>function<span style="color: #000000; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">&#40;</span>params<span style="color: #000000; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
         ret = EXCEPTION_CONTINUE_SEARCH;
   <span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
   <span style="color: #0000ff;">return</span> ret;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
DWORD WINAPI AntiDebugThread<span style="color: #000000; font-weight: bold;">&#40;</span>PVOID<span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
   InitializeMiniFuncIdx<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>; <span style="color: #006600;">// start minifunction mapping</span>
&nbsp;
   <span style="color: #006600;">// 2. ad aeternum (or almost)</span>
   <span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">true</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">&#123;</span>
      <span style="color: #006600;">//FilterException();</span>
&nbsp;
      __try <span style="color: #006600;">// the extern try waits for an exit command</span>
      <span style="color: #000000; font-weight: bold;">&#123;</span>
         __try <span style="color: #006600;">// the intern try stays generating exceptions continuously</span>
         <span style="color: #000000; font-weight: bold;">&#123;</span>
            __asm <span style="color: #0000ff;">int</span> <span style="color: #000000;">3</span>
         <span style="color: #000000; font-weight: bold;">&#125;</span>
         <span style="color: #006600;">// FilterException is the function who runs minifunctions</span>
         __except<span style="color: #000000; font-weight: bold;">&#40;</span> FilterException<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
         <span style="color: #000000; font-weight: bold;">&#123;</span>
				<span style="color: #006600;">// we can put some fake code here</span>
         <span style="color: #000000; font-weight: bold;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">&#125;</span>
      __except<span style="color: #000000; font-weight: bold;">&#40;</span> EXCEPTION_EXECUTE_HANDLER <span style="color: #000000; font-weight: bold;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">&#123;</span>
         <span style="color: #0000ff;">break</span>; <span style="color: #006600;">// get out from ad aeternum (to the limbo?)</span>
      <span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
      Sleep<span style="color: #000000; font-weight: bold;">&#40;</span>g_waitTime<span style="color: #000000; font-weight: bold;">&#41;</span>;
   <span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
   <span style="color: #0000ff;">return</span> ERROR_SUCCESS;
<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">/** and God said: 'int main!'
*/</span>
<span style="color: #0000ff;">int</span> main<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&#123;</span>
&nbsp;
   DWORD ret = ERROR_SUCCESS;
   DWORD tid = <span style="color: #000000;">0</span>;
   HANDLE antiDebugThr;
&nbsp;
   <span style="color: #006600;">// 1. we create the thread that is going to run the commands</span>
   antiDebugThr = CreateThread<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">NULL</span>, <span style="color: #000000;">0</span>, AntiDebugThread, <span style="color: #0000ff;">NULL</span>, <span style="color: #000000;">0</span>, &amp;tid<span style="color: #000000; font-weight: bold;">&#41;</span>;;
&nbsp;
   <span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> antiDebugThr <span style="color: #000000; font-weight: bold;">&#41;</span>
   <span style="color: #000000; font-weight: bold;">&#123;</span>
      <span style="color: #006600;">// 3. for each item in the script (function numbers)</span>
      <span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #0000ff;">cin</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">&#123;</span>
         <span style="color: #0000ff;">cout</span> &lt;&lt; <span style="color: #666666;">"Type something<span style="color: #666666;">\n</span>"</span>;
&nbsp;
         <span style="color: #006600;">// 4. tells the thread to run the function number N</span>
         getline<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">cin</span>, g_currentLine<span style="color: #000000; font-weight: bold;">&#41;</span>;
&nbsp;
         <span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> WaitForSingleObject<span style="color: #000000; font-weight: bold;">&#40;</span>antiDebugThr, g_waitTime * <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&#41;</span> != WAIT_TIMEOUT <span style="color: #000000; font-weight: bold;">&#41;</span>
            <span style="color: #0000ff;">break</span>;
      <span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
      GetExitCodeThread<span style="color: #000000; font-weight: bold;">&#40;</span>antiDebugThr, &amp;ret<span style="color: #000000; font-weight: bold;">&#41;</span>;
      CloseHandle<span style="color: #000000; font-weight: bold;">&#40;</span>antiDebugThr<span style="color: #000000; font-weight: bold;">&#41;</span>, antiDebugThr = <span style="color: #0000ff;">NULL</span>;
   <span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
   <span style="color: #006600;">// 6. end of execution.</span>
   <span style="color: #0000ff;">return</span> <span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000000; font-weight: bold;">&#41;</span> ret;
<span style="color: #000000; font-weight: bold;">&#125;</span></pre></p>
<p>The <strong>strength</strong> in this protection is to confound the attacker easily in the first steps (days, months...). Its <strong>weakness</strong> is the simplicity for the solution, since the attacker eventually realize what is going on. It is so easy that I will let it as an exercise for my readers.</p>
<p>In the next part we will se an alternative to make the code clearer and easy to use in the every day by a security software developer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/antidebugging-using-exceptions-part-one/feed</wfw:commentRss>
		</item>
		<item>
		<title>How to run anything as a service</title>
		<link>http://www.caloni.com.br/blog/en/archives/how-to-run-anything-as-a-service</link>
		<comments>http://www.caloni.com.br/blog/en/archives/how-to-run-anything-as-a-service#comments</comments>
		<pubDate>Tue, 27 May 2008 09:00:05 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/?p=15</guid>
		<description><![CDATA[The biggest advantage running an application as a service, interactive or not, is to allow its start before a logon be performed. An example that happens to me is the need of debugging a GINA. In order to do this, I need the Visual Studio remote debugger be started before logon. The easiest and fastest [...]]]></description>
			<content:encoded><![CDATA[<p>The biggest advantage running an application as a service, interactive or not, is to allow its start before a logon be performed. An example that happens to me is the need of debugging a <a href="http://www.caloni.com.br/blog/archives/gina-x-credential-provider" title="O que é uma GINA?">GINA</a>. In order to do this, I need the Visual Studio remote debugger be started before logon. The easiest and fastest solution is to run <strong>Msvcmon</strong>, the server part of debugging, as a service.</p>
<p>Today I&#8217;ve figured out a pretty interesting shortcut to achieve it.</p>
<h4>Service Controller (or SC)</h4>
<p>An <a href="http://www.alex-ionescu.com/?p=59" title="Alex Ionescu">Alex Ionescu article</a> talks about this command line application used to create, initiate and remove services. Even not being the article focus, I found the information pretty useful, since I didn&#8217;t know such app. Soon some ideas starting to born in my mind:</p>
<blockquote><p>&#8220;What if I used this guy to run notepad?&#8221;</p></blockquote>
<p>Well, the Notepad is the default test victim. Soon, the following line would prove possible to run it in the system account:</p>
<pre>sc create Notepad binpath= "%systemroot%\NOTEPAD.EXE" type= interact type= own</pre>
<p>However, as every service, it is supposed to communicate with the Windows Service Manager. Since Notepad even &#8220;knows&#8221; it is now a superpowerful service, the service initialization time is expired and <a href="http://msdn2.microsoft.com/en-us/library/ms685150.aspx" title="Service Control Manager">SCM</a> kills the process.</p>
<pre>&gt;net start notepad
The service is not responding to the control function.

More help is available by typing NET HELPMSG 2186.</pre>
<p>As would say my friend <a href="http://codebehind.wordpress.com/" title="Code Behind">Thiago</a>, &#8220;not good&#8221;.</p>
<p>&#8220;Yet however&#8221;, SCM doesn&#8217;t kill the child processes from the service-process. Bug? Feature? Workaround? Whatever it is, it can be used to initiate our beloved msvcmon:</p>
<pre>set binpath=%systemroot%\system32\cmd.exe /c c:\Tools\msvcmon.exe -tcpip -anyuser -timeout -1
sc create Msvcmon binpath= "%binpath%" type= interact type= own</pre>
<p>Now, when we start Msvcmon service, the process cmd.exe will be create, that on the other hand will run the msvcmon.exe target process. Cmd in this case will only wait for its imminent death.</p>
<p><a href="http://www.caloni.com.br/blog/wp-content/uploads/msvcmon-service.png" title="MsvcMon Service"><img src="http://www.caloni.com.br/blog/wp-content/uploads/msvcmon-service.png" alt="MsvcMon Service" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/how-to-run-anything-as-a-service/feed</wfw:commentRss>
		</item>
		<item>
		<title>Funky do-while</title>
		<link>http://www.caloni.com.br/blog/en/archives/funky-do-while</link>
		<comments>http://www.caloni.com.br/blog/en/archives/funky-do-while#comments</comments>
		<pubDate>Wed, 13 Feb 2008 16:34:29 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/?p=14</guid>
		<description><![CDATA[

It's a known habit to use do-while constructions when there's a need to define a macro that has more than one command instead of using the { simple multicommand brackets }. What was never clear is why this is so.
Let's imagine a trace macro that's enabled in debug mode, whilst kept in silence in release [...]]]></description>
			<content:encoded><![CDATA[

<p>It's a known habit to use do-while constructions when there's a need to define a macro that has more than one command instead of using the { simple multicommand brackets }. What was never clear is why this is so.</p>
<p>Let's imagine a trace macro that's enabled in debug mode, whilst kept in silence in release builds:</p>
<p><pre><span style="color: #006600;">#ifdef NDEBUG</span>
&nbsp;
<span style="color: #006600;">#define MYTRACE( message ) /* nothing */</span>
&nbsp;
<span style="color: #006600;">#else</span>
&nbsp;
<span style="color: #006600;">#define MYTRACE( message )        \</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>                              \
		<span style="color: #0000ff;">char</span> buffer<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000;">500</span><span style="color: #000000; font-weight: bold;">&#93;</span>;           \
		<span style="color: #0000ff;">sprintf</span><span style="color: #000000; font-weight: bold;">&#40;</span>buffer,             \
			<span style="color: #666666;">"MYTRACE: %s(%d) %s<span style="color: #666666;">\n</span>"</span>,  \
			__FILE__,                \
			__LINE__,                \
			message<span style="color: #000000; font-weight: bold;">&#41;</span>;                \
		OutputDebugString<span style="color: #000000; font-weight: bold;">&#40;</span>buffer<span style="color: #000000; font-weight: bold;">&#41;</span>;  \
	<span style="color: #000000; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006600;">#endif /* NDEBUG */ </span>
&nbsp;</pre></p>
<p>Nothing much, but it seems to work. But, as we going to see in the following lines, it is really a buggy piece of code, since a call inside an if-else construction simply doesn't work.</p>
<p><pre><span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> exploded<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	MYTRACE<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #666666;">"Oh, my God"</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #0000ff;">else</span>
	MYTRACE<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #666666;">"That's right"</span><span style="color: #000000; font-weight: bold;">&#41;</span>;</pre></p>
<pre>error C2181: illegal else without matching if</pre>
<p>Why's that? In order to answer this question, we need to look closer into the result code from the preprocessor, just replacing the macro for its piece of code:</p>
<p><pre><span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> exploded<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #0000ff;">char</span> buffer<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000;">500</span><span style="color: #000000; font-weight: bold;">&#93;</span>;
		sprintf<span style="color: #000000; font-weight: bold;">&#40;</span>buffer,
			<span style="color: #666666;">"MYTRACE: %s(%d) %s<span style="color: #666666;">\n</span>"</span>,
			__FILE__,
			__LINE__,
			<span style="color: #666666;">"Oh, my God"</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
		OutputDebugString<span style="color: #000000; font-weight: bold;">&#40;</span>buffer<span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>;
<span style="color: #0000ff;">else</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #0000ff;">char</span> buffer<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000;">500</span><span style="color: #000000; font-weight: bold;">&#93;</span>;
		sprintf<span style="color: #000000; font-weight: bold;">&#40;</span>buffer,
			<span style="color: #666666;">"MYTRACE: %s(%d) %s<span style="color: #666666;">\n</span>"</span>,
			__FILE__,
			__LINE__,
			<span style="color: #666666;">"That's right"</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
		OutputDebugString<span style="color: #000000; font-weight: bold;">&#40;</span>buffer<span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>;</pre></p>
<p>So, that's why. When we call a macro, generally we use the funcion-call syntax, putting a semicolon in the end. This is the right way to call a function, but in the macro case, it's a disaster, because it creates two commands instead of one (an empty semicolon, despite doing nothing, it's a valid command). So that's what the compiler does:</p>
<pre>if( instruction )
{
	/* a lot of comands */

} /* here I would expect an else or new instruction */

<font color="#ff0000">; /* a new command! okay, no else this time */</font>

else /* wait! what this else is doing here without an if?!?! */
{
	/* more commands */
}</pre>
<p>Think about the empty command as if it was a real command, what is the easier way to realize the compiler error:</p>
<pre>if( error() )
{
	printf("error");
}

<font color="#ff0000">printf("here we go");</font>

else /* llegal else without matching if! */
{
	printf("okay");
}</pre>
<p>For this reason, the tradicional way to  skip this common error is to use a valid construction who asks for a semicolon in the end. Fortunately, language C has such construction, and it is... right, the <strong>do-while</strong>!</p>
<pre>do
{
	/* multiple commands here */
}
while( expression ) <font color="#ff0000">;</font> /* I expect a semicolon here, in order
                         to end the do-while instruction */</pre>
<p>So we can rewrite our trace macro the right way, even being a funcky one:</p>
<p><pre><span style="color: #006600;">#ifdef NDEBUG</span>
&nbsp;
<span style="color: #006600;">#define MYTRACE( message ) /* nothing */</span>
&nbsp;
<span style="color: #006600;">#else</span>
&nbsp;
<span style="color: #006600;">#define MYTRACE( message )        \</span>
	<span style="color: #0000ff;">do</span>                             \
	<span style="color: #000000; font-weight: bold;">&#123;</span>                              \
		<span style="color: #0000ff;">char</span> buffer<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000;">500</span><span style="color: #000000; font-weight: bold;">&#93;</span>;           \
		sprintf<span style="color: #000000; font-weight: bold;">&#40;</span>buffer,             \
			<span style="color: #666666;">"MYTRACE: %s(%d) %s<span style="color: #666666;">\n</span>"</span>,  \
			__FILE__,                \
			__LINE__,                \
			message<span style="color: #000000; font-weight: bold;">&#41;</span>;                \
		<span style="color: #0000ff;">printf</span><span style="color: #000000; font-weight: bold;">&#40;</span>buffer<span style="color: #000000; font-weight: bold;">&#41;</span>;             \
	<span style="color: #000000; font-weight: bold;">&#125;</span>                              \
	<span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #006600;">#endif /* NDEBUG */ </span>
&nbsp;</pre></p>
<p>Using a do-while (with a false expression inside the test to execute the block just once) the if-else construction is allowed and working properly:</p>
<p><pre><span style="color: #0000ff;">if</span><span style="color: #000000; font-weight: bold;">&#40;</span> exploded<span style="color: #000000; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&#41;</span>
	<span style="color: #0000ff;">do</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #0000ff;">char</span> buffer<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000;">500</span><span style="color: #000000; font-weight: bold;">&#93;</span>;
		sprintf<span style="color: #000000; font-weight: bold;">&#40;</span>buffer,
			<span style="color: #666666;">"MYTRACE: %s(%d) %s<span style="color: #666666;">\n</span>"</span>,
			__FILE__,
			__LINE__,
			<span style="color: #666666;">"Oh, my God"</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
		OutputDebugString<span style="color: #000000; font-weight: bold;">&#40;</span>buffer<span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
	<span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&#41;</span>;
<span style="color: #0000ff;">else</span>
	<span style="color: #0000ff;">do</span>
	<span style="color: #000000; font-weight: bold;">&#123;</span>
		<span style="color: #0000ff;">char</span> buffer<span style="color: #000000; font-weight: bold;">&#91;</span><span style="color: #000000;">500</span><span style="color: #000000; font-weight: bold;">&#93;</span>;
		sprintf<span style="color: #000000; font-weight: bold;">&#40;</span>buffer,
			<span style="color: #666666;">"MYTRACE: %s(%d) %s<span style="color: #666666;">\n</span>"</span>,
			__FILE__,
			__LINE__,
			<span style="color: #666666;">"That's right"</span><span style="color: #000000; font-weight: bold;">&#41;</span>;
		OutputDebugString<span style="color: #000000; font-weight: bold;">&#40;</span>buffer<span style="color: #000000; font-weight: bold;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">&#125;</span>
	<span style="color: #0000ff;">while</span><span style="color: #000000; font-weight: bold;">&#40;</span> <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">&#41;</span>;</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/funky-do-while/feed</wfw:commentRss>
		</item>
		<item>
		<title>Silly regex trick: finding the project who failed inside a big VS solution</title>
		<link>http://www.caloni.com.br/blog/en/archives/silly-regex-trick-finding-the-project-who-failed-inside-a-vs-big-solution</link>
		<comments>http://www.caloni.com.br/blog/en/archives/silly-regex-trick-finding-the-project-who-failed-inside-a-vs-big-solution#comments</comments>
		<pubDate>Thu, 07 Feb 2008 10:30:06 +0000</pubDate>
		<dc:creator>Wanderley Caloni</dc:creator>
		
		<category><![CDATA[Nothing]]></category>

		<guid isPermaLink="false">http://www.caloni.com.br/blog/en/?p=13</guid>
		<description><![CDATA[I know what you going to think about this one: "silly trick". That's why I just put it in the title. Anyway, that is something I use everyday, so I thought it might be useful to who cares about productivity.
Let's say you have to manage a big solution in Visual Studio made of more than [...]]]></description>
			<content:encoded><![CDATA[<p>I know what you going to think about this one: "silly trick". That's why I just put it in the title. Anyway, that is something I use everyday, so I thought it might be useful to who cares about productivity.</p>
<p>Let's say you have to manage a big solution in Visual Studio made of more than 30 projects, and needs to rebuild all them. Suddenly, something goes wrong. The question is: how to discover, in a heartbeat, what project has failed?</p>
<p><a href="http://www.caloni.com.br/blog/wp-content/uploads/find-error-regex2.png" title="Find Error in VS projects using regex"><img src="http://www.caloni.com.br/blog/wp-content/uploads/find-error-regex2.png" alt="Find Error in VS projects using regex" /></a></p>
<p>Note that you need to enable "Regular Expressions" option in the Find Dialog (not shown here).</p>
<p>What I'm saying inside this regex is "find the first number different from zero followed by a space and the letters err". This lead us to the first project who has at least one error:</p>
<pre>------ Build started: Project: FailedProj, Configuration: Release Win32 ------
Compiling...
stdafx.cpp
Compiling...
FailedProj.cpp
.\FailedProj.cpp(2477) : error C2039: 'Blablabla' : is not a member of 'IBlabla'
Build log was saved at "file://c:\Projects\...\Release\BuildLog.htm"
FailedProj - <font color="#ff0000">2 err</font>or(s), 0 warning(s)</pre>
<p>If you think "what about when a project generates more than 9 errors? the regex wouldn't be able to catch this case", well,  you're right. Anyway, that's the quicker form to search for the unsuccessful project inside a big solution. A more complex yet complete regex would be:</p>
<pre>[1-9][0-9]* err</pre>
<p>For me, the first version is enough. It is faster to type, simpler to catch and solves my problem. I hope it can solve yours =)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caloni.com.br/blog/en/archives/silly-regex-trick-finding-the-project-who-failed-inside-a-vs-big-solution/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
