C++ Hooking

(using Dev-C++ 5.11 on Windows)
compile and run following .cpp file
Virtual Function Hook
---------------------------------------------------------------------------------------------------------------------------
#include<iostream>

#include<windows.h>

using namespace std;

class C{
    public:
    virtual void VF(int);
};

void C::VF(int a){
    printf("VF is called\n") ;
}

typedef void (__thiscall* VF_type) (int );
VF_type org_VF;

void __fastcall hk_VF(int a){
    printf("hook func is called.\n");
   
    org_VF(a);
}
int main(){
    C* pC = new C();
    void** base = *(void***)pC;
   
    org_VF = (VF_type)base[0];
    org_VF(0);
   
    //DWORD oldProtection;
    //VirtualProtect(base, 4, PAGE_EXECUTE_READWRITE, &oldProtection);
   
    base[0] = (void*) &hk_VF;
   
    //VirtualProtect(base, 4, oldProtection, 0);
    pC->VF(0);
    return 0;
---------------------------------------------------------------------------------------------------------------------------
uncomment above code will run perfectly.

留言

這個網誌中的熱門文章

組合語言 Assembly Language