<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Hex blog</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/" />
    <link rel="self" type="application/atom+xml" href="http://hexblog.com/atom.xml" />
   <id>tag:hexblog.com,2010://1</id>
    <updated>2010-08-24T14:57:52Z</updated>
    <subtitle>About IDA Pro, decompilation, programming, binary program analysis, information security.</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type 3.2</generator>
 
<entry>
    <title>Recon 2010: Intro to Embedded Reverse Engineering for PC reversers</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/08/recon_2010_slides.html" />
    <id>tag:hexblog.com,2010://1.127</id>
    <published>2010-08-24T14:11:09Z</published>
    <updated>2010-08-24T14:57:52Z</updated>
    
    <summary>In July I had the honor to speak at the Recon conference in Montreal, Canada. It was my first conference but I really liked the experience. I hope I&apos;ll be able to attend it in future. The presentations were recorded...</summary>
    <author>
        <name>Igor Skochinsky</name>
        <uri>ilfak</uri>
    </author>
            <category term="Security" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>In July I had the honor to speak at the Recon conference in Montreal, Canada. It was my first conference but I really liked the experience. I hope I'll be able to attend it in future.<br />
The presentations were recorded and hopefully will appear on the Recon site soon but for now you can check out the slides (<a href="http://hexblog.com/files/recon%202010%20Skochinsky.odp">ODP</a>, <a href="http://hexblog.com/files/recon%202010%20Skochinsky.pdf">PDF</a>). I have also uploaded some of the tools I mentioned, most notably various filesystem extractors compiled for Win32 (<a href="http://hexblog.com/files/recon_2010_tools.zip">download</a>).</p>]]>
        
    </content>
</entry>
<entry>
    <title>Implementing command completion for IDAPython</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/07/python_command_completion.html" />
    <id>tag:hexblog.com,2010://1.126</id>
    <published>2010-07-19T14:12:29Z</published>
    <updated>2010-07-19T21:48:14Z</updated>
    
    <summary>In this blog post we are going to illustrate how to use the command line interpreter (CLI) interface from Python and how to write a basic command completion functionality for the Python CLI....</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>In this blog post we are going to illustrate how to use the command line interpreter (CLI) interface from Python and how to write a basic command completion functionality for the Python CLI.</p>]]>
        <![CDATA[<h2>Understanding the CLI structure</h2>
IDA Pro SDK allows programmers to implement command line interpreters with the use of the <b>cli_t</b> structure:

<pre><blockquote style="background-color:lightblue">struct cli_t
{
  size_t size;                  // Size of this structure
  int32 flags;                  // Feature bits
  const char *sname;            // Short name (displayed on the button)
  const char *lname;            // Long name (displayed in the menu)
  const char *hint;             // Hint for the input line
  // callback: the user pressed Enter
  bool (idaapi *execute_line)(const char *line);

  // callback: the user pressed Tab (optional)
  bool (idaapi *complete_line)(
        qstring *completion,
        const char *prefix,
        int n,
        const char *line,
        int prefix_start);

  // callback: a keyboard key has been pressed (optional)
  bool (idaapi *keydown)(
        qstring *line,
        int *p_x,
        int *p_sellen,
        int *vk_key,
        int shift);
};
</blockquote></pre>

For example, the IDAPython plugin defines its CLI like this:

<pre><blockquote style="background-color:lightblue">static const cli_t cli_python =
{
    sizeof(cli_t),
    0,
    "Python",
    "Python - IDAPython plugin",
    "Enter any Python expression",
    IDAPython_cli_execute_line,
    NULL, // No completion support
    NULL // No keydown support
};
</blockquote></pre>

And registers/unregisters it with:
<pre><blockquote style="background-color:lightblue">void install_command_interpreter(const cli_t *cp);
void remove_command_interpreter(const cli_t *cp);
</blockquote></pre>

<h2>The CLI in Python</h2>

The CLI functionality has been added in <a href="http://code.google.com/p/idapython/source/detail?r=315" target="_blank">IDAPython 1.4.1</a>. Let us suppose that we want to reimplement the Python CLI in Python (rather than in C++), we would do it like this:

<pre><blockquote style="background-color:lightblue">class pycli_t(idaapi.<b>cli_t</b>):
    flags = 0
    sname = &quot;PyPython&quot;
    lname = &quot;Python - PyCLI&quot;
    hint  = &quot;Enter any Python statement&quot;

    def <b>OnExecuteLine</b>(self, line):
        &quot;&quot;&quot;
        The user pressed Enter.
        @param line: typed line(s)
        @return Boolean: True-executed line, False-ask for more lines
        &quot;&quot;&quot;
        try:
            <b>exec</b>(line, globals(), globals())
        except Exception, e:
            print str(e) + &quot;\n&quot; + traceback.format_exc()
        return True

</blockquote></pre>

Summary:
<ol>
    <li>Subclass <b>idaapi.cli_t</b>
    <li>Define the CLI short name, long name and hint
    <li>Declare the OnExecuteLine handler and use Python's exec() to execute a line
</ol>

To try this code, simply instantiate a CLI and register it:
<pre><blockquote style="background-color:lightblue">pycli = pycli_t()
pycli.register()
</blockquote></pre>

And later to unregister the CLI:
<pre><blockquote style="background-color:lightblue">pycli.unregister()</blockquote></pre>

<h3>Adding command completion</h3>

In order to add command completion, we need to implement the OnCompleteLine() callback:

<pre><blockquote style="background-color:lightblue">class cli_t:
    def <b>OnCompleteLine</b>(self, prefix, n, line, prefix_start):
        &quot;&quot;&quot;
        The user pressed Tab. Find a completion number N for prefix PREFIX

        This callback is optional.

        @param prefix: Line prefix at prefix_start (string)
        @param n: completion number (int)
        @param line: the current line (string)
        @param prefix_start: the index where PREFIX starts in LINE (int)

        @return: None or a String with the completion value
        &quot;&quot;&quot;
        print &quot;OnCompleteLine: pfx=%s n=%d line=%s pfx_st=%d&quot; % (prefix, n, line, prefix_start)
        return None
</blockquote></pre>

This callback is invoked everytime the user presses TAB in the CLI. The callback receives:
<ul>
    <li>prefix: the prefix at the cursor position
    <li>n: the completion number. If this callback succeeded the first time (when n == 0), then IDA will call this callback again with n=1 (and so on) asking for the next possible completion value
    <li>line: the whole line
    <li>prefix_start: the index of the prefix in the line
</ul>

For example typing "os.path.ex" and pressing TAB would call:
<pre><blockquote style="background-color:lightblue">OnCompleteLine(prefix=&quot;ex&quot;, n=0, line=&quot;print os.path.ex&quot;, prefix_start=14)</blockquote></pre>

For demonstration purposes, here is a very simple algorithm to guess what could complete the "os.path.ex" expression:
<ol>
  <li>Parse the identifier: Since IDA passes the line, prefix and the prefix start index, we need to get the whole expression including the prefix (we need &quot;os.path.ex&quot;).
  This can be done by scanning backwards from prefix_start until we encounter a non identifier character:
<pre><blockquote style="background-color:lightblue">def parse_identifier(line, prefix, prefix_start):
    id_start = prefix_start
    while id_start &gt; 0:
        ch = line[id_start]
        if not ch.isalpha() and ch != '.' and ch != '_':
            id_start += 1
            break
        id_start -= 1
    return line[id_start:prefix_start + len(prefix)]
</blockquote></pre>
  <li>Fetch the attributes with dir(): Given the full identifier name (example &quot;os.path.ex&quot;), we split the name by '.' and use a getattr() loop starting from the __main__ module up to the last split value:
<pre><blockquote style="background-color:lightblue">def get_completion(id, prefix):
    try:
        parts = id.split('.')
        m = sys.modules['__main__']
        for i in xrange(0, len(parts)-1):
            m = getattr(m, parts[i])
    except Exception, e:
        return None
    else:
        completion = [x for x in <b>dir</b>(m) if x.<b>startswith</b>(prefix)]

        return completion if len(completion) else None
</blockquote></pre>
<li>Implementing OnCompleteLine(): We parse the identifier and get a list of possible completion values only if n==0, otherwise we return the next possible value in the list we previously built:
<pre><blockquote style="background-color:lightblue">def OnCompleteLine(self, prefix, n, line, prefix_start):
    # new search?
    if n == 0:
        self.n = n
        id = self.<b>parse_identifier</b>(line, prefix, prefix_start)
        self.completion = self.<b>get_completion</b>(id, prefix)
    return <b>None</b> if (self.completion is None) or (n &gt;= len(self.completion)) else <b>self.completion[n]</b>
</blockquote></pre>
</ol>

With this approach we could complete something like this:
<pre><blockquote style="background-color:lightblue">f = open('somefile.txt', 'r')
f.re^TAB =&gt; f.read, f.readinto, f.readline or f.readlines</blockquote></pre>
Or:
<pre><blockquote style="background-color:lightblue">print idau^TAB.GetRe^TAB =&gt; print idautils.GetRegisterList</blockquote></pre>

The sample script can be downloaded from <a href="http://hexblog.com/ida_pro/files/pycli.py">here</a> and a win32 build of the latest IDAPython plugin (with completion integrated in the plugin) can be downloaded from <a href="http://code.google.com/p/idapython/downloads/detail?name=idapython-1.4.1_ida5.7_py2.5_win32.zip">here</a>.]]>
    </content>
</entry>
<entry>
    <title>Running scripts from the command line with idascript</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/07/running_scripts_from_the_comma.html" />
    <id>tag:hexblog.com,2010://1.125</id>
    <published>2010-07-08T13:44:54Z</published>
    <updated>2010-07-08T16:48:32Z</updated>
    
    <summary>In this blog post we are going to demonstrate how the &apos;-S&apos; and &apos;-t&apos; switches (that were introduced in IDA Pro 5.7) can be used to run IDC, Python or other supported scripts from the command line as if they...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>In this blog post we are going to demonstrate how the '-S' and '-t' switches (that were introduced in <a href="http://www.hex-rays.com/idapro/57/index.htm" target="_blank">IDA Pro 5.7</a>) can be used to run IDC, Python or other supported scripts from the command line as if they were standlone scripts and how to use the <b>idascript</b> utility</p>
<img src="http://hexblog.com/ida_pro/pix/idascript_intro.gif"><br/>
]]>
        <![CDATA[<h2>Background</h2>

In order to run a script from the command line, IDA Pro needs to know which script to launch. We can specify the script and its argument via the "-S" switch:

<pre><blockquote style="background-color:lightblue">idag <b>-Sfirst.idc</b> mydatabase.idb</blockquote></pre>

Or:
<pre><blockquote style="background-color:lightblue">idag <b>-S&quot;first.idc <i>arg1</i> <i>arg2</i>&quot;</b> mydatabase.idb</blockquote></pre>

In case the script does not require a database (for example, it works with the debugger and attaches to existing processes), then IDA Pro will be satisfied with the &quot;-t&quot; (create a temporary database) switch:

<pre><blockquote style="background-color:lightblue">idag -S&quot;first.idc <i>arg1</i> <i>arg2</i>&quot; <b>-t</b></blockquote></pre>

Where <b>first.idc</b>:
<pre><blockquote style="background-color:lightblue">#include &lt;idc.idc&gt;

static main()
{
  Message(&quot;Hello world from IDC!\n&quot;);
  return 0;
}</blockquote></pre>

If we run IDA Pro with the following command:
<pre><blockquote style="background-color:lightblue">idag -Sfirst.idc -t</blockquote></pre>

We notice two things:
<ol>
<li>Nothing is printed in the console window: This is because the message will show in the output window instead:<br/>
<img src="http://hexblog.com/ida_pro/pix/idascript_first.gif"><br/><br/>
(It is possible to save all the text in the output window by using the <b>IDALOG</b> environment variable.)<br/><br/>
<li>IDA Pro remains open and does not close: To exit IDA Pro when the script finishes, use Exit() IDC function.
</ol>

In the following section, we will address those two problems with <i>idascript.idc</i> and <i>idascript.py</i> helper scripts and the <i>idascript</i> utility.

<h2>Running scripts from the command line</h2>

<p>In order to print to the console window, we will not use IDC / Message() instead we will write to a file and when IDA Pro exits we will display the contents of that file.</p>


Our second attempt with <b>second.idc</b>:
<pre><blockquote style="background-color:lightblue">extern g_idcutil_logfile;
static LogInit()
{
  g_idcutil_logfile = fopen(&quot;<b>idaout.txt</b>&quot;, &quot;w&quot;);
  if (g_idcutil_logfile == 0)
    return 0;
  return 1;
}

static LogWrite(str)
{
  if (g_idcutil_logfile != 0)
    return fprintf(g_idcutil_logfile, &quot;%s&quot;, str);
  return -1;
}

static LogTerm()
{
  if (g_idcutil_logfile == 0)
    return;
  fclose(g_idcutil_logfile);
  g_idcutil_logfile = 0;
}

static main()
{
  LogInit(); // Open log file
  LogWrite(&quot;Hello world from IDC!\n&quot;); // Write to log file
  LogTerm(); // Close log file

  Exit(0); // Exit IDA Pro
}</blockquote></pre>

Now let us run IDA Pro:

<pre><blockquote style="background-color:lightblue">idag -Ssecond.idc -t
</blockquote></pre>
and type afterwards:
<pre><blockquote style="background-color:lightblue">type idaout.txt</blockquote></pre>

to get the following output:
<pre><blockquote style="background-color:lightblue">Hello world from IDC!</blockquote></pre>

<p>To simplify this whole process, we wrote a small win32 command line utility called <b>idascript:</b>
<pre><blockquote style="background-color:lightblue">IDAScript 1.0 (c) Hex-Rays - A tool to run IDA Pro scripts from the command line

It can be used in two modes:

a) With a database:

        idascript database.idb script.(idc|py|...) [arg1 [arg2 [arg3 [...]]]]

b) With a temporary database:

        idascript script.(idc|py|...) [arg1 [arg2 [arg3 [...]]]]
</blockquote></pre>
</p>
Since we will be using LogInit(), LogTerm(), LogWrite() and other helper functions over and over, we moved those common functions to <b>idascript.idc</b>.<br/>

<p>The script <b>first.idc</b> can now be rewritten like this:</p>
<pre><blockquote style="background-color:lightblue">#include &lt;idc.idc&gt;
#include &quot;idascript.idc&quot;

static main()
{
  InitUtils(); // calls LogInit()

  Print((&quot;Hello world from IDC!\n&quot;)); // Macro that calls LogWrite()

  Quit(0); // calls LogTerm() following by Exit()
}
</blockquote></pre>

As for IDAPython, we wrote a small class to redirect all output to <b>idaout.txt</b>:
<pre><blockquote style="background-color:lightblue">import sys

class ToFileStdOut(object):
    def __init__(self):
        self.outfile = open(&quot;<b>idaout.txt</b>&quot;, &quot;w&quot;)

    def write(self, text):
        self.outfile.write(text)

    def flush(self):
        self.outfile.flush()

    def isatty(self):
        return False

    def __del__(self):
        self.outfile.close()

sys.stdout = sys.stderr = ToFileStdOut()
</blockquote></pre>

Thus, <b>hello.py</b> can be written like this:
<pre><blockquote style="background-color:lightblue">import idc
<b>import idascript
</b>
print &quot;Hello world from IDAPython\n&quot;
for i in xrange(1, len(<b>idc.ARGV</b>)):
    print &quot;ARGV[%d]=%s&quot; % (i, idc.ARGV[i])

idc.Exit(0)</blockquote></pre>
<h2>Sample scripts</h2>

<h3>Process list</h3>

The sample script <b>listprocs.idc</b> will enumerate all processes and display their ID and name:
<pre><blockquote style="background-color:lightblue">#include &lt;idc.idc&gt;
#include &lt;idascript.idc&gt;

static main()
{
  InitUtils();

  <b>LoadDebugger</b>(&quot;win32&quot;, 0);
  auto q = <b>GetProcessQty</b>(), i;

  for (i=0;i&lt;q;i++)
    Print((&quot;[%08X] %s\n&quot;, <b>GetProcessPid</b>(i), <b>GetProcessName</b>(i)));

  Quit(0);
}
</blockquote></pre>

<h3>Kill process</h3>
The <b>killproc.idc</b> script illustrates how to find processes by name and terminate them one by one:

<pre><blockquote style="background-color:lightblue">#include &lt;idc.idc&gt;
#include &quot;idascript.idc&quot;
#include &quot;procutil.idc&quot;

static main()
{
  InitUtils();

  // Load the debugger
  LoadDebugger(&quot;win32&quot;, 0);

  // Get parameters
  if (<b>ARGV</b>.count &lt; 1)
    QuitMsg(0, &quot;Usage: killproc.idc ProcessName\n&quot;);

  auto procs = <b>FindProcessByName</b>(ARGV[1]), i;
  if (procs.count == 0)
    QuitMsg(-1, &quot;No process(es) with name &quot; + ARGV[1]);

  for (i=procs.count-1;i&gt;=0;i--)
  {
    auto pid = procs[i];
    Print((&quot;killing pid: %X\n&quot;, pid));
    <b>KillProcess</b>(pid);
  }

  Quit(0);
}</blockquote></pre>

To test the script, let us suppose we have a few instances of notepad.exe we want to kill:
<pre><blockquote style="background-color:lightblue">D:\idascript&gt;idascript killproc.idc notepad.exe
killing pid: 878
killing pid: 14C8

D:\idascript></blockquote></pre>

<p>We used here the "ARGV" variable that contains all the parameters passed to IDA Pro via the -S switch, FindProcessByName() utility function and KillProcess() (check procutil.idc)</p>

The trick behind terminating a process is to attach and call StopDebugger(). The following is an excerpt from <b>procutil.idc</b> utility script:
<pre><blockquote style="background-color:lightblue">static KillProcess(pid)
{
  if (!AttachToProcess(pid))
    return 0;

  StopDebugger(); // Terminate the current process

  // Normally, we should get a PROCESS_EXIT event
  GetDebuggerEvent(WFNE_SUSP, -1);
}
</blockquote></pre>

<h3>Process information</h3>

The <b>procinfo.idc</b> script will display thread count, register information and the command line arguments of the process in question:

<pre><blockquote style="background-color:lightblue">#include &quot;idascript.idc&quot;
#include &quot;procutil.idc&quot;

static <b>DumpProcessInfo</b>()
{
  // Retrieve command line via Appcall
  Print((&quot;Command line: %s\n&quot;, <b>GetProcessCommandLine</b>()));

  // Enum modules
  Print((&quot;Module list:\n------------\n&quot;));

  auto x;
  for (x = <b>GetFirstModule</b>();x!=BADADDR;x=<b>GetNextModule</b>(x))
    Print((&quot;Module [%08X] [%08X] %s\n&quot;, x, <b>GetModuleSize</b>(x), <b>GetModuleName</b>(x)));

  Print((&quot;\nThread list:\n------------\n&quot;));
  for (x=GetThreadQty()-1;x&gt;=0;x--)
  {
    auto tid = GetThreadId(x);
    Print((&quot;Thread [%x]\n&quot;, tid));
    SelectThread(tid);
    Print((&quot;  EIP=%08X ESP=%08X EBP=%08X\n&quot;, <b>Eip</b>, <b>Esp</b>, <b>Ebp</b>));
  }
}

static main()
{
  InitUtils();

  // Load the debugger
  LoadDebugger(&quot;win32&quot;, 0);

  // Get parameters
  if (ARGV.count &lt; 2)
    QuitMsg(0, &quot;Usage: killproc.idc ProcessName\n&quot;);

  auto procs = FindProcessByName(ARGV[1]), i;
  for (i=procs.count-1;i&gt;=0;i--)
  {
    auto pid = procs[i];
    if (!AttachToProcess(pid))
    {
      Print((&quot;Could not attach to pid=%x\n&quot;, pid));
      continue;
    }
    DumpProcessInfo();
    DetachFromProcess();
  }

  Quit(0);
}</blockquote></pre>

The function <b>GetProcessCommandLine</b> is implemented (using <a href="http://hexblog.com/2010/01/practical_appcall_examples_1.html" target="_blank">Appcall</a>) like this:
<pre><blockquote style="background-color:lightblue">static GetProcessCommandLine()
{
  // Get address of the GetCommandLine API
  auto e, <b>GetCmdLn</b> = LocByName(&quot;kernel32_GetCommandLineA&quot;);

  if (GetCmdLn == BADADDR)
    return 0;

  // Set its prototype for Appcall
  SetType(GetCmdLn, &quot;char * __stdcall x();&quot;);
  try
  {
    // Retrieve the command line using Appcall
    return <b>GetCmdLn()</b>;
  }
  catch (e)
  {
    return 0;
  }
}
</blockquote></pre>

<h3>Extracting function body</h3>

So far we did not really need a specific database to work with. In the following example (<b>funcextract.idc</b>) we will demonstrate how to extract the body of a function from a given database:
<pre><blockquote style="background-color:lightblue">#include &lt;idc.idc&gt;
#include &quot;idascript.idc&quot;

static main()
{
  InitUtils();

  if (<b>ARGV.count</b> &lt; 2)
    QuitMsg(0, &quot;Usage: funcextract.idc FuncName OutFile&quot;);

  // Resolve name
  auto ea = <b>LocByName</b>(ARGV[1]);
  if (ea == BADADDR)
    QuitMsg(0, sprintf(&quot;Function '%s' not found!&quot;, ARGV[1]));

  // Get function start
  ea = <b>GetFunctionAttr</b>(ea, <b>FUNCATTR_START</b>);
  if (ea == BADADDR)
    QuitMsg(0, &quot;Could not determine function start!\n&quot;);

  // size = end - start
  auto sz = <b>GetFunctionAttr</b>(ea, <b>FUNCATTR_END</b>) - ea;

  auto fp = fopen(ARGV[2], &quot;wb&quot;);
  if (fp == 0)
    QuitMsg(-1, &quot;Failed to create output file\n&quot;);

  <b>savefile</b>(fp, 0, ea, sz);
  fclose(fp);

  Print((&quot;Successfully extracted %d byte(s) from '%s'&quot;, sz, ARGV[1]));
  Quit(0);
}
</blockquote></pre>

To test the script, we use idascript utility and pass a database name:
<pre><blockquote style="background-color:lightblue">
D:\idascript><b>idascript</b> <i>ar.idb</i> funcextract.idc start start.bin
Successfully extracted 89 byte(s) from 'start'
D:\idascript></blockquote></pre>

<h3>Other ideas</h3>

There are other ideas that can be implemented to create useful command line tools:
<ul>
  <li>Process memory read/write: Check the <b>rwproc.idc</b> script that allows you to read from the process memory to a file or the other way round.
  <li>Associate .IDC with idascript.exe: This allows you to double-click on IDC scripts to run them from the Windows Explorer
  <li>Scriptable debugger: Write scripts to debug a certain process and extract needed information
  <li>...
</ul>

<h2>Installing the idascript utility</h2>

Please download idascript and the needed scripts from <a href="http://hexblog.com/ida_pro/files/idascript_files.zip">here</a> and follow these steps:

<ol>
  <li>Copy idascript.exe to the installation directory of IDA Pro (say %IDA%)
  <li>Add IDA Pro directory to the PATH environment variable
  <li>Copy idascript.idc and procutil.idc to %IDA%\idc
  <li>Copy idascript.py to %IDA%\python
  <li>Optional: Associate *.idc files with idascript.exe
</ol>

Comments and suggestions are welcome!
]]>
    </content>
</entry>
<entry>
    <title>IDA Pro 5.7 highlights</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/07/ida_pro_57_highlights.html" />
    <id>tag:hexblog.com,2010://1.124</id>
    <published>2010-07-02T16:52:05Z</published>
    <updated>2010-07-24T16:17:29Z</updated>
    
    <summary>We have released a IDA Pro 5.7 few days ago. The complete whatsnew can be found here. In this blog post we will highlight some of the major changes and additions of this release....</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[We have released a IDA Pro 5.7 few days ago. The complete whatsnew can be found <a href="http://www.hex-rays.com/idapro/57/index.htm" target="_blank">here</a>.
In this blog post we will highlight some of the major changes and additions of this release.
]]>
        <![CDATA[<!-- -------------------------------------------------------------------------------- -->
<h2>Debuggers</h2>

Among the various changes and additions to the debugger kernel and modules, we:

<ul>
    <li>added support for MMX/XMM registers:<br/><br/>
    <img src="http://hexblog.com/ida_pro/pix/rel57_dbg_xmm.gif"><br/><br/>
    <li>added more actions to the modules window:<br/><br/>
    <img src="http://hexblog.com/ida_pro/pix/rel57_modcmenu.gif"><br/><br/>
    <ul>
      <li>Load debug symbols: Load additional PDB symbols
      <li>Jump to module base: Jumps to the module base in the current view
      <li>Analyze module: Converts the module segments to non-debugger segments and analyzes the module. Handy when analyzing crashdump files
    </ul><br/>
    <li>added Bochs 2.4.2 support.<br/>Bochs 2.4.2 introduced range read/write physical watchpoints. If a watchpoint was added from the Bochs command line interface IDA Pro will suspend the execution when the <a href="http://bochs.sourceforge.net/doc/docbook/user/internal-debugger.html#AEN3071" target="_blank">watchpoint</a> triggers.
</ul>

<!-- -------------------------------------------------------------------------------- -->
<h3>Bochs Linux debugger plugin</h3>

If you found Bochs debugger plugin useful in the past (e.g. for <a href="http://hexblog.com/2009/09/develop_your_master_boot_recor.html" target="_blank">low level programming</a>, <a href="http://hexblog.com/2008/11/bochs_plugin_goes_alpha.html" target="_blank">malware</a> and code snippet emulation), then you may take advantage of the same functionality under Linux / MacOS.

<p><img src="http://hexblog.com/ida_pro/pix/rel57_bochs_select.gif"/><br/>
(Debugger selection)<br/></p>
<p><img src="http://hexblog.com/ida_pro/pix/rel57_bochs_pemode.gif"/><br/>
(Debugger configuration)</p>
<p><img src="http://hexblog.com/ida_pro/pix/rel57_bochs_running.gif"/><br/>
(Debugger running Under Ubuntu 9 x86)</p>
Please refer to the <a href="http://www.hex-rays.com/idapro/debugger/bochs_linux_primer.pdf">tutorial</a> to learn more how to configure and use the plugin.

<!-- -------------------------------------------------------------------------------- -->
<h3>WinDbg debugger</h3>

<p>Apart from bug fixes and minor speed improvements, we added <a href="http://msdn.microsoft.com/en-us/library/ff552274(VS.85).aspx" target="_blank">non-invasive</a> debugging support. This ability to attach to processes that are already being debugged comes handy when you want to create crashdumps or inspect handles and other kernel objects.</p>

<p>Make sure you enable this option from the Debugger/Debugger Options/Specific debugger options dialog:</p>
<img src="http://hexblog.com/ida_pro/pix/rel57_windbg_noninvasive.gif"><br/>

<p>If you are debugging 64-bit applications using idag64, the Windbg plugin will offer to run the debugger server for you automatically:</p>
<img src="http://hexblog.com/ida_pro/pix/rel57_windbg_serverlaunch.gif"><br/>
<br/>
<img src="http://hexblog.com/ida_pro/pix/rel57_windbg_serverlaunched.gif"><br/>

<p>When the debugger server is no longer needed make sure to terminate it.</p>

<!-- -------------------------------------------------------------------------------- -->
<h2>Scripting</h2>

<!-- -------------------------------------------------------------------------------- -->
<h3>Processor modules and Plugins</h3>
<p>It is now possible to write scriptable <a href="http://hexblog.com/2010/01/pdf_file_loader_to_extract_and_1.html" target="_blank">loaders</a>, <a href="http://hexblog.com/2010/02/scriptable_processor_modules.html" target="_blank">processor modules</a> and <a href="http://hexblog.com/2010/03/scriptable_plugins.html" target="_blank">plugins</a>. If you always wanted your scripts to automatically execute when a database is loaded and unload/deinitialize when the database is closed, then turn your script into a plugin script with just a few additional lines of code.</p>

<p>If we get enough requests about writing debugger modules using scripts, we may add this facility in the future.</p>

<h3>IDAPython improvements</h3>

<p>We refactored and improved the <a href="http://code.google.com/p/idapython/" target="_blank">IDAPython</a> (now version 1.4.0) plugin (and the <i>extlang_t</i> interface by adding new facilities to call object methods, query properties and so on).<br/> This has lead to significant speed gains as demonstrated by <a href="http://blog.zynamics.com/2010/06/29/ida2sql-exporting-ida-databases-to-mysql/" target="_blank">Ero Carrera's</a> blog post.</p>

<p>We also <a href="http://www.hex-rays.com/idapro/idapython_docs/" target="_blank">documented</a> all the manually wrapped functions and utility classes which were poorly documented with the example scripts.</p>

Please refer to the documentation of the pseudo module <a href="http://www.hex-rays.com/idapro/idapython_docs/pywraps-module.html" target="_blank">pywraps</a> for more information.

<h2>The graphical user interface</h2>

We did some last minute changes to the GUI and some of the features described <a href="http://hexblog.com/2010/05/ui_and_scripting_improvements.html">before</a> were changed:
<ul>
  <li>The recent scripts window can be configured to be a dockable window or a modal dialog (check idagui.cfg / RECENT_SCRIPTS_MODAL)
  <li>No need to hold the Alt key in order to jump to identifiers, instead simply double click on it
  <li>Output window is now searchable: use Alt-T to start the search and Ctrl-T to search for the next match
</ul>

<h2>Kernel and processor modules</h2>

<h3>ARM module</h3>
<p>We have added support for almost all ARMv7 instructions, including NEON (aka Advanced SIMD). NEON instructions can be found in the code made for Cortex-A8 processors, such as the one in iPhone 3GS and iPad.</p>

<img src="http://hexblog.com/ida_pro/pix/rel57_arm_neon.gif"><br/>

Because ARM uses new, unified syntax for NEON and VFP (Vector Floating Point) instructions in ARMv7, we use the new syntax if NEON is enabled.
Otherwise we still display old mnemonics for VFP instructions, as they're what most people are used to.<br/>
The only instructions still missing from ARMv7 are ThumbEE instructions which are supposed to be used for JIT compilation of bytecode-based languages. We have not yet encountered any real-life code using it.
<br/><br/>
You can choose which architecture version to use when disassembling ARM code. This can be done interactively in the "Processor-specific options dialog" :<br/><br/>

<img src="http://hexblog.com/ida_pro/pix/rel57_arm_archopts.gif"><br/>

via the command-line:<br/>

<pre>idag -parm:ARMv6T2 firmware.bin</pre>

or by editing IDA.CFG:<br/>

<pre>ARM_DEFAULT_ARCHITECTURE = "ARMv6";</pre>

For ARM Mach-O files or ELF files that include EABI attributes, the architecture version is set automatically from the flags in the file.<br/>
<img src="http://hexblog.com/ida_pro/pix/rel57_arm_flags_elf.gif"><br/><br/>
<img src="http://hexblog.com/ida_pro/pix/rel57_arm_flags_macho.gif"><br/>

<h3>MIPS module</h3>
We have improved the register tracing and now almost all indirect code and data references are recognized. Here's one of the many samples:<br/>

Before:<br/>
<img src="http://hexblog.com/ida_pro/pix/rel57_mips_before.gif"><br/>
After:<br/>
<img src="http://hexblog.com/ida_pro/pix/rel57_mips_after.gif"><br/>
<br/>
We have also added decoding of the MIPS16e instructions jrc, jalrc, save, restore etc.).<br/>
<img src="http://hexblog.com/ida_pro/pix/rel57_mips_mips16e.gif"><br/>

<h3>PC module</h3>
One small but important new feature is the improvement in the parsing of SEH (Structured Exception Handling) in Win32 files.
It is especially useful when disassembling drivers which use SEH extensively.<br/>
<img src="http://hexblog.com/ida_pro/pix/rel57_pc_seh.gif"><br/>
Notice that the finally handler is not converted into a separate function as before (because of the call), but is correctly added to the main function.<br/>

<h3>Python processor modules</h3>
We added two new processor module scripts written entirely in Python. They can be used as a template when developing your own.

<ul>
  <li><b>ebc.py</b>: EFI Byte code processor module:<br/><br/>
  <img src="http://hexblog.com/ida_pro/pix/rel57_ebc.gif"><br/><br/>
  <li><b>msp430.py</b>: MSP430 is a simple 27-instructions 16-bit RISC processor from TI.<br/><br/>
  <img src="http://hexblog.com/ida_pro/pix/rel57_msp430.gif"><br/>
</ul>

<h2>Closing words</h2>

<p>We hope that the new features make your reversing job more easier. Please feel free to send us comments, suggestions and feature requests.</p>

<p>Last but not least, we expect to start the beta testing of the new <a target="_blank" href="http://hexblog.com/2010/03/preview_of_the_next_generation.html">IDA Qt</a> interface soon. If you are interested and have an active IDA Pro license do not hesitate to <a href="mailto:support@hex-rays.com?subject=IDA Qt - Beta  request">contact</a> us.</p>]]>
    </content>
</entry>
<entry>
    <title>Extending IDC and IDAPython</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/06/extending_idc_and_idapython_1.html" />
    <id>tag:hexblog.com,2010://1.123</id>
    <published>2010-06-23T15:30:31Z</published>
    <updated>2010-06-23T16:37:19Z</updated>
    
    <summary>Scripting with IDA Pro is very useful to automate tasks, write scripts or do batch analysis, nonetheless one problem is commonly faced by script writers: the lack of a certain function from the scripting language. In the blog post going...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[Scripting with IDA Pro is very useful to automate tasks, write scripts or do batch analysis, nonetheless one problem is commonly faced by script writers: the lack of a certain function from the scripting language.<br/>

In the blog post going to demonstrate how to extend both IDC and IDAPython to add new functions.<br/>]]>
        <![CDATA[<h2>Extending IDC</h2>

<p>Every IDC variable is represented by an <b>idc_value_t</b> object in the SDK. The IDC variable type is stored in the <b>idc_value-&gt;vtype</b> field and have one of the following values:</p>

<ul>
    <li>VT_LONG: Used to store 32bit signed numbers (or 64bit numbers if __EA64__ is set). Use <b>idc_value-&gt;set_long()</b> to set a value and <b>idc_value-&gt;num</b> to read it.
    <li>VT_STR2: Used to store arbitrary strings. Use <b>idc_value-&gt;set_string()</b> to set a value, <b>idc_value-&gt;c_str()</b> to get a C string or <b>idc_value-&gt;qstr()</b> to get a qstring instance.
    <li>VT_INT64: Used to store 64bit signed numbers. Use <b>idc_value-&gt;i64</b> to read the value and idc_value-&gt;set_int64() to set a value
    <li>VT_OBJ: Used to represent objects. Such idc variable is created via the <b>VarObject()</b> and attributes are managed by <b>VarSetAttr()</b> / <b>VarGetAttr()</b>
    <li>VT_PVOID: Use to store arbitrary untyped values. Use idc_value-&gt;pvoid to read this value and idc_value-&gt;set_pvoid() to set it
</ul>

Now that idc_value_t is covered, let us explain how to add a new IDC function in two steps:

<ol>
  <li>Writing the IDC function callback
  <li>Registering the function
</ol>

<h3>Writing the callback</h3>

The callback is defined as:
<pre><blockquote style="background-color:lightblue">typedef error_t idaapi idc_func_t(idc_value_t *argv,idc_value_t *r);</blockquote></pre>

<p>The <b>argv</b> array is initialized by the IDC interpreter and contains the passed arguments and the <b>r</b> argument is set by the callback to return values to the IDC interpreter.</p>

<h3>Registering an IDC function</h3>

The IDC function callback can be registered with the IDC interpreter using this function:
<pre><blockquote style="background-color:lightblue">bool set_idc_func(const char *name, idc_func_t *fp, const char *args);</blockquote></pre>

Where:
<ul>
  <li>name: designates the IDC function name to be added
  <li>fp: IDC function callback (written in the previous step)
  <li>args: A zero terminated character array containing the expected arguments types (VT_xxx). For example, an IDC function that expects two numbers and one string as arguments have the following args value: <i>{VT_LONG, VT_LONG, VT_STR2, 0}</i>
</ul>

When the plugin is about to unload (in its term() callback), it should unregister the IDC function by calling <b>set_idc_func(name, NULL, NULL)</b>
<h3>Example</h3>

For the sake of demonstration, let us add getenv() to IDC:

<pre><blockquote style="background-color:lightblue">static const char idc_getenv_args[] = { VT_STR2, 0 };
static const char idc_getenv_name[] = &quot;getenv&quot;;

static error_t idaapi idc_getenv(idc_value_t *argv, idc_value_t *res)
{
  char *env = getenv(argv[0].c_str());
  res-&gt;set_string(env == NULL ? &quot;&quot; : env);
  return eOk;
}

int idaapi init()
{
  set_idc_func(idc_getenv_name, idc_getenv, idc_getenv_args );
  return PLUGIN_KEEP;
}

void idaapi term(void)
{
  // Unregister
  set_idc_func(idc_getenv_name, NULL, NULL);
}
</blockquote></pre>

<h2>Extending IDAPython</h2>

It is possible to extend IDAPython in two ways:
<ul>
  <li>Modifying the <a href="http://code.google.com/p/idapython/" target="_blank">source code</a> of IDAPython
  <li>Writing a plugin to extend Python scripting
</ul>

<p>While the former method requires basic knowledge of <a href="http://www.swig.org/">SWIG</a>, both methods require some understanding of the <a href="http://docs.python.org/c-api/" target="_blank">Python C API</a>.<br/>
In this article, we will use the second method because it is more practical and does not require modification to IDAPython itself.</p>

This process involes three steps:
<ol>
  <li>Initializing the Python C API
  <li>Writing the callback(s)
  <li>Registering the function(s)
</ol>

If you're new to the Python C API, you could still follow and understand the code used in this blog post without refering to this <a href="http://docs.python.org/extending/" target="_blank">tutorial</a>.

<h3>Writing the callback</h3>

Let us wrap the following function:
<pre><blockquote style="background-color:lightblue">// ints.hpp
idaman ssize_t ida_export get_predef_insn_cmt(
        const insn_t &cmd,
        char *buf,
        size_t bufsize);
</blockquote></pre>
which is used to retrieve the predifined comment of a given instruction.<br/>
<p>
We want to expose it as the following Python function:</p>
<pre><blockquote style="background-color:lightblue">def get_predef_insn_cmt(ea):
    """Return instruction comments

    @param ea: Instruction ea
    @return: None on failure or a string containing the instruction comment
    """
    pass
 </blockquote></pre></body>

 Here is the callback:
<pre><blockquote style="background-color:lightblue">static PyObject *py_getpredef_insn_cmt(PyObject * /*self*/, PyObject *args)
{
  do 
  {
    // Parse arguments
    unsigned long ea;
    if (<b>!PyArg_ParseTuple</b>(args, &quot;k&quot;, &amp;ea))
      break;

    // Decode instruction
    if (decode_insn(get_screen_ea()) == 0)
      break;

    // Get comments
    char buf[MAXSTR], *p = buf;
    p += qsnprintf(buf, sizeof(buf), &quot;%s: &quot;, ph.instruc[cmd.itype].name);

    if (get_predef_insn_cmt(cmd, p, sizeof(buf) - (p - buf)) == -1 || *p == '\0')
      break;

    // Return comment as a string
    return <b>PyString_FromString</b>(buf);
  } while (false);
  Py_RETURN_NONE;
}
</blockquote></pre>

<ul>
  <li>PyArg_ParseTuple() is used to parse the arguments. This function can be likened to sscanf(). For a description about the format string please refer to the <a href="http://docs.python.org/c-api/arg.html#Py_BuildValue" target="_blank">Py_BuildValue()</a> documentation
  <li>get_predef_insn_cmt() is used to fetch the comment and PyString_FromString() is used to return a string Python object
  <li>In case of failures we use the Py_RETURN_NONE macro to return the Py_NONE value (None in Python)
</ul>

<h3>Registering the callback(s)</h3>
Unlike IDC callback registration, Python C API allows you to register more than a function at once. This is done by describing all the callbacks in a <b>PyMethodDef</b> array:
<pre><blockquote style="background-color:lightblue">static PyMethodDef py_methods[] =
{
  {&quot;<b>getpredef_insn_cmt</b>&quot;,  py_getpredef_insn_cmt, METH_VARARGS, &quot;&quot;},
  {NULL, NULL, 0, NULL}
};
</blockquote></pre>

and then calling the registration function:
<pre><blockquote style="background-color:lightblue">Py_InitModule("idapyext", py_methods);</blockquote></pre>

<p>Finally, to use this function, make sure you "<i>import idapyext</i>" before calling <i>getpredef_insn_cmt</i></p>

<p>The source code used in the blog post can be downloaded from <a href="http://hexblog.com/ida_pro/files/idcpyext.zip">here</a></p>]]>
    </content>
</entry>
<entry>
    <title>UI and scripting improvements</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/05/ui_and_scripting_improvements.html" />
    <id>tag:hexblog.com,2010://1.122</id>
    <published>2010-05-26T14:54:37Z</published>
    <updated>2010-07-24T16:10:22Z</updated>
    
    <summary>In addition to the previously covered features we&apos;ve already added, we took the opportunity to get to the bottom of it and add even more scripting facilities where possible along with minor but convenient UI enhancements. In this blog entry,...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>In addition to the <a href="http://hexblog.com/2010/03/scriptable_plugins.html">previously</a> 
<a href="http://hexblog.com/2010/02/scriptable_processor_modules.html">covered</a> features
we've already added, we took the opportunity to get to the bottom of it and add even more scripting facilities where possible along with minor but convenient UI enhancements.
In this blog entry, we will introduce some of the new features in the coming version of IDA Pro.</p>]]>
        <![CDATA[<h2>Output window</h2>

<p>We received many requests from our clients to make the output window a text control so users select portions of text instead of a whole line.</p>

The output window is now a rich text control instead of a listbox, allowing the users to:
<ul>
  <li>Select or delete portions of text
  <li>Jump to any address from the output by Alt-clicking on it
</ul>
<img src="http://hexblog.com/ida_pro/pix/uiadd_select_out.gif"><br/>

The Alt-click will work not just for numerical addresses but also for identifiers which are location names in the database or even registers during debugging.

<h2>Unified script facilities in the UI</h2>

We have replaced the "File / IDC file" menu with "File / Script file", allowing the user a unified way to run any supported script file.<br/>
IDA Pro will match the file's extension with a registered extlang (or the built-in IDC engine or Python plugin) and run the file with it.<br/>
<p><img src="http://hexblog.com/ida_pro/pix/uiadd_script_menu.gif"></p>

<h2>Added "Recent scripts" window</h2>

<p>We have dropped the "Recent IDC Scripts" toolbar and replaced it with a script list (similar to IDAPython's <b>Alt-7</b>).<br/>
This dialog is common to the text and the graphical interfaces of IDA Pro, and will remember any script (not just IDC). It is accessible via the Windows menu or the <b>Alt-F7</b> hotkey:
<p>
<img src="http://hexblog.com/ida_pro/pix/uiadd_rscript_menu.gif">
</p>

Recent scripts will be displayed in a chooser:<br/>
<p><img src="http://hexblog.com/ida_pro/pix/uiadd_rscript_win.gif"/><br/>

(Note that the history has both Python and IDC scripts)</p>

The standard chooser hotkeys can be used: <b>Ctrl-E</b> to edit the script, <b>Ins</b> to browse for a new script, <b>Del</b> to remove it and <b>Alt-T</b> to search.

<h2>Added new '-t' command line switch</h2>

This switch will start IDA Pro with a temporary database. This is useful if you want to run scripts that do not neccesarily need a specific input file.<br/>

For example, a script can use the debugger to attach to a process, gather some information and detach.<br/>

<p>In the future blog entries we will show how to use this in real life scenarios.</p>

<h2>Improved the '-S' command line switch</h2>

The -S switch can now run not just IDC but any supported script file (again, based on the script file extension).

It now supports passing arguments to the script:
<pre><blockquote style="background-color:lightblue">idag -t -S&quot;hello.idc arg1 arg2 \&quot;arg 3\&quot; arg4&quot;</blockquote></pre>

With <b>hello.idc</b>:

<pre><blockquote style="background-color:lightblue">#include &lt;idc.idc&gt;

static main()
{
  auto i;
  for (i=0;i&lt;ARGV.count;i++)
    Message(&quot;ARG[%d]=%s\n&quot;, i, ARGV[i]);
}
</blockquote></pre>
After IDA Pro runs, such an output will be displayed:

<pre><blockquote style="background-color:lightblue">ARG[0]=hello.idc
ARG[1]=arg1
ARG[2]=arg2
ARG[3]=arg 3
ARG[4]=arg4</blockquote></pre>


In case you're wondering, the same can be accomplished with IDAPython:

<pre><blockquote style="background-color:lightblue">import idc

for i in xrange(0, len(idc.ARGV)):
    print &quot;ARG[%d]=%s&quot; % (i, idc.ARGV[i])
</blockquote></pre>

The ARGV array is stored in the <b>idc</b> module.

<h2>Auto-indent in the script input box</h2>
A small but often irritating pecularity of IDA's script boxes (<b>Shift-F2</b> for IDC and <b>Alt-8</b> for Python) is that it they don't auto-indent.
This is particularly annoying for Python which requires proper indentation.
In the new version we have implemented auto-indent, so writing Python script snippets is not an excercise in frustration anymore.
<br/><br/>
We hope that these new features, while minor, will make your work more productive.]]>
    </content>
</entry>
<entry>
    <title>ARM decompiler beta is coming</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/05/arm_decompiler_beta_is_coming.html" />
    <id>tag:hexblog.com,2010://1.121</id>
    <published>2010-05-12T17:44:33Z</published>
    <updated>2010-05-12T18:05:53Z</updated>
    
    <summary>We have the beta version of the ARM decompiler almost ready! Below is a short demo of how it works now: If you are interested in participating in the beta testing and you have an active x86 decompiler license, please...</summary>
    <author>
        <name>Ilfak Guilfanov</name>
        
    </author>
            <category term="Decompilation" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>We have the beta version of the ARM decompiler almost ready! Below is a short demo of how it works now:<br />
<p><br />
<center><br />
<a href="http://hex-rays.com/video/arm_decompiler_beta.html"><br />
<img src="/decompilation/pix/arm_beta_icon.gif" /><br />
</a><br />
</center><br />
<p><br />
If you are interested in participating in the beta testing and you have an active x86 decompiler license, please send us a message. Thanks!</p>]]>
        
    </content>
</entry>
<entry>
    <title>Kernel debugging with IDA Pro / Windbg plugin and VirtualKd</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/04/kernel_debugging_with_ida_pro_1.html" />
    <id>tag:hexblog.com,2010://1.120</id>
    <published>2010-04-30T10:54:19Z</published>
    <updated>2010-05-25T08:39:17Z</updated>
    
    <summary>The other day we received an email support question asking if IDA Pro / Windbg debugger plugin works with VirtualKd, a tool that allows speeding up (up to 45x) Windows kernel module debugging using VMWare and VirtualBox virtual machines. After...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>The other day we received an email support question asking if IDA Pro / Windbg debugger plugin works with <a href="http://virtualkd.sysprogs.org/" target="_blank">VirtualKd</a>, a tool <i>that allows speeding up (up to 45x) Windows kernel module debugging using VMWare and VirtualBox virtual machines</i>. After we installed and experimented with VirtualKd, our answer was "yes, certainly". This blog entry aims at illustrating how to configure VirtualKd to be used with IDA Pro / Windbg plugin and VMWare.</p>
<img src="http://hexblog.com/ida_pro/pix/kd_cover.gif"><br/>]]>
        <![CDATA[<h2>Installing VirtualKd</h2>
<p>First download the VirtualKd <a href="http://virtualkd.sysprogs.org/#installation" target="_blank">package</a>, unzip its contents and copy the contents of the 'target' folder to the VMWare guest OS and run 'vminstall.exe'. After this step, reboot the guest and run the 'vmmon.exe' (or 'vmmon64.exe') inside the host. If everything is successful, you should see something like this:</p>
<img src="http://hexblog.com/ida_pro/pix/kd_vmon.png"><br/>

<p>Please take note of the 'Pipe name' value as shown in the screenshot. We will use this value when configuring the Windbg plugin.</p>

<h2>Configuring IDA Pro / Windbg plugin</h2>

<p>First, run IDA Pro without an input database and select Debugger / Attach / Windbg plugin:</p>

<img src="http://hexblog.com/ida_pro/pix/kd_attach.png"/><br/>

<p>Next, we get this screen:</p>

<img src="http://hexblog.com/ida_pro/pix/kd_connstr.png"/><br/>

<p>Here we enter a connection string (designating that the com port is a pipe). The pipe name is the same value we read from the 'vmmon' tool from the above steps.</p>

<p>Before pressing OK, we need to make sure that the Windbg plugin is properly configured. Let us verify that by pressing on the 'Debug options' button:</p>
<img src="http://hexblog.com/ida_pro/pix/kd_dbgsetup.png"/><br/>

<p>This dialog is used to configure the debugger in general, it is common to all debugger modules. Since we don't need to change any of those settings now, let us instead select 'Set specific options':</p>

<img src="http://hexblog.com/ida_pro/pix/kd_specopt.png"/><br/>

<p>Make sure that these two options are properly configured:</p>
<ol>
  <li>Kernel debugging is selected
  <li>Debugging tools path is correctly entered. Please note that even if you're debugging an x64 kernel you still need to point to the x86 version of the debugging tools.
</ol>

<p>After we configured everything, simply press OK all the way until the attach dialog shows up:</p>
<img src="http://hexblog.com/ida_pro/pix/kd_attaching.png"/><br/>

<p>Pressing OK one last time will start the debugging session:</p>

<img src="http://hexblog.com/ida_pro/pix/kd_dbg.gif"/><br/>

<p>After debugging for a while, we were really amazed at the speed improvement achieved with the help of VirtualKd (nice work VirtualKd team). Kernel debugging speed using the Windbg plugin is almost comparable to the local win32 debugger plugin.</p>

For a more elaborate tutorial on configuring and using the Windbg plugin please check our <a href="http://www.hex-rays.com/idapro/idasupport.htm" target="_blank">support</a> page.]]>
    </content>
</entry>
<entry>
    <title>Book Review: The Art of Assembly Language, 2nd Edition</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/04/book_review_the_art_of_assembl_1.html" />
    <id>tag:hexblog.com,2010://1.119</id>
    <published>2010-04-28T16:23:36Z</published>
    <updated>2010-05-25T08:39:33Z</updated>
    
    <summary>Have you ever tried to teach x86 assembly language programming to someone coming from high level language programming background and discovered that it was hard? Before being able to write a simple &quot;Hello World&quot; program one needs to know a...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="Programming" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[Have you ever tried to teach x86 assembly language programming to someone coming from high level language programming background and discovered that it was hard?<br/>
<p>
Before being able to write a simple "Hello World" program one needs to know a fair deal about the x86 architecture, the assembler language and the operating system. Obviously this is not the case with high level languages such as C for example.<br/></p>

I was reading <a href="http://nostarch.com/assembly2.htm" target="_blank">The Art of Asssembly Language, 2nd edition</a> book by Randall Hyde the other day and really enjoyed his approach to teaching the assembly language programming.<br/>
<p><img src="http://nostarch.com/images/assembly2_cov.png"></p>]]>
        <![CDATA[In his book, Randall introduces the reader to the <a href="http://webster.cs.ucr.edu/AsmTools/HLA/dnld.html">HLA</a> (High Level Assembler) compiler which will be used as a tool to learn the x86 assembly language.<br/><br/>

The syntax of HLA can be thought of as a hybrid of Pascal and assembly language. Here's a sample "Hello world" HLA program:
<pre><blockquote style="background-color:lightblue">program helloWorld;
#include( &quot;stdlib.hhf&quot; );
begin helloWorld;
    stdout.put( &quot;Hello, World of Assembly Language&quot;, nl );
end helloWorld;
</blockquote></pre>

You may argue that this was not an assembly language program, but what about this:
<pre><blockquote style="background-color:lightblue">program h;
#include(&quot;stdlib.hhf&quot;);

static
    s: string := &quot;Hello World!&quot;;

procedure chksum_str; @returns( &quot;eax&quot; );
begin chksum_str;
    mov(s, edi);
    xor(ebx, ebx);
    xor(eax, eax);
    while( mov( [edi], al ) &lt;&gt; #0 ) do
        inc(edi); // advance str ptr
        add(eax, ebx);
    endwhile;
    mov(ebx, eax);
end chksum_str;

begin h;
  stdout.put(&quot;The checksum of '&quot;, s, &quot; is: 0x&quot;);
  chksum_str();
  stdout.put(eax);
  if ( eax == 1234 ) then
    stdout.put(&quot;Special chksum!&quot;, nl);
  endif;
end h;
</blockquote></pre>

Although this is also not pure assembler syntax, the newcomer will enjoy learning about the x86 architecture and instruction set with the help of the features provided by HLA:
<ul>
  <li>Ability to create user types
  <li>Exception handling
  <li>Control and repetition structures (and other constructs found in high level languages)
  <li>Classes and objects
  <li>Various libaries: stl, file i/o, os, array manipulation, math, etc...
  <li>etc...
</ul>

Throughout the book, before a programming concept is introduced, Randall talks about the necessary background information (architecture and instruction set) and then explains how to put the concept into practive using HLA.<br/>

<p>For example, in Chapter 5 (Procedures and Units), he explains in detail how the stack is set up during a procedure call, how local variables are allocated and how to access the arguments, followed by explanation on how to use HLA to write procedures. Similarly in Chapter 6, when talking about FPU arithmetics, the author carefully explains about the FPU data and controls registers, related instruction set and finally how to use HLA to do FPU arithmetics.</p>

If your aim is to learn assembly language in order to start reverse engineering, this book is probably not for you, however I highly recommend this book for programmers:
<ul>
  <li>That always wanted to learn the x86 assembly language but found it difficult to start with. This book is well organized and easy to read
  <li>That want to teach the basics of the x86 architecture and assembly language
  <li>That want to put together an x86 assembly program quickly. HLA compiler and the built-in libraries give you the convenience of a high-level programming language and the power of a low level language. If you use the standard libraries provided by HLA then your program is portable
</ul>]]>
    </content>
</entry>
<entry>
    <title>Environment variable editor</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/04/environment_variable_editor.html" />
    <id>tag:hexblog.com,2010://1.118</id>
    <published>2010-04-05T15:04:38Z</published>
    <updated>2010-05-10T09:57:10Z</updated>
    
    <summary>Normally, to change environment variables in a running process, one has to terminate the process, edit the environment variables and re-run the process. In this blog entry we are going to write an IDAPython script that allows us to add,...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>Normally, to change environment variables in a running process, one has to terminate the process, edit the environment variables and re-run the process. In this blog entry we are going to write an IDAPython script that allows us to add, edit or delete environment variables in a running process directly. To achieve this we will use <a target="_blank" href="http://hexblog.com/2010/01/introducing_the_appcall_featur_1.html">Appcall</a> to manage the variables and a <a target="_blank" href="http://hexblog.com/2010/03/using_custom_viewers_from_idap.html">custom viewer</a> that serves as the graphical interface.</p>
<p><img alt="envedit.gif" src="http://hexblog.com/ida_pro/pix/envedit.gif" width="672" height="479" /></p>]]>
        <![CDATA[<h2>Background</h2>
In MS Windows, environment variables can be managed using various API calls. For our script we are only interested in 3 calls:
<ul>
  <li><a href="http://msdn.microsoft.com/en-us/library/ms683187(VS.85).aspx">GetEnvironmentStrings</a>: Returns a block of memory pointing to all the environment variables in the process. This block is a list of zero terminated strings, the end of the list is marked with two <b>\0</b> characters (such list is also known as MULTI_SZ)
  <li><a href="http://msdn.microsoft.com/en-us/library/ms683151(VS.85).aspx" target="_blank">FreeEnvironmentStrings</a>: Frees the block returned by GetEnvironmentStrings()
  <li><a href="http://msdn.microsoft.com/en-us/library/ms686206(VS.85).aspx" target="_blank">SetEnvironmentVariable</a>: Creates or edits an environment variable
</ul>

In order to modify the environment variables in a process, we need to issue those API calls in the context of that process. One solution is to inject a DLL into the running process and ask it to call those APIs. Another simpler solution is to write a script that uses Appcall to issue those API calls.

<h2>Setting up Appcall</h2>
Let us use Appcall to retrieve 3 callable objects corresponding to the APIs in question:
<pre><blockquote style="background-color:lightblue">GetEnvironmentStrings = 
    Appcall.proto("kernel32_GetEnvironmentStringsA", 
                  "unsigned int __stdcall GetEnvironmentStrings();")

SetEnvironmentVariable = 
    Appcall.proto("kernel32_SetEnvironmentVariableA", 
                  "int __stdcall SetEnvironmentVariable(const char *lpName, const char *lpValue);")

FreeEnvironmentStrings = 
    Appcall.proto("kernel32_FreeEnvironmentStringsA", 
                  "int __stdcall FreeEnvironmentStrings(unsigned int block);")
</blockquote></pre>
Now we can use those callable objects to call the APIs in the context of the debugged process, for <a name="setexample">example</a>:

<pre><blockquote style="background-color:lightblue"># Add a new environment variable
SetEnvironmentVariable("MY_VAR", "Some value")</blockquote></pre>
Please notes that:
<ul>
  <li>Both GetEnvironmentStrings() and FreeEnvironmentStrings() prototypes are re-declared to accept an <b>unsigned int</b> instead of a character pointer because Appcall does not support the MULTI_SZ type.
  <li>We are using the ASCII version of those APIs. Modifying the script to work with the unicode version is not very difficult to achieve.</p>
</ul>
<!-- ----------------------------------------------------------------------------------------------------------------------------- -->
<h2>Retrieving all environment variables</h2>

We need to write a function to retrieve all the environment variables in a list:
<ol>
  <li>Call the GetEnvironmentStrings() to retrieve a pointer to the environment block in the process. Save that pointer
  <li>Read the debuggee's memory at that block and retrieve the variables accordingly.
  <li>Free the block by calling FreeEnvironmentStrings()
</ol>

To read the debuggee's memory, we can use idc.Bytes(), idaapi.get_many_bytes() or idaapi.dbg_read_memory(). Since we don't know how big the block is, we will read one character at a time and process it.<br/>
For demonstration purposes, we will use yet another mechanism to read from the debuggee's memory as if we were reading from a file. We will use the <b>loader_input_t</b> class followed by a call to <b>open_memory()</b> function:
<pre><blockquote style="background-color:lightblue">def Variables():
    &quot;&quot;&quot;Returns all environment blocks&quot;&quot;&quot;

    # Get all environment strings
    env_ptr = <b>GetEnvironmentStrings</b>()
    if env_ptr == 0:
        return None

    # Always refresh the memory after a call 
    # that could change the memory configuration
    idc.<b>RefreshDebuggerMemory</b>()

    # Open process memory
    f = idaapi.<b>loader_input_t</b>()
    f.<b>open_memory</b>(env_ptr)

    # Parse all environment variables into a list of variables
    vars = []
    var = []
    while True:
        ch = f.<b>get_char</b>()
        if not ch:
            break
        if ch == '\x00':
            # double-null -&gt; end of list
            if not var:
                break
            vars.append(''.join(var))
            var = []
        else:
            var.append(ch)
    f.close()

    # Free the block
    <b>FreeEnvironmentStrings</b>(env_ptr)

    return vars
</blockquote></pre>

<h2>Writing a custom viewer to manage the environment variables</h2>

Now that we have all the required supporting code, we can write a custom viewer that does the following:
<ul>
  <li>Retrieves all the environment variables: call GetEnvironmentStrings() and parse the result
  <ul>
    <li>Parse each environment variable into KEY/VALUE components
    <li>Add a colored line into the custom viewer with different colors for the KEY and VALUE
  </ul>
  <li>Add popup menu entries and handle keypresses to support three operations:
  <ul>
    <li>Insert: Ask the user for a key and value using the <b>idaapi.askstr</b>() then call the SetEnvironmentVariable() API as shown in the <a href="#setexample">previous example</a>
    <li>Edit: Retrieve the cursor position, extract the key name, ask the user for a new value only (w/o asking for a key) and call the SetEnvironmentVariable() API
    <li>Delete: Retrieve the key name and call SetEnvironmentVariable() API with value pointing to NULL
  </ul>
</ul>

Before running the script, make sure that the debugged process is suspended. Right-click on an environment variable to edit/insert or delete.
<p>Please download the script from <a href="http://hexblog.com/ida_pro/files/envedit.py">here</a>.</p>]]>
    </content>
</entry>
<entry>
    <title>Scriptable plugins</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/03/scriptable_plugins.html" />
    <id>tag:hexblog.com,2010://1.117</id>
    <published>2010-03-29T17:28:17Z</published>
    <updated>2010-06-11T11:28:37Z</updated>
    
    <summary>In IDA Pro 5.6 we added support for loader scripts, last month we added processor module scripts support, and now by adding support for scriptable plugins (for the next version of IDA) it will be possible to write all sort...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>In IDA Pro 5.6 we added support for <a href="http://hexblog.com/2010/01/pdf_file_loader_to_extract_and_1.html">loader scripts</a>, last month we added <a href="http://hexblog.com/2010/02/scriptable_processor_modules.html">processor module scripts</a> support, and now by adding support for scriptable plugins (for the next version of IDA) it will be possible to write all sort of IDA Pro extensions using scripting languages.</br>

<p><img src="http://hexblog.com/ida_pro/pix/plgscr_idc.gif" width="525" height="511"/><br/>
(A plugin script written using IDC)</p>]]>
        <![CDATA[<h2>Background</h2>
Apart from writing scripts, writing plugin modules using the SDK is the second easiest extension to code for IDA Pro. In short, plugins are DLLs that export the <b>PLUGIN</b> symbol which is an instance of <b>plugin_t</b> (check loader.hpp):
<ul>
  <li>init/run/term callbacks: These callbacks are called when IDA initializes, invokes and terminates the plugin.
  <li>flags: The plugin flags describe the kind of the plugin. A plugin could have no flags (flags = 0) which means that this is an ordinary plugin. Or the plugin could act as a processor module extension, thus the <b>PLUGIN_PROC</b> flag. Another type of plugins are the debugger plugins which use the <b>PLUGIN_DBG</b> flag.
  <li>Description and hotkey: The remaining <i>plugin_t</i> entries are used to designate a name, help text, comment, and a hotkey for the plugin.
</ul>

The <b>init</b> callback is called when the plugin is loaded by IDA. It decides whether to load the plugin or skip it by returning one of the following constants:
<ul>
  <li>PLUGIN_OK: Plugin agrees to work with the current database but will be loaded only when it is about to be invoked.
  <li>PLUGIN_SKIP: Plugin does not want to work with the current database. For example a plugin may choose to work with certain input file types.
  <li><a name="PLUGIN_KEEP">PLUGIN_KEEP</a>: Plugin agrees to work with the current database and will remain loaded until the database is closed. It is important to return this value if you hook to notification events or register any sort of callbacks.
</ul>

A plugin can be invoked using its hotkey or programmatically using the RunPlugin() (in IDC) or run_plugin() (in the SDK / loader.hpp). Upon invocation, the <b>run</b> callback: 
<pre><blockquote style="background-color:lightblue">int idaapi run(int arg)</blockquote></pre>
is executed. Before explaining the use of the <i>arg</i> argument, it is important to know that plugins can also be configured in the <b>plugins.cfg</b> file, which has the following format:
<pre><blockquote style="background-color:lightblue">; plugin_name     filename     hotkey  arg  [flags]
Extract_File      extract      0       1
Extract_Block     extract      0       2
Extract_Item      extract      Alt-F8  3
</blockquote></pre>
(In this example, the 'extract' plugin provide at least 3 different functionalities depending on the passed argument value)

<p>If a plugin is not present in plugins.cfg and is invoked using the "Edit/Plugins" menu or with the hotkey described in its <i>PLUGIN</i> entry then <i>run()</i> will be invoked with <i>arg=0</i>. Configured plugins, when invoked, will pass to the <i>run()</i> the argument value specified in the configuration file.</p>

<h2>Scriptable plugins</h2>

Scriptable plugins behave exactly like a native plugin (written using the SDK). To write a plugin using a scripting language, declare a function called <b>PLUGIN_ENTRY</b> that returns a <i>plugin_t</i> instance (or an object containing all attributes of a plugin_t object). This is an example script written in IDAPython:
<pre><blockquote style="background-color:lightblue">import idaapi

class myplugin_t(<b>idaapi.plugin_t</b>):
    flags = idaapi.<b>PLUGIN_UNL</b>
    comment = &quot;This is a comment&quot;
    help = &quot;This is help&quot;
    wanted_name = &quot;My Python plugin&quot;
    wanted_hotkey = &quot;Alt-F8&quot;

    def <b>init</b>(self):
        idaapi.msg(&quot;init() called!\n&quot;)
        return idaapi.PLUGIN_OK

    def <b>run</b>(self, arg):
        idaapi.msg(&quot;run() called with %d!\n&quot; % arg)

    def <b>term</b>(self):
        idaapi.msg(&quot;term() called!\n&quot;)

def <b>PLUGIN_ENTRY</b>():
    return myplugin_t()
</blockquote></pre>

In this example, the plugin flag <i>PLUGIN_UNL</i> instructs IDA to directly unload the plugin after it has been invoked. This flag is very useful if your plugin script does a specific task and finishes. On the other hand, if your script works with forms (choosers, custom viewers, etc.), or registers callbacks (hotkeys, event notifications, etc.) then this flag should not be used and <i>init()</i> should return <i>PLUGIN_KEEP</i> (as discussed <a href="#PLUGIN_KEEP">earlier</a>) instead of <i>PLUGIN_OK</i>.

<p>Scriptable plugins are so easy to write, and deploy (a simply copy/paste in the plugins folder) and very useful in case you want your plugin to load and unload automatically like native plugins.</p>]]>
    </content>
</entry>
<entry>
    <title>Using custom viewers from IDAPython</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/03/using_custom_viewers_from_idap.html" />
    <id>tag:hexblog.com,2010://1.116</id>
    <published>2010-03-25T18:37:03Z</published>
    <updated>2010-05-10T09:57:52Z</updated>
    
    <summary>Custom viewers can be used to display arbitrary textual information and can be used in any IDA plugin.They are used in IDA-View, Hex-View, Enum and struct views and the Hex-Rays decompiler. In this blog entry we are going to write...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>Custom viewers can be used to display arbitrary textual information and can be used in any IDA <a href="http://hexblog.com/2007/04/very_simple_custom_viewer.html">plugin</a>.They are used in IDA-View, Hex-View, Enum and struct views and the Hex-Rays decompiler.</br></p>
In this blog entry we are going to write an ASM file viewer in order to demonstrate how to create a custom viewer and populate it with colored lines.<br/>

<img alt="asmview.gif" src="http://hexblog.com/ida_pro/pix/asmview.gif" width="575" height="525" /><br/>]]>
        <![CDATA[<h2>Writing a custom viewer</h2>
The simplest custom viewer which does not handle any events (like key presses, mouse or cursor position movements, displaying hints, etc) can be created like this:
<pre><blockquote style="background-color:lightblue">v = idaapi.simplecustviewer_t()
if v.Create("Simple custom viewer"):
    for i in xrange(1, 11): 
        v.AddLine("Line %d" % i)
    v.Show()
else:
    print "Failed to create viewer"
</blockquote></pre>

If handling events is required then one has to derive from <b>idaapi.simplecustviewer_t</b> class and implement the required callbacks:<br/>
<pre><blockquote style="background-color:lightblue">class mycv_t(<b>simplecustviewer_t</b>):
    def Create(self, sn=None):
        # Form the title
        title = &quot;Simple custom view test&quot;
        if sn:
            title += &quot; %d&quot; % sn

        # Create the customview
        if not simplecustviewer_t.Create(self, title):
            return False

        self.menu_hello = self.AddPopupMenu(&quot;Hello&quot;)
        self.menu_world = self.AddPopupMenu(&quot;World&quot;)

        for i in xrange(0, 100):
            self.AddLine(&quot;Line %d&quot; % i)

        return True

    def OnKeydown(self, vkey, shift):
        # ESCAPE?
        if vkey == 27:
            self.Close()
        # Goto?
        elif vkey == ord('G'):
            n = self.GetLineNo()
            if n is not None:
                v = idc.AskLong(self.GetLineNo(), &quot;Where to go?&quot;)
                if v:
                    self.Jump(v, 0, 5)
        elif vkey == ord('R'):
            print &quot;refreshing....&quot;
            self.Refresh()
        else:
            return False
        return True

    def OnPopupMenu(self, menu_id):
        if menu_id == self.menu_hello:
            print &quot;Hello&quot;
        elif menu_id == self.menu_world:
            print &quot;World&quot;
        else:
            # Unhandled
            return False
        return True
</blockquote></pre>

To instantiate a custom viewer window:
<pre><blockquote style="background-color:lightblue">view = mycv_t()
if view.Create(1):
    view.Show()
</blockquote></pre>
Or many custom viewers:
<pre><blockquote style="background-color:lightblue">def make_many(n):
    L = []
    for i in xrange(1, n+1):
        v = mycv_t()
        if not v.Create(i):
            break
        v.Show()
        L.append(v)
    return L
# Create 20 views
V = make_many(20)
</blockquote></pre>
Please note that no two views should have the same title. To check if a window with a given title exists and then to close it, you can use:
<pre><blockquote style="background-color:lightblue">f = idaapi.find_tform("Simple custom view test 2")
if f:
    idaapi.close_tform(f, 0)</blockquote></pre>

<p>For a more comprehensive example on custom viewers, please check the <a target="_blank" href="http://code.google.com/p/idapython/source/browse/trunk/examples/ex_custview.py">ex_custview.py</a> example.</p>

<h2>Using colored lines</h2>
<p>To use colored lines, we have to embed color tags to them. All the available foreground colors are defined in <b>lines.hpp</b> header file. The color codes are related to various item kinds in IDA, for example here are some colors:</p>
<table border="1">
<tr bgcolor="lightblue">
    <td>Color name</td>
    <td>Description</td>
</tr>
<tr>
    <td>SCOLOR_REGCMT</td>
    <td>Regular comment</td>
</tr>
<tr>
    <td>SCOLOR_RPTCMT</td>
    <td>Repeatable comment</td>
</tr>
<tr>
    <td>SCOLOR_INSN</td>
    <td>Instruction</td>
</tr>
<tr>
    <td>SCOLOR_KEYWORD</td>
    <td>Keywords</td>
</tr>
<tr>
    <td>SCOLOR_STRING</td>
    <td>String constant in instruction</td>
</tr>
</table></p>
<p>There are also special color tags treated as escape sequence codes (the concept is similar to <a target="_blank" href="http://en.wikipedia.org/wiki/ANSI_escape_code">ANSI escape codes</a>). They are used to determine how a line is rendered, to mark the beginning/end of a certain color, to insert address marks, or to mark UTF8 string beginnings/endings:</p>
<table border="1">
<tr bgcolor="lightblue">
    <td>Color name</td>
    <td>Description</td>
</tr>
<tr>
    <td>SCOLOR_ON</td>
    <td>Escape character (ON)</td>
</tr>
<tr>
    <td>SCOLOR_OFF</td>
    <td>Escape character (OFF)</td>
</tr>
<tr>
    <td>SCOLOR_INV</td>
    <td>Escape character (Inverse colors)</td>
</tr>
<tr>
    <td>SCOLOR_UTF8</td>
    <td>Following text is UTF-8 encoded</td>
</tr>
<tr>
    <td>SCOLOR_STRING</td>
    <td>String constant in instruction</td>
</tr>
</table>

<br/>In the IDA SDK, a colored line is explained to have the following structure:
<pre><blockquote style="background-color:lightblue">//      A typical color sequence looks like this:
//
//      COLOR_ON COLOR_xxx text COLOR_OFF COLOR_xxx
</blockquote></pre>
Luckily, we don't have to form the colored lines manually, instead we can use helper functions:

<pre><blockquote style="background-color:lightblue">colored_line = idaapi.COLSTR("Hello", idaapi.SCOLOR_REG) + " " + idaapi.COLSTR("World", idaapi.SCOLOR_STRING)</blockquote></pre>

If we look at colored_line contents we can see the following:
<pre><blockquote style="background-color:lightblue">'\x01!Hello\x02! \x01\x0bWorld\x02\x0b'</blockquote></pre>
Which is interpreted as:
<pre><blockquote style="background-color:lightblue">COLOR_ON SCOLOR_REG=0x21 Hello COLOR_OFF COLOR=0x21 SPACE COLOR_ON SCOLOR_STRING=0x0B World COLOR_OFF COLOR=0x0B</blockquote></pre>

<p>In order to strip back color tags from a colored line, use <b>tag_remove()</b>:
<pre><blockquote style="background-color:lightblue">line = idaapi.tag_remove(colored_line)</blockquote></pre>

<h2>Writing an ASM file viewer</h2>

Now that we covered all the needed information, let us write a very basic assembly file viewer. To accomplish the task, we need two things:
<ol>
  <li>ASM tokenizer: It should be able to recognize comments, strings and identifiers. For the identifiers, we will take into consideration only register names, instruction names and directives.
    <ul>
      <li>Instruction names: To get all the instruction names we use <b>idautils.GetInstructionList()</b> which returns all the instruction names from the processor module (the ph.instruc array)
      <li>Register names: Similarly we can use <b>idautils.GetRegisterList()</b>
    </ul>
  <li>Custom viewer to render the text: We derive from <b>simplecustviewer_t</b> to handle key presses and popup menu actions
</ol>

<p>The tokenizer (<b>asm_colorizer_t</b> class) will go over the text and when it identifies a token it will call one of the following functions: as_string(), as_comment(), as_num(), and as_id(). Those functions will use <b>idaapi.COLSTR()</b> to colorize the token appropriately. At the end of each line, the tokenizer will call the add_line() method to add the line (after it has been colored).</p>
The custom viewer (implemented by the <b>asmview_t</b> class) will inherit from both asm_colorizer_t and simplecustviewer_t:

<pre><blockquote style="background-color:lightblue">class asmview_t(<b>idaapi.simplecustview_t</b>, <b>asm_colorizer_t</b>):
    def Create(self, fn):
        # Create the customview
        if not idaapi.simplecustview_t.Create(self, &quot;ASM View - %s&quot; % os.path.basename(fn)):
            return False

        self.instruction_list = idautils.<b>GetInstructionList</b>()
        self.instruction_list.extend([&quot;ret&quot;])
        self.register_list    = idautils.<b>GetRegisterList</b>()
        self.register_list.extend([&quot;eax&quot;, &quot;ebx&quot;, &quot;ecx&quot;, &quot;edx&quot;, &quot;edi&quot;, &quot;esi&quot;, &quot;ebp&quot;, &quot;esp&quot;])

        self.fn = fn
        if not self.reload_file():
            return False

        self.id_refresh = self.AddPopupMenu(&quot;Refresh&quot;)
        self.id_close   = self.AddPopupMenu(&quot;Close&quot;)

        return True

    def reload_file(self):
        if not self.colorize_file(self.fn):
            self.Close()
            return False
        return True

    def colorize_file(self, fn):
        try:
            f = open(fn, &quot;r&quot;)
            lines = f.readlines()
            f.close()
            self.ClearLines()
            self.colorize(lines)
            return True
        except:
            return False

    def <b>add_line</b>(self, s=None):
        if not s:
            s = &quot;&quot;
        self.AddLine(s)

    def <b>as_comment</b>(self, s):
        return idaapi.COLSTR(s, idaapi.SCOLOR_RPTCMT)

    def <b>as_id</b>(self, s):
        t = s.lower()
        if t in self.<b>register_list:</b>
            return idaapi.COLSTR(s, idaapi.SCOLOR_REG)
        elif t in self.<b>instruction_list:</b>
            return idaapi.COLSTR(s, idaapi.SCOLOR_INSN)
        else:
            return s

    def <b>as_string</b>(self, s):
        return idaapi.COLSTR(s, idaapi.SCOLOR_STRING)

    def <b>as_num</b>(self, s):
        return idaapi.COLSTR(s, idaapi.SCOLOR_NUMBER)

    def <b>as_directive</b>(self, s):
        return idaapi.COLSTR(s, idaapi.SCOLOR_KEYWORD)

    def <b>OnPopupMenu</b>(self, menu_id):
        if self.id_refresh == menu_id:
            return self.reload_file()
        elif self.id_close == menu_id:
            self.Close()
            return True
        return False

    def <b>OnKeydown</b>(self, vkey, shift):
        # ESCAPE
        if vkey == 27:
            self.Close()
            return True
        return False
</blockquote></pre>


<p>This blog entry inspired you to write a new plugin? Feel free to participate in our <a href="http://hex-rays.com/contest.shtml">plugin contest</a>! </p>

The ASM viewer script can be downloaded from <a href="http://hexblog.com/ida_pro/files/asmview.py">here</a> (note: it requires IDAPython <a href="http://code.google.com/p/idapython/source/detail?r=289">r289</a>).]]>
    </content>
</entry>
<entry>
    <title>Preview of the new cross-platform IDA Pro GUI </title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/03/preview_of_the_next_generation.html" />
    <id>tag:hexblog.com,2010://1.115</id>
    <published>2010-03-10T11:33:17Z</published>
    <updated>2010-05-07T10:15:25Z</updated>
    
    <summary>Preview of the new cross-platform IDA Pro GUI
</summary>
    <author>
        <name>Daniel Pistelli</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>In order to provide our customers with the best user experience and in order to target many different platforms, the IDA Pro graphical user interface is currently being rewritten using the <a href="http://qt.nokia.com/">Qt technology</a>.</p>

<p>Qt (pronounced "cute") is a cross-platform application and UI framework and the Win32 VCL-based IDA Pro interface is being ported to it. The goal is to provide all the features available in the current GUI while maintaining the maximum compatibility with plugins and other external modules.</p>

<p>Here is a screenshot of the current build of <strong>idaqt</strong> running on Ubuntu:</p>

<p><a href="http://hexblog.com/ida_pro/pix/idaqt_preview_100310_1.html" onclick="window.open('http://hexblog.com/ida_pro/pix/idaqt_preview_100310_1.html','popup','width=1680,height=1001,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img alt="idaqt_preview_100310_thumb_1.jpg" src="http://hexblog.com/ida_pro/pix/idaqt_preview_100310_thumb_1.jpg" width="680" height="405" border="0" /></a><br></p>

<p>You can click on the images to enlarge them. </p>]]>
        <![CDATA[<p>From the first version <strong>idaqt</strong> will include a fully functional graphing, which is, as it is possible to notice from the screenshot, already implemented. The same is true for hints, navigation band and all other advanced IDA Pro features.</p>

<p><a href="http://hexblog.com/ida_pro/pix/idaqt_preview_100310_2.html" onclick="window.open('http://hexblog.com/ida_pro/pix/idaqt_preview_100310_2.html','popup','width=1680,height=981,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img alt="idaqt_preview_100310_thumb_2.jpg" src="http://hexblog.com/ida_pro/pix/idaqt_preview_100310_thumb_2.jpg" width="680" height="397" border="0" /></a><br></p>

<p>This is <strong>idaqt</strong> on Windows 7. The text view looks exactly the same and all other features like choosers and forms will be available with no exception on all supported platforms.</p>

<p><a href="http://hexblog.com/ida_pro/pix/idaqt_preview_100310_3.html" onclick="window.open('http://hexblog.com/ida_pro/pix/idaqt_preview_100310_3.html','popup','width=1679,height=981,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img alt="idaqt_preview_100310_thumb_3.jpg" src="http://hexblog.com/ida_pro/pix/idaqt_preview_100310_thumb_3.jpg" width="680" height="397" border="0" /></a><br></p>

<p>The full range of options and customizations which the Win32 interface provides will be available as well.</p>

<p>As you can see, apart from the still to be implemented docking, the new interface looks pretty much the same as the Win32 one.</p>

<p>Not only will it be possible to deploy the same native graphical interface to Windows, OS X, Linux and other platforms which in the future may become popular, but the quality of the user experience and the further development capabilities will be hugely increased thanks to an advanced framework such as Qt.</p>

<p>Although <strong>idaqt</strong> is going to replace the current GUI completely, for some time they will be deployed together in order to fix any incompatibility issues and to give third party developers the necessary time to thoroughly test their products against the new interface.</p>]]>
    </content>
</entry>
<entry>
    <title>Custom data types and formats</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/02/custom_data_types_and_formats_1.html" />
    <id>tag:hexblog.com,2010://1.114</id>
    <published>2010-02-25T17:48:44Z</published>
    <updated>2010-04-01T10:51:34Z</updated>
    
    <summary>Another new feature that will be available in the upcoming version of IDA Pro is the ability to create and render custom data types and formats. (Embedded instructions disassembled and rendered along side with x86 code)...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>Another new feature that will be available in the upcoming version of IDA Pro is the ability to create and render custom data types and formats.</p>
<img src="http://hexblog.com/ida_pro/pix/custdata_cover.gif"/><br/>
(Embedded instructions disassembled and rendered along side with x86 code)<br/>]]>
        <![CDATA[<!-- ============================================================================== -->
<h2>What are custom types and formats</h2>

<ul>
<li>Custom data type: A custom type is basically just a way to tag some bytes for later display with custom format, when the built-in IDA types (dt_byte, dt_word, etc) are not enough.
For example: an XMM vector, a Pascal string, a half-precision (16 bits) floating-point number, a 16:32 far pointer (fword), uleb128 number and so on.
To define a custom type, you need to provide its name, size (fixed or dynamically calculated), keyword for disassembly and a few other attributes.
<li>Custom data format:
The custom data format allows you do display a custom or built-in data type in any way you like. You can register several formats for each type and switch the representation.
For example, you might want to switch the display of the same 16-byte XMM vector between four floats or two doubles.
A format definition includes callback for printing (to display) and scanning (used during debugging to change the register values).
</ul>

For example, here is a custom MAKE_DWORD format applied to the built-in dword type:<br/>
<img src="http://hexblog.com/ida_pro/pix/custdata_mkdword.gif"/><br/>
<p>Its implementation is very simple:</p>

<img src="http://hexblog.com/ida_pro/pix/custdata_mkdword_code.gif"/><br/>
<p>Next we illustrate some possible usages of custom types and formats. Other uses are also possible too, it is up to your imagination.</p>

<!-- ============================================================================== -->
<h2>Decoding embedded bytecodes</h2>
Imagine you are debugging an x86 program that implements its own VM and embeddes them in the program.<br/>The classical solution for this problem can be:
<ul>
  <li>Write a dedicated processor module and then load the extracted bytecodes separately
  <li>Or define the bytecodes as bytes and then use comments to describe the real meaning of those bytecodes.
</ul>
<p>With this new addition, one can just write a custom data type to handle the situation:</p>
<img src="http://hexblog.com/ida_pro/pix/custdata_vm_data.gif"/><br/>

<p>And if you happen to have a situation where the bytecodes are operands to instructions (as means of obfuscation), you can still apply the custom format on those operands:</p>
<img src="http://hexblog.com/ida_pro/pix/custdata_vm_opr.gif"/><br/>
<p>The <a target="_blank" href="http://hexblog.com/2010/02/scriptable_processor_modules.html">previous</a> blog entry showed how to write processor modules using Python. What if one simply uses the "import" statement to import a full-blown processor module script and use it in the custom data types/formats? ;)</p>

<!-- ============================================================================== -->
<h2>Displaying resource strings</h2>
<p>When reversing MS Windows applications, one can encounter string IDs, but then how to easily and nicely go fetch the data and display it in the disassembly listing?<br/>
Normally, one would have to use a resource editor to extract the string value corresponding to the string id, then to create an enum in IDA for each string ID with a repeatable comment:</p>
<img src="http://hexblog.com/ida_pro/pix/custdata_rsrc_enum.gif"/><br/>
<p>That works, but what about writing your own custom format instead:</p>
<img src="http://hexblog.com/ida_pro/pix/custdata_rsrc_menu.gif"/><br/>
<p>And then applying it directly without having to use a resource editor to extract the string value, have the custom format do that programmatically for you :</p>
<img src="http://hexblog.com/ida_pro/pix/custdata_rsrc.gif"/><br/>

<p>This is how a resource string custom format handler can look like:</p>
<img src="http://hexblog.com/ida_pro/pix/custdata_rsrc_code.gif"/><br/>

<p>To take a closer look at it, you can <a href="http://hexblog.com/ida_pro/files/custdata_files.zip">download</a> the custom data type handler script along with the source code of the simplevm assembler/disassembler and the C program that was used in this article.<br/>

<!-- Thank you, you know who you are. -->]]>
    </content>
</entry>
<entry>
    <title>Scriptable Processor modules</title>
    <link rel="alternate" type="text/html" href="http://hexblog.com/2010/02/scriptable_processor_modules.html" />
    <id>tag:hexblog.com,2010://1.113</id>
    <published>2010-02-16T17:38:51Z</published>
    <updated>2010-03-22T11:21:33Z</updated>
    
    <summary>One of the new features we are preparing for the next version of IDA is the ability to write processor modules using your favorite scripting language. After realizing how handy it is to write file loaders using scripting languages, we...</summary>
    <author>
        <name>Elias Bachaalany</name>
        
    </author>
            <category term="IDA Pro" />
    
    <content type="html" xml:lang="en" xml:base="http://hexblog.com/">
        <![CDATA[<p>One of the new features we are preparing for the next version of IDA is the ability to write processor modules using your favorite scripting language.<br/>
After realizing how handy it is to write <a href="http://hexblog.com/2010/01/pdf_file_loader_to_extract_and_1.html">file loaders</a> using scripting languages, we set out to making the same thing for processor modules. As an exercise for this new feature, we implemented a processor module for the <a href="http://en.wikipedia.org/wiki/Extensible_Firmware_Interface" target="_blank">EFI bytecode</a>.</p>
<img src="http://hexblog.com/ida_pro/pix/scriptproc_idagraph.gif" width="688" height="470" /><br/>]]>
        <![CDATA[<h2>Background</h2>

In IDA Pro, a processor module implementation is usually split into four parts:
<ol>
  <li>Processor, assembler, instructions and registers definitions (ins.cpp/.hpp, reg.cpp)
  <li>Decoder (ana.cpp): decodes an instruction into an insn_t structure (the 'cmd' global variable)
  <li>Emulation (emu.cpp): emulates instructions, creates appropriate cross references, traces the stack, recognizes code patterns, etc...
  <li>Output (out.cpp): outputs the result to the screen
</ol>

The processor module is described using the <b>processor_t</b> structure. It holds pointers to registers, instructions, processor module name and other callbacks (ana, emu, out, notify, ...). <br/>
The assembler is described using the <b>asm_t</b> structure. It holds pointers to the assembler syntax and other callbacks.<br/>
For more information about structures and functions used in IDA API and processor modules (e.g. insn_t), see <a href="http://www.binarypool.com/idapluginwriting/">this great tutorial</a> by Steve Micallef.

<h2>Writing a processor module in Python</h2>

To write a processor module in Python, we follow similar logic.

<ol>
  <li>Write the <b>get_idp_desc()</b> function. It simply tells IDA what processors the module can handle.
  <pre><blockquote style="background-color:lightblue">def get_idp_desc():
    return &quot;EFI Byte code:ebc&quot;
</blockquote></pre>
<p>The return value means that this processor is named "EFI Byte code" and its shortname is "ebc". Thus a subsequent call to <b>set_processor_type('ebc')</b> from the part of a file loader will succeed.</p>

In case of the <b>pc</b> processor module, which can handle many variations of x86 architecture, the string looks like this:<pre><blockquote style="background-color:lightblue">Intel 80x86 processors:8086:80286r:80286p:80386r:80386p:...</blockquote></pre>
  <li>Define the registers and instructions:
  <pre><blockquote style="background-color:lightblue"># Registers definition
  <b>proc_Registers</b> = [
      # General purpose registers
      &quot;R0&quot;,
      &quot;R1&quot;,
      ...,
      &quot;R10&quot;,
      ...
  ]

  # Instructions definition
  <b>proc_Instructions</b> = [
      {'name': 'INSN1', 'feature': CF_USE1},
      {'name': 'INSN2', 'feature': CF_USE1 | CF_CHG1}
      ...
  ]
  </blockquote></pre>
  <li>Write the <b>get_idp_def()</b> function. It should return a dictionary similar to the <b>processor_t</b> structure with the processor, assembler, instructions and registers definitions.<br/>
  <pre><blockquote style="background-color:lightblue"># This function returns the processor module definition
def get_idp_def():
    return {
        'version': IDP_INTERFACE_VERSION,

        # IDP id
        'id' : 0x8000 + 1,

        # Processor features
        'flag' : PR_USE32 | PRN_HEX | PR_RNAMESOK,

        # short processor names
        # Each name should be shorter than 9 characters
        '<b>psnames</b>': ['ebc'],

        # long processor names
        # No restriction on name lengthes.
        '<b>plnames</b>': ['EFI Byte code'],

        # number of registers
        'regsNum': len(proc_Registers),

        # register names
        'regNames': <b>proc_Registers</b>,
      
        # Array of instructions
        'instruc': <b>proc_Instructions</b>,
        ....
        '<b>assembler</b>': \
        {
                # flag
                'flag' : ASH_HEXF3 | AS_UNEQU | AS_COLON | ASB_BINF4 | AS_N2CHR,

                # Assembler name (displayed in menus)
                'name': &quot;EFI bytecode assembler&quot;,
                ...
                # byte directive
                'a_byte': &quot;db&quot;,

                # word directive
                'a_word': &quot;dw&quot;,

                # remove if not allowed
                'a_dword': &quot;dd&quot;,
                ...

        } # Assembler
    }
</blockquote></pre>
</ol>

Now that we finished all the declarations, we can implement the decoder (or analyzer), emulator and the output callbacks.
<ul>
  <li>The analyzer looks like this:<pre><blockquote style="background-color:lightblue">def <b>ph_ana</b>():
    &quot;&quot;&quot;
    Decodes an instruction into the global variable 'cmd'
    Current address is pre-filled in cmd.ea
    &quot;&quot;&quot;
    cmd = idaapi.cmd

    # take opcode byte
    b = ua_next_byte()
    # decode and fill cmd.Operands etc...
    # ...

    # Return decoded instruction size or zero
    return cmd.size
</blockquote></pre>
<br/>
And decoding one instruction/filling the 'cmd' variable may look like this:<pre><blockquote style="background-color:lightblue">def decode_JMP8(opbyte, cmd):
    conditional   = (opbyte &amp; 0x80) != 0
    cs            = (opbyte &amp; 0x40) != 0
    cmd.Op1.type  = o_near
    cmd.Op1.dtyp  = dt_byte
    addr          = ua_next_byte()
    cmd.Op1.addr  = (as_signed(addr, 8) * 2) + cmd.size + cmd.ea

    if conditional:
        cmd.auxpref = FL_CS if cs else FL_NCS

    return True
</blockquote></pre>
  <li>The emulator:<pre><blockquote style="background-color:lightblue"># Emulate instruction, create cross-references, plan to analyze
# subsequent instructions, modify flags etc. Upon entrance to this function
# all information about the instruction is in 'cmd' structure.
# If zero is returned, the kernel will delete the instruction.
def <b>ph_emu</b>():
    aux = cmd.auxpref
    Feature = cmd.get_canon_feature()

    if Feature &amp; CF_USE1:
        handle_operand(cmd.Op1, 1)
    if Feature &amp; CF_CHG1:
        handle_operand(cmd.Op1, 0)
    if Feature &amp; CF_USE2:
        handle_operand(cmd.Op2, 1)
    if Feature &amp; CF_CHG2:
        handle_operand(cmd.Op2, 0)
    if Feature &amp; CF_JUMP:
        QueueMark(Q_jumps, cmd.ea)

    # add flow xref
    if Feature &amp; CF_STOP == 0:
        ua_add_cref(0, cmd.ea + cmd.size, fl_F)

    return 1
</blockquote></pre>
  <li>The output callback:<pre><blockquote style="background-color:lightblue"># Generate text representation of an instruction in 'cmd' structure.
# This function shouldn't change the database, flags or anything else.
# All these actions should be performed only by ph_emu() function.
def <b>ph_out</b>():
    cmd = idaapi.cmd
    # Init output buffer
    buf = idaapi.init_output_buffer(1024)

    # First, output the instruction mnemonic
    OutMnem()

    # Output the first operand if present (this invokes the ph_outop callback)
    out_one_operand( 0 )

    # Output the rest of the operands
    for i in xrange(1, 3):
        op = cmd[i]

        if op.type == o_void:
            break

        out_symbol(',')
        OutChar(' ')
        out_one_operand(i)

    # Terminate the output buffer
    term_output_buffer()

    # Emit the line
    cvar.gl_comm = 1
    MakeLine(buf)
</blockquote></pre>Note that the previous callbacks are very similar to their C language counterparts.
</ul>

<p>
Although this feature will not work with the current version of IDA Pro, you can download the <a href="http://hexblog.com/ida_pro/files/scriptproc_ebc.py">EBC script</a> sample for a preview of how a module would look.</p>

<p>If you like this feature, make sure to apply for the beta testing of next version when we announce it!</p>]]>
    </content>
</entry>

</feed> 

