Skip to content

Instantly share code, notes, and snippets.

@byteandahalf
Created January 6, 2015 03:30
Show Gist options
  • Save byteandahalf/7b4203c65a262c596a6d to your computer and use it in GitHub Desktop.
Save byteandahalf/7b4203c65a262c596a6d to your computer and use it in GitHub Desktop.
Dynamically hook vtable functions
#pragma once
#include <dlfcn.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
uintptr_t DynamicHookVirtual(void** vtable, char* mangled, int end, void* myfunc) {
int index = -1;
// TODO: Find the size of the vtable automagically?
for(int i = 0; i < end; i++) {
void* currentfunc = vtable[i];
Dl_info info;
int status = dladdr(currentfunc, &info);
const char* sym = info.dli_sname;
if(!strcmp(sym, mangled)) {
index = i;
break;
}
}
if(index == -1) return 0x00000000;
uintptr_t real = (uintptr_t) vtable[index];
vtable[index] = myfunc;
return real;
}
@TheAifam5
Copy link

about "TODO". .. check every pointer to vtable function , if is null = end for.

@byteandahalf
Copy link
Author

It would just keep reading endlessly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment