zoukankan      html  css  js  c++  java
  • 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")  

     


  • 相关阅读:
    day02操作系统/编程语言分类/python解释器介绍/python解释器多版本共存
    网络编程-互联网协议(网络编程)/基于TCP协议的套接字通信/加上通信循环/加上连接循环
    每天新的英语单词
    re模块(正则表达式)
    包的使用/time与datetime模块/random模块/打印进度条/shutil模块
    模块的使用之import/模块的使用之 from import/区分python文件的两种用途
    列表生成式与生成器表达式模块的使用模块的使用之from.......import区分python文件的两种用途文件的搜索路径
    三元表达式/函数的递归/匿名函数及其应用/map函数/reduce函数/ filter函数
    函数的嵌套/名称空间/作用域/函数对象/闭包函数
    跟未名学Office
  • 原文地址:https://www.cnblogs.com/ahuo/p/2653905.html
Copyright © 2011-2022 走看看