前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux下用户操作记录审计环境的部署记录

linux下用户操作记录审计环境的部署记录

作者头像
洗尽了浮华
发布2018-01-23 15:21:06
2K0
发布2018-01-23 15:21:06
举报
文章被收录于专栏:散尽浮华散尽浮华

通常,我们运维管理人员需要知道一台服务器上有哪些用户登录过,在服务器上执行了哪些命令,干了哪些事情,这就要求记录服务器上所用登录用户的操作信息,这对于安全维护来说很有必要。废话不多说了,下面直接记录做法:

代码语言:javascript
复制
1)查看及管理当前登录用户
使用w命令查看当前登录用户正在使用的进程信息,w命令用于显示已经登录系统的用户的名称,以及它们正在做的事。该命令所使用的信息来源于/var/run/utmp文件。w命令输出的信息包括:
-> 用户名称
-> 用户的机器名称或tty号
-> 远程主机地址
-> 用户登录系统的时间
-> 空闲时间(作用不大)
-> 附加到tty(终端)的进程所用的时间(JCPU时间)
-> 当前进程所用时间(PCPU时间)
-> 用户当前正在使用的命令
 
[root@test ~]# w
 13:54:14 up 2 days,  2:53,  4 users,  load average: 0.02, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Mon11    3:44m  0.11s  0.06s -bash
test     pts/0    172.16.255.202   10:11    1:48m  0.11s  0.03s vim userinfo.text
nanli    pts/3    172.16.255.196   12:01    1:52m  0.00s  0.00s -bash
work     pts/4    172.116.55.13   12:08    0.00s  0.02s  0.00s w
 
此外,可以使用"who am i"查看使用该命令的用户及进程,使用who查看所有登录用户进程信息,这些查看命令大同小异;
 
2、使用pkill强制退出登录的用户
 
使用pkill可以结束当前登录用户的进程,从而强制退出用户登录,具体使用可以结合w命令;
-> 使用w查看当前登录的用户,注意TTY所示登录进程终端号
-> 使用"pkill –9 -t TTY终端号" 结束该进程所对应用户登录(可根据FROM的IP地址或主机号来判断)
[root@test ~]# pkill -9 pts/4
[root@test ~]# w
 13:59:23 up 2 days,  2:56,  4 users,  load average: 0.02, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Mon11    3:44m  0.11s  0.06s -bash
test     pts/0    172.16.255.202   10:11    1:48m  0.11s  0.03s vim userinfo.text
 
2)查看所有登录用户的操作历史
在Linux系统的环境下,不管是root用户还是其它的用户只有登陆系统后用进入操作我们都可以通过命令history来查看历史记录。可是假如一台服务器多人登陆,一天因为某人误操作了删除
了重要的数据。这时候通过查看历史记录(命令:history)是没有什么意义了(因为history只针对登录用户下执行有效,即使root用户也无法得到其它用户histotry历史)。那有没有什么
办法实现通过记录登陆后的IP地址和某用户名所操作的历史记录呢?答案肯定是有的!
 
通过在/etc/profile文件底部添加以下代码就可以实现:
[root@test ~]# cat /etc/profile
......
#记录每个用户的操作信息
export PS1='[\u@\h \w]# '
history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi
if [ ! -d /opt/history ]
then
mkdir /opt/history
chmod 777 /opt/history
fi
if [ ! -d /opt/history/${LOGNAME} ]
then
mkdir /opt/history/${LOGNAME}
chmod 300 /opt/history/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +"%Y%m%d_%H%M%S"`
export HISTFILE="/opt/history/${LOGNAME}/${USER_IP} history.$DT"
chmod 600 /opt/history/${LOGNAME}/*history* 2>/dev/null
 
[root@test ~]# source /etc/profile     #使得上面配置生效
 
上面脚本在系统的/opt下新建个history目录,记录所有登陆过系统的用户和IP地址(文件名),每当用户登录/退出会创建相应的文件,该文件保存这段用户登录时期内操作历史,可以用这个
方法来监测系统的安全性。
------------------------------------------------------------------------------------------------------------------------------------------
上面的显示跟默认的linux终端显示不太习惯。现在要求终端里切换路径后,只显示当前的简介路径,不显示全部路径,并且后面带上#或$符号,那么只需要将上面的第一行
PS1参数后面的设置如下:
1)只显示当前简介路径,不显示全路径,显示#号。注意下面在"#"符号后面空出一格,这样终端的"#"符号跟命令之间就有了一格的距离,习惯而已!
PS1="[\u@\h \W]# "
2)只显示当前简介路径,不显示全路径,显示$号。注意下面的"$"符号后面空出一格。
PS1="[\u@\h \W]\$ "

这里我在脚本选择第(1)种带"#"号显示(也可以两种都不选,直接将第一行PS1的设置给去掉,这样就是默认的了终端显示.线上使用的话,推荐使用这种默认的),生效后的终
端显示内容和linux默认显示的一样。即export PS1="[\u@\h \W]# "
------------------------------------------------------------------------------------------------------------------------------------------

比如:使用nanli账号操作:
[root@test ~]# su - nanbo
[nanbo@test ~]# echo "hahahahah"
hahahahah
[nanbo@test ~]# cd /usr/local/
[nanbo@test local]# ls
bin  etc  games  include  lib  lib64  libexec  libzip  man  openssl  sbin  share  src
[nanbo@test local]# cat /etc/passwd
[nanbo@test local]# ls /var/log/messages
/var/log/messages

然后退出nanli账号,查看用户操作信息
[nanbo@test local]# logout
[root@test ~]# cd /opt/
[root@test opt]# ls
history  rh
[root@test opt]# cd history/
[root@test history]# ls
nanbo  root
[root@test history]# cd nanbo/
[root@test nanbo]# ls
172.16.255.193 history.20170816_150403
[root@test nanbo]# cat 172.16.255.193\ history.20170816_150403 
#1502867049
echo "hahahahah"
#1502867052
cd /usr/local/
#1502867053
ls
#1502867056
cat /etc/passwd
#1502867062
ls /var/log/messages

过一段时间,root操作记录也会有

代码语言:javascript
复制
[root@test ~]# cd /opt/history/
[root@test history]# ls
nanbo  root
[root@test history]# cd root/
[root@test root]# ll
total 32K
d-wx------ 2 root root 4.0K Aug 16 23:07 .
drwxrwxrwx 4 root root 4.0K Aug 16 15:04 ..
-rw------- 1 root root 2.4K Aug 16 16:43 192.168.1.193 history.20170816_134444
-rw------- 1 root root 1.2K Aug 16 17:05 192.168.1.193 history.20170816_150350
-rw------- 1 root root  251 Aug 16 18:43 192.168.1.202 history.20170816_184256
-rw------- 1 root root   18 Aug 16 20:54 192.168.1.213 history.20170816_205434
-rw------- 1 root root  329 Aug 16 23:07 192.168.1.213 history.20170816_210614
-rw------- 1 root root  185 Aug 16 15:24 172.29.20.24 history.20170816_150535
[root@test root]# cat 192.168.1.193\ history.20170816_134444 
#1502861816
cat /etc/profile
#1502861851
ls
#1502861862
vim /etc/profile
#1502861881
source /etc/profile
#1502861887
cd /usr/local/
#1502861894
vim /etc/profile
#1502861906
source /etc/profile

----------------------------------------------------------------------------------------------------------------------------------------------------------------------- 还有一种方案:这个方案会在每个用户退出登录 时把用户所执行的每一个命令都发送给日志守护进程rsyslogd,你也可通过配置“/etc/rsyslog.conf”进一步将日志发送给日志服务器:

代码语言:javascript
复制
操作如下:
把下面内容添加到/etc/profile文件底部
[root@elk-node1 ~]# vim /etc/profile
........
 
#设置history格式
export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`who am i 2>/dev/null| \
awk '{print $NF}'|sed -e 's/[()]//g'`] "
 
#登录时清空当前缓存
echo "" > .bash_history
 
#记录shell执行的每一条命令
export PROMPT_COMMAND='\
if [ -z "$OLD_PWD" ];then
export OLD_PWD=$PWD;
fi;
if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then
logger -t `whoami`_shell_cmd "[$OLD_PWD]$(history 1)";
fi ;
export LAST_CMD="$(history 1)";
export OLD_PWD=$PWD;'

[root@elk-node1 ~]# sudo /etc/profile

测试:
分别使用kevin,grace账号登陆这台服务器进行一些操作。
然后发现/var/log/message日志文件中已经记录了这两个用户的各自操作了~
[root@elk-node2 ~]# tail -20 /var/log/messages
Oct 24 14:16:04 elk-node2 root_shell_cmd: [/root] 321 [2016-10-24 14:16:04] [gateway] tail -100 /var/log/messages
Oct 24 14:19:09 elk-node2 root_shell_cmd: [/root] 322 [2016-10-24 14:18:51] [gateway] vim /etc/profile
Oct 24 14:19:12 elk-node2 su: (to kevin) root on pts/0
Oct 24 14:19:25 elk-node2 root_shell_cmd: [/root] 315 [2016-10-24 14:19:23] [gateway] tail -f /var/log/messages
Oct 24 14:19:40 elk-node2 kevin_shell_cmd: [/home/kevin] 2 [2016-10-24 14:19:40] [gateway] echo "123456" > test
Oct 24 14:19:43 elk-node2 kevin_shell_cmd: [/home/kevin] 3 [2016-10-24 14:19:43] [gateway] uptime
Oct 24 14:19:45 elk-node2 kevin_shell_cmd: [/home/kevin] 4 [2016-10-24 14:19:45] [gateway] who
Oct 24 14:19:47 elk-node2 kevin_shell_cmd: [/home/kevin] 5 [2016-10-24 14:19:47] [gateway] last
Oct 24 14:19:48 elk-node2 root_shell_cmd: [/root] 323 [2016-10-24 14:19:12] [gateway] su - kevin
Oct 24 14:19:52 elk-node2 su: (to grace) root on pts/0
Oct 24 14:20:00 elk-node2 grace_shell_cmd: [/usr/local/src] 2 [2016-10-24 14:20:00] [gateway] ls
Oct 24 14:20:03 elk-node2 grace_shell_cmd: [/usr/local/src] 3 [2016-10-24 14:20:03] [gateway] date
Oct 24 14:20:11 elk-node2 grace_shell_cmd: [/usr/local/src] 4 [2016-10-24 14:20:11] [gateway] free -m
Oct 24 14:20:12 elk-node2 root_shell_cmd: [/root] 324 [2016-10-24 14:19:52] [gateway] su - grace
Oct 24 14:20:23 elk-node2 root_shell_cmd: [/root] 316 [2016-10-24 14:20:18] [gateway] tail -f /etc/sudoers
Oct 24 14:20:30 elk-node2 root_shell_cmd: [/root] 317 [2016-10-24 14:20:24] [gateway] tail -f /var/log/messages
Oct 24 14:20:35 elk-node2 root_shell_cmd: [/root] 318 [2016-10-24 14:20:35] [gateway] tail -100 /var/log/messages
Oct 24 14:20:46 elk-node2 su: (to kevin) root on pts/0
Oct 24 14:23:42 elk-node2 root_shell_cmd: [/root] 325 [2016-10-24 14:20:46] [gateway] su - kevin
Oct 24 14:23:45 elk-node2 root_shell_cmd: [/root] 326 [2016-10-24 14:23:45] [gateway] cat /etc/profile
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
日志服务
日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档