Linux系統安全加固之使用iptables限制指定使用者訪問網路

在對Linux作業系統進行加固時,基於最小許可權法則,我們可能需要限制特定賬號的網路訪問許可權,如用來執行ngnix、mysql等服務的服務賬號,這些賬號一般不用連線網路或只跟少數幾個IP有通訊需求,我們可以使用iptables的owner模組進行限制:

root@kali:~# iptables -m owner ——helpiptables v1。8。3……owner match options:[!] ——uid-owner userid[-userid]      Match local UID[!] ——gid-owner groupid[-groupid]    Match local GID[!] ——socket-exists                  Match if socket existsroot@kali:~#

1、在2。6。18之前的Linux核心版本,owner模組還支援——cmd-owner、——pid-owner、——sid-owner等引數,分別對程序名稱、程序ID以及session ID進行過濾。

2、owner模組針對本機發起的流量進行匹配,所以它只能應用於OUTPUT方向。

在下面的例子中,使用--uid-owner引數限制使用者test只能訪問192.168.30.0/24網段:

root@kali:~# sudo -u test ping 192。168。3。254 -n -c 3PING 192。168。3。254 (192。168。3。254) 56(84) bytes of data。64 bytes from 192。168。3。254: icmp_seq=1 ttl=63 time=1。70 ms64 bytes from 192。168。3。254: icmp_seq=2 ttl=63 time=1。41 ms64 bytes from 192。168。3。254: icmp_seq=3 ttl=63 time=2。04 ms——- 192。168。3。254 ping statistics ——-3 packets transmitted, 3 received, 0% packet loss, time 2006msrtt min/avg/max/mdev = 1。407/1。716/2。042/0。259 msroot@kali:~# iptables -A OUTPUT -m owner ——uid-owner test -d 192。168。30。0/24 -j ACCEPTroot@kali:~# iptables -A OUTPUT -m owner ——uid-owner test  -j DROProot@kali:~# root@kali:~# sudo -u test ping 192。168。3。254 -n -c 3PING 192。168。3。254 (192。168。3。254) 56(84) bytes of data。ping: sendmsg: Operation not permittedping: sendmsg: Operation not permittedping: sendmsg: Operation not permitted——- 192。168。3。254 ping statistics ——-3 packets transmitted, 0 received, 100% packet loss, time 2057msroot@kali:~# sudo -u test ping 192。168。30。254 -n -c 3PING 192。168。30。254 (192。168。30。254) 56(84) bytes of data。64 bytes from 192。168。30。254: icmp_seq=1 ttl=64 time=0。388 ms64 bytes from 192。168。30。254: icmp_seq=2 ttl=64 time=0。327 ms64 bytes from 192。168。30。254: icmp_seq=3 ttl=64 time=0。289 ms——- 192。168。30。254 ping statistics ——-3 packets transmitted, 3 received, 0% packet loss, time 2049msrtt min/avg/max/mdev = 0。289/0。334/0。388/0。040 msroot@kali:~# root@kali:~# iptables -L -nv | grep owner    3   252 ACCEPT     all  ——  *      *       0。0。0。0/0            192。168。30。0/24      owner UID match 1000    3   252 DROP       all  ——  *      *       0。0。0。0/0            0。0。0。0/0            owner UID match 1000root@kali:~# root@kali:~#

還可以基於使用者組進行限制:

root@kali:~# id testuid=1000(test) gid=1000(test) groups=1000(test)root@kali:~# iptables -A OUTPUT -m owner ——gid-owner test -d 192。168。30。0/24 -j ACCEPTroot@kali:~# iptables -A OUTPUT -m owner ——gid-owner test  -j DROP

--uid-owner及--gid-owner還可以使用使用者ID,效果跟使用使用者名稱一樣:

root@kali:~# iptables -A OUTPUT -m owner ——uid-owner 1000 -d 192。168。30。0/24 -j ACCEPTroot@kali:~# iptables -A OUTPUT -m owner ——uid-owner 1000 -j DROProot@kali:~# iptables -Z OUTPUTroot@kali:~# sudo -u test ping 192。168。3。254 -n -c 3PING 192。168。3。254 (192。168。3。254) 56(84) bytes of data。ping: sendmsg: Operation not permittedping: sendmsg: Operation not permittedping: sendmsg: Operation not permitted——- 192。168。3。254 ping statistics ——-3 packets transmitted, 0 received, 100% packet loss, time 2029msroot@kali:~# iptables -L -nv | grep owner    0     0 ACCEPT     all  ——  *      *       0。0。0。0/0            192。168。30。0/24      owner UID match 1000    3   252 DROP       all  ——  *      *       0。0。0。0/0            0。0。0。0/0            owner UID match 1000root@kali:~#

使用者ID也可以指定一個範圍:

root@kali:~# iptables -A OUTPUT -m owner ——uid-owner 1000-1001 -d 192。168。30。0/24 -j ACCEPTroot@kali:~# iptables -A OUTPUT -m owner ——uid-owner 1000-1001 -j DROP

--socket-exists引數用於匹配屬於本地socket的流量,即本地傳送或接受的流量,在下面的例子中我們使用--socket-exists引數匹配到了當前SSH會話的流量:

root@kali:~# netstat -nptla | grep :22tcp        0      0 0。0。0。0:22              0。0。0。0:*               LISTEN      906/sshd            tcp        0      0 1。1。1。21:22             1。1。1。1:53849           ESTABLISHED 12968/sshd: root@pt tcp6       0      0 :::22                   :::*                    LISTEN      906/sshd            root@kali:~# root@kali:~# iptables -A OUTPUT -m owner ——socket-exists -d 1。1。1。1 -j ACCEPTroot@kali:~# root@kali:~# iptables -L -nv | grep owner   42  4088 ACCEPT     all  ——  *      *       0。0。0。0/0            1。1。1。1              owner socket existsroot@kali:~#