<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michal Strehovský: home &#187; Security</title>
	<atom:link href="http://migeel.sk/blog/category/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://migeel.sk</link>
	<description>Windows development and other random stuff</description>
	<lastBuildDate>Mon, 15 Mar 2010 14:31:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Don&#8217;t fool your users</title>
		<link>http://migeel.sk/blog/2007/08/11/dont-fool-your-users/</link>
		<comments>http://migeel.sk/blog/2007/08/11/dont-fool-your-users/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 11:27:31 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://migeel.sk/blog/2007/08/11/dont-fool-your-users/</guid>
		<description><![CDATA[Today, I finally got into reading the series on anticracking I mentioned in my previous post. In one of the articles, I found this suggestion on what to do if you detect that your program is being crackedâ€ :

Instead of crashing the program, you should wait several days and then change the way the program reacts. [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I finally got into reading the series on anticracking I mentioned in my <a href="http://migeel.sk/blog/2007/08/05/careful-with-those-optimisations/">previous post</a>. In one of the articles, I found this suggestion on what to do if you detect that your program is being crackedâ€ :</p>
<blockquote><p>
Instead of crashing the program, you should wait several days and then change the way the program reacts. For example, in a graphical program when the user of illegal version picks green colour, the program will draw with blue colour.
</p></blockquote>
<p>The intention of this is clear: discrediting the cracker. If he doesn&#8217;t notice this additional protection layer and ships his (unfinished) crack, his credit among the cracker community will be degraded.</p>
<p>In reality, though, the one with degraded credit will be you. <em>&#8220;Do not use the X program. It&#8217;s full of bugs and works in an unpredictable way.&#8221;</em></p>
<p>People do not usually associate bugs in programs with unfinished cracks. If a program works just fine after it was cracked, people tend to forget that the program was ever cracked. All errors that show up after a certain period of time will be automatically associated with you.</p>
<p>If you want to include delayed checks in your protection, make sure they behave in a direct way. You detected that your program is partially cracked? Show a message boxâ€¡. Display a message on the application title bar. Inform your users. Do not let them make false assumptions about your program.</p>
<p>â€  There are many ways how to detect this &#8211; a checksum doesn&#8217;t match, the registration verification procedure returned <code>true</code> even though the code supplyed was intentionally not valid, etc.</p>
<p>â€¡ Of course, do not forget to hide the message in the code appropriately. You don&#8217;t want to bring the attention to this code, do you?</p>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2007/08/11/dont-fool-your-users/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Careful with those optimisations</title>
		<link>http://migeel.sk/blog/2007/08/05/careful-with-those-optimisations/</link>
		<comments>http://migeel.sk/blog/2007/08/05/careful-with-those-optimisations/#comments</comments>
		<pubDate>Sun, 05 Aug 2007 15:55:35 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://beta.migeel.sk/blog/2007/08/05/careful-with-those-optimisations/</guid>
		<description><![CDATA[I was looking over some older computer magazines today and found a promising series of articles on anticracking in a Slovak IT magazine called InfoWare. I didn&#8217;t really have much time to read the whole series, so I just peeked at the enclosed source codes.
One code snippet caught my attention in particular. The code went [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking over some older computer magazines today and found a promising series of articles on anticracking in a Slovak IT magazine called <a href="http://www.infoware.sk">InfoWare</a>. I didn&#8217;t really have much time to read the whole series, so I just peeked at the enclosed source codes.</p>
<p>One code snippet caught my attention in particular. The code went like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">float</span> sumOfNumbers <span style="color: #339933;">=</span> <span style="color: #0000dd;">90</span> <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">+</span> <span style="color:#800080;">0.03</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">50</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">300</span> <span style="color: #339933;">+</span> <span style="color:#800080;">0.3</span> <span style="color: #339933;">-</span> <span style="color: #0000dd;">50</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>sumOfNumbers <span style="color: #339933;">==</span> <span style="color:#800080;">389.33</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// everything's all right</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// now confuse the cracker</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The text around this snippet was talking something about making constants in programs more confusing.</p>
<p>Well &#8211; in source code, this is really terrifying and confusing. It works well, if you want to protect your <em>source code</em> against modifications by the mystified programmers (or by you). It will hardly confuse a cracker, who sees only the binary version. The reason? Compiler optimizations.</p>
<p>I made a little experiment with this code:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">float</span> f <span style="color: #339933;">=</span> <span style="color:#800080;">3.1</span> <span style="color: #339933;">+</span> <span style="color:#800080;">5.8</span> <span style="color: #339933;">+</span> <span style="color:#800080;">1.1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>f <span style="color: #339933;">==</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Aloha&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I compiled it in Visual C++ with full compiler optimizations (the &#8220;release mode&#8221;) and glanced at the produced code. The above code was compiled into this:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f; font-weight: bold;">Test</span><span style="color: #339933;">.</span>004020E4                       <span style="color: #666666; font-style: italic;">; /format = &quot;Aloha&quot;</span>
<span style="color: #00007f; font-weight: bold;">call</span> <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #00007f;">ds</span><span style="color: #339933;">:</span><span style="color: #009900; font-weight: bold;">&#91;</span>&lt;&amp;MSVCR80<span style="color: #339933;">.</span>printf&gt;<span style="color: #009900; font-weight: bold;">&#93;</span>    <span style="color: #666666; font-style: italic;">; \printf</span></pre></div></div>

<p>Only the printf call was left from the original code. Not very confusing anymore, is it?</p>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2007/08/05/careful-with-those-optimisations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Advanced self-modifying code</title>
		<link>http://migeel.sk/blog/2007/08/02/advanced-self-modifying-code/</link>
		<comments>http://migeel.sk/blog/2007/08/02/advanced-self-modifying-code/#comments</comments>
		<pubDate>Thu, 02 Aug 2007 07:46:18 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://beta.migeel.sk/blog/2007/08/07/advanced-self-modifying-code/</guid>
		<description><![CDATA[Self-modifying code (SMC) belongs to the strongest weapons of software protection programmers. I already presented the basic principles behind SMC in my series of cracking prevention articles. In this article we are going to dive deeper and take a closer look at some advanced techniques like polymorphism and metamorphism.
Polymorphism first appeared in a computer virus [...]]]></description>
			<content:encoded><![CDATA[<p>Self-modifying code (SMC) belongs to the strongest weapons of software protection programmers. I already presented the basic principles behind SMC in my <a href="http://migeel.sk/articles">series of cracking prevention articles</a>. In this article we are going to dive deeper and take a closer look at some advanced techniques like polymorphism and metamorphism.</p>
<p>Polymorphism first appeared in a computer virus called 1260 as a method designed to hide the virus from anti-virus software. The anti-virus software at that time used patterns to identify malicious code inside of executables. Because polymorphism made the representation of the virus code different in each infected file, the anti-virus creators could not find a unique pattern identifying it.</p>
<p>The concept was simple: at the beginning of the virus, there was a simple decryption routine that decrypted the rest of the virus in memory. Then the actual virus code was run. When the virus found its new victim, it changed the decryption key a little, encrypted itself with this new key and placed its encrypted code together with decryption code into the executable file.</p>
<h3>Polymorphism: the easy way</h3>
<p>The easiest approach to polymorphism looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">al</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">12h</span> <span style="color: #666666; font-style: italic;">; set the key</span>
<span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">edi</span><span style="color: #339933;">,</span> codeEnd <span style="color: #666666; font-style: italic;">; starting address</span>
<span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span> codeEnd <span style="color: #339933;">-</span> codeStart <span style="color: #666666; font-style: italic;">; length of encrypted block</span>
&nbsp;
<span style="color: #666666; font-style: italic;">; now decrypt the code, starting from the last byte</span>
decryptLoop<span style="color: #339933;">:</span>
<span style="color: #00007f; font-weight: bold;">xor</span> <span style="color: #000000; font-weight: bold;">byte</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edi</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">al</span> <span style="color: #666666; font-style: italic;">; decrypt byte</span>
<span style="color: #00007f; font-weight: bold;">dec</span> <span style="color: #00007f;">edi</span> <span style="color: #666666; font-style: italic;">; move to the next byte</span>
<span style="color: #00007f; font-weight: bold;">loop</span> decryptLoop
&nbsp;
codeStart<span style="color: #339933;">:</span>
<span style="color: #666666; font-style: italic;">; put encrypted code here</span>
codeEnd<span style="color: #339933;">:</span></pre></div></div>

<p>This kind of polymorphism is easy to implement and seems as a pretty good weapon. But let&#8217;s have a look at this code from a crackers point of view: the code between codeStart and codeEnd is encrypted, so he can&#8217;t see what will happen after the loop instruction. The easiest method to go through this problem is to place a hardware or memory breakpoint after the loop instruction and wait for the code to decrypt (BPX breakpoint won&#8217;t do here because it would alter the byte after the loop instruction &#8211; this would result in garbage after the decryption).</p>
<p>The point of polymorphism is to force the cracker to run our program &#8211; to make static disassembly useless. The problem with this polymorphic engine is that it&#8217;s too transparent &#8211; the cracker doesn&#8217;t have to run this code. He just has to look at the decryption routine and write a macro for IDA (or similar disassembler) to decrypt the protected code for him. Then he can NOP the decryption code out of the executable.</p>
<h3>Advanced polymorphism</h3>
<p>More advanced polymorphic engines not only change the key protecting the sensitive code &#8211; they also change the algorithm doing the decryption. A good polymorhic engine usually has these features:</p>
<ul>
<li>generates different instructions which do the same thing</li>
<li>swaps groups of instructions</li>
<li>creates calls to dummy routines</li>
<li>generates lots of conditionals jumps</li>
<li>embeds anti-debugging tricks</li>
<li>inserts junk instructions into real code</li>
</ul>
<p>A combination of these techniques makes debugging of the decryption routine really hard and painful.</p>
<h3>Generating different instruction which do the same thing</h3>
<p>As a programmer you already know that there is always more than one way to do one thing. As an example, let&#8217;s solve this task: set the EAX register to value 100h.</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">; simple assignment</span>
<span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">100h</span>
&nbsp;
<span style="color: #666666; font-style: italic;">; using stack</span>
<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #0000ff;">100h</span>
<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">eax</span>
&nbsp;
<span style="color: #666666; font-style: italic;">; first zero register, then do a binary or</span>
<span style="color: #00007f; font-weight: bold;">sub</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #00007f;">eax</span>
<span style="color: #00007f; font-weight: bold;">or</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">100h</span>
&nbsp;
<span style="color: #666666; font-style: italic;">; this will even hide the assigned value from cracker's eyes</span>
<span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">12345778h</span> <span style="color: #666666; font-style: italic;">; 12345778h = 100h xor 12345678h</span>
<span style="color: #00007f; font-weight: bold;">xor</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">12345678h</span></pre></div></div>

<h3>Intermediate languages</h3>
<p>Implementation of this is pretty straightforward. To represent the polymorphic decryptor, we create an intermediate language (a language of an abstract machine) represented by triplets. The internal representation of the decryptor will look like this:</p>
<pre>
[move, eax, 100h]
[jump, label_id, null]
[increment, eax, null]
</pre>
<p>Code generator will then go through the intermediate code and generate native code for each triplet. Each triplet will have one or more native code alternatives and the code generator will always pick one at random.</p>
<p>The representation using an intermediate language can also assist us in another tasks like swapping instructions or inserting garbage code into real code. Theory behind intermediate languages and optimizations (i.e. modifications of existing code while preserving the functionality) is explained in every book about compiler design.</p>
<h3>Problems with classical polymorphism</h3>
<p>The biggest problem with classical polymorphism is that after the polymorphic decryption routine finishes, the sensitive code is left naked in memory. This means that if the cracker manages to get thought all the weird and hard-to-debug computer-generated code, he will find clean and comprehensible original code. This is something we need to prevent.</p>
<p>To avoid this, we can divide our code into smaller modules and put each of them into its own polymorphic envelope. This will make crackers life harder, because he will never see whole code at once and he will have to trace through the polymorphic decryptors annoyingly often.</p>
<p>But even this approach has its downside: it is the fact that the cracker is still given the opportunity to see the comprehensible original code.</p>
<h3>Metamorphism as an effective weapon</h3>
<p>The solution of this problem is called metamorphism. From the outside it is similar to polymorphism &#8211; it creates different code for each application. But the key difference between polymorphism and metamorphism is that while polymorphism encrypts the sensitive code and creates a unique decryptor for it, metamorphism morphs the sensitive code to make it almost impossible to understand by a human.</p>
<p>With metamorphism it&#8217;s possible to create kilobytes of morphed code from several bytes of original code. Manual tracing of such code can easily take days or even weeks of hard work, with poor results (the cracker will never see the original code as with polymorphism).</p>
<h3>Metamorphism &#8211; an example</h3>
<p>Metamorphic engine first takes existing code, analyzes it using an internal disassembler, morphs the internal representation of code and then generates morphed native code. Let&#8217;s have an example:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">1h</span>
<span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span> <span style="color: #00007f;">Ah</span></pre></div></div>

<p>The resulting morphed code can look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #00007f; font-weight: bold;">xor</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #00007f;">eax</span>
<span style="color: #00007f; font-weight: bold;">inc</span> <span style="color: #00007f;">eax</span>
<span style="color: #00007f; font-weight: bold;">sub</span> <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ecx</span>
<span style="color: #00007f; font-weight: bold;">inc</span> <span style="color: #00007f;">ecx</span>
<span style="color: #00007f; font-weight: bold;">sal</span> <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">2</span>
<span style="color: #00007f; font-weight: bold;">inc</span> <span style="color: #00007f;">ecx</span>
<span style="color: #00007f; font-weight: bold;">sal</span> <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">1</span></pre></div></div>

<h3>Implementation details</h3>
<p>The main difference between implementation of polymorphism and metamorphism lays in the fact that polymorphism doesn&#8217;t change the original code. It only hides it.</p>
<p>On the other hand, metamorphism changes the original code and thus has to cope with several problems:</p>
<ul>
<li>Code flow: because each instruction is replaced with several new instructions, the length of code blocks changes. Engine has to detect and repair all jumping coordinates or function calls within the code to match new positions of code blocks.</li>
<li>Registers used as pointers: the same problem as with code flow.</li>
<li>Detecting data in code: most compilers today place some data in the text section of executable, together with code (e.g. between functions). An attempt to handle data as code (i.e. mutate it) could have fatal consequences.</li>
</ul>
<p>This is the reason why metamorphism is never used for whole application, only for the protection itself.</p>
<h3>Partial and full metamorphism</h3>
<p>Because of great complexity of the task of writing a metamorphic engine, many commercially available protections resort to partial metamorphism. They decide not to write a full morpher but select only a small subset of instructions that will be morphed. The other instructions are left without change.</p>
<p>This approach fulfills the goal of metamorphism only partially. While it&#8217;s still harder to understand the generated code, it&#8217;s not as hard as with full metamorphism. The reason for this is that the subset of affected instructions is usually too small to generate sufficient amount of confusing code.</p>
<p>The complexity of writing a full metamorphic engine is also proven by the fact, that at the time of writing this article, the only commercially available protection offering full metamorphism was SVKP 2.0. You can find it at <a href="http://www.defendion.com">www.defendion.com</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2007/08/02/advanced-self-modifying-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
