Port-knocking 简单教程

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。

posted @ 2018-03-07 16:37  小小leo  阅读(2901)  评论(0编辑  收藏  举报