23.03.2013 Views

Quick introduction to reverse engineering for beginners

Quick introduction to reverse engineering for beginners

Quick introduction to reverse engineering for beginners

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

lea ecx, DWORD PTR _c2$[ebp]<br />

call ??0c@@QAE@HH@Z ; c::c<br />

lea ecx, DWORD PTR _c1$[ebp]<br />

call ?dump@c@@QAEXXZ ; c::dump<br />

lea ecx, DWORD PTR _c2$[ebp]<br />

call ?dump@c@@QAEXXZ ; c::dump<br />

xor eax, eax<br />

mov esp, ebp<br />

pop ebp<br />

ret 0<br />

_main ENDP<br />

So what’s going on. For each object (instance of class c) 8 bytes allocated, that’s exactly size of 2 variables<br />

s<strong>to</strong>rage.<br />

For c1 a default argumentless construc<strong>to</strong>r ??0c@@QAE@XZ is called. For c2 another construc<strong>to</strong>r ??0c@@QAE@HH@Z<br />

is called and two numbers are passed as arguments.<br />

A pointer <strong>to</strong> object (this in C++ terminology) is passed in ECX register. This is called thiscall 2.5.4 —<br />

a pointer <strong>to</strong> object passing method.<br />

MSVC doing it using ECX register. Needless <strong>to</strong> say, it’s not a standardized method, other compilers could<br />

do it differently, <strong>for</strong> example, via first function argument (like GCC).<br />

Why these functions has so odd names? That’s name mangling 59 .<br />

C++ class may contain several methods sharing the same name but having different arguments — that’s<br />

polymorphism. And of course, different classes may own methods sharing the same name.<br />

Name mangling allows <strong>to</strong> encode class name + method name + all method argument types in one ASCIIstring,<br />

which will be used as internal function name. That’s all because neither linker, nor DLL operation<br />

system loader (mangled names may be among DLL exports as well) knows nothing about C++ or OOP.<br />

dump() function called two times after.<br />

Now let’s see construc<strong>to</strong>rs’ code:<br />

_this$ = -4 ; size = 4<br />

??0c@@QAE@XZ PROC ; c::c, COMDAT<br />

; _this$ = ecx<br />

push ebp<br />

mov ebp, esp<br />

push ecx<br />

mov DWORD PTR _this$[ebp], ecx<br />

mov eax, DWORD PTR _this$[ebp]<br />

mov DWORD PTR [eax], 667 ; 0000029bH<br />

mov ecx, DWORD PTR _this$[ebp]<br />

mov DWORD PTR [ecx+4], 999 ; 000003e7H<br />

mov eax, DWORD PTR _this$[ebp]<br />

mov esp, ebp<br />

pop ebp<br />

ret 0<br />

??0c@@QAE@XZ ENDP ; c::c<br />

_this$ = -4 ; size = 4<br />

_a$ = 8 ; size = 4<br />

_b$ = 12 ; size = 4<br />

??0c@@QAE@HH@Z PROC ; c::c, COMDAT<br />

; _this$ = ecx<br />

push ebp<br />

mov ebp, esp<br />

59 Wikipedia: Name mangling<br />

80

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!