摘要:1、起因最近linux服务器上一些文件呗篡改,想追查已经查不到记录了,所以得想个办法记录下所有用户的操作记录。一般大家通常会采用history来记录,但是history有个缺陷就是默认是1000行,当然你也可以vim/etc/profile将1000修改成1000000行,但是这只是比较笼统的做法,看不到详细的用户来源已经操作记录,比如来源ip地址、操作时间、操作用户等。所以我们不得不自己写代码来实现这样的功能。2、自动记录脚本编写脚本如下:historyUSER=`whoa
1、起因
最近linux服务器上一些文件呗篡改,想追查已经查不到记录了,所以得想个办法记录下所有用户的操作记录。
一般大家通常会采用history来记录,但是history有个缺陷就是默认是1000行,当然你也可以vim /etc/profile将1000修改成1000000行,但是这只是比较笼统的做法,看不到详细的用户来源已经操作记录,比如来源ip地址、操作时间、操作用户等。
所以我们不得不自己写代码来实现这样的功能。
2、自动记录脚本
编写脚本如下:
USER=whoami
USER_IP=who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'
if [ “
U
S
E
R
I
P
"
=
"
"
]
;
t
h
e
n
U
S
E
R
I
P
=
‘
h
o
s
t
n
a
m
e
‘
f
i
i
f
[
!
−
d
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
]
;
t
h
e
n
m
k
d
i
r
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
c
h
m
o
d
777
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
f
i
i
f
[
!
−
d
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
/
USER_IP" = "" ]; then USER_IP=`hostname` fi if [ ! -d /var/log/history ]; then mkdir /var/log/history chmod 777 /var/log/history fi if [ ! -d /var/log/history/
USERIP"=""];thenUSERIP=‘hostname‘fiif[!−d/var/log/history];thenmkdir/var/log/historychmod777/var/log/historyfiif[!−d/var/log/history/{LOGNAME} ]; then
mkdir /var/log/history/
L
O
G
N
A
M
E
c
h
m
o
d
300
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
/
{LOGNAME} chmod 300 /var/log/history/
LOGNAMEchmod300/var/log/history/{LOGNAME}
fi
export HISTSIZE=4096
DT=date +"%Y%m%d_%H:%M:%S"
export HISTFILE=”/var/log/history/
L
O
G
N
A
M
E
/
{LOGNAME}/
LOGNAME/{USER}@KaTeX parse error: Expected group after '_' at position 10: {USER_IP}_̲DT"
chmod 600 /var/log/history/
L
O
G
N
A
M
E
/
∗
h
i
s
t
o
r
y
∗
2
>
/
d
e
v
/
n
u
l
l
上
述
内
容
需
要
放
在
/
e
t
c
/
p
r
o
f
i
l
e
文
件
的
末
尾
。
这
里
默
认
写
了
记
录
日
志
文
件
的
根
目
录
是
:
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
,
这
个
目
录
需
要
初
始
化
建
立
,
然
后
通
过
“
e
x
p
o
r
t
H
I
S
T
F
I
L
E
=
"
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
/
{LOGNAME}/*history* 2>/dev/null 上述内容需要放在/etc/profile文件的末尾。这里默认写了记录日志文件的根目录是:/var/log/history,这个目录需要初始化建立,然后通过“exportHISTFILE="/var/log/history/
LOGNAME/∗history∗2>/dev/null上述内容需要放在/etc/profile文件的末尾。这里默认写了记录日志文件的根目录是:/var/log/history,这个目录需要初始化建立,然后通过“exportHISTFILE="/var/log/history/{LOGNAME}/
U
S
E
R
@
{USER}@
USER@{USER_IP}_
D
T
"
”
可
以
看
到
记
录
日
志
的
路
径
是
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
/
DT" ”可以看到记录日志的路径是/var/log/history/
DT"”可以看到记录日志的路径是/var/log/history/{LOGNAME},所以这个目录也需要事先建立,有多少个用户,就要建立多少个目录,而且要把目录的使用权限赋予相对应的用户。
而每次用户登录到退出都会产生以用户名、登录ip地址、操作时间为文件名的文件,文件里面包含本次用户的所有操作记录。
##################用户操作审计#############################
USER=whoami
USER_IP=who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'
if [ “
U
S
E
R
I
P
"
=
"
"
]
;
t
h
e
n
U
S
E
R
I
P
=
‘
h
o
s
t
n
a
m
e
‘
f
i
i
f
[
!
−
d
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
]
;
t
h
e
n
m
k
d
i
r
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
c
h
m
o
d
777
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
f
i
i
f
[
!
−
d
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
/
USER_IP" = "" ]; then USER_IP=`hostname` fi if [ ! -d /var/log/history ]; then mkdir /var/log/history chmod 777 /var/log/history fi if [ ! -d /var/log/history/
USERIP"=""];thenUSERIP=‘hostname‘fiif[!−d/var/log/history];thenmkdir/var/log/historychmod777/var/log/historyfiif[!−d/var/log/history/{LOGNAME} ]; then
mkdir /var/log/history/
L
O
G
N
A
M
E
c
h
m
o
d
300
/
v
a
r
/
l
o
g
/
h
i
s
t
o
r
y
/
{LOGNAME} chmod 300 /var/log/history/
LOGNAMEchmod300/var/log/history/{LOGNAME}
fi
export HISTSIZE=4096
DT=date +"%Y%m%d_%H:%M:%S"
export HISTFILE=”/var/log/history/
L
O
G
N
A
M
E
/
{LOGNAME}/
LOGNAME/{USER}@KaTeX parse error: Expected group after '_' at position 10: {USER_IP}_̲DT"
chmod 600 /var/log/history/${LOGNAME}/history 2>/dev/null
#############################################################################
3、建立初始化目录
先去/home查看有多少个用户
[aliyunzixun@xxx.com_test_static1_11_35 history]# ll /home/
total 28
drwxr-xr-x 2 adminuser adminuser 4096 Nov 17 21:53 adminuser
drwxr-xr-x 2 fastdfs fastdfs 4096 Nov 17 21:53 fastdfs
drwxr-xr-x 2 loguser loguser 4096 Nov 17 19:43 loguser
drwxr-xr-x 2 nginx nginx 4096 Nov 17 20:54 nginx
d-wx------ 2 root root 4096 Nov 17 21:53 root
drwxr-xr-x 2 tomcat tomcat 4096 Nov 17 19:42 tomcat
drwxr-xr-x 2 zabbix zabbix 4096 Nov 17 19:42 zabbix
然后开始建立初始化目录并授权,每个目录对应的用户都有权限:
[aliyunzixun@xxx.com_test_static1_11_35 log]# mkdir /var/log/history/fastdfs
[aliyunzixun@xxx.com_test_static1_11_35 history]# chown fastdfs:fastdfs fastdfs -R
[aliyunzixun@xxx.com_test_static1_11_35 log]# mkdir /var/log/history/tomcat
[aliyunzixun@xxx.com_test_static1_11_35 log]# chown -R tomcat:tomcat /var/log/history/tomcat
[aliyunzixun@xxx.com_test_static1_11_35 log]# mkdir /var/log/history/zabbix
[aliyunzixun@xxx.com_test_static1_11_35 log]# chown -R zabbix:zabbix /var/log/history/zabbix
[aliyunzixun@xxx.com_test_static1_11_35 log]# mkdir /var/log/history/loguser
[aliyunzixun@xxx.com_test_static1_11_35 log]# chown -R loguser:loguser /var/log/history/loguser
[aliyunzixun@xxx.com_test_static1_11_35 log]# mkdir /var/log/history/adminuser
[aliyunzixun@xxx.com_test_static1_11_35 log]# chown -R adminuser:adminuser /var/log/history/adminuser
4、用户登录验证
用其中一个用户fastdfs登录进程操作
[aliyunzixun@xxx.com_test_static1_11_35 ~]$ sudo -i
[sudo] password for adminuser:
[aliyunzixun@xxx.com_test_static1_11_35 ~]# su - fastdfs
[aliyunzixun@xxx.com_test_static1_11_35 ~]$ cp test1.log test3.log
[aliyunzixun@xxx.com_test_static1_11_35 ~]$ echo “test 1”> test3.log
[aliyunzixun@xxx.com_test_static1_11_35 ~]$ echo “1” >> test3.log
[aliyunzixun@xxx.com_test_static1_11_35 ~]$ echo “2” >> test3.log
[aliyunzixun@xxx.com_test_static1_11_35 ~]$ echo “3” >> test3.log
[aliyunzixun@xxx.com_test_static1_11_35 ~]$ more test3.log
test 1
1
2
3
[aliyunzixun@xxx.com_test_static1_11_35 ~]$
然后退出用户,重新登录进去日志目录/var/log/history/fastdfs/查看有最新的记录,一次用户登录到退出就会保存成一个日志文件记录:
进入日志目录
[aliyunzixun@xxx.com_test_static1_11_35 fastdfs]# cd /var/log/history/fastdfs/
查看有2个操作日志记录
[aliyunzixun@xxx.com_test_static1_11_35 fastdfs]# ll
total 8
-rw------- 1 fastdfs fastdfs 83 Nov 17 21:53 aliyunzixun@xxx.com_20161117_21:53:16
-rw------- 1 fastdfs fastdfs 139 Nov 17 21:59 aliyunzixun@xxx.com_20161117_21:56:47
打开当前的操作记录日志
[aliyunzixun@xxx.com_test_static1_11_35 fastdfs]# more aliyunzixun@xxx.com_20161117_21:56:47
cp test1.log test3.log
echo “test 1”> test3.log
echo “1” >> test3.log
echo “2” >> test3.log
echo “3” >> test3.log
more test3.log
exit