首頁 > 軟體

Linux下不同運維人員共用root 賬戶許可權審計

2020-06-16 17:33:42

一、為什麼?

  在中小型企業,公司不同運維人員基本都是以root 賬戶進行伺服器的登陸管理,缺少了賬戶許可權審計制度。不出問題還好,出了問題,就很難找出源頭。

  這裡介紹下,如何利用編譯bash 使不同的用戶端在使用root 登陸伺服器使,記錄各自的操作,並且可以在結合ELK 紀錄檔分析系統,來收集登陸操作紀錄檔

二、環境

  伺服器:CentOS 6.5、Development tools、使用金鑰認證,SElinux 關閉。

  用戶端:生成金鑰對,用於登入伺服器 (2台)

三、搭建部署 (伺服器操作 192.168.30.72)

3.1 下載編譯bash

[root@open1 ~]# wget http://ftp.gnu.org/gnu/bash/bash-4.1.tar.gz
[root@open1 ~]# tar xvf bash-4.1.tar.gz
[root@open1 ~]# cd bash-4.1

3.2 先修改下 config-top.c檔案,大概94行、104行,由於c 語言中 注釋是/**/ ,所以不要刪除錯了。修改如下:

[root@open1 bash-4.1]# vim config-top.c
#define SSH_SOURCE_BASHRC
#define SYSLOG_HISTORY

3.3 修改下bashhist.c 檔案,讓終端上的命令記錄到系統messages 中,並且以指定的格式。並傳入獲得的變數。修改後的內容如下:

[root@open1 bash-4.1]# vim bashhist.c
#... 省略部分段落
void
bash_syslog_history (line)
    const char *line;
{
  char trunc[SYSLOG_MAXLEN];
    const char *p;
    p = getenv("NAME_OF_KEY");
  if (strlen(line) < SYSLOG_MAXLEN)
    syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY: PID=%d PPID=%d SID=%d  User=%s USER=%s CMD=%s", getpid(), getppid(), getsid(getpid()),  current_user.user_name, p, line);
  else
    {
      strncpy (trunc, line, SYSLOG_MAXLEN);
      trunc[SYSLOG_MAXLEN - 1] = ' ';
      syslog (SYSLOG_FACILITY|SYSLOG_LEVEL, "HISTORY (TRUNCATED): PID=%d  PPID=%d SID=%d User=%s USER=%s CMD=%s", getpid(), getppid(), getsid(getpid()), current_user.user_name, p, trunc);
    }
}

3.4 設定安裝路徑,編譯安裝,編譯到/usr/local/目錄下。

[root@open1 bash-4.1]# ./configure --prefix=/usr/local/bash_new
[root@open1 bash-4.1]# make && make install
...
if test "bash" = "gettext-tools"; then 
          /bin/sh /root/bash-4.1/./support/mkinstalldirs /usr/local/bash_new/share/gettext/po; 
          for file in Makefile.in.in remove-potcdate.sin quot.sed boldquot.sed en@quot.header en@boldquot.header insert-header.sin Rules-quot   Makevars.template; do 
            /usr/bin/install -c -m 644 ./$file 
                            /usr/local/bash_new/share/gettext/po/$file; 
          done; 
          for file in Makevars; do 
            rm -f /usr/local/bash_new/share/gettext/po/$file; 
          done; 
        else 
          : ; 
        fi
make[1]: Leaving directory `/root/bash-4.1/po'

編譯完成後,將新的bash 追加到 /etc/shells 中,並修改root使用者的登陸shell 環境為新編譯的shell。如下

[root@open1 bash-4.1]# echo "/usr/local/bash_new/bin/bash" >> /etc/shells
[root@open1 bash-4.1]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/usr/local/bash_new/bin/bash
 
[root@open1 bash-4.1]# vim /etc/passwd
root:x:0:0:root:/root:/usr/local/bash_new/bin/bash

登出當前root使用者,重新登陸後,檢視/var/log/messages,如下就可以看到記錄了操作命令

四、SSH用戶端生成金鑰部分

4.1 在client1上(192.168.30.99)操作,使用者zhangsan

[root@rsyslog ~]# ssh-keygen -t rsa -C "root@zhangsan"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
c8:bd:5d:3b:a5:d9:6d:09:b6:5f:db:55:1d:43:96:3d root@zhangsan
The key's randomart image is:
+--[ RSA 2048]----+
|               oo|
|              oE.|
|               o.|
|     . o        +|
|      o S   .o. o|
|         o ..*o.o|
|        . . =...=|
|             ...=|
|               o.|
+-----------------+

-t 加密演算法
-C 注釋 (加上這個也是為了最後進行對伺服器存取人員進行辨別的一個關鍵點)

將公鑰上傳到伺服器上的.ssh/authorized_keys 檔案中。ssh-copy-id 命令會自動在伺服器上建立.ssh/authorized_keys檔案,即使該目錄不存在,並自動賦予600許可權。

[root@rsyslog ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.30.72
root@192.168.30.72's password:
Now try logging into the machine, with "ssh 'root@192.168.30.72'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

4.3 client 2(192.168.30.71) 上同樣的操作,使用者lisi

[root@swift3 ~]# ssh-keygen -t rsa -C "root@lisi" Generating public/private rsa key pair.
Enter
file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty
for no passphrase):
Enter same passphrase again:
Your identification has been saved
in /root/.ssh/id_rsa.
Your public key has been saved
in /root/.ssh/id_rsa.pub.
The key fingerprint is:
8f:
56:66:91:0c:6e:86:3b:90:19:42:9c:ab:9e:00:f6 root@lisi
The key
's randomart image is:
+--[ RSA 2048]----+
| oo..  .        |
|  o. + o o .    |
|  .+ . + +      |
|...  . +  .    |
|o..  o S +      |
|o  E  . *      |
|o .    o .      |
| o    .        |
|                |
+-----------------+

上傳公鑰到伺服器上

[root@swift3 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.30.72
The authenticity of host '192.168.30.72 (192.168.30.72)' can't be established.
RSA key fingerprint is 8f:a7:1b:8d:e4:92:ad:ae:ea:1b:fb:67:0b:0b:7c:ac.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.30.72' (RSA) to the list of known hosts.
root@192.168.30.72's password:
Now try logging into the machine, with "ssh 'root@192.168.30.72'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

4.4 現在去伺服器上驗證下該檔案。

[root@open1 ~]# cat ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6fM+bpWEP3luauvOjmTB55ugUzVVMesmHCw4RNZ/C2e+KGXAYuxuAmEBbMcXQQj7OTAqVCQ0PWja58wReyZ7etiUGAtvoSBmSBpTPXteBMl40kDn4GdmXQ9UT/jnQ9gSZUQYJLMLJGWJks9S4xUI2cZ7oIytclrsUnKuOA1U6+luIJwJu9z7ya5OXh5FmmJQFnYtAEIhrLt4S8Ru5S00c0jiQCRk3RFlHYNc0IR02MXMH7d9bq7l04heAcT/y1EBS3EwINX8r0y6OridjJPCwxnm1sSfMKvLAbq/B+ufDjEOp7Y2SatL3qXaiP7NxdnhoJ4+Xar0zCoYi2A9oRGgUQ== root@zhangsan
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAupGSgXOWpQfzOVkHXYqN2BjMiAyaFRdKs6Wam3xGpMYRjZbIFX14kNR4CbrQtbUK8YonZPYdXG589blFmqF17sUPCNEqZEjCNer+yzDu+hYg/jAn4dCVtTBqUtBsTYUHSHIR0srruJ9keHNgU9aDRok8nulMUi/9Ej0NJZsBQ2npVNCf0YHgAd/ON5VsBYVLPvAT/cG3MuCjg5mgtU59qgAHyLKxkfpVc0/TRZ4eamX/1V0dsCxx9oYDbpn4YKLBAOaAS4kF6qEdrwRh0ssyWtWOo/CdyfLXKgwdbPtPfWZ63SM7wY7bAtcdxxu/QDkYVP+4oDfAtMxXZlY2bT5qMQ== root@lisi
View Code

現在上面兩個用戶端已經可以免金鑰登陸了,現在去伺服器上設定,並建立指令碼。

五、設定伺服器

5.1 在log目錄下建立一個 keys 檔案,用於登陸時存進去公鑰,之後對其進行取出判斷的

[root@open1 ~]# touch /var/log/keys

建立檢測指令碼,內容如下:

[root@open1 ~]# cat /etc/CheckUser.sh
#
!/bin/bash #conding:utf-8
pid
=$PPID
#在自己home目錄得到所有的key,如果
/var/log/keys 沒有的時候,新增進去
while read line
do
grep "$line" /var/log/keys >/dev/null || echo "$line" >> /var/log/keys
done < $HOME/.ssh/authorized_keys #得到每個key的指紋cat /var/log/keys | while read LINE
do
NAME
=$(echo $LINE | awk '{print $3}')
echo $LINE >/tmp/keys.log.$pid
KEY
=$(ssh-keygen -l -f /tmp/keys.log.$pid | awk '{print $2}')
grep "$KEY $NAME" /var/log/ssh_key_fing >/dev/null || echo "$KEY $NAME" >> /var/log/ssh_key_fing
done
#如果是root使用者,secure檔案裡面是通過PPID號驗證指紋
if [ $UID == 0 ]
then
ppid
=$PPID
else
#如果不是root使用者,驗證指紋的是另外一個進程號
ppid
=`/bin/ps -ef | grep $PPID |grep 'sshd:' |awk '{print $3}'`
fi
#得到RSA_KEY和NAME_OF_KEY,用來bash4.1得到歷史記錄
RSA_KEY
=`/bin/egrep 'Found matching RSA key' /var/log/secure | /bin/egrep "$ppid" | /bin/awk '{print $NF}' | tail -1`
if [ -n "$RSA_KEY" ];then
NAME_OF_KEY
=`/bin/egrep "$RSA_KEY" /var/log/ssh_key_fing | /bin/awk '{print $NF}'`
fi
#把NAME_OF_KEY設定為唯讀
readonly NAME_OF_KEY
export NAME_OF_KEY
/bin/rm /tmp/keys.log.$pid

5.2 設定 profile,在檔案末尾新增一行內容,如下:

[root@open1 ~]# echo "test -f /etc/CheckUser.sh && . /etc/CheckUser.sh" >> /etc/profile

在/etc/bashrc 末尾新增下面內容:

[root@open1 ~]# tail -1f /etc/bashrc
test -z "$BASH_EXECUTION_STRING" || { test -f /etc/CheckUser.sh && . /etc/CheckUser.sh; logger -t -bash -s "HISTORY $SSH_CLIENT USER=$NAME_OF_KEY CMD=$BASH_EXECUTION_STRING " >/dev/null 2>&1;}

5.3 修改sshd 組態檔,開啟debug 模式,並重新啟動sshd 服務

[root@open1 ~]# sed -i 's/#LogLevel INFO/LogLevel DEBUG/g' /etc/ssh/sshd_config
[root@open1 ~]# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

六、驗證

6.1 在client1 上進行登陸,並刪除個檔案試下(zhangsan)

6.2 在client2 上進行登陸,也刪除個檔案,並執行個重新啟動服務的命令(lisi)

6.3 去伺服器上檢視下 messages 紀錄檔,內容如下

通過上圖,可以看出,不通使用者的用戶端通過公鑰登陸的方式,分辨出了誰操作了什麼,什麼時候操作的。

(註:上圖第4段 swift1 是這台伺服器的主機名,由於我只是執行了hostname 命令修改主機名,並沒有修改networks,所以核心裡還是之前的名字:swift1。)

七、結束

  通過這種方式,極大的解決了多root 使用者登陸操作,無法審計的問題。並且可以結合紀錄檔轉發,將系統紀錄檔轉發到其它伺服器,即使主機被黑了,也能具體的審查登陸時間以及做了哪些操作。

本文永久更新連結地址http://www.linuxidc.com/Linux/2016-09/135091.htm


IT145.com E-mail:sddin#qq.com