📚 OpenRCE is preserved as a read-only archive. Launched at RECon Montreal in 2005. Registration and posting are disabled.








Flag: Tornado! Hurricane!

 Forums >>  IDA Pro  >>  Code Analysis Order & Register Tracking Problem

Topic created on: January 12, 2008 12:49 CST by Ixje .

I'm trying to enhance an existing processor module by hooking the necessary callbacks (custom_ana, custom_emu,..out etc) in a plugin module. Now I could use a little clarification and/or tips with doing this.

The final question is:
Is there an easy way to track if the instruction being decoded in ana() is decoded for the first time and if it's in the correct program flow?


I�m sure this question is a bit vague so below is some background information (warning long story)

Goal
I want to track 1 of the cpu's registers based on if the instruction is interesting or not (does it modifies the register I want to track?). This tracking has to be done in the ana() function because I also want to change "cmd" if interesting and from reading the comments in de SDK, the emu() function isn't allowed to change "cmd". It is important that the register is tracked in the correct flow of the program. What is �correct�? Read on.

Scenario
Given the following instructions
0x0000   instruction A with 1 operand (Op1 has xref to 0x000B)
0x0001   instruction B
0x0002   instruction C (jump) 0x000B
...
0x000B instruction D


Where we call the interesting register I want to track reg X with initial value 2
Instruction A would add 2 to reg X (this should be done in the ana() function)
Instruction B would decrease reg X by 1
Instruction D multiplies reg X by 4

The original (and �correct�) program flow would be 0x0000, 0x0001, 0x0002, 0x000B where regX should change like this:

Start regX = 2
Execute instruction A �> regX = 4
Execute instruction B -> regX = 3
Execute instruction C -> regX = 3
Execute instruction D -> regX = 12

live problem
What I actually experience is:
Start regX = 2
Execute instruction A -> regX = 4
Execute instruction D -> regX = 16
Execute instruction B -> regX = 15
Execute instruction C
Etc.

So I started tracking the order of the functions being called by IDA to figure out what was causing this. As far as I understand the way IDA analyzes code is in this order:
call ana() decode instruction and fill cmd (determine itype, type, dtyp, size etc)
call emu() create xrefs
call out() print mnemonic & operand(s)

If I'm correct the complete process of analyzing an instruction at address X is not fully done until out() is called, right?

What I see happening in my live problem is:
Ana (0x0000) regX = 4
Emu (0x0000) // Now here it detects that the first operand should have a XREF to 0x000B
// and calls something like ua_add_dref(x,x,x)
Ana (0x000B) regX = 16 // I believe that the ua_add_dref(x,x,x) function triggers the Ana() function again causing regX to change while it shouldn�t (the original flow of the program wouldn�t have executed instruction D yet).
Emu (0x000B)
Ana (0x0000) // I�m not 100% sure this one was called again before out() but I can�t access the code&binary from home so I�m doing this out of my head.
Out (0x0000)


Within ana() I can track whether the instruction at address X is decoded for the first time by logging it�s address in e.g. a vector and do a lookup. Based on this I could decide if I should change regX or not. What I can�t seem to do is identify if the instruction being decoded is actually decoded for the first time AND in the correct flow of the program.

As you saw above 0x000B gets decoded for the first time before it would have actually been had it followed the original program flow. So here�s my question again:

Is there an easy way to track if the instruction being decoded in ana() is decoded for the first time and if it's in the correct program flow?

Thanks for reading this long post :)

  PSUJobu     January 14, 2008 06:44.04 CST
I'm not aware of any way to do what you want.  You would be better off posting this to the Research & Resources board of the Hex-Rays forum.  Though the SDK is officially unsupported, Ilfak is usually great about answering questions anyway...

FWIW, what you're doing sounds more in line with x86emu rather than disassembly.  In other words, you might be better off with a plugin that does emulation rather than trying to hook the disassembler engine, which does not strictly follow instruction flow.

  Ixje   January 15, 2008 02:24.43 CST
Thanks for the advice.

And you're right. I sort off need an partial emu (although its not x86) to get a more useful disassembly. E.g. there's an instruction that jumps to the address currently in register X. By partially emulating the instructions that change register X I could figure out where to jump which would save allot of time. All the information to track register X is available, I just need to find a way to do it so I can use it while disassembling.

Note: Registration is required to post to the forums.

There are 31,328 total registered users.


Recently Created Topics
[help] Unpacking VMP...
Mar/12
Reverse Engineering ...
Jul/06
let 'IDAPython' impo...
Sep/24
set 'IDAPython' as t...
Sep/24
GuessType return une...
Sep/20
About retrieving the...
Sep/07
How to find specific...
Aug/15
How to get data depe...
Jul/07
Identify RVA data in...
May/06
Question about memor...
Dec/12


Recent Forum Posts
Finding the procedur...
rolEYder
Question about debbu...
rolEYder
Identify RVA data in...
sohlow
let 'IDAPython' impo...
sohlow
How to find specific...
hackgreti
Problem with ollydbg
sh3dow
How can I write olly...
sh3dow
New LoadMAP plugin v...
mefisto...
Intel pin in loaded ...
djnemo
OOP_RE tool available?
Bl4ckm4n


Recent Blog Entries
halsten
Mar/14
Breaking IonCUBE VM

oleavr
Oct/24
Anatomy of a code tracer

hasherezade
Sep/24
IAT Patcher - new tool for ...

oleavr
Aug/27
CryptoShark: code tracer ba...

oleavr
Jun/25
Build a debugger in 5 minutes

More ...


Recent Blog Comments
nieo on:
Mar/22
IAT Patcher - new tool for ...

djnemo on:
Nov/17
Kernel debugger vs user mod...

acel on:
Nov/14
Kernel debugger vs user mod...

pedram on:
Dec/21
frida.github.io: scriptable...

capadleman on:
Jun/19
Using NtCreateThreadEx for ...

More ...


Imagery
SoySauce Blueprint
Jun 6, 2008

[+] expand

View Gallery (11) / Submit