Ansible 安全 之【命令审计】

服务器记录命令

实现该功能要求如下:
1.接受审计的登录用户默认shell必须为bash
2.bash版本至少3.00或以上

需要该要求的原因是实现功能的方法需要用到history命令的HISTTIMEFORMAT变量和PROMPT_COMMAND变量.
对于其他的shell我并未测试.如果其他shell可以实现这两个变量的功能那么理论上也可以使用.

实现方法如下:
使用root用户 操作

1.创建一个审计日志文件 
mkdir /var/log/shell_audit
touch /var/log/shell_audit/audit.log
2.将日志文件所有者赋予一个最低权限的用户  
chown nobody:nobody /var/log/shell_audit/audit.log
3.给该日志文件赋予所有人的写权限  
chmod 002 /var/log/shell_audit/audit.log
4.设置文件权限,使所有用户对该文件只有追加权限  
chattr +a /var/log/shell_audit/audit.log

5.编辑/etc/profile
在末尾添加下面内容
HISTSIZE=2048
HISTTIMEFORMAT="%Y/%m/%d %T   ";export HISTTIMEFORMAT
export HISTORY_FILE=/var/log/shell_audit/audit.log
export PROMPT_COMMAND='{ code=$?;thisHistID=`history 1|awk "{print \\$1}"`;lastCommand=`history 1| awk "{\\$1=\"\" ;print}"`;user=`id -un`;whoStr=(`who -u am i`);realUser=${whoStr[0]};logDay=${whoStr[2]};logTime=${whoStr[3]};pid=${whoStr[5]};ip=${whoStr[6]};if [ ${thisHistID}x != ${lastHistID}x ];then echo -E `date "+%Y/%m/%d %H:%M:%S"` $user\($realUser\)@$ip[PID:$pid][LOGIN:$logDay $logTime] --- [$PWD]$lastCommand [$code];lastHistID=$thisHistID;fi; } >> $HISTORY_FILE' 
 

重新登录后,即可看到/var/log/shell_audit/audit.log刷新的实时日志
2017/07/08 16:12:00 root(root)@(192.168.77.1)[PID:127876][LOGIN:2017-07-08 15:55] --- [/root] 2017/07/08 16:12:00 whoami [0]
2017/07/08 16:17:41 root(root)@(192.168.77.1)[PID:127876][LOGIN:2017-07-08 15:55] --- [/root] 2017/07/08 16:17:41 logrotate -vf /etc/logrotate.d/shell_audit [0]
2017/07/08 16:19:18 root(root)@(192.168.77.1)[PID:127876][LOGIN:2017-07-08 15:55] --- [/root] 2017/07/08 16:19:17 last [0]
2017/07/08 16:19:19 root(root)@(192.168.77.1)[PID:127876][LOGIN:2017-07-08 15:55] --- [/root] 2017/07/08 16:19:19 list [127]
2017/07/08 16:19:21 root(root)@(192.168.77.1)[PID:127876][LOGIN:2017-07-08 15:55] --- [/root] 2017/07/08 16:19:21 what [127]
2017/07/08 16:19:32 root(root)@(192.168.77.1)[PID:127876][LOGIN:2017-07-08 15:55] --- [/root] 2017/07/08 16:19:29 top [0]
2017/07/08 16:19:35 root(root)@(192.168.77.1)[PID:127876][LOGIN:2017-07-08 15:55] --- [/root] 2017/07/08 16:19:35 ps aux [0]


2017/07/08 16:12:00 记录时间/命令执行完成时间
root(root)  执行命令的用户(最初登录的用户) 
@(192.168.77.1) 登录的IP
[PID:127876]    最初登录时的LOGIN产生的PID
[LOGIN:2017-07-08 15:55]    命令执行开始的时间
[/root] 命令执行的当前目录
2017/07/08 16:12:00 命令执行开始的时间
whoami  执行的命令
[0] 命令返回的状态码

6. 设置audit.log的日志轮换
~]# cat /etc/logrotate.d/shell_audit 
/var/log/shell_audit/audit.log { 
    weekly  
    missingok 
    dateext 
    rotate 100
    sharedscripts 
    prerotate 
    /usr/bin/chattr -a /var/log/shell_audit/audit.log 
    endscript 
    sharedscripts 
    postrotate 
      /bin/touch /var/log/shell_audit/audit.log
      /bin/chmod 002 /var/log/shell_audit/audit.log
      /bin/chown nobody:nobody /var/log/shell_audit/audit.log
      /usr/bin/chattr +a /var/log/shell_audit/audit.log
    endscript 
}

可以测试一下!刚跑过了一次。
~]# logrotate -vf /etc/logrotate.d/shell_audit 
reading config file /etc/logrotate.d/shell_audit
reading config info for /var/log/shell_audit/audit.log 

Handling 1 logs

rotating pattern: /var/log/shell_audit/audit.log  forced from command line (100 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/shell_audit/audit.log
  log needs rotating
rotating log /var/log/shell_audit/audit.log, log->rotateCount is 100
dateext suffix '-20170708'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
running prerotate script
renaming /var/log/shell_audit/audit.log to /var/log/shell_audit/audit.log-20170708
running postrotate script

客户端记录日志

我们在使用xshell的时候,可以设置日志记录。

image.png

重新连接,在xshell窗口输入命令,该该窗口的所有信息都会记录到日志文件中。

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 14,301评论 2 33
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,037评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,984评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,126评论 6 342
  • 我去你去过的城市 走过你走过的街区 看不见你的脚步 就想象着你行色匆匆的样子 也许在地铁线交错的时候 曾为失之交臂...
    码上说阅读 1,873评论 0 1

友情链接更多精彩内容