yankuncheng / plthook

Hook function calls by replacing PLT(Procedure Linkage Table) entries.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PLTHook

Build Status Build status

What is plthook.

A utility library to hook library function calls issued by specified object files (executable and libraries). This modifies PLT (Procedure Linkage Table) entries in ELF format used on most Unixes or IAT (Import Address Table) entries in PE format used on Windows.

Usage

If you have a library libfoo.so.1 and want to intercept a function call recv() without modifying the library, put plthook.h and plthook_elf.c, plthook_win32.c or plthook_osx.c in your source tree and add the following code.

/* This function is called instead of recv() called by libfoo.so.1  */
static ssize_t my_recv(int sockfd, void *buf, size_t len, int flags)
{
    ssize_t rv;
    
    ... do your task: logging, etc. ...
    rv = recv(sockfd, buf, len, flags); /* call real recv(). */
    ... do your task: logging, check received data, etc. ...
    return rv;
}
    
int install_hook_function()
{
    plthook_t *plthook;
    
    if (plthook_open(&plthook, "libfoo.so.1") != 0) {
        printf("plthook_open error: %s\n", plthook_error());
        return -1;
    }
    if (plthook_replace(plthook, "recv", (void*)my_recv, NULL) != 0) {
        printf("plthook_replace error: %s\n", plthook_error());
        plthook_close(plthook);
        return -1;
    }
    plthook_close(plthook);
    return 0;
}

Another Usage

PLTHook provides a function enumerating PLT/IAT entries.

void print_plt_entries(const char *filename)
{
    plthook_t *plthook;
    unsigned int pos = 0; /* This must be initialized with zero. */
    const char *name;
    void **addr;

    if (plthook_open(&plthook, filename) != 0) {
        printf("plthook_open error: %s\n", plthook_error());
        return -1;
    }
    while (plthook_enum(plthook, &pos, &name, &addr) == 0) {
        printf("%p(%p) %s\n", addr, *addr, name);
    }
    plthook_close(plthook);
    return 0;
}

Supported Platforms

Platform source file
Linux i386 and x86_64 plthook_elf.c
Windows 32-bit and x64 (MSVC, Mingw32 and Cygwin) plthook_win32.c
macOS plthook_osx.c
Solaris x86_64 plthook_elf.c
FreeBSD i386 and x86_64 except i386 program on x86_64 OS plthook_elf.c

IMO, unix-like platforms except AIX and HP-UX pa-risc(32bit) could be supported by plthook_elf.c with small modification as long as the object file format is ELF.

License

2-clause BSD-style license.

About

Hook function calls by replacing PLT(Procedure Linkage Table) entries.


Languages

Language:C 98.0%Language:Makefile 2.0%