knockd安装配置
yum -y install libcap*
进入https://pkgs.org/download/knock-server 可下载需要的knock-server 的rpm包
wget -c http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
这时可能需要安装nux-dextop仓库执行下面命令
yum install -y epel-release.noarch
rpm -Uvh nux-dextop-release*rpm
yum -y install knock-server
在centos7上设定三个端口作为knockd的敲击端口 如5060:udp 7060:udp 8090:udp
firewall-cmd --zone=public --add-port=5060/udp --permanent
firewall-cmd --zone=public --add-port=7060/udp --permanent
firewall-cmd --zone=public --add-port=8090/udp --permanent
firewall-cmd --reload
查看public级别的信息 可以看到端口已设定好
firewall-cmd --zone=public --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources: services: dhcpv6-client ssh
ports: 5060/udp 7060/udp 8090/udp
protocols: masquerade: no
forward-ports: source-ports: icmp-blocks: rich rules:
//查看knock-server的配置说明
man knockd
vi /etc/knockd.conf
修改为
[options]
logfile = /var/log/knockd.log
[opencloseSSH]
sequence = 8090:udp,5060:tcp,7060:udp
seq_timeout = 30
tcpflags = syn
start_command = /bin/firewall-cmd --zone=public --add-rich-rule="rule family="ipv4" source address="%IP%" service name="ssh" accept"
cmd_timeout = 30
stop_command = /bin/firewall-cmd --zone=public --remove-rich-rule="rule family="ipv4" source address="%IP%" service name="ssh" accept"
配置文件标准配置
[options]
logfile = /var/log/knockd.log
Interface = ens33
[openSSH]
sequence = 7000,8000,9000
seq_timeout = 5
command = /usr/sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
tcpflags = syn
[closeSSH]
sequence = 9000,8000,7000
seq_timeout = 5
command = /usr/sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
tcpflags = syn
解释上面的命令
logfile knockd的运行日志文件,当然也可以配置成UseSyslog 则使用系统的syslog日志功能
sequence 设置客户端敲击的端口及每个端口的协议类型
opencloseSSH 开启与关闭SSH 防火墙的设定项目
seq_timeout 需要在多少时间里完成端口的敲击
tcpflags 来源封包所需带有的封包标志,一般来说, UDP 封包不会有ack
start_command 如果有完成端口的敲击后执行的命令
stop_command 用户执行断线后执行的命令
cmd_timeout 若设定stop_command 则需此设定,订定开始与结束防火墙的时间
(上面的
/bin/firewall-cmd --zone=public --add-rich-rule="rule family="ipv4" source address="%IP%" service name="ssh" accept"
也可以替换成如下
firewall-cmd --permanent --zone=public --add-service=ssh
/bin/firewall-cmd --zone=public --remove-rich-rule="rule family="ipv4" source address="%IP%" service name="ssh" accept"
也可以替换成如下
/usr/bin/firewall-cmd --zone=public --remove-service=ssh
)
移除ssh服务有两种方式如下
(1)
/usr/bin/firewall-cmd --zone=public --remove-service=ssh
//别再执行firewall-cmd --reload 否则你上面的命令就会消失
(2)
也可以
/usr/bin/firewall-cmd --zone=public --remove-service=ssh --permanent
firewall-cmd --reload
service knockd start //启动
mack安装knock客服端
brew install knock (如果没有安装brew请先安装brew)
knock -v 192.169.3.4 8090:udp 7060:udp 5060:udp | ssh root@192.169.3.4