linux操作日志审计

#日常工作中服务器很多,也会出现很多问题,但是出现问题怎么查询原因呢?history,last可以查看到一些记录,但是并不完整,也无法准确追溯到谁,在什么时间,执行了什么,这个时候如果服务器被黑,或者被恶作剧,就无法快速准确判断,并定位问题,如果开启了操作日志审计就可以很好的判断问题,同时集中管理归档,增加系统的安全性.

#环境

系统:CentOS Linux release 7.4.1708 (Core)

客户端:172.16.10.25

服务端:172.16.10.181

#工具

1,logger

logger是一个shell接口,可以通过该接口使用rsyslog的日志模块。

Usage:
 logger [options] [message]

Options:
 -T, --tcp             use TCP only
 -d, --udp             use UDP only
 -i, --id              log the process ID too
 -f, --file <file>     log the contents of this file
 -h, --help            display this help text and exit
 -S, --size <num>      maximum size for a single message (default 1024)
 -n, --server <name>   write to this remote syslog server
 -P, --port <port>     use this port for UDP or TCP connection
 -p, --priority <prio> mark given message with this priority
 -s, --stderr          output message to standard error as well
 -t, --tag <tag>       mark every line with this tag
 -u, --socket <socket> write to this Unix socket
 -V, --version         output version information and exit

2,rsyslog

rsyslog是syslog的加强版,可以用作客户端及服务器,我们可以使用local0~local7来自定义设备传输至rsyslog。

3,prompt_command

Linux系统的环境变量PROMPTCOMMAND的内容会在bash提示符显示之前被执行。该环境变量的默认值是 history -a 功能是将目前新增的history追加到histfiles 中,默认写入隐藏文件~/.bashhistory中

#部署

1,部署rsyslog日志服务器server端

vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP, 172.16.0.0/16
$template IpTemplate,"/var/log/remote/%FROMHOST-IP%/%$YEAR%-%$MONTH%-%$DAY%.log"
:fromhost-ip, !isequal, "127.0.0.1" ?IpTemplate
& ~



$ModLoad imudp            #配置服务开启udp协议
$UDPServerRun 514         #配置udp 514为服务运行端口
$AllowedSender UDP        #配置udp的白名单
$template IpTemplate      #配置模板,以客户端ip为目录,以日期命名文件
:fromhost-ip, !isequal, "127.0.0.1" ?IpTemplate      #把非本地传输的日志按照指定的模板存放
& ~                       #& 表示已经匹配处理的内容,~ 表示不再进行其他处理

#重启服务
systemctl restart rsyslog

 

2,部署客户端rsyslog配置

1,配置prompt_command

vim /etc/bashrc
readonly PROMPT_COMMAND='logger -p local3.notice -t bash "$(ifconfig | grep -E "eth|em" -A 1 | grep "10.10" | grep -oP "(?<=addr:)[\d\.]+")  $(who am i |awk "{print \$1\" \"\$2\" \"\$3\" \"\$4\" \"\$5}") [`pwd`] currentuser=$(whoami) command=$(history 1 | { read x cmd; echo "$cmd"; })"'

#load环境变量,!!!这步贼重要,如果不执行恐无法记录当前操作
source /etc/bashrc

local3.notice 使我们自定义的设备,用于rsyslog调用; 
bash 是我们为每行打印的信息打印的tag; 
ifconfig | grep -E “eth|em” -A 1 | grep “10.10” | grep -oP “(?<=addr:)[\d.]+用于获取我们服务器的ip; 
who am i |awk “{print $1\” \”$2\” \”$3\” \”$4\” \”$5}”用于获取我们当前用户的登录信息; 
pwd用于列出我们当前所在的目录; 
whoami用于获取我们当前切换的执行命令的用户,例如我们从test 用户 sudo -i,执行命令的用户为root,但是登录的用户test,方便我们区分; 
command 是我们当前用户执行的命令。

注意: 
1.我们需要在/etc/bashrc或/etc/profile中添加环境变量,用于所有用户。 
2.export PROMPT_COMMAND 如果将PROMPT_COMMAND导出到用户工作区,那么对于有经验的用户就可以做赋值操作 export PROMPT_COMMAND =“” ,简单的语法就会导致记录功能当前session端不可用,所以PROMPT_COMMAND必须设置成只读的属性,readonly PROMPT_COMMAND

2,配置rsyslog客户端

vim /etc/rsyslog.conf
*.info;mail.none;authpriv.none;cron.none;local3.none                /var/log/messages
local3.notice /var/log/audit.log
local3.notice @172.16.10.181

#重启服务
systemctl restart rsyslog


*.info;mail.none;authpriv.none;cron.none;local3.none    #设置local3不写入message 
local3.notice /var/log/audit.log     #保存到本地的文件
local3.notice @172.16.10.181         #发给远程日志服务器

local3.notice 是在logger中定义的设备,notice是打印日志的级别,用于rsyslog调用并将打印信息输出至指定文件。

3,配置轮转日志

vim /etc/logrotate.d/rsyslog
/var/log/audit.log{
    daily
    rotate 4
    missingok
    notifempty
    nocompress
    create
    dateext
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

#手动强制轮转
logrotate -vf /etc/logrotate.d/rsyslog

#测试

1,客户端

[root@manage-dns25-ctos7 ~]# tail /var/log/audit.log 
Jan 26 17:26:25 manage-dns25-ctos7 bash:  admin pts/0 2018-01-26 10:34 (172.16.1.12) [/root] currentuser=root command=tail -F xferin.log
Jan 26 17:26:28 manage-dns25-ctos7 bash:  admin pts/0 2018-01-26 10:34 (172.16.1.12) [/root] currentuser=root command=cat /etc/redhat-release

2,服务端

[root@test-yumwei10_181-ctos ~]# tail /var/log/remote/172.16.1
172.16.10.21/ 172.16.10.25/ 172.16.10.80/ 172.16.12.1/  172.16.12.2/  
[root@test-yumwei10_181-ctos ~]# tail /var/log/remote/172.16.10.25/2018-01-26.log 
Jan 26 17:26:25 manage-dns25-ctos7 bash:  admin pts/0 2018-01-26 10:34 (172.16.1.12) [/root] currentuser=root command=tail -F xferin.log
Jan 26 17:26:28 manage-dns25-ctos7 bash:  admin pts/0 2018-01-26 10:34 (172.16.1.12) [/root] currentuser=root command=cat /etc/redhat-release
Jan 26 17:28:00 manage-dns25-ctos7 bash:  admin pts/0 2018-01-26 10:34 (172.16.1.12) [/root] currentuser=root command=tail /var/log/audit.log

 

此条目发表在linux分类目录。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注