nixCraft Poll

Topics

Linux audit files to see who made changes to a file

Posted by Vivek Gite [Last updated: September 7, 2007]

This is one of the key questions many new sys admin ask:

How do I audit file events such as read / write etc? How can I use audit to see who changed a file in Linux?

The answer is to use 2.6 kernel’s audit system. Modern Linux kernel (2.6.x) comes with auditd daemon. It’s responsible for writing audit records to the disk. During startup, the rules in /etc/audit.rules are read by this daemon. You can open /etc/audit.rules file and make changes such as setup audit file log location and other option. The default file is good enough to get started with auditd.

In order to use audit facility you need to use following utilities
=> auditctl - a command to assist controlling the kernel’s audit system. You can get status, and add or delete rules into kernel audit system. Setting a watch on a file is accomplished using this command:

=> ausearch - a command that can query the audit daemon logs based for events based on different search criteria.

=> aureport - a tool that produces summary reports of the audit system logs.

Note that following all instructions are tested on CentOS 4.x and Fedora Core and RHEL 4/5 Linux.

Task: install audit package

The audit package contains the user space utilities for storing and searching the audit records generate by the audit subsystem in the Linux 2.6 kernel. CentOS/Red Hat and Fedora core includes audit rpm package. Use yum or up2date command to install package
# yum install audit
or
# up2date install audit

Auto start auditd service on boot
# ntsysv
OR
# chkconfig auditd on
Now start service:
# /etc/init.d/auditd start

How do I set a watch on a file for auditing?

Let us say you would like to audit a /etc/passwd file. You need to type command as follows:
# auditctl -w /etc/passwd -p war -k password-file

Where,

In short you are monitoring (read as watching) a /etc/passwd file for anyone (including syscall) that may perform a write, append or read operation on a file.

Wait for some time or as a normal user run command as follows:
$ grep 'something' /etc/passwd
$ vi /etc/passwd

Following are more examples:

File System audit rules

Add a watch on "/etc/shadow" with the arbitrary filterkey "shadow-file" that generates records for "reads, writes, executes, and appends" on "shadow"
# auditctl -w /etc/shadow -k shadow-file -p rwxa

syscall audit rule

The next rule suppresses auditing for mount syscall exits
# auditctl -a exit,never -S mount

File system audit rule

Add a watch "tmp" with a NULL filterkey that generates records "executes" on "/tmp" (good for a webserver)
# auditctl -w /tmp -p e -k webserver-watch-tmp

syscall audit rule using pid

To see all syscalls made by a program called sshd (pid - 1005):
# auditctl -a entry,always -S all -F pid=1005

How do I find out who changed or accessed a file /etc/passwd?

Use ausearch command as follows:
# ausearch -f /etc/passwd
OR
# ausearch -f /etc/passwd | less
OR
# ausearch -f /etc/passwd -i | less
Where,

Output:

----
type=PATH msg=audit(03/16/2007 14:52:59.985:55) : name=/etc/passwd flags=follow,open inode=23087346 dev=08:02 mode=file,644 ouid=root ogid=root rdev=00:00
type=CWD msg=audit(03/16/2007 14:52:59.985:55) :  cwd=/webroot/home/lighttpd
type=FS_INODE msg=audit(03/16/2007 14:52:59.985:55) : inode=23087346 inode_uid=root inode_gid=root inode_dev=08:02 inode_rdev=00:00
type=FS_WATCH msg=audit(03/16/2007 14:52:59.985:55) : watch_inode=23087346 watch=passwd filterkey=password-file perm=read,write,append perm_mask=read
type=SYSCALL msg=audit(03/16/2007 14:52:59.985:55) : arch=x86_64 syscall=open success=yes exit=3 a0=7fbffffcb4 a1=0 a2=2 a3=6171d0 items=1 pid=12551 auid=unknown(4294967295) uid=lighttpd gid=lighttpd euid=lighttpd suid=lighttpd fsuid=lighttpd egid=lighttpd sgid=lighttpd fsgid=lighttpd comm=grep exe=/bin/grep

Let us try to understand output

So from log files you can clearly see who read file using grep or made changes to a file using vi/vim text editor. Log provides tons of other information. You need to read man pages and documentation to understand raw log format.

Other useful examples

Search for events with date and time stamps. if the date is omitted, today is assumed. If the time is omitted, now is assumed. Use 24 hour clock time rather than AM or PM to specify time. An example date is 10/24/05. An example of time is 18:00:00.
# ausearch -ts today -k password-file
# ausearch -ts 3/12/07 -k password-file

Search for an event matching the given executable name using -x option. For example find out who has accessed /etc/passwd using rm command:
# ausearch -ts today -k password-file -x rm
# ausearch -ts 3/12/07 -k password-file -x rm

Search for an event with the given user name (UID). For example find out if user vivek (uid 506) try to open /etc/passwd:
# ausearch -ts today -k password-file -x rm -ui 506
# ausearch -k password-file -ui 506

Other auditing related posts

Further readings

Updated for accuracy.

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. James Musil Says:

    In the line “auditctl -w /etc/passwd -k shadow-file -p rwxa” you mean /etc/shadow not /etc/passwd.

  2. nixcraft Says:

    James,

    Thanks for heads up, post has been updated.

  3. GH Snijders Says:

    Very interesting article, thanks alot.

    I did spot one small detail, though:

    “So from log files you can clearly see who made changes to a file using grep commands.”

    Grep is a tool to *read* files, not change them… ;)

  4. nixcraft Says:

    GH,

    Heh… I was suppose to use vim as an example but somehow I did pickup grep. Anyway post has been updated

    Appreciate your post.

  5. Rodrigo Says:

    Question, i need a file monitor to tell me which files are being used on a few folders, can i use auditd? is it compatible with Redhat 7.3? is there a GUI to use with this?

    If this is not what i need.. can you point me to what i need or something close?

  6. nixcraft Says:

    Rodrigo,

    RH 7.3 does not support auditd; also a big security risk for such old disro.

    Get Cent OS 4.x or FC 6/7

  7. Rodrigo Says:

    Sadly the box running RH 7.3 is a live production box for a multinational company, I cant just get a new OS installed on that server, we will be at least another 6 months before migrating to a new system.

    Do you perhaps have an idea of what tool I could use to monitor files in a folder that have been accessed during a period of time?

    BTW… great site.

  8. motumboe Says:

    Found this article following this link: http://beranger.org/index.php?article=2722

    Two great blogs, my comps
    :-)

  9. nixcraft Says:

    @motumboe, thanks for feedback :D

    @Rodrigo you can write your own perl scripts

  10. links for 2007-04-30 « Donghai Ma Says:

    [...] Linux audit files to see who made changes to a file | nixCraft (tags: linux security tutorial) [...]

  11. Linux Auditing Problems - log file getting large - nixCraft Linux Forum Says:

    [...] two command and try following resources: Article about audit log visualization Audit System FAQ Linux audit files to see who made changes to a file | nixCraft Let me know if you need any further help! __________________ Vivek Play hard stay [...]

  12. Ken Says:

    When I try to set up a file watch, it fails. When I do an auditctl -l, i get this at the bottom:

    File system watches not supported

    Any ideas on whats wrong?

    (btw, I’m guessing that I can get around this by tracing syscalls based on the files’ inode numbers, but thats messy, and hard to maintain…)

  13. tiger74 Says:

    @nixcraft,
    Thank you for such a great article.
    But, I’m confused, it seems that there is no man page for the audit.rules?

    @rodrigo,
    You can use tripwire with similar function. It detects file changes.

  14. ike Says:

    :-) Wow. This is great article.

  15. Ken Says:

    I got the same error:

    File system watches not supported

    Did you ever resolve this?

    Thanks John

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.