您的位置:首页 > 运维架构 > Linux

Linux审计功能实现两种实现方式

2013-08-16 18:27 337 查看
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命令,他的犯罪记录也不会被抹杀掉的,这就叫做"要想人不知,除非己莫为",不要当坏蛋哦~~~

本文出自 “zhangdh开放空间” 博客,请务必保留此出处http://linuxblind.blog.51cto.com/7616603/1275160
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: