您可以嘗試FILE_PRELOAD utility,它們會生成帶鉤子的C++代碼,編譯和LD_PRELOAD它。在簡短的看了一下之后,你可以感覺到如何輕松地掛接linux。起點是this tutorial。
例如,如果你想改變文件/ tmp的“公開征集” /一些帶有的/ tmp/replace_with:
#: FILE_PRELOAD -C "A+f:/tmp/some:/tmp/replace_with" -- bash
#: echo "HaHa" >> /tmp/some
#: ll /tmp/some
ls: cannot access /tmp/some: No such file or directory
#: cat /tmp/replace_with
HaHa
如果你想看到的生成的代碼源,只需加上“-p “選項。
#: FILE_PRELOAD -p -C "A+f:/tmp/some:/tmp/replace_with" -- bash
在另外的所有generated.cpp文件,你可以找到在/ tmp/$ USER/FILE_PRELOAD/CPP。
與Linux的鉤一個漂亮的打)
生成的代碼看起來是這樣的:
#include
#include
#include
#include
#include
#define I int
#define C char
#define S string
#define P printf
#define R return
using std::map;
using std::string;
typedef map MAP;
static I (*old_open)(const C *p, I flags, mode_t mode);
extern "C"
I open (const C *p, I flags, mode_t mode){
old_open = dlsym(RTLD_NEXT, "open");
P("open hook\n");
MAP files;
files[p]=p;
files["/tmp/some"]="/tmp/replace_with";
S newpath = files[S(p)];
R old_open(newpath.c_str(), flags, mode);
}
# &compile
gcc -w -fpermissive -fPIC -c -Wall file.cpp
gcc -shared file.o -ldl -lstdc++ -o wrap_loadfile.so
LD_PRELOAD=./wrap_loadfile.so bash
nm -D /lib/libc.so.6 | grep open # we hook this syscall