Posso agganciare le funzioni in librerie collegate?
-
01-10-2019 - |
Domanda
Utilizzando EasyHook ho agganciato con successo entrambe le funzioni esportate e conosciute funzioni vtable per le varie classi C ++. In tutti questi casi indirizzare programmi hanno DLL utilizzati.
fornito So che l'indirizzo del punto di ingresso di una funzione, è possibile fare lo stesso quando una libreria è stata collegata al programma di destinazione invece di essere una libreria separata?
Soluzione
Appare con EasyHook è possibile collegare qualsiasi subroutine il cui indirizzo è calcolabile.
Nel mio caso aggancio statico legati SSL_read e SSL_write in OpenSSL era così semplice come identificare gli spostamenti con il mio preferito debugger e quindi l'installazione di ganci.
// delegate for EasyHook:
[UnmanagedFunctionPointer(CallingConvention.Cdecl,
SetLastError = true, CharSet = CharSet.Ansi)]
delegate Int32 SLL_readDelegate(IntPtr SSL_ptr, IntPtr buffer, Int32 length);
// import SSL_read (I actually did it manually, but this will work in most cases)
/* proto from ssl_lib.c -> int SSL_read(SSL *s,void *buf,int num) */
[DllImport("ssleay32.dll", SetLastError = true)]
public static extern Int32 SSL_read(IntPtr ssl, IntPtr buffer, Int32 len);
// the new routine
static Int32 SSL_readCallback(IntPtr SSL_ptr, IntPtr buffer, Int32 length)
{
/* call the imported SSL_read */
int ret = SSL_read(SSL_ptr, buffer, length);
/* TODO: your code here, e.g:
* string log_me = Marshal.PtrToString(buffer, ret);
*/
return ret;
}
Ora tutto quello che resta è quello di installare il gancio:
private LocalHook sslReadHook;
public void Run(RemoteHooking.IContext InContext, String InArg1)
{
// ... initialization code omitted for brevity
/* the value for ssl_read_addr is made up in this example
* you'll need to study your target and how it's loaded(?) to
* identify the addresses you want to hook
*/
int ssl_read_addr = 0x12345678; /* made up for examples sake */
sslReadHook = LocalHook.Create(new IntPtr(ssl_read_addr),
new SSL_readDelegate(SSL_readCallback), this);
// ...
}
Devo dire che in questo esempio avrete bisogno libeay32.dll e ssleay32.dll quanto quest'ultimo dipende dal primo.
Happy aggancio!