首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何破解虚拟表?

如何破解虚拟表?
EN

Stack Overflow用户
提问于 2009-10-09 06:27:10
回答 9查看 16.5K关注 0票数 14

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

代码语言:javascript
复制
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;
}
EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2009-10-09 06:57:23

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

代码语言:javascript
复制
// 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;
}
票数 18
EN

Stack Overflow用户

发布于 2009-10-09 06:30:37

V-Table是一个实现细节。

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

如果你问我如何破解一个使用以下命令构建的程序的vtable:

编译器版本构建

那么可能会有人知道答案。

票数 11
EN

Stack Overflow用户

发布于 2009-10-09 11:54:52

代码语言:javascript
复制
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;
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1542108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档