Question

Is it possible to call a known function (We know the function type, name and argumets) inside a program using DLL Injection?

For example there is following function in my program:

void func(string text)
{
    cout << text << endl;
}

How to call it from a DLL?

Was it helpful?

Solution

It is possible to call an arbitrary function given only prototype and it's address in memory, however you have to be able to call it on the other process address space (this can be done by linking to shared library or attaching debugger)

typedef int func(void);
func* f = (func*)0xabcd123;
int i = f(); // execute

However I have tried also copy the code to executable part of memory and running it with something like this:

  char code[] = {0x55,0x48,0x89,0xe5,0x89,0x7d,0xfc,0x48,0x89,0x75,
                 0xf0,0xb8,0x2a,0x00,0x00,0x00,0xc9,
                 0xc3,0x00};    // it is just compiled: return  42 
                                // (with prologue, etc)
  void *buf;

  /* copy code to executable buffer */    
  buf = mmap (0,sizeof(code),PROT_READ|PROT_WRITE|PROT_EXEC,
              MAP_PRIVATE|MAP_ANON,-1,0);
  memcpy (buf, code, sizeof(code));

  /* run code */
  int i = ((int (*) (void))buf)();
  printf("the code returned: %d\n", i);

so if you know the size of code to execute it looks as possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top