您可以尝试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