Reverse Engineering for Beginners

(avery) #1

CHAPTER 51. C++ CHAPTER 51. C++


};


Nevertheless, if we cast theboxtype to apointer to an int array, and we modify the array ofint-s that we have, we can succeed.


void hack_oop_encapsulation(class box o)
{
unsigned int
ptr_to_object=reinterpret_cast<unsigned int*>(o);
ptr_to_object[1]=123;
};


This function’s code is very simple—it can be said that the function takes a pointer to an array ofint-s for input and writes
123 to the secondint:


?hack_oop_encapsulation@@YAXPAVbox@@@Z PROC ; hack_oop_encapsulation
mov eax, DWORD PTR _o$[esp-4]
mov DWORD PTR [eax+4], 123
ret 0
?hack_oop_encapsulation@@YAXPAVbox@@@Z ENDP ; hack_oop_encapsulation


Let’s check how it works:


int main()
{
box b(1, 10, 20, 30);


b.dump();

hack_oop_encapsulation(&b);

b.dump();

return 0;
};


Let’s run:


this is box. color=1, width=10, height=20, depth=30
this is box. color=1, width=123, height=20, depth=30


We see that the encapsulation is just protection of class fields only in the compilation stage. The C++ compiler is not
allowing the generation of code that modifies protected fields straightforwardly, nevertheless, it is possible with the help of
dirty hacks.


51.1.4 Multiple inheritance.


Multiple inheritance is creating a class which inherits fields and methods from two or more classes.


Let’s write a simple example again:


#include <stdio.h>


class box
{
public:
int width, height, depth;
box() { };
box(int width, int height, int depth)
{
this->width=width;
this->height=height;
this->depth=depth;
};
void dump()
{
printf ("this is box. width=%d, height=%d, depth=%d\n", width, height, depth);
};
int get_volume()
{
return width height depth;

Free download pdf