C++ 类的逆向分析
#include <iostream>
#include <stdio.h>
class Person
{
public:
Person()
{
age = 31;
printf("Person: Person() called\n");
}
virtual ~Person()
{
printf("Person: ~Person() called\n");
}
virtual int getAge()
{
return age;
}
virtual void setAge(int age_)
{
age = age_;
}
private:
int age;
};
class Student: public Person
{
public:
Student()
{
printf("Student: Student() called\n");
}
virtual ~Student()
{
printf("Student: ~Student() called\n");
}
virtual int getAge()
{
printf("Student: ~getAge() called\n");
return 18;
}
};
int main(int argc, char const *argv[])
{
Person* person = new (Student);
person->setAge(28);
person->getAge();
delete (person);
return 0;
}
app8.cpp
中有两个类,Student 继承自 Person 并覆盖了它的 getAge()。为便于逆向分析 app8,代码中没引入 STL,且在执行这两个类的构造和析构时用 printf() 打印一条语句
- 编译并优化:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d80396e3b64696417cdcd2aa0ded3ad2.png)
- 拖入 IDA,F5:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a6b2ed47f6e6beed947eb2bd1839fb08.png)
- 上述伪代码中没有函数名和类的符号信息,只有
sub_xxx
型的函数调用和多级指针转换的函数调用
- 要想逆向分析这段代码,得掌握 C++ 类对象模型及 C++ 编译器的相关知识
- 一个 C++ 类包含多个方法和字段。将类加载到内存后,用结构体表示如下:
struct xxx {
void* ptr1;
void* ptr2;
...
var_type1 var1;
var_type2 var2;
...
};
</