iptable 规则示例
这些规则可用于允许或者阻止来自特定主机或者网络的 ssh 连接
阻止 192.168.1.10 连接本地主机 192.168.1.6
[root@test1 ~]# iptables -I INPUT -s 192.168.1.10 -p tcp --dport ssh -j REJECT
让我们尝试连接 192.168.1.6
[root@test ~]# ssh 192.168.1.6 -v OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to 192.168.1.6 [192.168.1.6] port 22. debug1: connect to address 192.168.1.6 port 22: Connection refused ssh: connect to host 192.168.1.6 port 22: Connection refused
它立即抛出“连接被拒绝”Connection refused 错误
让我们检查 192.168.1.10 在 192.168.1.6 上收到的数据包尝试
[root@test1 ~]# iptables -L INPUT -v Chain INPUT (policy ACCEPT 19 packets, 1263 bytes) pkts bytes target prot opt in out source destination 1 120 REJECT tcp -- any any 192.168.1.10 anywhere tcp dpt:ssh reject-with icmp-port-unreachable
根据日志 1 可以看到尝试从 192.168.1.10 连接到我们的机器
要阻止来自特定主机的 ssh 连接:
[root@test1 ~]# iptables -I INPUT -s 192.168.1.10 -p tcp --dport ssh -j DROP
现在让我们尝试从 192.168.1.10 连接我们的 192.168.1.6
[root@test ~]# ssh 192.168.1.6 -v OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to 192.168.1.6 [192.168.1.6] port 22. debug1: connect to address 192.168.1.6 port 22: Connection timed out ssh: connect to host 192.168.1.6 port 22: Connection timed out
当我们看到一条与上一条规则相比的新消息时。
其中,我们正在接受来自远程主机的 SYN 信号,但我们没有响应它,因此两台主机之间没有成功建立连接。
让我们看看试图连接192.168.1.6时发送的数据包数
[root@test1 ~]# iptables -L INPUT -v Chain INPUT (policy ACCEPT 22 packets, 1552 bytes) pkts bytes target prot opt in out source destination 3 180 DROP tcp -- any any 192.168.1.10 anywhere tcp dpt:ssh
在抛出“连接超时”错误之前,192.168.1.10 进行了 3 个数据包或者尝试
阻止来自 192.168.1.10 的 ESTABLISHED 状态的 ssh 连接
[root@test1 ~]# iptables -I INPUT -s 192.168.1.10 -p tcp --dport ssh -m state --state ESTABLISHED -j REJECT
让我们尝试从 192.168.1.10 连接
[root@test ~]# ssh 192.168.1.6 -v OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to 192.168.1.6 [192.168.1.6] port 22. debug1: Connection established. debug1: permanently_set_uid: 0/0 debug1: identity file /root/.ssh/identity type -1 debug1: identity file /root/.ssh/id_rsa type -1 debug1: identity file /root/.ssh/id_dsa type -1
建立连接后尝试卡在这里。
允许来自 192.168.1.10 的 ssh 连接
# iptables -I INPUT -s 192.168.1.10 -p tcp --dport ssh -j ACCEPT
测试
[root@test ~]# ssh 192.168.1.6 root@192.168.1.6's password: Last login: Thu Mar 6 11:19:32 2014 from 192.168.1.2 [root@test1 ~]#
我们已成功连接
更严格的规则来阻止或者允许相同的规则(将 ACCEPT 替换为 REJECT 以阻止)
[root@test1 ~]# iptables -I INPUT -s 192.168.1.10 -p tcp --dport ssh -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
状态 | 说明 |
---|---|
NEW | 意味着数据包已启动新连接,或者与两个方向上没有看到数据包的连接相关联 |
ESTABLISHED | 意思是数据包与连接在两个方向上的数据包相关联 |
RELATED | 意味着数据包正在启动新连接,但与现有连接相关联,例如FTP数据传输或者ICMP错误 |
阻止来自所有主机(除了 192.168.1.2)的 ssh 连接,
# iptables -I INPUT ! -s 192.168.1.2 -p tcp --dport ssh -m state --state NEW,ESTABLISHED,RELATED -j REJECT
Let us also log this message to verify our rule # iptables -I INPUT ! -s 192.168.1.2 -p tcp --dport ssh -m state --state NEW,ESTABLISHED,RELATED -j LOG --log-prefix "BLOCK SSH "
尝试从 192.168.1.0/24 网络中的任何其他机器执行 ssh
[root@test ~]# ssh 192.168.1.6 ssh: connect to host 192.168.1.6 port 22: Connection refused
在日志中验证
# cat /var/log/iptables | grep "BLOCK SSH" Mar 6 14:03:55 test1 kernel: BLOCK SSH IN=eth3 OUT= MAC=00:0c:29:51:aa:e1:00:0c:29:a3:f5:fa:08:00 SRC=192.168.1.10 DST=192.168.1.6 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=43914 DF PROTO=TCP SPT=35026 DPT=22 WINDOW=14600 RES=0x00 SYN URGP=0
阻止 192.168.1.0/24 子网的传出 ssh 连接
# iptables -I OUTPUT -d 192.168.1.0/24 -p tcp --dport 22 -j REJECT
检查确认
# ssh -v 192.168.1.6 OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Nov 2013 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to 192.168.1.6 [192.168.1.6] port 22. debug1: connect to address 192.168.1.6 port 22: Connection refused ssh: connect to host 192.168.1.6 port 22: Connection refused
因此,如我们所见,localhost 不允许与 192.168.1.0/24 子网建立 ssh 连接