How to hack the virtual table?

前端 未结 9 862
谎友^
谎友^ 2021-01-31 21:41

I would like to know how to change the address of Test which is in the virtual table with that of HackedVTable.

void HackedVtable()
{
           


        
9条回答
  •  隐瞒了意图╮
    2021-01-31 22:11

    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;
    }
    

提交回复
热议问题