歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux審計功能實現兩種實現方式

Linux審計功能實現兩種實現方式

日期:2017/2/28 14:44:25   编辑:Linux教程

Linux審計功能實現的兩種實現方式:由於當前網絡安全很問題很突出,黑客們又都很強大,不怕外患,像小公司不足被黑客盯住,就怕內憂,歷史記錄怎麼能少呢?linux系統本身雖然提供了歷史命令的記錄功能(記錄在:~/.bash_history),默認也能記錄1000條之多,但是容易被清除,依然不安全,所以需要采取別的方式記錄下服務器操作的日志,這樣"壞蛋"將無處可遁,下面將介紹兩種簡單的實現方式:

第一種:

#將下面這段內容添加在/etc/profile文件末尾,完事後執行source /etc/profile使之生效。

HISTSIZE=1000

HISTTIMEFORMAT="%Y/%m/%d %T ";export HISTTIMEFORMAT

export HISTORY_FILE=/var/log/audit.log

export PROMPT_COMMAND='{ thisHistID=`history 1|awk "{print \\$1}"`;lastCommand=`history 1| awk "{\\$1=\"\" ;print}"`;user=`id -un`;whoStr=(`who -u am i`);realUser=${whoStr[0]};logMonth=${whoStr[2]};logDay=${whoStr[3]};logTime=${whoStr[4]};pid=${whoStr[6]};ip=${whoStr[7]};if [ ${thisHistID}x != ${lastHistID}x ];then echo -E `date "+%Y/%m/%d %H:%M:%S"` $user\($realUser\)@$ip[PID:$pid][LOGIN:$logMonth $logDay $logTime] --- $lastCommand ;lastHistID=$thisHistID;fi; } >> $HISTORY_FILE'

#然後便可查看是否生效了呢?

[root@test2 ~]# cat /var/log/audit.log

2013/08/14 14:18:42 root(root)@[PID:(192.168.101.110)][LOGIN:2013-08-14 14:18 .] --- 2013/08/09 09:22:57 cat /etc/sysctl.conf

2013/08/14 14:19:16 root(root)@[PID:(192.168.101.110)][LOGIN:2013-08-14 14:18 .] --- 2013/08/14 14:19:16 cd /usr/local/nginx/conf/sites-enabled/

2013/08/14 14:19:17 root(root)@[PID:(192.168.101.110)][LOGIN:2013-08-14 14:18 .] --- 2013/08/14 14:19:17 ll

2013/08/14 14:19:27 root(root)@[PID:(192.168.101.110)][LOGIN:2013-08-14 14:18 .] --- 2013/08/14 14:19:27 cat awstats.conf

2013/08/14 14:21:04 root(root)@[PID:(192.168.101.110)][LOGIN:2013-08-14 14:18 .] --- 2013/08/14 14:21:04 cat /etc/profile

第二種:

#將下面這段內容添加在/etc/profile文件末尾,完事後執行source /etc/profile使之生效。

function log2syslog

{

declare command

command=$(fc -ln -0)

logger -p local1.notice -t bash -i — $SSH_CLIENT :$USER : $command

}

trap log2syslog DEBUG

[root@test2 u1]# tail -f -n100 /var/log/messages

Aug 16 18:22:36 test2 bash[4460]: — 192.168.101.116 63383 22 :root : vim /etc/profile

第二種方式目前有一個缺陷就是每次記錄的命令,同一條會出現多次,這是待完善的地方。

#哈哈,就算"壞蛋"執行了history -c命令,他的犯罪記錄也不會被抹殺掉的,這就叫做"要想人不知,除非己莫為",不要當壞蛋哦~~~

相關閱讀: Linux 系統添加操作記錄審計 http://www.linuxidc.com/Linux/2013-01/78748.htm

Copyright © Linux教程網 All Rights Reserved