Linux钩子拦截删除文件,在Linux中保存钩子文件

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值