Microsoft Visual C++ 逆向第二部分:类、方法和RTTI

2024-08-26 10:32

本文主要是介绍Microsoft Visual C++ 逆向第二部分:类、方法和RTTI,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Microsoft Visual c++是Win32使用最广泛的编译器,所以Win32逆向工作者熟悉其内部工作方式是很重要的。能够识别编译器生成的粘合代码有助于快速将注意力集中在程序员编写的实际代码上。它还有助于恢复项目的高层结构。在这篇由2部分组成的文章的第二部分(请参阅:第一部分:异常处理)中,我将介绍如何在MSVC中实现c++机制,包括类布局、虚函数、RTTI。假如您熟悉基本的c++和汇编语言。

基本类布局

为了说明下面的内容,让我们思考这个简单的例子:

    class A
    {
      int a1;
    public:
      virtual int A_virt1();
      virtual int A_virt2();
      static void A_static1();
      void A_simple1();
    };

    class B
    {
      int b1;
      int b2;
    public:
      virtual int B_virt1();
      virtual int B_virt2();
    };

    class C: public A, public B
    {
      int c1;
    public:
      virtual int A_virt2();
      virtual int B_virt2();
    };

在大多数情况下,MSVC以以下顺序排列类:

 

1. 虚函数表的指针(_vtable_或_vftable_),仅当类有虚方法且基类中没有合适的表可以重用时才添加。

2. 基类

3.类成员

虚函数表按虚方法第一次出现的顺序由虚方法的地址组成。重载函数的地址替换基类中函数的地址。

 

因此,我们的三个类的布局将如下所示:

  class A size(8):+---0  | {vfptr}4  | a1+---A's vftable:0  | &A::A_virt14  | &A::A_virt2class B size(12):+---0  | {vfptr}4  | b18  | b2+---B's vftable:0  | &B::B_virt14  | &B::B_virt2class C size(24):+---| +--- (base class A)0  | | {vfptr}4  | | a1| +---| +--- (base class B)8  | | {vfptr}12  | | b116  | | b2| +---20  | c1+---C's vftable for A:0  | &A::A_virt14  | &C::A_virt2C's vftable for B:0  | &B::B_virt14  | &C::B_virt2


The above diagram was produced by the VC8 compiler using an undocumented switch. To see the class layouts produced by the compiler, use: -d1reportSingleClassLayout to see the layout of a single class -d1reportAllClassLayout to see the layouts of all classes (including internal CRT classes) The layouts are dumped to stdout. 

As you can see, C has two vftables, since it has inherited two classes which both already had virtual functions. Address of C::A_virt2 replaces address of A::A_virt2 in C's vftable for A, and C::B_virt2 replaces B::B_virt2 in the other table. 
 

Calling Conventions and Class Methods


All class methods in MSVC by default use _thiscall_ convention. Class instance address (_this_ pointer) is passed as a hidden parameter in the ecx register. In the method body the compiler usually tucks it away immediately in some other register (e.g. esi or edi) and/or stack variable. All further adressing of the class members is done through that register and/or variable. However, when implementing COM classes, _stdcall_ convention is used. The following is an overview of the various class method types. 

1) Static Methods
Static methods do not need a class instance, so they work the same way as common functions. No _this_ pointer is passed to them. Thus it's not possible to reliably distinguish static methods from simple functions. Example: 
 

    A::A_static1();call    A::A_static1


2) Simple Methods
Simple methods need a class instance, so _this_ pointer is passed to them as a hidden first parameter, usually using _thiscall_ convention, i.e. in _ecx_ register. When the base object is not situated at the beginning of the derived class, _this_ pointer needs to be adjusted to point to the actual beginning of the base subobject before calling the function. Example: 
 

    ;pC->A_simple1(1);;esi = pCpush    1mov ecx, esicall    A::A_simple1;pC->B_simple1(2,3);;esi = pClea edi, [esi+8] ;adjust thispush    3push    2mov ecx, edicall    B::B_simple1


As you see, _this_ pointer is adjusted to point to the B subobject before calling B's method. 

3) Virtual Methods
To call a virtual method the compiler first needs to fetch the function address from the _vftable_ and then call the function at that address same way as a simple method (i.e. passing _this_ pointer as an implicit parameter). Example: 
 

    ;pC->A_virt2();esi = pCmov eax, [esi]  ;fetch virtual table pointermov ecx, esicall [eax+4]  ;call second virtual method;pC->B_virt1();edi = pClea edi, [esi+8] ;adjust this pointermov eax, [edi]   ;fetch virtual table pointermov ecx, edicall [eax]       ;call first virtual method


4) Constructors and Destructors
Constructors and destructors work similar to a simple method: they get an implicit _this_ pointer as the first parameter (e.g. ecx in case of _thiscall_ convention). Constructor returns the _this_ pointer in eax, even though formally it has no return value. 
 

RTTI Implementation


RTTI (Run-Time Type Identification) is special compiler-generated information which is used to support C++ operators like dynamic_cast<> and typeid(), and also for C++ exceptions. Due to its nature, RTTI is only required (and generated) for polymorphic classes, i.e. classes with virtual functions. 

MSVC compiler puts a pointer to the structure called "Complete Object Locator" just before the vftable. The structure is called so because it allows compiler to find the location of the complete object from a specific vftable pointer (since a class can have several of them). COL looks like following: 
 

struct RTTICompleteObjectLocator
{DWORD signature; //always zero ?DWORD offset;    //offset of this vtable in the complete classDWORD cdOffset;  //constructor displacement offsetstruct TypeDescriptor* pTypeDescriptor; //TypeDescriptor of the com

这篇关于Microsoft Visual C++ 逆向第二部分:类、方法和RTTI的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1108312

相关文章

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

Java循环创建对象内存溢出的解决方法

《Java循环创建对象内存溢出的解决方法》在Java中,如果在循环中不当地创建大量对象而不及时释放内存,很容易导致内存溢出(OutOfMemoryError),所以本文给大家介绍了Java循环创建对象... 目录问题1. 解决方案2. 示例代码2.1 原始版本(可能导致内存溢出)2.2 修改后的版本问题在

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb

一文详解Python中数据清洗与处理的常用方法

《一文详解Python中数据清洗与处理的常用方法》在数据处理与分析过程中,缺失值、重复值、异常值等问题是常见的挑战,本文总结了多种数据清洗与处理方法,文中的示例代码简洁易懂,有需要的小伙伴可以参考下... 目录缺失值处理重复值处理异常值处理数据类型转换文本清洗数据分组统计数据分箱数据标准化在数据处理与分析过

Java中Object类的常用方法小结

《Java中Object类的常用方法小结》JavaObject类是所有类的父类,位于java.lang包中,本文为大家整理了一些Object类的常用方法,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. public boolean equals(Object obj)2. public int ha

golang1.23版本之前 Timer Reset方法无法正确使用

《golang1.23版本之前TimerReset方法无法正确使用》在Go1.23之前,使用`time.Reset`函数时需要先调用`Stop`并明确从timer的channel中抽取出东西,以避... 目录golang1.23 之前 Reset ​到底有什么问题golang1.23 之前到底应该如何正确的

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

Python调用另一个py文件并传递参数常见的方法及其应用场景

《Python调用另一个py文件并传递参数常见的方法及其应用场景》:本文主要介绍在Python中调用另一个py文件并传递参数的几种常见方法,包括使用import语句、exec函数、subproce... 目录前言1. 使用import语句1.1 基本用法1.2 导入特定函数1.3 处理文件路径2. 使用ex

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI