linux下用戶操做記錄審計環境的部署記錄

 

一般,咱們運維管理人員須要知道一臺服務器上有哪些用戶登陸過,在服務器上執行了哪些命令,幹了哪些事情,這就要求記錄服務器上所用登陸用戶的操做信息,這對於安全維護來講頗有必要。廢話很少說了,下面直接記錄作法:node

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查看全部登陸用戶進程信息,這些查看命令大同小異;
 
二、使用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操做記錄也會有linux

[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」進一步將日誌發送給日誌服務器:shell

操做以下:
把下面內容添加到/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
相關文章
相關標籤/搜索