如何破解虚拟表?

问题描述 投票:14回答:9

我想知道如何使用Test更改虚拟表中的HackedVTable的地址。

void HackedVtable()
{
    cout << "Hacked V-Table" << endl;
}

class Base
{    
public:
    virtual Test()  { cout <<"base";    }
    virtual Test1() { cout << "Test 1"; }
    void *prt;
    Base(){}
};

class Derived : public Base
{
public: 
    Test()
    {
        cout <<"derived";
    }
};

int main()
{    
    Base b1;

    b1.Test(); // how to change this so that `HackedVtable` should be called instead of `Test`?

    return 0;
}

答案将不胜感激。

提前致谢。

c++ virtual
9个回答
17
投票

这适用于32位MSVC版本(它是一些生产代码的非常简化的版本,已经使用了一年多)。请注意,您的替换方法必须明确指定this参数(指针)。

// you can get the VTable location either by dereferencing the
// first pointer in the object or by analyzing the compiled binary.
unsigned long VTableLocation = 0U;
// then you have to figure out which slot the function is in. this is easy
// since they're in the same order as they are declared in the class definition.
// just make sure to update the index if 1) the function declarations are
// re-ordered and/or 2) virtual methods are added/removed from any base type.
unsigned VTableOffset = 0U;
typedef void (__thiscall Base::*FunctionType)(const Base*);
FunctionType* vtable = reinterpret_cast<FunctionType*>(VTableLocation);

bool hooked = false;
HANDLE process = ::GetCurrentProcess();
DWORD protection = PAGE_READWRITE;
DWORD oldProtection;
if ( ::VirtualProtectEx( process, &vtable[VTableOffset], sizeof(int), protection, &oldProtection ) )
{
    vtable[VTableOffset] = static_cast<FunctionType>(&ReplacementMethod);

    if ( ::VirtualProtectEx( process, &vtable[VTableOffset], sizeof(int), oldProtection, &oldProtection ) )
        hooked = true;
}

9
投票

V-Table是一个实现细节。

编译器不需要使用它(它恰好是实现虚函数的最简单方法)。但是说每个编译器可以(并且确实)稍微不同地实现它,因此对你的问题没有答案。

如果你问我如何破解用于构建的程序的vtable:

编译器<X>版本<Y>构建<Z>

然后有人可能知道答案。


7
投票
void HackedVtable()
{
    cout << "Hacked V-Table" << endl;
}

class Base
{

public:
       virtual Test()  { cout <<"base";    }
       virtual Test1() { cout << "Test 1"; }
       void *prt;
       Base(){}
};

class Derived:public Base
{
    public: 
           Test() 
           {
                   cout <<"derived";
           }
};

typedef void (*FUNPTR)();
typedef struct
{
   FUNPTR funptr;
} VTable;


int main()
{

    Base b1;
    Base *b1ptr = &b;

    VTable vtable;
    vtable.funptr = HackedVtable;

    VTable *vptr = &vtable;
    memcpy ( &b1, &vptr, sizeof(long) );

    b1ptr->Test();

    //b1.Test(); // how to change this so that HackedVtable() should be called instead of Test()

    return 0;
}

1
投票

我不认为有便携的方式。主要是因为编译器优化和每个目标之间的不同架构ABI。

但是C ++为您提供了完全相同的功能,为什么不使用它呢?

void HackedVtable()
{
    cout << "Hacked V-Table" << endl;
}

class Base
{
public:
       virtual Test()  { cout <<"base";    }
       virtual Test1() { cout << "Test 1"; }
       void *prt;
       Base(){}
};

class Derived : public Base
{
    public: 
           Test() 
           {
                HackedVtable(); // <-- NOTE
           }
};

int main()
{
    Derived b1; // <-- NOTE

    b1.Test();

    return 0;
}

1
投票

实现同样的事情的另一种方法是通过检查类似的代码:

GObject的:

http://en.wikipedia.org/wiki/Gobject

GLib的:

http://en.wikipedia.org/wiki/GLib

附:

http://en.wikipedia.org/wiki/Vala_%28programming_language%29

那些人想要使用面向对象和类的编程语言,但“C ++”并不符合他们的要求。然后,他们采用“普通C”,并使用记录和指针模拟对象,包括虚拟方法表。最终得到了一种名为“Vala”的类似语言,他们自己的“C ++”语言(用他们自己的V.M.T.)。

另一个相关链接:

http://en.wikipedia.org/wiki/Virtual_method_table

http://www.artima.com/insidejvm/ed2/jvmP.html

干杯。


1
投票

在Mac OS X 10.10.3 + gcc 4.8.3下,以下代码运行良好。

void HackedVtable()
{
    cout << "Hacked V-Table" << endl;
}

class Base
{    
public:
    virtual void Test()  { cout << "base" << endl;    }
    virtual void Test1() { cout << "Test 1" << endl; }
    void *prt;
    Base(){}
};

class Derived : public Base
{
public: 
    void Test()
    {
        cout << "derived" << endl;
    }
};

int main()
{    
    Base b1;
    Base* pb1 = &b1;

    *(*(void***)pb1) = (void*) HackedVtable;
    pb1->Test();

    //It works for all the Base instance
    Base b2;
    Base* pb2 = &b2;
    pb2->Test();

    //But Derived's virtual function table is separated from Base's
    Derived d1;
    Derived* pd1 = &d1;
    pd1->Test();
    *(*(void***)pd1) = (void*) HackedVtable;
    pd1->Test();

    return 0;
}

它的输出:

$ g++ h.cpp; ./a.out
Hacked V-Table
Hacked V-Table
derived
Hacked V-Table

我在Ubuntu 12.04 + g ++ 4.9.0下测试相同的代码。但是,它不起作用并出现分段错误。似乎Linux在只读区域(例如rodata)中分配虚拟功能表以禁止黑客入侵。


0
投票

好吧很容易搞清楚。找到hte VTAble指针(在visual studio中它的前4/8字节)。然后进入Test的正常调用(进入汇编程序),你会看到它跳转到Vtable然后跳转到你的测试函数。要覆盖测试,只需替换VTable中跳转的指针。


0
投票

这通常被称为“虚拟桌挂钩”或类似的东西。如果您打算使用它,那么我建议使用SourceHook库。它是为游戏模型中的黑客闭源游戏引擎而开发的。例如,它在idTech4成为开源之前用于The Dark Mod


0
投票

我不认为vTable是在只读区域,因为它是动态填充的。它失败的唯一方法是编译器绝对确定在编译时调用哪个实现,并使用直接函数调用(去虚拟化)跳过vTable查找。

编辑:正如@groovyspaceman所指出的那样,我看到我使用了错误的措辞。 vTable类成员指针是可变的,vTable本身是编译器生成的,它依赖于系统和编译器是否可以或不能被修改。

© www.soinside.com 2019 - 2024. All rights reserved.