<?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>Hex Blog</title>
	<atom:link href="http://www.hexblog.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.hexblog.com</link>
	<description>State-of-the-art code analysis</description>
	<lastBuildDate>Fri, 17 May 2013 09:29:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Vulnerability fix for bTree engine</title>
		<link>http://www.hexblog.com/?p=764</link>
		<comments>http://www.hexblog.com/?p=764#comments</comments>
		<pubDate>Fri, 17 May 2013 09:28:45 +0000</pubDate>
		<dc:creator>Ilfak Guilfanov</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=764</guid>
		<description><![CDATA[Just a quick note for all IDA users. We published a fix for potential vulnerability in IDA. Please check out https://www.hex-rays.com/vulnfix.shtml. It does not seem to be exploitable but we prefer to be on the safe side. Feel free to &#8230; <a href="http://www.hexblog.com/?p=764">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Just a quick note for all IDA users.<br />
We published a fix for potential vulnerability in IDA. Please check out <a href="https://www.hex-rays.com/vulnfix.shtml" title="https://www.hex-rays.com/vulnfix.shtml">https://www.hex-rays.com/vulnfix.shtml</a>. It does not seem to be exploitable but we prefer to be on the safe side. Feel free to download and copy it to your plugins subdirectory. The plugin will validate all opened databases and protect you from malformed idbs.<br />
Thanks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=764</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading your own modules from your IDAPython scripts with idaapi.require()</title>
		<link>http://www.hexblog.com/?p=749</link>
		<comments>http://www.hexblog.com/?p=749#comments</comments>
		<pubDate>Mon, 06 May 2013 10:45:32 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[IDA Pro]]></category>
		<category><![CDATA[IDAPython]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=749</guid>
		<description><![CDATA[TL;DR If you were using import to import your own &#8220;currently-in-development&#8221; modules from your IDAPython scripts, you may want to use idaapi.require(), starting with IDA 6.5. Rationale When using IDAPython scripts, users were sometimes facing the following issue Specifically: User &#8230; <a href="http://www.hexblog.com/?p=749">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h1>TL;DR</h1>
<p>If you were using <code>import</code> to import your own &#8220;currently-in-development&#8221; modules from your IDAPython scripts, you may want to use <code>idaapi.require()</code>, starting with IDA 6.5.</p>
<h1>Rationale</h1>
<p>When using IDAPython scripts, users were sometimes facing <a href="https://code.google.com/p/idapython/issues/detail?id=42">the following issue</a></p>
<p>Specifically:</p>
<ul>
<li>User loads script</li>
<li>Script <code>import</code>s user&#8217;s module <code>mymodule</code></li>
<li>Script ends</li>
<li>User modifies code of <code>mymodule</code> (Note: the module is modified, not the script)</li>
<li>User reloads script</li>
<li>Modifications to <code>mymodule</code> aren&#8217;t taken into consideration.</li>
</ul>
<p>While that&#8217;s perfectly understandable (the python runtime doesn&#8217;t have to reload <code>mymodule</code> if it has been compiled &amp; loaded already), this is somewhat of an annoyance for users that were importing modules that were often modified.</p>
<h1>IDA &lt;= 6.4: Ensuring a user-specified module gets reloaded, by destroying it.</h1>
<p>Up until IDA 6.4, the IDAPython plugin <a href="https://code.google.com/p/idapython/source/detail?r=273">would do some magic after you have run your user script</a>.<br />
(click &#8220;expand all&#8221; to reveal the diff)</p>
<p>The sequence becomes:</p>
<ul>
<li>User loads script</li>
<li>Script <code>import</code>s user&#8217;s module <code>mymodule</code></li>
<li>Script ends</li>
<li>[module <code>mymodule</code> is deleted]</li>
<li>User modifies code of <code>mymodule</code></li>
<li>User reloads script</li>
<li>Modifications to <code>mymodule</code> are taken into consideration, since module was deleted.</li>
</ul>
<p>Unfortunately we have to stop doing this because:</p>
<ul>
<li>That prevents us from using python-based hooks to be used after the script is finished (see below).</li>
<li>That goes against the rest of the python philosophy (i.e., modifications to objects are not reverted), and is therefore unexpected.</li>
</ul>
<h3>Issues with hooks.</h3>
<p>Imagine you have the following script, <code>dbghooks.py</code>:</p>
<pre>
from idaapi import *
import mydbghelpers

class MyHooks(DBG_Hooks):

  def __init__(self):
    ...

  def dbg_bpt(self, tid, ea):
    mydbghelpers.do_something()
    return 0

  def dbg_step_into(self):
    ...

hooks = MyHooks()
hooks.hook()
</pre>
<ul>
<li>User loads script</li>
<li>Scripts <code>import</code>s <code>mydbghelpers</code></li>
<li>Script creates instance of <code>MyHooks</code>, and hooks it into IDA&#8217;s debugger APIs</li>
<li>Script ends</li>
<li>[module <code>mydbghelpers</code> is deleted]</li>
<li>User runs debugger, and a breakpoint is hit. Two things can happen:
<ul>
<li>The hook fails executing</li>
<li>IDA crashes (that can happen if the form <code>from mydbghelpers import *</code> was used)</li>
</ul>
</li>
</ul>
<h1>IDA &gt; 6.4: Introducing <code>idaapi.require()</code></h1>
<p>Everywhere else in python, when you modify a runtime object, those changes will remain visible.</p>
<p>We decided it would be better to not go against that standard behaviour anymore, and provide a helper to achieve the same results as what was achieved before with the deletion of user modules.</p>
<p>You can now import &amp; re-import of a module with: <code>idaapi.require(name)</code></p>
<p>Here is its definition:</p>
<pre>
def require(modulename):
    if modulename in sys.modules.keys():
        reload(sys.modules[modulename])
    else:
        import importlib
        import inspect
        m = importlib.import_module(modulename)
        frame_obj, filename, line_number, function_name, lines, index = inspect.stack()[1]
        importer_module = inspect.getmodule(frame_obj)
        if importer_module is None: # No importer module; called from command line
            importer_module = sys.modules['__main__']
        setattr(importer_module, modulename, m)
        sys.modules[modulename] = m
</pre>
</p>
<h3>Example</h3>
<p>The example debugger hooks script above becomes:</p>
<pre>
from idaapi import *
idaapi.require("mydbghelpers")

class MyHooks(DBG_Hooks):

  def __init__(self):
    ...

  def dbg_bpt(self, tid, ea):
    mydbghelpers.do_something()
    return 0

  def dbg_step_into(self):
    ...

hooks = MyHooks()
hooks.hook()
</pre>
<p>I.e., only the second line changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=749</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing PIP packages, and using them from IDA on a 64-bit machine</title>
		<link>http://www.hexblog.com/?p=726</link>
		<comments>http://www.hexblog.com/?p=726#comments</comments>
		<pubDate>Tue, 30 Apr 2013 09:29:27 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[idapro]]></category>
		<category><![CDATA[IDAPython]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=726</guid>
		<description><![CDATA[Recently, one of our customers came to us asking how he should proceed to be able to install python packages, using PIP, and use those from IDA. The issue he was facing is that his system is a 64-bit Ubuntu &#8230; <a href="http://www.hexblog.com/?p=726">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Recently, one of our customers came to us asking how he should proceed to be able to install python packages, using PIP, and use those from IDA.</p>
<p>The issue he was facing is that his system is a 64-bit Ubuntu 12.04 VM.<br />
Therefore using the Ubuntu-bundled PIP will just result in installing the desired package (let&#8217;s say <code>ssdeep</code>) for the system Python runtime, which is a 64-bit runtime and therefore not compatible with IDA.</p>
<p>The best (as in: cleanest) solution I have found is to:</p>
<ul>
<li>build a 32-bits python on the system.</li>
<li><code>pip</code>-install packages in that 32-bits python&#8217;s sub-directories.</li>
<li>export <code>PYTHONPATH</code> to point to the 32-bits python&#8217;s sub-directories.</li>
</ul>
<p>We figured we&#8217;d write it down here just in case it might help others.</p>
<h1>Prerequisites</h1>
<ul>
<li>Install autoconf</li>
<li>Install ia32-libs</li>
</ul>
<h1>Building &amp; installing a 32-bits python</h1>
<ul>
<li><code>..$ export LD_LIBRARY_PATH=/lib/i386-linux-gnu/:/usr/lib32:$LD_LIBRARY_PATH</code></li>
<li>Download Python2.7.4
<ul>
<li><strong>Note:</strong>You should make sure that the MD5 checksum and the size of the file you downloaded match those that are advertised on the page. That would prevent a man-in-the-middle attacker from providing you with a malicious Python bundle.
            </li>
</ul>
</li>
<li>Build it. Note that you&#8217;ll probably have to sudo-create a few symlinks. I had to do this, on the Ubuntu 12.04 64-bit VM I tested this on:
<ul>
<li><code>/lib/i386-linux-gnu/libssl.so</code> ⇒ <code>/lib/i386-linux-gnu/libssl.so.1.0.0</code></li>
<li><code>/lib/i386-linux-gnu/libcrypto.so</code> ⇒ <code>/lib/i386-linux-gnu/libcrypto.so.1.0.0</code></li>
<li><code>/lib/i386-linux-gnu/libz.so</code> ⇒ <code>/lib/i386-linux-gnu/libz.so.1</code></li>
</ul>
</li>
<li>For the sake of completeness, here are my build commands (don&#8217;t forget the flags, of course):
<ul>
<li>
<pre style="overflow: auto">..$ CFLAGS=-m32 LDFLAGS=-m32 ./configure --prefix=/opt/Python2.7.4-32bits</pre>
</li>
<li>
<pre style="overflow: auto">..$ CFLAGS=-m32 LDFLAGS=-m32 make -j 8</pre>
</li>
</ul>
</li>
</ul>
<h3>Once the build completes</h3>
<p>Here&#8217;s what I have as last lines of the build:</p>
<pre style="overflow: auto">INFO: Can't locate Tcl/Tk libs and/or headers

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _curses            _curses_panel
_sqlite3           _tkinter           bsddb185
bz2                dbm                gdbm
readline           sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module's name.</pre>
<p>If you see, below that, that it failed to build, say <code>'binascii'</code>, then something went wrong.</p>
<p>Make sure you run <code>make -j 1</code> to check out what went wrong (i.e., what library it claims not being able to find)</p>
<p>Once you have succesfully built your 32-bits Python, it&#8217;s time to install it: <code>sudo make install</code></p>
<h3>Trying your freshly-built python</h3>
<pre style="overflow: auto">..$ /opt/Python2.7.4-32bits/bin/python2.7
Python 2.7.4 (default, Apr 26 2013, 16:03:38)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import binascii
&gt;&gt;&gt;</pre>
<p>No complaint so far. Good.</p>
<h3>Checking that <code>pkg_resources</code> is available.</h3>
<p>Try importing <code>pkg_resources</code>. If it fails, you&#8217;ll probably have to do the following:</p>
<pre style="overflow: auto">..$ cd /tmp
..$ curl -O http://python-distribute.org/distribute_setup.py
..$ less distribute_setup.py  # (*)
..$ sudo /opt/Python2.7.4-32bits/bin/python2.7 distribute_setup.py</pre>
<p>That will print out quite a fair amount of info, and should succeed.</p>
<p><strong>(*) Note:</strong> A careful reader has pointed out that it would be fairly easy to intercept (man-in-the-middle) such an HTTP request, and serve malicious content that would then be piped (as root) to Python.<br />
That&#8217;s why I think it&#8217;s important to mention, as a third step (i.e., <code>less ...</code>), that the code that was downloaded should ideally be checked. Hopefully, <a href="http://python-distribute.org">http://python-distribute.org</a> will soon <a href="https://bitbucket.org/tarek/distribute/issue/354/distribute_setuppy-should-use-https-not">provide HTTPS support</a>, which will limit such MITM attack risks.</p>
<h3>Trying your freshly-built python, again</h3>
<p>We want to make sure <code>pkg_resources</code> can be imported.</p>
<pre style="overflow: auto">..$ /opt/Python2.7.4-32bits/bin/python2.7
Python 2.7.4 (default, Apr 26 2013, 16:03:38)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import pkg_resources
&gt;&gt;&gt;</pre>
<p>Still no complaint. Good.</p>
<p>If yours complains, you&#8217;ll have to first make sure you fix whatever is causing it to fail, because the next will not work without that.</p>
<h1>Installing PIP for your new Python build</h1>
<p>Since using your system&#8217;s PIP will probably not work (as it would build &amp; install things in a 64-bits python sub-directory), you&#8217;ll have to install a PIP package specifically for your freshly-built Python.</p>
<p>Here&#8217;s how I proceeded:</p>
<pre style="overflow: auto">..$ cd /tmp;
..$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py;
..$ sudo /opt/Python2.7.4-32bits/bin/python2.7 get-pip.py</pre>
<p>PIP is now installed.</p>
<h1>PIP-installing a package (i.e., <code>ssdeep</code>)</h1>
<p>To download/build/install the <code>ssdeep</code> package I ran, as root (either that, or you&#8217;ll have to give your user the rights to write in /opt/Python2.7.4-32bits):</p>
<pre style="overflow: auto">..$ su
Password:
root ..$ export CFLAGS=-m32
root ..$ export LDFLAGS=-m32
root ..$ export LD_LIBRARY_PATH=/lib/i386-linux-gnu/:/usr/lib32:$LD_LIBRARY_PATH
root ..$ /opt/Python2.7.4-32bits/bin/python2.7 /opt/Python2.7.4-32bits/bin/pip install ssdeep</pre>
<p>Notice how I use my freshly-built python, with my fresly-installed PIP (and not the system one.)</p>
<p>Note: Don&#8217;t forget the <code>export</code> lines, or PIP will partially build stuff for x64, and partially for x86. That, as you can guess, won&#8217;t quite work.</p>
<p>If you forgot the <code>export</code> lines and started building anyway (and the build failed because of the mixed architecture issue I just wrote about), make sure you delete whatever is in <code>/tmp/pip-build-*</code>, so that there won&#8217;t be stale object files of inappropriate architecture in there.</p>
<h1>Check out the PIP-installed package works</h1>
<pre style="overflow: auto">..$ /opt/Python2.7.4-32bits/bin/python2.7
Python 2.7.4 (default, Apr 26 2013, 16:03:38)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import ssdeep
&gt;&gt;&gt; ssdeep
&lt;module 'ssdeep' from '/opt/Python2.7.4-32bits/lib/python2.7/site-packages/ssdeep.so'&gt;
&gt;&gt;&gt; dir(ssdeep)
['Error', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__', '__version__', 'compare', 'hash', 'hash_from_file', 'sys']
&gt;&gt;&gt;</pre>
<p>So far so good.</p>
<h1>Testing the PIP-installed package in IDA</h1>
<p>Since that&#8217;s still the goal (though you might have forgotten by now, given the amount of directions above.. <img src='http://www.hexblog.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ),<br />
we&#8217;ll now try and make use of that PIP-installed package in IDA.</p>
<ul>
<li><code>..$ export PYTHONPATH=/opt/Python2.7.4-32bits/lib/python2.7/site-packages:/opt/Python2.7.4-32bits/:$PYTHONPATH</code></li>
<li><code>..$ idaq</code></li>
</ul>
<p>If all went well, typing <code>import ssdeep</code> in the Python input line should properly, silently, nicely import the package.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=726</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recon 2012: Compiler Internals</title>
		<link>http://www.hexblog.com/?p=704</link>
		<comments>http://www.hexblog.com/?p=704#comments</comments>
		<pubDate>Fri, 22 Jun 2012 17:49:47 +0000</pubDate>
		<dc:creator>Igor Skochinsky</dc:creator>
				<category><![CDATA[IDA Pro]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=704</guid>
		<description><![CDATA[This year I again was lucky to present at Recon in Montreal. There were many great talks as usual. I combined the topic of my last year&#8217;s talk on C++ reversing and my OpenRCE article on Visual C++ internals. New &#8230; <a href="http://www.hexblog.com/?p=704">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>This year I again was lucky to present at <a href="http://recon.cx/">Recon</a> in Montreal. There were many great talks as usual. I combined the topic of my last year&#8217;s talk on C++ reversing and my OpenRCE article on Visual C++ internals. New material was implementation of exceptions and RTTI in MSVC x64 and GCC (including Apple&#8217;s iOS).</p>
<p>The videos are not up yet but here are the slides of my presentation and a few demo scripts I made for it to parse GCC&#8217;s RTTI structures and exception tables. I also added my old scripts from OpenRCE which I amended slightly for the current IDA versions (mostly changed hotkeys).</p>
<p><a href='http://www.hexblog.com/wp-content/uploads/2012/06/Recon-2012-Skochinsky-Compiler-Internals.pdf'>Slides</a><br />
<a href='http://www.hexblog.com/wp-content/uploads/2012/06/recon-2012-skochinsky-scripts.zip'>Scripts</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=704</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Calling IDA APIs from IDAPython with ctypes</title>
		<link>http://www.hexblog.com/?p=695</link>
		<comments>http://www.hexblog.com/?p=695#comments</comments>
		<pubDate>Thu, 05 Apr 2012 17:10:57 +0000</pubDate>
		<dc:creator>Igor Skochinsky</dc:creator>
				<category><![CDATA[IDA Pro]]></category>
		<category><![CDATA[IDAPython]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=695</guid>
		<description><![CDATA[IDAPython provides wrappers for a big chunk of IDA SDK. Still, there are some APIs that are not wrapped because of SWIG limitations or just because we didn&#8217;t get to them yet. Recently, I needed to test the get_loader_name() API &#8230; <a href="http://www.hexblog.com/?p=695">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>IDAPython provides wrappers for a big chunk of IDA SDK. Still, there are some APIs that are not wrapped because of SWIG limitations or just because we didn&#8217;t get to them yet. Recently, I needed to test the get_loader_name() API which is not available in IDAPython but I didn&#8217;t want to write a full plugin just for one call. For such cases it&#8217;s often possible to use the <a href="http://docs.python.org/library/ctypes.html">ctypes module</a> to call the function manually.</p>
<p>The IDA APIs are provided by the kernel dynamic library. In Windows, it&#8217;s called <strong>ida.wll</strong> (or <strong>ida64.wll</strong>), in Linux <strong>libida[64].so</strong> and on OS X <strong>libida[64].dylib</strong>. <strong>ctypes</strong> provides a nice feature that dynamically creates a callable wrapper for a DLL export by treating it as an attribute of a special class instance. Here&#8217;s how to get that instance under the three platforms supported by IDA:</p>
<p><code>
<pre>import ctypes
idaname = "ida64" if __EA64__ else "ida"
if sys.platform == "win32":
    dll = ctypes.windll[idaname + ".wll"]
elif sys.platform == "linux2":
    dll = ctypes.cdll["lib" + idaname + ".so"]
elif sys.platform == "darwin":
    dll = ctypes.cdll["lib" + idaname + ".dylib"]
</pre>
<p></code></p>
<p>We use &#8220;<strong>windll</strong>&#8221; because IDA APIs use <strong>stdcall</strong> calling convention on Windows (check the definition of <strong>idaapi</strong> in pro.h).</p>
<p>Now we just need to call our function just as if it was an attribute of the &#8220;dll&#8221; object. But first we need to prepare the arguments. Here&#8217;s the declaration from loader.hpp:</p>
<p><code>idaman ssize_t ida_export get_loader_name(char *buf, size_t bufsize);</code></p>
<p><strong>ctypes</strong> provides a convenience functions for creating character buffers:</p>
<p><code>buf = ctypes.create_string_buffer(256)</code></p>
<p>And now we can call the function:</p>
<p><code>dll.get_loader_name(buf, 256)</code></p>
<p>To retrieve the contents of the buffer as a Python byte string, just use its .raw attribute. The complete script now looks like this:</p>
<p><code>
<pre>import ctypes
idaname = "ida64" if __EA64__ else "ida"
if sys.platform == "win32":
    dll = ctypes.windll[idaname + ".wll"]
elif sys.platform == "linux2":
    dll = ctypes.cdll["lib" + idaname + ".so"]
elif sys.platform == "darwin":
    dll = ctypes.cdll["lib" + idaname + ".dylib"]
buf = ctypes.create_string_buffer(256)
dll.get_loader_name(buf, 256)
print "loader:", buf.raw</pre>
<p></code></p>
<p><strong>ctypes</strong> offers many means to interface with C code, so you can use it to call almost any IDA API.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=695</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The trace replayer</title>
		<link>http://www.hexblog.com/?p=669</link>
		<comments>http://www.hexblog.com/?p=669#comments</comments>
		<pubDate>Tue, 24 Jan 2012 15:29:30 +0000</pubDate>
		<dc:creator>joxean</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=669</guid>
		<description><![CDATA[One of the new features that will be available in the next version of IDA is a trace re-player. This pseudo-debugger allows to re-play execution traces of programs debugged in IDA. The replayer debugger allows replaying traces recorded with any of &#8230; <a href="http://www.hexblog.com/?p=669">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>One of the new features that will be available in the next version of IDA is a trace re-player. This pseudo-debugger allows to re-play execution traces of programs debugged in IDA. The replayer debugger allows replaying traces recorded with any of the currently supported debuggers, ranging from local Linux or win32 debuggers to remote GDB targets. Currently supported targets include x86, x86_64, ARM, MIPS and PPC.</p>
<p>When we are re-playing a recorded trace, we can step forward and backward, set breakpoints, inspect register values, change the instruction pointer to any recorded IP, etc&#8230;</p>
<p>Also, trace management capabilities have been added to IDA in order to allow saving and loading recorded execution traces. Let&#8217;s see an example.</p>
<p><span id="more-669"></span> <strong></strong></p>
<p><strong>A vulnerable sample program</strong></p>
<p><strong></strong>For this blog post, I will show you how this plugin can be used to analyze a bug in a toy executable program. This sample application receives 2 arguments: a message to display and the size of it. The program checks if the size of the given buffer (calling strlen) is longer than the size specified, printing out an error message and exiting. If not, memory of the given size is reserved for a local variable, the contents of the buffer copied to it and a message based on this string printed out to stdout. After this, the memory reserved is freed and the application simply exits.</p>
<p>In this application there is a little integer overflow bug that can be triggered giving to the size argument a negative value. Let&#8217;s record a trace of the program crashing and replay it in IDA to understand why the program is crashing:</p>
<ul>
<li>Set a breakpoint in the entry point.</li>
<li>Set the program arguments to “<em>whatever</em> -1” in Debugger → Process options.</li>
<li>Run the application with the correspondent debugger (in my case, the “Local Linux&#8221; debugger).</li>
<li>When the breakpoint is reached, enable instruction tracing (via the menu item “Debugger → Tracing → Instruction tracing”).</li>
<li>Let the application continue (press F9).</li>
</ul>
<p><span class="Apple-style-span" style="color: #444444;font-family: 'Helvetica Neue', Arial, Helvetica, 'Nimbus Sans L', sans-serif;font-size: 13px;line-height: 19px">At some point it will crash with a message similar to this:</span></p>
<p><a href="http://www.hexblog.com/wp-content/uploads/2012/01/pic1.png"><img class="aligncenter size-medium wp-image-672" src="http://www.hexblog.com/wp-content/uploads/2012/01/pic1-300x63.png" alt="" width="300" height="63" /></a></p>
<p align="JUSTIFY">When the application crashes, stop the debugger, go to the trace window (Debugger → Tracing → Trace window) and save the trace to a file (right click on the window and, from the pop-up menu, select the option Other options → Save binary trace file to disk). Specify the file name and a description for this trace and click OK:</p>
<p align="JUSTIFY"><a href="http://www.hexblog.com/wp-content/uploads/2012/01/pic2.png"><img class="aligncenter size-medium wp-image-673" src="http://www.hexblog.com/wp-content/uploads/2012/01/pic2-300x93.png" alt="" width="300" height="93" /></a></p>
<p align="JUSTIFY">Next, switch to the “Trace replayer debugger” (from the menu Debugger → Switch to debugger, and then select &#8220;Trace replayer&#8221;). After this, go to the trace window to see where is it crashing and set a breakpoint in the function call that segfaults. In our example, it&#8217;s crashing in a call to strcpy in function “foo” as we may see here:</p>
<p align="JUSTIFY"><a href="http://www.hexblog.com/wp-content/uploads/2012/01/pic3.png"><img class="aligncenter size-medium wp-image-674" src="http://www.hexblog.com/wp-content/uploads/2012/01/pic3-300x91.png" alt="" width="300" height="91" /></a></p>
<p align="JUSTIFY">We will set a breakpoint in the call to strcpy in function “foo” and press F9 to replay the trace. When the breakpoint is reached we have all the register values as they were when the program was really executed (click to enlarge):</p>
<p align="JUSTIFY"><a href="http://www.hexblog.com/wp-content/uploads/2012/01/pic4.png"><img class="aligncenter size-medium wp-image-675" src="http://www.hexblog.com/wp-content/uploads/2012/01/pic4-300x190.png" alt="" width="300" height="190" /></a></p>
<p align="JUSTIFY">There is a check at 0&#215;08048502 for the size of the given buffer and the size given in the command line. As -1 is lower than strlen(“AAAA”) the developer didn&#8217;t expected the program to reach the basic block where the malloc and strcpy calls are made. There is another bug here: before the call to strcpy there is a call to malloc to reserve memory with the size we gave in the command line, but the developer didn&#8217;t performed any check to see if the memory was correctly reserved or not. Let&#8217;s step back to this position by selecting from the menu Debugger → Step back. Click this menu item various times until EIP points to 0x804851D. Alternatively, at the IDC prompt in the bottom part of IDA, we may enter the command “StepBack()” and press enter. No matter how we moved to the instruction after the malloc call, we will see the following in IDA:</p>
<p align="JUSTIFY"><a href="http://www.hexblog.com/wp-content/uploads/2012/01/pic5.png"><img class="aligncenter size-medium wp-image-676" src="http://www.hexblog.com/wp-content/uploads/2012/01/pic5-300x168.png" alt="" width="300" height="168" /></a></p>
<p align="JUSTIFY">The program failed to reserve memory as the returned pointer is NULL. Step back until the “call _malloc” instruction to see the size passed to malloc:</p>
<p align="JUSTIFY"><a href="http://www.hexblog.com/wp-content/uploads/2012/01/pic6.png"><img class="aligncenter size-medium wp-image-677" src="http://www.hexblog.com/wp-content/uploads/2012/01/pic6-300x144.png" alt="" width="300" height="144" /></a></p>
<p align="JUSTIFY">The program is trying to reserve 0xFFFFFFFF bytes (4 GB) and it fails to do so. Let&#8217;s analyse how the program reached the point where the memory is reserved, the strcpy call is performed, etc&#8230; when it wasn&#8217;t supposed to do so. Click on the 1<sup>st</sup> instruction of the function “foo”, right click and select from the pop-up menu “Set IP” (this way, we are telling the re-player to change IP to the nearest event with this IP). The instruction pointer and all the other register values changes:</p>
<p align="JUSTIFY"><a href="http://www.hexblog.com/wp-content/uploads/2012/01/pic7.png"><img class="aligncenter size-medium wp-image-678" src="http://www.hexblog.com/wp-content/uploads/2012/01/pic7-300x94.png" alt="" width="300" height="94" /></a></p>
<p align="JUSTIFY">Now, step over the instructions until we reach the JBE one (alternatively, we could simply right click in the JBE instruction and select from the pop-up menu “Set IP”). Once moved to this instruction, we will see the following register values:</p>
<p align="JUSTIFY"><a href="http://www.hexblog.com/wp-content/uploads/2012/01/pic8.png"><img class="aligncenter size-medium wp-image-679" src="http://www.hexblog.com/wp-content/uploads/2012/01/pic8-300x119.png" alt="" width="300" height="119" /></a></p>
<p align="JUSTIFY">The result of the call to strlen passing the given buffer (unsigned) is compared against the size value we gave in the command line (which is calculated as atoi(argv[2]), which returns a signed integer). Then, an unsigned comparison (JBE) is performed and the check &#8220;0&#215;4 &lt;= 0xFFFFFFFF&#8221; passes. Now you can document this bug and continue searching for more or fix it and recompile your application.</p>
<p align="JUSTIFY">We hope you like this new IDA feature!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=669</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Code viewer, forms &amp; timers</title>
		<link>http://www.hexblog.com/?p=536</link>
		<comments>http://www.hexblog.com/?p=536#comments</comments>
		<pubDate>Thu, 20 Oct 2011 11:06:08 +0000</pubDate>
		<dc:creator>Daniel Pistelli</dc:creator>
				<category><![CDATA[IDA Pro]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=536</guid>
		<description><![CDATA[In this post I&#8217;ll present some new things in IDA 6.2. There&#8217;s a new control, the code viewer, some additions to forms and the introduction of timers to discuss. All these new features have been exposed to the SDK, so &#8230; <a href="http://www.hexblog.com/?p=536">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>In this post I&#8217;ll present some new things in IDA 6.2. There&#8217;s a new control, the code viewer, some additions to forms and the introduction of timers to discuss. All these new features have been exposed to the SDK, so that our users can benefit from them too. <img src='http://www.hexblog.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img src="http://www.hexblog.com/wp-content/uploads/2011/10/codeviewer.gif" alt="code viewer" /></p>
<p><span id="more-536"></span></p>
<h2>The code viewer</h2>
<p>The first new thing I&#8217;m going to talk about is the code viewer. This control is just a container for a generic custom viewer. In fact, I just took the custom viewer sample plugin and added some lines to add the code viewer:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">  <span style="color: #666666;">// create a custom viewer</span>
  si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>cv <span style="color: #000080;">=</span> create_custom_viewer<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span>, <span style="color: #0000ff;">NULL</span>, <span style="color: #000040;">&amp;</span>s1, <span style="color: #000040;">&amp;</span>s2, <span style="color: #000040;">&amp;</span>s1, <span style="color: #0000dd;">0</span>, <span style="color: #000040;">&amp;</span>si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>sv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// create a code viewer container for the custom viewer</span>
  si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>codeview <span style="color: #000080;">=</span> create_code_viewer<span style="color: #008000;">&#40;</span>form, si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>cv, CDVF_LINEICONS<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// set handlers for the code viewer</span>
  set_code_viewer_line_handlers<span style="color: #008000;">&#40;</span>si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>codeview, lines_click, <span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">NULL</span>, lines_icon, lines_linenum<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// draw maximal 2 icons in the lines widget</span>
  set_code_viewer_lines_icon_margin<span style="color: #008000;">&#40;</span>si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>codeview, <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>After having created the custom viewer with <strong>create_custom_viewer</strong>, it is possible to create a code viewer by passing the custom viewer as argument to <strong>create_code_viewer</strong>.</p>
<p>The code viewer basically features for the moment a widget left to the embedded custom viewer called &#8216;the line widget&#8217;. This control can show the current line number, either automatically or by providing it ourselves. It can also show one or more icons and it informs us about user interaction such as mouse clicks.</p>
<p>As you can see, <strong>create_code_viewer</strong> was called specifying the CDVF_LINEICONS flag, which instructs the code viewer that we want icons to be drawn inside the line widget.</p>
<p>Available flags are:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define CDVF_NOLINES        0x0001    // don't show line numbers</span>
<span style="color: #339900;">#define CDVF_LINEICONS      0x0002    // icons can be drawn over the line control</span>
<span style="color: #339900;">#define CDVF_STATUSBAR      0x0004    // keep the status bar in the custom viewer</span></pre></td></tr></table></div>

<p>However, specifying the flag to show the icons is not enough. We also need to specify how the maximum number of icons we want to display on a single line:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">  <span style="color: #666666;">// draw maximal 2 icons in the lines widget</span>
  set_code_viewer_lines_icon_margin<span style="color: #008000;">&#40;</span>si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>codeview, <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Then there&#8217;s the callback we have set by calling <strong>set_code_viewer_line_handlers</strong>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> idaapi lines_icon<span style="color: #008000;">&#40;</span>
        TCustomControl <span style="color: #000040;">*</span> <span style="color: #ff0000; font-style: italic;">/*cv*/</span>,
        <span style="color: #0000ff;">const</span> place_t <span style="color: #000040;">*</span>p,
        <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span>x,
        <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #ff0000; font-style: italic;">/*ud*/</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">bool</span> b <span style="color: #000080;">=</span> p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>touval<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">6</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #000040;">*</span>x <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// setting the highest bit signals that there's another icon </span>
  <span style="color: #666666;">// to draw on the current line</span>
  <span style="color: #0000ff;">int</span> icon_id <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>x <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">51</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">177</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> b <span style="color: #008080;">?</span> <span style="color: #208080;">0x80000000</span> <span style="color: #000040;">|</span> icon_id <span style="color: #008080;">:</span> icon_id<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This callback needs some explanation. The line widget calls this callback for every line. The argument <strong>x</strong> represents the position of the icon to draw. Thus, for every line the callback is called the first time with *<strong>x</strong> == 0. The return value of the callback is the id of the icon to display. If you want to display another icon for the current line, then set the highest bit in the return value. If you don&#8217;t want to display an icon at all for the current line, simply return -1.</p>
<p>You might wonder why <strong>x</strong> is a pointer. Imagine you don&#8217;t want to display an icon at position 0, but skip that position and display an icon at the next one. This can be done without calling the callback again, since we can set the value of <strong>x</strong>. Remember that you can <a href="http://www.hexblog.com/?p=265" title="custom icons">load custom icons</a> and use them in the code viewer.</p>
<p>Apart from the usual mouse and context menu callbacks, there&#8217;s a handy callback to be notified when the user clicks in the space reserved for icons:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> idaapi lines_click<span style="color: #008000;">&#40;</span>
        TCustomControl <span style="color: #000040;">*</span> <span style="color: #ff0000; font-style: italic;">/*cv*/</span>,
        <span style="color: #0000ff;">const</span> place_t <span style="color: #000040;">*</span>p,
        <span style="color: #0000ff;">int</span> pos,
        <span style="color: #0000ff;">int</span> <span style="color: #ff0000; font-style: italic;">/*shift*/</span>,
        <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #ff0000; font-style: italic;">/*ud*/</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> p <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span> <span style="color: #008000;">&#41;</span>
    msg<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Line click at position: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, pos<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">else</span>
    msg<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Click occurred outside of a line<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This is useful to set/unset an icon for instance.</p>
<p>I&#8217;ve also created another sample plugin with the code viewer featuring a hex view.</p>
<p><img src="http://www.hexblog.com/wp-content/uploads/2011/10/hexview.gif" alt="hex view" /></p>
<p>This can give you some more ideas of what this control might good for. Most of the logic behind this sample stands in the custom view and not in the code view, which, in this case, handles everything automatically without the use of callbacks:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">  <span style="color: #666666;">// create a code viewer container for the custom view</span>
  si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>hexview <span style="color: #000080;">=</span> create_code_viewer<span style="color: #008000;">&#40;</span>form, si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>cv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// set the radix and alignment for the offsets</span>
  set_code_viewer_lines_radix<span style="color: #008000;">&#40;</span>si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>hexview, <span style="color: #0000dd;">16</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  set_code_viewer_lines_alignment<span style="color: #008000;">&#40;</span>si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>hexview, si<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>data.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&gt;</span> <span style="color: #208080;">0xFFFFFFFF</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">16</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h2>New forms controls</h2>
<p>Forms were certainly lacking two very important controls: a generic combo box and a multi-line edit. The combo box comes in two variants: editable and read-only. This is how to declare a combo:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000080;">&lt;</span>title<span style="color: #008080;">:</span>b<span style="color: #008080;">:</span>is_editable<span style="color: #008080;">:</span>width<span style="color: #008080;">::</span><span style="color: #000080;">&gt;</span></pre></td></tr></table></div>

<p>When <strong>is_editable</strong> is omitted or zero, then the combo is read-only, otherwise it&#8217;s editable. Every combo takes two arguments. The first argument is a qstrvec_t * to populate the combo with items, while the second one is either an int * or a qstring * to specify the current selection. If the combo is read-only, then the second argument is an int * specifying an index into the qstrvec_t list, otherwise it&#8217;s a qstring * with the current text of the combo. Getting and setting the value of a combo follows the same int/qstring rule.</p>
<p>A multi-line edit is declared in this way:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000080;">&lt;</span>title<span style="color: #008080;">:</span>t<span style="color: #008080;">::</span><span style="color: #007788;">width</span><span style="color: #008080;">::</span><span style="color: #000080;">&gt;</span></pre></td></tr></table></div>

<p>This control is more complex than the combo and requires a structure as argument:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Multi line text control: used to embed a text control in a form</span>
<span style="color: #0000ff;">struct</span> textctrl_info_t
<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">size_t</span>  cb<span style="color: #008080;">;</span>                 <span style="color: #666666;">// size of this structure</span>
   qstring text<span style="color: #008080;">;</span>               <span style="color: #666666;">// in, out: text control value</span>
   <span style="color: #0000ff;">uint16</span>  flags<span style="color: #008080;">;</span>
<span style="color: #339900;">#define TXTF_AUTOINDENT 0x0001 // auto-indent on new line</span>
<span style="color: #339900;">#define TXTF_ACCEPTTABS 0x0002 // Tab key inserts 'tabsize' spaces</span>
<span style="color: #339900;">#define TXTF_READONLY   0x0004 // text cannot be edited (but can be selected and copied)</span>
<span style="color: #339900;">#define TXTF_SELECTED   0x0008 // shows the field with its text selected</span>
<span style="color: #339900;">#define TXTF_MODIFIED   0x0010 // gets/sets the modified status</span>
<span style="color: #339900;">#define TXTF_FIXEDFONT  0x0020 // the control uses IDA's fixed font</span>
   <span style="color: #0000ff;">uint16</span>  tabsize<span style="color: #008080;">;</span>            <span style="color: #666666;">// how many spaces a single tab will indent</span>
   textctrl_info_t<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span> cb<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>textctrl_info_t<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>, flags<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>, tabsize<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Most of the fields are self-evident, but two flags require an explanation. <strong>TXTF_ACCEPTTABS</strong> tells the control that the user can use tabs and they will be converted to the number of spaces specified by <strong>tabsize</strong>. <strong>TXTF_AUTOINDENT</strong> enables auto-indentation, not only when return is pressed, but also by removing tab block spaces on backspace. This feature is very useful when you want code to be entered for instance. In fact, the new script dialog in IDA 6.2 was created by using it.</p>
<p><img src="http://www.hexblog.com/wp-content/uploads/2011/10/scriptdlg.gif" alt="script dialog" /></p>
<p>Don&#8217;t forget the <strong>TXTF_FIXEDFONT</strong> flag to use a fixed font. Here&#8217;s a small sample demonstrating both the combo and edit:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//--------------------------------------------------------------------------</span>
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> idaapi modcb<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> fid, form_actions_t <span style="color: #000040;">&amp;</span>fa<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span> fid <span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">case</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">:</span>
      <span style="color: #008000;">&#123;</span>
        msg<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;The selection of the combo has changed!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #666666;">// set the text of edit to the text of the current combo item</span>
        <span style="color: #0000ff;">int</span> sel<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> fa.<span style="color: #007788;">get_field_value</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span>, <span style="color: #000040;">&amp;</span>sel<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
          qstrvec_t <span style="color: #000040;">*</span>list <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>qstrvec_t <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>fa.<span style="color: #007788;">get_ud</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
          textctrl_info_t ti<span style="color: #008080;">;</span>
          ti.<span style="color: #007788;">flags</span> <span style="color: #000080;">=</span> TXTF_SELECTED<span style="color: #008080;">;</span>
          ti.<span style="color: #007788;">text</span> <span style="color: #000080;">=</span> list<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>at<span style="color: #008000;">&#40;</span>sel<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
          fa.<span style="color: #007788;">set_field_value</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">11</span>, <span style="color: #000040;">&amp;</span>ti<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
      <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">case</span> <span style="color: #0000dd;">11</span><span style="color: #008080;">:</span>
      msg<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;The multi-line edit text has changed!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">//--------------------------------------------------------------------------</span>
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> idaapi run<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> form<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span>
    <span style="color: #FF0000;">&quot;Combo - Multi-line Edit sample<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
    <span style="color: #FF0000;">&quot;%/%*&quot;</span>
    <span style="color: #FF0000;">&quot; &lt;Combo:b10::40::&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
    <span style="color: #FF0000;">&quot; &lt;Text:t11::40::&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
    <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
  qstrvec_t list<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>items<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span>
  <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">&quot;first&quot;</span>, <span style="color: #FF0000;">&quot;second&quot;</span>, <span style="color: #FF0000;">&quot;third&quot;</span>, <span style="color: #FF0000;">&quot;fourth&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> qnumber<span style="color: #008000;">&#40;</span>items<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span>
    list.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>items<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">int</span> sel <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
  textctrl_info_t ti<span style="color: #008080;">;</span>
  ti.<span style="color: #007788;">flags</span> <span style="color: #000080;">=</span> TXTF_SELECTED<span style="color: #008080;">;</span>
  ti.<span style="color: #007788;">text</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;Some text&quot;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> AskUsingForm_c<span style="color: #008000;">&#40;</span>form, modcb, <span style="color: #000040;">&amp;</span>list, <span style="color: #000040;">&amp;</span>list, <span style="color: #000040;">&amp;</span>sel, <span style="color: #000040;">&amp;</span>ti<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    msg<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Combo selection: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, list<span style="color: #008000;">&#91;</span>sel<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    msg<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Text: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, ti.<span style="color: #007788;">text</span>.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In this small sample you can see another small addition to the forms syntax: following the usual <strong>%/</strong> to specify the callback of the form there&#8217;s a <strong>%*</strong>. This new sequence makes it possible to specify a user data pointer (&#038;list), which can later on be retrieved from the callback like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">qstrvec_t <span style="color: #000040;">*</span>list <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>qstrvec_t <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>fa.<span style="color: #007788;">get_ud</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Both the combo box and the multi-line edit can be used in the text version of IDA as well.</p>
<h2>Timers</h2>
<p>Timers are very easy to use and are available in the text version of IDA as well. These are the prototypes of the functions:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">qtimer_t register_timer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> interval, <span style="color: #0000ff;">int</span> <span style="color: #008000;">&#40;</span>idaapi <span style="color: #000040;">*</span>callback<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>ud<span style="color: #008000;">&#41;</span>, <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>ud<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">bool</span> unregister_timer<span style="color: #008000;">&#40;</span>qtimer_t t<span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<p>Timer functions are thread-safe and the callback is executed in the context of the main thread. The callback can return -1 to unregister the timer, while any other return greater than or equal to 0 defines the new interval of the timer.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// the timer event message will be displayed 5 times</span>
<span style="color: #0000ff;">int</span> idaapi timer_callback<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  msg<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;timer event<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #000040;">++</span>i <span style="color: #000080;">==</span> <span style="color: #0000dd;">5</span> <span style="color: #008080;">?</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">1000</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// register the timer with a 1-second interval</span>
register_timer<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span>, timer_callback, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>That&#8217;s all! You can download both code viewer plugin samples from <a href="http://www.hexblog.com/wp-content/uploads/2011/10/codeviewer_samples.zip" title="code viewer samples">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=536</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New features in Hex-Rays Decompiler 1.6</title>
		<link>http://www.hexblog.com/?p=544</link>
		<comments>http://www.hexblog.com/?p=544#comments</comments>
		<pubDate>Mon, 10 Oct 2011 19:19:52 +0000</pubDate>
		<dc:creator>Igor Skochinsky</dc:creator>
				<category><![CDATA[Decompilation]]></category>
		<category><![CDATA[hexrays]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=544</guid>
		<description><![CDATA[Last week we released IDA 6.2 and Hex-Rays Decompiler 1.6. Many of the new IDA features have been described in previous posts, but there have been notable additions in the decompiler as well. They will let you make the decompilation &#8230; <a href="http://www.hexblog.com/?p=544">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Last week we released IDA 6.2 and Hex-Rays Decompiler 1.6. Many of the new IDA features have been described in previous posts, but there have been notable additions in the decompiler as well. They will let you make the decompilation cleaner and closer to the original source. However, it might be not very obvious how to use some of them, so we will describe them in more detail.</p>
<h3>1. Variable mapping</h3>
<p>This is probably the simplest new feature and can be used without any extra preparation.</p>
<p>Sometimes the compiler stores the same variable in several places (e.g. a register and a stack slot). While the decompiler often manages to combine such locations, sometimes it&#8217;s not able to prove that they always contain the same value (especially in presence of calls that take address of stack variables). In such cases the user can help by performing such a merge or mapping manually.</p>
<p>Consider the following very common case:</p>
<p>
<pre><span style="font-family: fixedsys; white-space: pre; color: blue"><span style="color: navy">int __stdcall SciFreeFilterInstance(_FILTER_INSTANCE *pFilterInstance)
</span><span style="color: navy">{
  <span style="color: gray">_FILTER_INSTANCE *v1</span><span style="color: navy">; </span><span style="color: gray">// </span><span style="color: #8080ff">esi@1</span></span>

  <span style="color: #8080ff">v1 </span><span style="color: navy">= </span><span style="color: #8080ff">pFilterInstance</span><span style="color: navy">;
  if ( </span><span style="color: #8080ff">pFilterInstance</span><span style="color: navy">-&gt;Signature != 'FrtS' )
    </span><span style="color: #ff00ff">RtlAssert</span><span style="color: navy">(
      </span><span style="color: green">&quot;(pFilterInstance)-&gt;Signature==SIGN_FILTER_INSTANCE&quot;</span><span style="color: navy">,
      </span><span style="color: green">&quot;d:\\xpsprtm\\drivers\\wdm\\dvd\\class\\codinit.c&quot;</span><span style="color: navy">,
      0x17A2u,
      0);
  </span>StreamClassDebugPrint<span style="color: navy">(2, </span><span style="color: green">"Freeing filterinstance %p still open streams\n"</span><span style="color: navy">, </span><span style="color: #8080ff">v1</span><span style="color: navy">);</span>
</span></pre>
<p><p>The compiler copied an incoming argument (<tt>pFilterInstance</tt>) into a register (<tt>v1==esi</tt>). To get rid of the extra name, right-click the left-hand variable and choose &#8220;Map to another variable&#8221;, or place cursor on it and press &#8216;=&#8217;:</p>
<p><a href="http://www.hexblog.com/wp-content/uploads/2011/10/mapvar2.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="mapvar2" border="0" alt="mapvar2" src="http://www.hexblog.com/wp-content/uploads/2011/10/mapvar2_thumb.png" width="292" height="248"></a></p>
<p>Choose the right-hand variable from the list.</p>
<p><a href="http://www.hexblog.com/wp-content/uploads/2011/10/mapvar3.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="mapvar3" border="0" alt="mapvar3" src="http://www.hexblog.com/wp-content/uploads/2011/10/mapvar3_thumb.png" width="517" height="182"></a></p>
<p>Once decompilation is refreshed, both the left-hand variable (v1) and the assignment are gone. Now we have only one variable &#8211; the incoming argument.</p>
<p>
<pre><span style="white-space: pre; font-family: Fixedsys; color: blue;"><span style="color:navy">int __stdcall SciFreeFilterInstance(_FILTER_INSTANCE *pFilterInstance)
</span><span style="color:navy">{</span>
  <span style="color:navy">if ( </span><span style="color:#8080ff">pFilterInstance</span><span style="color:navy">-&gt;Signature != &#039;FrtS&#039; )
    </span><span style="color:#ff00ff">RtlAssert</span><span style="color:navy">(
      &quot;</span><span style="color:green">(pFilterInstance)-&gt;Signature==SIGN_FILTER_INSTANCE&quot;</span><span style="color:navy">,
      &quot;</span><span style="color:green">d:\\xpsprtm\\drivers\\wdm\\dvd\\class\\codinit.c&quot;</span><span style="color:navy">,
      0x17A2u,
      0);
  </span>StreamClassDebugPrint<span style="color:navy">(2, &quot;</span><span style="color:green">Freeing filterinstance %p still open streams\n&quot;</span><span style="color:navy">,
    </span><span style="color:#8080ff">pFilterInstance</span><span style="color:navy">);</span></span></pre>
</p>
<p>You can map several variables to the same name, if necessary.</p>
<p>Made a mistake or mapped too much? It&#8217;s simple to fix. Right-click the wrongly mapped name and choose &#8220;Unmap variables&#8221;. Then choose the variable you want to see again.</p>
<h3>2. Union selection.</h3>
<p>This feature, naturally, only applies to unions. That means that you need to have union types in your database and assign the types to some variables or fields.</p>
<p>Normally the decompiler tries to choose a union field which matches the expression best, but sometimes there are several equally valid matches, and sometimes other types in the expression are wrong. In such cases, you can override the decompiler&#8217;s decision. For example, this code is common in Windows drivers:</p>
<p>
<pre><span style="white-space: pre; font-family: Fixedsys; color: blue;"><span style="color:navy">NTSTATUS __stdcall DispatchDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
  </span><span style="color:gray">PIO_STACK_LOCATION stacklocation</span><span style="color:navy">; </span><span style="color:gray">// </span><span style="color:#8080ff">ebx@1

  </span><span style="color:#8080ff">stacklocation </span><span style="color:navy">= </span><span style="color:#8080ff">Irp</span><span style="color:navy">-&gt;Tail.Overlay.CurrentStackLocation;
  if ( *&amp;</span><span style="color:#8080ff">stacklocation</span><span style="color:navy">-&gt;Parameters.Create.FileAttributes == 0x224010 )
  {
    </span><span style="color:#8080ff">v8 </span><span style="color:navy">= </span><span style="color:#8080ff">stacklocation</span><span style="color:navy">-&gt;Parameters.Create.Options == 20;
    if ( !</span><span style="color:#8080ff">v8 </span><span style="color:navy">)
      goto LABEL_18;
    if ( </span><span style="color:#8080ff">stacklocation</span><span style="color:navy">-&gt;Parameters.Create.SecurityContext &lt; 1 )
      goto LABEL_87;
    </span><span style="color:#8080ff">v23 </span><span style="color:navy">= </span><span style="color:#8080ff">Irp</span><span style="color:navy">-&gt;AssociatedIrp.MasterIrp;</span></span></pre>
</p>
<p>Since we know we&#8217;re in a DeviceControl handler, it&#8217;s likely the code is inspecting the Parameters.DeviceIoControl substructure and not Parameters.Create.</p>
<p>Right-click the field and choose &#8220;Select union field&#8221;, or place cursor on it and press Alt-Y.</p>
<p><a href="http://www.hexblog.com/wp-content/uploads/2011/10/selunion2.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="selunion2" border="0" alt="selunion2" src="http://www.hexblog.com/wp-content/uploads/2011/10/selunion2_thumb.png" width="429" height="170"></a></p>
<p>Choose the Parameters.DeviceIoControl.IoControlCode field.</p>
<p><a href="http://www.hexblog.com/wp-content/uploads/2011/10/selunion3.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="selunion3" border="0" alt="selunion3" src="http://www.hexblog.com/wp-content/uploads/2011/10/selunion3_thumb.png" width="615" height="297"></a></p>
<p>Other references to Parameters.Create can be fixed the same way. The updated decompilation makes more sense:</p>
<pre><span style="white-space: pre; font-family: Fixedsys; color: blue;"><span style="color:navy">NTSTATUS __stdcall DispatchDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
  </span><span style="color:gray">PIO_STACK_LOCATION stacklocation</span><span style="color:navy">; </span><span style="color:gray">// </span><span style="color:#8080ff">ebx@1

  </span><span style="color:#8080ff">stacklocation </span><span style="color:navy">= </span><span style="color:#8080ff">Irp</span><span style="color:navy">-&gt;Tail.Overlay.CurrentStackLocation;
  if ( </span><span style="color:#8080ff">stacklocation</span><span style="color:navy">-&gt;Parameters.DeviceIoControl.IoControlCode == 0x224010 )
  {
    </span><span style="color:#8080ff">v8 </span><span style="color:navy">= </span><span style="color:#8080ff">stacklocation</span><span style="color:navy">-&gt;Parameters.DeviceIoControl.InputBufferLength == 20;
    if ( !</span><span style="color:#8080ff">v8 </span><span style="color:navy">)
      goto LABEL_18;
    if ( </span><span style="color:#8080ff">stacklocation</span><span style="color:navy">-&gt;Parameters.DeviceIoControl.OutputBufferLength &lt; 1 )
      goto LABEL_87;</span></span></pre>
</p>
<h3>3. CONTAINING_RECORD macro</h3>
<p><a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff542043">This macro</a> is commonly use in Windows drivers to get a pointer to the parent structure when we have a pointer to one of its fields.</p>
<p>For example, consider these two structures, used in a driver:</p>
<pre>struct _HW_STREAM_OBJECT {
  ULONG  SizeOfThisPacket;
  ULONG  StreamNumber;
  PVOID  HwStreamExtension;
  ...
} HW_STREAM_OBJECT, *PHW_STREAM_OBJECT;

struct _STREAM_OBJECT
{
  _COMMON_OBJECT ComObj;
  _FILE_OBJECT *FilterFileObject;
  _FILE_OBJECT *FileObject;
  _FILTER_INSTANCE *FilterInstance;
  <strong>_HW_STREAM_OBJECT HwStreamObject;</strong>
  ...
};</pre>
</p>
<p>The following function accepts a pointer to _HW_STREAM_OBJECT:</p>
<p>
<pre>void __cdecl StreamClassStreamNotification(
  int NotificationType,
  _HW_STREAM_OBJECT *StreamObject,
  _HW_STREAM_REQUEST_BLOCK *pSrb,
  _KSEVENT_ENTRY *EventEntry,
  GUID *EventSet,
  ULONG EventId);</pre>
</p>
<p>But immediately converts it into the containing _STREAM_OBJECT:</p>
<p>
<pre><span style="white-space: pre; font-family: Fixedsys; color: blue;"><span style="color:navy">mov     eax, [ebp+</span><span style="color:green">StreamObject</span><span style="color:navy">]
test    eax, eax
push    ebx
push    esi
lea     esi, [eax-</span>_STREAM_OBJECT.HwStreamObject<span style="color:navy">]</span>
</span></pre>
</p>
<p>Default decompilation doesn&#8217;t look great:</p>
<p>
<pre><span style="font-family: fixedsys; white-space: pre; color: blue"><span style="background: #eeffff">  <span style="color: gray">char *v6</span><span style="color: navy">; </span><span style="color: gray">// </span><span style="color: #8080ff">esi@1</span></span>
  <span style="color: #8080ff">v6 </span><span style="color: navy">= (</span><span style="color: gray">char *</span><span style="color: navy">)&amp;</span><span style="color: #8080ff">StreamObject</span><span style="color: navy">[-2] - 36;</span>
</span></pre>
</p>
<p>There are two ways to make it nicer:</p>
<ol>
<li>Change type of v6 to be _STREAM_OBJECT*. The decompiler will detect that the expression &#8220;lines up&#8221; and convert it to use the macro.
<li>Right-click on the delta being subtracted (-36), select &#8220;Structure offset&#8221; and choose _STREAM_OBJECT from the list. </li>
</ol>
<p>In both cases you should get a nice expression:</p>
<p>
<pre><span style="font-family: fixedsys; white-space: pre; color: blue"><span style="color: #8080ff">  v6 </span><span style="color: navy">= </span>CONTAINING_RECORD<span style="color: navy">(</span><span style="color: #8080ff">StreamObject</span><span style="color: navy">, </span>_STREAM_OBJECT<span style="color: navy">, </span>HwStreamObject<span style="color: navy">);</span></span></pre>
</p>
<p><em>N.B.</em>: currently you need to refresh the decompilation (press F5) to see the changes. We&#8217;ll improve it to happen automatically in future.</p>
<h3>4. Kernel and user-mode macros involving fs segment access.</h3>
<p>On Windows, the <tt>fs</tt> segment is used to store various thread-specific (for user-mode) or processor-specific (for kernel mode) data. Hex-Rays Decompiler 1.6 detects the most common ways of accessing them and converts them to corresponding macros. However, this functionality requires presence of specific types in the database. For user mode, it is the <tt>_TEB</tt> structure, for kernel mode it&#8217;s the <tt>KPCR</tt> structure.</p>
<p>For example, consider the following code:</p>
<p>
<pre><span style="font-family: fixedsys; white-space: pre; color: blue"><span style="color: navy">mov     eax, large fs:</span><span style="color: green">18h
</span><span style="color: navy">mov     eax, [eax+</span><span style="color: green">30h</span><span style="color: navy">]
</span><span style="color: navy">push    </span><span style="color: green">24h
</span><span style="color: navy">push    </span><span style="color: green">8
</span><span style="color: navy">push    dword ptr [eax+</span><span style="color: green">18h</span><span style="color: navy">]
</span><span style="color: navy">call    ds:</span><span style="color: #ff00ff">__imp__RtlAllocateHeap@12 </span><span style="color: gray">; RtlAllocateHeap(x,x,x)
</span><span style="color: navy">mov     esi, eax</span></span></pre>
</p>
<p>If you don&#8217;t have the <tt>_TEB</tt> structure in types, this will be decompiled to:</p>
<p>
<pre><span style="font-family: fixedsys; white-space: pre; color: blue">  <span style="color: #8080ff">v5 </span><span style="color: navy">= </span><span style="color: #ff00ff">RtlAllocateHeap</span><span style="color: navy">(*(</span><span style="color: gray">_DWORD *</span><span style="color: navy">)(*(</span><span style="color: gray">_DWORD *</span><span style="color: navy">)(</span>__readfsdword<span style="color: navy">(24) + 48) + 24), 8, 36);</span></span></pre>
</p>
<p>However, if you do add the type, it will look much nicer:</p>
<p>
<pre><span style="font-family: fixedsys; white-space: pre; color: blue">  <span style="color: #8080ff">v5 </span><span style="color: navy">= </span><span style="color: #ff00ff">RtlAllocateHeap</span><span style="color: navy">(</span>NtCurrentTeb<span style="color: navy">()-&gt;ProcessEnvironmentBlock-&gt;ProcessHeap, 8, 36);</span></span></pre>
</p>
<p>Currently we support the following macros:</p>
<table>
<tbody>
<tr>
<th>Macro</th>
<th>Required types</th>
</tr>
<tr>
<td>NtCurrentTeb</td>
<td>_TEB</td>
</tr>
<tr>
<td>KeGetPcr</td>
<td>KPCR</td>
</tr>
<tr>
<td>KeGetCurrentPrcb</td>
<td>KPCR, KPCRB</td>
</tr>
<tr>
<td>KeGetCurrentProcessorNumber</td>
<td>KPCR</td>
</tr>
<tr>
<td>KeGetCurrentThread</td>
<td>KPCR, _KTHREAD</td>
</tr>
</tbody>
</table>
<p><em>Hint</em>: the easiest way to get <tt>_TEB</tt> or <tt>KPCR</tt> types into your database is using the PDB plugin. Invoke it from File|Load file|PDB file&#8230;, enter a path to kernel32.dll (for user-mode code) or ntoskrnl.exe (for kernel-mode code), and check the &#8220;Types only&#8221; checkbox.</p>
<p><a href="http://www.hexblog.com/wp-content/uploads/2011/10/kernpdb.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="kernpdb" border="0" alt="kernpdb" src="http://www.hexblog.com/wp-content/uploads/2011/10/kernpdb_thumb.png" width="685" height="206"></a></p>
<p>PDBs for those two files usually contain the necessary OS structures.</p>
<p>
<p>
We hope you will like these new additions. Note that the version 1.6 includes even more improvements and fixes, see <a href="http://www.hex-rays.com/products/decompiler/news.shtml#111005">the full list</a> of the new features and <a href="http://www.hex-rays.com/products/decompiler/v16_vs_v15.shtml">the comparison page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=544</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>IDA Pro 6.2 beta</title>
		<link>http://www.hexblog.com/?p=514</link>
		<comments>http://www.hexblog.com/?p=514#comments</comments>
		<pubDate>Mon, 12 Sep 2011 15:05:41 +0000</pubDate>
		<dc:creator>Igor Skochinsky</dc:creator>
				<category><![CDATA[IDA Pro]]></category>
		<category><![CDATA[beta]]></category>
		<category><![CDATA[IDA62]]></category>
		<category><![CDATA[idapro]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=514</guid>
		<description><![CDATA[Soon we are going to start testing the next IDA version. There will be many improvements. Some of them we have mentioned previously: Proximity view PE+ support for Bochs (64-bit PE files) UI shortcut editor Filters in choosers Database snapshots &#8230; <a href="http://www.hexblog.com/?p=514">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Soon we are going to start testing the next IDA version. There will be many improvements. Some of them we have mentioned previously:</p>
<p><a title="New feature in IDA 6.2: The proximity browser" href="http://www.hexblog.com/?p=468">Proximity view</a><br />
<a title="Unpacking mpress’ed PE+ DLLs with the Bochs plugin" href="http://www.hexblog.com/?p=403">PE+ support for Bochs (64-bit PE files)</a><br />
<a title="Filters &amp; Shortcuts" href="http://www.hexblog.com/?p=437">UI shortcut editor</a><br />
<a title="Filters &amp; Shortcuts" href="http://www.hexblog.com/?p=437">Filters in choosers</a><br />
<a title="IDA Pro 6.2 with database snapshots support" href="http://www.hexblog.com/?p=415">Database snapshots</a></p>
<p>Other new major features:</p>
<ul>
<li>GUI installers for Linux and OS X<br />
<a href="http://www.hexblog.com/wp-content/uploads/2011/09/setup_linux.png"><br />
<img class="alignnone size-full wp-image-517" src="http://www.hexblog.com/wp-content/uploads/2011/09/setup_linux.png" alt="" width="510" height="379" /></a><br />
<a href="http://www.hexblog.com/wp-content/uploads/2011/09/setup_osx1-e1315834636840.png"><img class="alignnone size-full wp-image-515" title="IDA Pro OS X setup" src="http://www.hexblog.com/wp-content/uploads/2011/09/setup_osx1-e1315834636840.png" alt="" width="580" height="452" /></a></li>
<li>Automatic check for new versions:<br />
<a href="http://www.hexblog.com/wp-content/uploads/2011/09/autocheck.png"><br />
<img class="alignnone size-full wp-image-518" title="autocheck" src="http://www.hexblog.com/wp-content/uploads/2011/09/autocheck1.png" alt="" width="600" height="344" /></a></li>
<li>Cross-references to structure members:<br />
<a href="http://www.hexblog.com/wp-content/uploads/2011/09/strxref.png"><br />
<img class="alignnone size-full wp-image-519" title="strxref" src="http://www.hexblog.com/wp-content/uploads/2011/09/strxref.png" alt="" width="600" height="368" /></a></li>
<li>Floating licenses: our licensing system is now more flexible and allows big enterprises to purchase floating licenses. Contact <a href="mailto:sales@hex-rays.com">sales@hex-rays.com</a> for more information.</li>
</ul>
<p>If you have an active license and would like to test the beta, please send a message to <a href="&quot;mailto:support@hex-rays.com?subject=IDA%20Pro%206.2%20Beta%20test">support@hex-rays.com</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=514</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Filters &amp; Shortcuts</title>
		<link>http://www.hexblog.com/?p=437</link>
		<comments>http://www.hexblog.com/?p=437#comments</comments>
		<pubDate>Mon, 05 Sep 2011 09:11:26 +0000</pubDate>
		<dc:creator>Daniel Pistelli</dc:creator>
				<category><![CDATA[IDA Pro]]></category>

		<guid isPermaLink="false">http://www.hexblog.com/?p=437</guid>
		<description><![CDATA[Two of the new UI highlights in the upcoming IDA release are filtering capability for choosers and shortcut management. I&#8217;ll be discussing them in this post, although seeing them live in action is much nicer. Filters Filters make it possible &#8230; <a href="http://www.hexblog.com/?p=437">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Two of the new UI highlights in the upcoming IDA release are filtering capability for choosers and shortcut management. I&#8217;ll be discussing them in this post, although seeing them live in action is much nicer. <img src='http://www.hexblog.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h2>Filters</h2>
<p>Filters make it possible to either show, hide or highlight one or more categories of items. But enough talk, let&#8217;s start with a screenshot.</p>
<p><img src="http://www.hexblog.com/wp-content/uploads/2011/08/filters_demo.gif" alt="Filters demo" /><br />
<span id="more-437"></span><br />
This list was created by including only items containing &#8220;str&#8221; and  &#8220;mem&#8221; in their function name and then by highlighting with different colors the remaining items.</p>
<p>Here&#8217;s the dialog which allows to add specific filters for any chooser.</p>
<p><img src="http://www.hexblog.com/wp-content/uploads/2011/08/filters_modify_dialog.gif" alt="Modify filters dialog" /></p>
<p>Don&#8217;t worry about the contrast, the chooser will automatically establish the best foreground color for each item given its background.</p>
<p>However, opening a dialog to set a filter may be cumbersome if the goal is just to quickly visualize a particular category of items. That&#8217;s why it will be possible to filter choosers by pressing a shortcut (Ctrl-F), via a small edit field popping up below the list.</p>
<p><img src="http://www.hexblog.com/wp-content/uploads/2011/08/quick_filter_demo.gif" alt="Quick filter demo" /></p>
<p>This edit field can filter items by additionally specifying three options: case sensitivity, regular expressions and whole words only.</p>
<p><img src="http://www.hexblog.com/wp-content/uploads/2011/08/quick_filter_ctx.gif" alt="Quick filter context menu" /></p>
<p>A lot of rewriting and optimizing had to be done to make sure that even with very huge lists (50,000+ items) the filtering would be fast.</p>
<h2>Shortcut management</h2>
<p>I think many users will appreciate that IDA Pro finally features a complete and advanced shortcut editor.</p>
<p><img src="http://www.hexblog.com/wp-content/uploads/2011/08/shortcut_editor.gif" alt="Shortcut editor" /></p>
<p>It&#8217;s not only possible to change shortcuts for built-in IDA actions, but also the default shortcuts of plugins, external menu entries and scripts. The shortcut editor will signal modified shortcuts (yellow), conflicting shortcuts (red) or whether both conditions are met (orange). It also provides some additional information about the action such as its origin (built-in, menu, plugin, script) and context. The context tells the user when this action can be executed, e.g.: some actions are globally active, while others may only be executed in the disassembly.</p>
<p>One interesting new addition to the action management is that an action now regains its lost shortcut if the conflict which caused the action to lose it ceases to exist.</p>
<p>And in case you wish to migrate your shortcut settings: all the modifications accomplished through the shortcut editor are saved to the &#8216;shortcuts.cfg&#8217; file inside the user&#8217;s directory.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexblog.com/?feed=rss2&#038;p=437</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
