0. "port knocking"
如字面意思,類似‘敲門’,只是這里敲的是‘端口’,而且需要按照順序‘敲’端口。如果敲擊規則匹配,則可以讓防火牆實時更改策略。從而達到開關防火牆的目的。
1. 配置 knockd 服務
cat /etc/knockd.conf
[options]
UseSyslog
[openSSH]
sequence = 7000,8000,9000
seq_timeout = 5
command = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
tcpflags = syn
[closeSSH]
sequence = 9000,8000,7000
seq_timeout = 5
command = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
tcpflags = syn
配置文件里有兩個參數:
- sequence 按照順序依次訪問端口,command執行的條件。比如這里是依次訪問7000, 8000, 9000端口,默認使用TCP訪問。
- command 當knockd監測到sequence端口訪問完成,然后執行此處command,這里為通過iptables開啟關閉ssh外部訪問。
2. 構造knock sequence
1. 直接手工構造
- 打開 SSH iptables
telnet <target> 7000
telnet <target> 8000
telnet <target> 9000
- 關閉 SSH iptables
telnet <target> 9000
telnet <target> 8000
telnet <target> 7000
2. 使用knock程序
開啟
knock <target> 7000 8000 9000
關閉
knock <target> 9000 8000 7000
3. NC 或者 Nmap
Open:
nc -z <target> 7000 8000 9000
Close:
nc -z <target> 9000 8000 7000
for x in 7000 8000 9000; do nmap -Pn --host_timeout 201 --max-retries 0 -p $x <target> ; done
3. 其他
Port-knocking 並不能作為一種獨立的安全防御措施,因為它屬於security by obscurity。