TCP_Wrappers和sudo權(quán)限管理

TCP_Wrappers介紹

工作在第四層(傳輸層)的TCP協(xié)議

對有狀態(tài)連接的特定服務進行安全檢測并實現(xiàn)訪問控制

以庫文件形式實現(xiàn)

某進程是否接受libwrap的控制取決于發(fā)起此進程的程序在編譯時是否針對libwrap進行編譯的

判斷服務程序是否能夠由tcp_wrapper進行訪問控制的方法:

ldd /PATH/TO/PROGRAM|grep libwrap.so  
strings PATH/TO/PROGRAM|grep libwrap.so
 #舉例 查看sshd依賴的庫 
[root@localhost ~]# ldd /usr/sbin/sshd|grep libwrap 
libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f376400a000)

TCP_Wrappers的使用

配置文件:/etc/hosts.allow, /etc/hosts.deny

幫助參考:man 5 hosts_access,man 5 hosts_options

檢查順序:hosts.allow,hosts.deny(默認允許)

注意:一旦前面規(guī)則匹配,直接生效,將不再繼續(xù)

基本語法:

[sshd@ip](:sshd@ip)
#服務名|程序名@ip|遠程主機列表
#IP表示:通過哪一個ip來訪問這個服務(程序)的時候,才通過tcp_wrappers來控制它;
#如果沒有寫,及所有的IP。
#:遠程主機列表

#Daemon_list
#單個應用程序的二進制文件名,而非服務名,例如vsftpd 
#以逗號或空格分隔的應用程序文件名列表,如:sshd,vsftpd 
ALL表示所有接受tcp_wrapper控制的服務程序 
主機有多個IP,可用@hostIP來實現(xiàn)控制

舉例演示:編寫腳本/root/bin/checkip.sh,每5分鐘檢查一次,如果發(fā)現(xiàn)通過ssh登錄失敗,次數(shù)超過10次,自動將此遠程IP放TcpWrapper的黑名單中予以禁止防問

法1:后臺執(zhí)行腳本

[root@C7-37-100-destop scripts]# vim checkip.sh   
#!/bin/bash
#
sleeptime=5m
#睡5分鐘
num=10
#定義超過次數(shù)
file=/etc/hosts.deny
#定義黑名單文件
while true;do
        lastb|grep ssh|awk '{print $3}'|uniq -c|while read conn 
ip;do
#循環(huán)讀取次數(shù),和ip
        if [ "$conn" -ge "$num" ];then
#判斷次數(shù)超過10次        
                grep -q ^ssh.*$ip $file
                #靜默模式過濾 去文件中的重復記錄
                #以ssh開頭的后跟任意字符加ip的在/etc/hosts.deny里是否存在
                [ $? -ne 0 ] && echo "ssh:$ip" >>$file
                #不等于0說明不存在,就添加到/etc/hosts.deny里
        fi
        done
sleep $sleeptime
done

[root@C7-37-100-destop scripts]# bash checkip.sh &
[1] 28092
#將其后臺運行
[root@C7-37-100-destop scripts]# lastb|grep ssh|awk '{print $3}'|uniq -c
      9 192.168.37.101
      5 192.168.37.103
      7 192.168.37.128
      3 192.168.37.100
     12 172.16.0.128
      2 192.168.37.101
      1 192.168.37.1  
#測試最后應該在/etc/hosts.deny里有如下內(nèi)容
[root@C7-37-100-destop scripts]# tail /etc/hosts.deny 
#
#               The rules in this file can also be set up in
#               /etc/hosts.allow with a 'deny' option instead.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
#

#腳本成功
ssh:172.16.0.128
法2:腳本實現(xiàn),加入定時任務中
#!/bin/bash
#
sleep 10
file=/etc/hosts.deny
num=10
lastb|awk '/ssh/{print $3}'|uniq -c|while read conn ip ;do

        if [ "$conn" -ge "$num" ];then
                grep -q ^ssh.*:$ip $file
                [ $? -ne 0 ] && echo "ssh:$ip" >>$file
        fi
done

#測試crontab
[root@C7-37-100-destop scripts]# crontab -e
no crontab for root - using an empty one
*/1 * * * * /bin/bash /data/scripts/checkip2.sh
#演示每1分鐘執(zhí)行一次,測試,實際用時改成5分鐘就好                                                          
"/tmp/crontab.LmLKs5" 1L, 48C written
crontab: installing new crontab
[root@C7-37-100-destop scripts]# crontab -l
*/5 * * * * /bin/bash /data/scripts/checkip2.sh


#可以用tail -f來監(jiān)控hosts.deny文件
[root@C7-37-100-destop scripts]# tail -f /etc/hosts.deny 
#
#               The rules in this file can also be set up in
#               /etc/hosts.allow with a 'deny' option instead.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
#

ssh:172.16.0.128

sudo介紹

? sudo能夠授權(quán)指定用戶在指定主機上運行某些命令。如果未授權(quán)用戶嘗試使
用 sudo,會提示聯(lián)系管理員
? sudo可以提供日志,記錄每個用戶使用sudo操作
? sudo為系統(tǒng)管理員提供配置文件,允許系統(tǒng)管理員集中地管理用戶的使用權(quán)
限和使用的主機
? sudo使用時間戳文件來完成類似“檢票”的系統(tǒng),默認存活期為5分鐘的“入
場券”
su的缺點,暴露root密碼,sudo不用暴露root密碼。

sudo的基本使用

  1. 通過visudo命令編輯配置文件,具有語法檢查功能

舉例演示:配置magedu用戶的sudo權(quán)限,允許magedu用戶擁有root權(quán)限

我們知道配置sudo權(quán)限,可以直接編輯配置文件愛你/etc/sudoers,但是其不具有語法檢查功能;當然配置好以后可以通過命令visudo -c來檢查語法;實際使用中,我們通過visudo命令編輯配置文件,具有語法檢查功能 ;

[root@c6-37-128-desktop ~]# visudo
magedu  ALL=(ALL)       ALL
#表示magedu用戶通過本機所有IP登陸時,可以使用所有身份運行所有命令。
#root用戶可以使用所有身份。
#也可以這樣寫
magedu  ALL=(root)       ALL

#測試
#1 先檢查是否有此用戶,沒有添加之
[root@c6-37-128-desktop ~]# tail -5 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zouyongbing:x:500:500:centos6:/home/zouyongbing:/bin/bash
wang:x:501:501::/home/wang:/bin/bash
mage:x:502:502::/home/mage:/bin/bash
[root@c6-37-128-desktop ~]# id magedu
id: magedu: No such user
[root@c6-37-128-desktop ~]# useradd magedu
[root@c6-37-128-desktop ~]# passwd magedu
Changing password for user magedu.
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.

#visudo添加
## Allow root to run any commands anywhere 
root    ALL=(ALL)       ALL
#一般寫在默認root的下面
magedu  ALL=(ALL)       ALL

#切換用戶測試之
[root@c6-37-128-desktop ~]# su - magedu
[magedu@c6-37-128-desktop ~]$ useradd baba
#不加sudo提示無權(quán)限
-bash: /usr/sbin/useradd: Permission denied
[magedu@c6-37-128-desktop ~]$ sudo useradd baba
#輸入magedu的秘密即可
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for magedu: 
[magedu@c6-37-128-desktop ~]$ id baba
#創(chuàng)建成功
uid=504(baba) gid=504(baba) groups=504(baba)

當然我們也可以不修改配置文件/etc/sudoers來完成,此操作,直接在/etc/sudoers.d/目錄下創(chuàng)建文件來完成sudo授權(quán)。

#visudo里刪除root    ALL=(ALL)       ALL
[root@localhost ~]# cd /etc/sudoers.d/
[root@localhost sudoers.d]# vim test
magedu  ALL=(root)      ALL
[root@c6-37-128-desktop ~]# cat /etc/sudoers.d/test 
magedu  ALL=(root)      ALL
#測試
[root@c6-37-128-desktop ~]# cat /etc/sudoers.d/test 
magedu  ALL=(root)      ALL
[root@c6-37-128-desktop ~]# su - magedu
[magedu@c6-37-128-desktop ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[magedu@c6-37-128-desktop ~]$ sudo cat /etc/shadow
[sudo] password for magedu: 
root:$1$kZlilHfn$ofRQglMH3PsEcXVasvcZe.:18388:0:99999:7:::
bin:*:17246:0:99999:7:::
daemon:*:17246:0:99999:7:::
adm:*:17246:0:99999:7:::
lp:*:17246:0:99999:7:::
sync:*:17246:0:99999:7:::
shutdown:*:17246:0:99999:7:::
halt:*:17246:0:99999:7:::
mail:*:17246:0:99999:7:::
uucp:*:17246:0:99999:7:::
operator:*:17246:0:99999:7:::
games:*:17246:0:99999:7:::
gopher:*:17246:0:99999:7:::
ftp:*:17246:0:99999:7:::
nobody:*:17246:0:99999:7:::
dbus:!!:18388::::::
usbmuxd:!!:18388::::::
rtkit:!!:18388::::::
avahi-autoipd:!!:18388::::::
vcsa:!!:18388::::::
abrt:!!:18388::::::
haldaemon:!!:18388::::::
ntp:!!:18388::::::
apache:!!:18388::::::
saslauth:!!:18388::::::
postfix:!!:18388::::::
gdm:!!:18388::::::
pulse:!!:18388::::::
sshd:!!:18388::::::
tcpdump:!!:18388::::::
zouyongbing:$1$kZlilHfn$ofRQglMH3PsEcXVasvcZe.:18388:0:99999:7:::
wang:$1$iJmct2C2$SPHEv7ymmWAGkMTS2iJj4/:18390:0:99999:7:::
mage:$1$votN2a/l$p0GAgf88PoeWUV7v3iRIZ0:18390:0:99999:7:::
magedu:$1$T64n9ifM$Uv3hxXj00u1x/IzmGKv240:18395:0:99999:7:::
baba:!!:18395:0:99999:7:::

也可以授權(quán)組,在/etc/sudoers文件里,默認有一個wheel組,只要將用戶加入其組,即可以具有root權(quán)限。

#先刪除test文件
## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL
#默認的可以將用戶加入wheel用戶組,就可以擁有root權(quán)限,將其啟用,然后將magedu添加到此組中也可。

[root@c6-37-128-desktop sudoers.d]# groupmems -g wheel -a magedu
[root@c6-37-128-desktop sudoers.d]# id magedu
uid=503(magedu) gid=503(magedu) groups=503(magedu),10(wheel)

#測試
[magedu@c6-37-128-desktop ~]$ sudo cat /etc/shadow
[sudo] password for magedu: 
root:$1$kZlilHfn$ofRQglMH3PsEcXVasvcZe.:18388:0:99999:7:::
bin:*:17246:0:99999:7:::
daemon:*:17246:0:99999:7:::
adm:*:17246:0:99999:7:::
lp:*:17246:0:99999:7:::
sync:*:17246:0:99999:7:::
shutdown:*:17246:0:99999:7:::
halt:*:17246:0:99999:7:::
mail:*:17246:0:99999:7:::
uucp:*:17246:0:99999:7:::
operator:*:17246:0:99999:7:::
games:*:17246:0:99999:7:::
gopher:*:17246:0:99999:7:::
ftp:*:17246:0:99999:7:::
nobody:*:17246:0:99999:7:::
dbus:!!:18388::::::
usbmuxd:!!:18388::::::
rtkit:!!:18388::::::
avahi-autoipd:!!:18388::::::
vcsa:!!:18388::::::
abrt:!!:18388::::::
haldaemon:!!:18388::::::
ntp:!!:18388::::::
apache:!!:18388::::::
saslauth:!!:18388::::::
postfix:!!:18388::::::
gdm:!!:18388::::::
pulse:!!:18388::::::
sshd:!!:18388::::::
tcpdump:!!:18388::::::
zouyongbing:$1$kZlilHfn$ofRQglMH3PsEcXVasvcZe.:18388:0:99999:7:::
wang:$1$iJmct2C2$SPHEv7ymmWAGkMTS2iJj4/:18390:0:99999:7:::
mage:$1$votN2a/l$p0GAgf88PoeWUV7v3iRIZ0:18390:0:99999:7:::
magedu:$1$T64n9ifM$Uv3hxXj00u1x/IzmGKv240:18395:0:99999:7:::
baba:!!:18395:0:99999:7:::
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容