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








Flag: Tornado! Hurricane!

 Forums >>  Target Specific - General  >>  this pointer woes

Topic created on: June 17, 2007 22:13 CDT by LordSephiroth .

This has been driving me insane (pun intended)....

This is my first experience reversing a C++ app, so forgive me if I am asking something completely obvious.

B63EC324     arg_0= dword ptr  8
B63EC324
B63EC324 000 push    ebp
B63EC325 004 mov     ebp, esp
B63EC327 004 push    esi
B63EC328 008 push    ebx
B63EC329 00C call    $+5             ; calls ahead 5 bytes
B63EC32E 010 pop     ebx             ; pops the current instruction pointer off the stack (points to itself)
B63EC32F 00C add     ebx, 0B0686h  
B63EC335 00C mov     eax, [ebp+arg_0]
B63EC338 00C cmp     dword ptr [eax+4], 0
B63EC33C 00C jnz     short loc_B63EC372

the value of ebx, after being incremented, seems to be pointing to a table of function pointers. My initial thought is that its to the vtable, now to where I'm confused:

B63EC372     loc_B63EC372:
B63EC372 00C sub     esp, 0Ch
B63EC375 018 mov     eax, [ebp+arg_0]
B63EC378 018 mov     eax, [eax+4]
B63EC37B 018 mov     edx, [eax]
B63EC37D 018 add     edx, 14h
B63EC380 018 mov     eax, [ebp+arg_0]
B63EC383 018 push    dword ptr [eax+4]
B63EC386 01C mov     eax, [edx]
B63EC388 01C call    eax

My definition of the this pointer may be wrong, but my assumption is that arg_0 is the this pointer and ebx is the vtable. Where I'm confused is when we get down to the second chunk, we've got several pointers being juggled, ebp+8 -> eax+4 -> eax -> edx -> eax (called). My initial understanding of the this pointer is that it points to the constructed objects, but the constructors are never called. So, what could eax be a pointer to? Is it just a pointer being passed as a parameter or am I completely missing something? Normally I'd try debugging it to find the address and see whats being passed into eax, but the pointer being called is null (this is a null pointer deref vulnerability I found a while ago).

I know this should be obvious, but I haven't found a lot of resources for reversing C++ and I thought I'd ask since its been driving me crazy.

Any help would be greatly appreciated.

  jms     June 17, 2007 22:36.22 CDT
Did you take a read of the C++ specific articles posted here? There are some goodies :) Off the top of my head I can't give you any love either..

  igorsk     June 18, 2007 03:44.39 CDT
call $+5/pop reg is a typical trick used by position-independent code, usually viruses or packers. It has nothing to do with C++. You should step through the code in the debugger and see what's the actual function being called. My guess is it's LoadLibrary or something similar.

  anonymouse     June 18, 2007 05:17.32 CDT
well judging from the address range it doesnt look like usermode address space c++ app or c++ Driver ??

assuming you are at DriverEntry of some sys file
ebp+arg_0 will contain PDriverObject

  LordSephiroth     June 18, 2007 09:15.35 CDT
Thank you for the replies.

I should have been clearer about the platform, this is a Linux app that was compiled under g++ (I think).

The result of these instructions:

B63EC329 00C call    $+5          
B63EC32E 010 pop     ebx            
B63EC32F 00C add     ebx, 0B0686h  

ebx is pointing to a large table of pointers, I'm assuming it is the vtable. Most of the functions that I've looked at have that same series of instructions, just the value in the add instruction is different (it ends up being the same value though).

If the the comparison at B63EC33C evaluates to 0, the next series of instructions has two calls, both of which have a single unconditional jmp to [ebx+somevalue]. For example, the first is call sub_B617EDB8 which contains a single instruction, jmp [ebx+4734h], I added the value of ebx+4734h and the address points to one of the locations in the table I mentioned earlier. I'll post the next series of instructions when I get home tonight, I should have done it last night.

I guess my biggest question right now is whether or not my definition of the this pointer is correct or not. From what I've been told and read, it appears to be pointing to a constructed object. So a member function of class A, is going to have a pointer to the constructed class A object which is the this pointer, correct?

Also, if eax is the this pointer, then what is at eax+4? These series of instructions makes me believe its a vtable, but if eax+4 is the vtable, then what is in ebx?

B63EC375 018 mov     eax, [ebp+arg_0]
B63EC378 018 mov     eax, [eax+4]
B63EC37B 018 mov     edx, [eax]
B63EC37D 018 add     edx, 14h
...
B63EC386 01C mov     eax, [edx]
B63EC388 01C call    eax

  gera     June 18, 2007 10:12.54 CDT
I bet this is linux and gcc. and the weird addresses are there because you have a system where everything is randomized... and... this code belongs to a library, right?

in this case, call+ebx pop is, as in a PIC program, to know where the library (could be PIC "EXE") is located. And the add is to bring ebx to the base of the .data section, or more probably to the GOT of the dynamic object.

I would bet this is not really a vtable, but the GOT (kind of import table), or could also be used to locate global data (like errno, for example).

Now, ebx is never used in this fragment of code, so I can't be sure.

In the second part, yes, it does look a lot more like function pointers and/or C++ object.

From the structure (cal [15+[[eax+4]]] I would say that arg_0 is an object who's first field is a pointer to another object who's virtual method #5 (14h=5*4) is called.

Or, I could be wrong, and this may have to do with multiple-inheritance in gcc, of which I don't know much.

care of sharing the name of the library? function called? application? etc?

definately, ebx is not really pointing to a vtable, it's the .data section of the library (the different constants in the add make sence, as the call pushes a different address on every function).

  LordSephiroth     June 19, 2007 07:25.13 CDT
Sorry for the delay.

Thats interesting, I never thought about the fact it could be the GOT. I figured it looked somewhat large to be a vtable, but I wasn't sure.

The library is libkhtml 4.2.0 from kdelibs-3.5.3 and the function I'm 'reversing' is DOM::Node::nodeType(). I have a .idb of it if you are interested, I'd be more than happy to send it to you. I was hoping that reversing something that I could reference the code for later would be somewhat beneficial, but it hasn't helped that much :p

  gera     June 19, 2007 07:34.22 CDT
If you have specific questions that I may try to answer, yes, I could see the .idb, "it shouldn't take long" (c)

  LordSephiroth     June 20, 2007 09:12.18 CDT
Thanks, I appreciate the offer. I'll let you know.

"it shouldn't take long", famous last words :p

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