<?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>Tue, 27 Sep 2011 03:33:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>AVG Mobilation for Windows Phone</title>
		<link>http://migeel.sk/blog/2011/09/08/avg-mobilation-for-windows-phone/</link>
		<comments>http://migeel.sk/blog/2011/09/08/avg-mobilation-for-windows-phone/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 06:38:41 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://migeel.sk/?p=235</guid>
		<description><![CDATA[A new app hit the Windows Phone marketplace today that claims to keep your device safe from malware. I immediately became interested in it because: I don&#8217;t know of any malware for the Windows Phone. Even if there was malware that misuses some kind of hole in the Windows Phone security model, this app wouldn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>A new app hit the Windows Phone marketplace today that claims to keep your device safe from malware. I immediately became interested in it because:</p>
<ol>
<li>I don&#8217;t know of any malware for the Windows Phone.</li>
<li>Even if there was malware that misuses some kind of hole in the Windows Phone security model, this app wouldn&#8217;t be able to catch it because of phone&#8217;s application model (unless the app itself uses some kind of security hole).</li>
<li>After installing it, it claimed it offers real time protection that would suggest it&#8217;s capable of running in the background.</li>
</ol>
<p>I would consider it a joke app, if it didn&#8217;t come from a well-known antivirus company. (Spoiler: It actually is a joke app, but the joke is on the antivirus company.)</p>
<h3>A look inside</h3>
<p>To satisfy my curiosity, I downloaded the XAP file of the app with <a href="http://mktwp7.codeplex.com/">Marketplace Browser and Downloader for Windows Phone 7</a> and opened it with Reflector. Surprise, surprise, this app was ported from Android (or at least that&#8217;s what *Droid namespace names suggest). Funny how the game has changed and instead of porting antivirus software from a Microsoft operating system to Linux, people started doing it the other way around.</p>
<p>The scanning UI is concentrated in the <code>DroidSecurityPOC.Scan</code> class and gets invoked in the <code>OnNavigatedTo</code> method. The <code>OnNavigatedTo</code> method is actually the first nugget:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnNavigatedTo<span style="color: #008000;">&#40;</span>NavigationEventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// uninteresting code removed</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// do the actual scanning, synchronously (we are scared of threads...)</span>
        <span style="color: #008000;">&#40;</span>Application<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span> <span style="color: #0600FF; font-weight: bold;">as</span> App<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">malwareCollection</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ScanContainingMedia</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">library</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// simulate work in the UI even though the scanning is already completed at this point</span>
        <span style="color: #008080; font-style: italic;">// people will love this</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">StartScan</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>The StartScan method looks at the number of files to scan, divides 5 seconds with that number and starts a timer to update the &#8220;currently scanned&#8221; file name in the UI. Scanning will always take 5+ seconds to complete (closer to 5 seconds if you have few files to scan) and most of the time will be spent waiting for the next timer event to fire. Because all the &#8220;scanning&#8221; already happened in the ScanContainingMedia method, long time before the UI was first updated.</p>
<h3>The scanning algorithm</h3>
<p>The <code>DroidSecurityPOC.Data.MalwareCollection</code> class is where the hilarity starts. The <code>ScanContainingMedia</code> method is where all the &#8220;scanning&#8221; happens. It&#8217;s split up in 2 parts: scanning your picture library and scanning your music library. The method doesn&#8217;t look at anything else (but that&#8217;s not much of a surprise given a marketplace application really cannot access anything else).</p>
<p>At this point, I was still giving the app a chance. Maybe it&#8217;s scanning for damaged files that can trigger known exploits in music players or picture viewers. All my hopes disappeared when I looked at the code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> ScanContainingMedia<span style="color: #008000;">&#40;</span>PictureCollection mediaFileCollectiont<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// uninteresting code removed</span>
    <span style="color: #008080; font-style: italic;">// malwareGroup contains a list of known &quot;malware&quot;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// for each picture in the library</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Picture picture <span style="color: #0600FF; font-weight: bold;">in</span> mediaFileCollectiont<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// for each known malware (because HashSet is overrated)</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> str <span style="color: #0600FF; font-weight: bold;">in</span> malwareGroup<span style="color: #008000;">.</span><span style="color: #0000FF;">MalwareGroupList</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// compare malware name with current file name (!!!!!!!)</span>
            <span style="color: #008080; font-style: italic;">// NOTE: We call ToLower() on each string to allocate a new string</span>
            <span style="color: #008080; font-style: italic;">// and never cache the result. This way the garbage collector will</span>
            <span style="color: #008080; font-style: italic;">// be busy picking up redundant trash and we can have some fun time</span>
            <span style="color: #008080; font-style: italic;">// with his daughter.</span>
            <span style="color: #008080; font-style: italic;">// Also, String.Equals(s1, s2, StringComparison.OrdinalIgnoreCase)</span>
            <span style="color: #008080; font-style: italic;">// is for pussies.</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>str<span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> picture<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// uninteresting code - add malware to a collection of &quot;effected malware&quot;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Basically, this code couldn&#8217;t be less bothered about the file contents. It only looks at the file name and if it matches the predicate, boom, it&#8217;s jailed. No questions asked. Do not pass Go. Do not collect $200.<br />
The list of &#8220;dangerous file names&#8221; is downloaded from a web service and Rafael Rivera can <a href="http://www.withinwindows.com/2011/09/07/the-only-time-youll-see-avg-security-suite-warn-you-about-malware-on-windows-phone-7/">show you</a> the current &#8220;definition file&#8221;.</p>
<p>The code also contains an unused method that hints at a future update that will actually look at the file contents, but the method makes me really scared:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> ScanEicar<span style="color: #008000;">&#40;</span>Picture picture<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Stream image <span style="color: #008000;">=</span> picture<span style="color: #008000;">.</span><span style="color: #0000FF;">GetImage</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    image<span style="color: #008000;">.</span><span style="color: #0000FF;">Position</span> <span style="color: #008000;">=</span> 0L<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>image<span style="color: #008000;">.</span><span style="color: #0000FF;">Position</span> <span style="color: #008000;">+</span> 0x44L<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;=</span> image<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// the garbage collector still doesn't seem to be busy enough, so</span>
        <span style="color: #008080; font-style: italic;">// let's allocate an array in a tight loop</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">70</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
        image<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#40;</span>buffer, <span style="color: #FF0000;">0</span>, 0x44<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// BLAM! Potentially triple the amount of allocated memory by allocating</span>
        <span style="color: #008080; font-style: italic;">// a string with the contents of the buffer. Note each character</span>
        <span style="color: #008080; font-style: italic;">// in a string takes up 2 bytes.</span>
        <span style="color: #008080; font-style: italic;">// Except Convert.ToString will actually return string &quot;System.Byte[]&quot;</span>
        <span style="color: #008080; font-style: italic;">// for each and every call. What the author probably wanted</span>
        <span style="color: #008080; font-style: italic;">// is Encoding.ASCII.GetString().</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>buffer<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            image<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Scanning fail: the call to image.Read() already moved the position</span>
        <span style="color: #008080; font-style: italic;">// by 0x44 bytes. What the author probably wanted to do is</span>
        <span style="color: #008080; font-style: italic;">// image.Position -= 0x43, but if he did that, the while loop would</span>
        <span style="color: #008080; font-style: italic;">// run for each byte in the file, allocating about 210 MB from the heap</span>
        <span style="color: #008080; font-style: italic;">// for a 1 MB file, so the algorithm is probably better off this way.</span>
        image<span style="color: #008000;">.</span><span style="color: #0000FF;">Position</span> <span style="color: #008000;">+=</span> 1L<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    image<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Everything (including the release date) hints at this being some kind of a summer intern project at AVG (if it&#8217;s not, it&#8217;s very disturbing). But AVG, c&#8217;mon. Interns do all kinds of wonky stuff. You really don&#8217;t need to ship all of it&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2011/09/08/avg-mobilation-for-windows-phone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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 [...]]]></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 [...]]]></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: #0000ff;">.004020E4</span>                       <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 [...]]]></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>

