linux下的文件审计功能(audit inotify)


为了满足这样的需求:记录文件变化、记录用户对文件的读写,甚至记录系统调用,文件变化通知。


本文介绍auditinotify.


什么是audit
The Linux Audit Subsystem is a system to Collect information regarding events occurring on the system(s) 
Kernel events (syscall events)
User events (audit-enabled programs)
syslog
会记录系统状态(硬件警告、软件的log), syslog属于应用层, log归咎与软件, 并不会记录所有动作. 于是audit来记录更多信息。

sudo apt-get install auditd
audit 命令
auditctl audit
系统管理工具,用来获取状态,增加删除监控规则。
ausearch
查询 audit log 工具
aureport
输出 audit 系统报告


auditctl
示例
auditctl -w /etc/passwd -p war -k password_file
auditctl -w /tmp -p e -k webserver_watch_tmp
-w
监控文件路径 /etc/passwd, 
-p
监控文件筛选 r( ) w( ) x( 执行 ) a( 属性改变 )
-k
筛选字符串,用于查询监控日志
auditctl -a exit,never -S mount
auditctl -a entry,always -S all -F pid=1005
-S
监控系统调用
-F
给出更多监控条件 (pid/path/egid/euid )


日志查询
设置了监控后,会在 /var/log/audit/audit.log 里出现日志。
可以用此命令查看日志:
ausearch -f /etc/passwd -x rm
-k  
利用 auditctl 指定的 key 查询
-x  
执行程序
# ausearch -ts today -k password-file
# ausearch -ts 3/12/07 -k password-file
-ts
指定时间后的 log (start time)
-te
指定时间前的 log (end time)


audit

libaudit
libaudit-python
不过完全找不到文档。我也觉得这个东西用得上的时候不多。除非 .....




下文介绍 inotify




什么是 inotify
inotify
是文件系统事件监控机制,是细粒度的、异步的机制。
inotify is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications. It replaces an earlier facility, dnotify, which had similar goals.


原理和实现
http://www.ibm.com/developerworks/cn/linux/l-inotifynew/
http://en.wikipedia.org/wiki/Inotify


inotifywait in shell 

此命令会在 inotify 事件发生的时候阻塞,使之便于脚本应用。
简单的示例:

1.  #!/bin/bash  

2.  inotifywait -mrq --event create,delete,modify,move --format '%w %e' /path | while read w e; do  

3.      if [ "$e" = "IGNORED" ]; then  

4.          continue  

5.      fi  

6.      rsync -az --delete $w username@ip:$w  

7.  done  



另外一个例子:

1.  #!/bin/sh  

2.  inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %f' \  

3.      -e close_write /home/ottocho | while read date time file;   

4.  do  

5.      rsync /home/ottocho/${file} ottocho::backup   

6.      echo "Inof: ${date} ${time}, ${file} backed up"  

7.  done  




inotify
rsync
很多人利用inotifyrsync实现实时同步文件,而基于inotify APIrsync commandsersync解决了这个问题。  
http://code.google.com/p/sersync/


python
inotify
inotify
有两个python,pyinotifyinofityx.区别在inotifyx的官网上作者如是说道:
inotifyx
C的拓展,没有使用ctypes,因此速度更快、在inotifyAPI变化时会更少出现问题。inotifyxinotify更轻的封装。pyinotify更复杂,它提供了很多inotifyx没有的特性,可这些特性在大多数情况下未必需要。inotifyx提供的API是基本不变的简易的。
http://www.alittletooquiet.net/software/inotifyx/
http://pyinotify.sourceforge.net/


inotifyx

以下是一个源自源代码的例子,非常简易,注释就不必了吧

1.  #!/usr/bin/env python   

2.    

3.  import pyinotify  

4.  import os  

5.  import sys  

6.    

7.  class WatchLogProceesing(pyinotify.ProcessEvent):  

8.      def process_IN_CLOSE_WRITE(self, event):  

9.          print "processing %s" % (event.pathname)  

10.    

11. def monitor_dir(directory):  

12.     wmn = pyinotify.WatchManager()  

13.     notifier = pyinotify.Notifier(wmn, WatchLogProceesing())  

14.     wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)  

15.     notifier.loop()  

16.           

17. monitor_dir("/home/ottocho")  



pyinotify

pyinotify
是更出名功能更全面的库。以下是一个超级简单的示例。更多详情:http://pyinotify.sourceforge.net/

1.  #!/usr/bin/env python   

2.    

3.  import pyinotify  

4.  import os  

5.  import sys  

6.    

7.  class WatchLogProceesing(pyinotify.ProcessEvent):  

8.      def process_IN_CLOSE_WRITE(self, event):  

9.          print "processing %s" % (event.pathname)  

10.    

11. def monitor_dir(directory):  

12.     wmn = pyinotify.WatchManager()  

13.     notifier = pyinotify.Notifier(wmn, WatchLogProceesing())  

14.     wmn.add_watch(directory, pyinotify.IN_CLOSE_WRITE)  

15.     notifier.loop()  

16.           

17. monitor_dir("/home/ottocho")  

 



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM