自己动手,DIY一台硬件WAF!
source link: https://www.freebuf.com/articles/es/218263.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
硬件WAF,好像是很高端,很神密的样子,而且很贵。今天向大家展示如何自己动手,DIY一台硬件WAF!
一、软硬件需求
a、服务器一台 b、系统:Linux CentOS c、软件:ShareWAF d、硬件需求:Bypass网卡
简单说明:
a、使用linux系统,是为了在系统中搭建网桥,有了网桥,就可以实现硬件WAF的透明代理效果。 b、软件用ShareWAF,ShareWAF是一款WAF软件,可以在云端部署,也支持灌装硬件(这一点很重要)。而且,防护功能不错,目前最大支持同时保护256个网站。 c、Bypass网卡,主要是实现断电、系统故障等情况下自动将硬件“变”成为网线,实现Bypass效果,达到各种问题下都不中断业务功能,也就是实现高可用。
如果没有Bypass网卡,或者不是很需要高可用,那么灌装出来的硬件是一台透明代理硬件WAF,只是不具备bypass功能。(一般情况下,除了银行之类,别的单位很少能用着这种级别的高可用设备,因为还可以多机热备嘛,所以,其实没有bypass网卡也没太大关系)。
二、准备工作
ETH1网口连通外网,并给其设置IP;
ETH2,ETH3为一双Bypass网口,为做网桥使用;
ETH3口连接内网web服务器。
三、系统配置,实现透明代理
(完整详细步骤,如果熟悉linux网络操作命令的话,其中有些环节可以跳过)
关闭centos 7自带防火墙
systemctl stop firewalld.service systemctl disable firewalld.service
安装iptables
yum -y install iptables yum -y install iptables-services
开启iptables
service iptables start
清除Iptables自带规则
iptables -F
安装ifconfig
yum -y install net-tools.x86_64
安装网桥
yum -y install bridge-utils
设置网桥
/sbin/modprobe bridge /usr/sbin/brctl addbr br0(设置网桥名为br0) /sbin/ifup enp4s0 (要加入网桥的网卡,通过ifconfig查看) /sbin/ifup enp5s0 (要加入网桥的网卡)
注:执行以上两步之前最好将能通的网线插在网口上,否则执行时间会稍长,会显示激活失败。
如果以上步骤报错:
无法创建 NMClient 对象GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: Method "GetManagedObjects" with signature "" on interface "org.freedesktop.DBus.ObjectManager" doesn't exist
这时,执行如下命令:
chkconfig NetworkManager off chkconfig network on service NetworkManager stop service network start /usr/sbin/brctl addif br0 enp4s0 /usr/sbin/brctl addif br0 enp5s0
设置网桥IP (例:192.168.1.73 设置一个在内网网段的IP)
ifconfig br0 192.168.1.73 netmask 255.255.255.0
开启网桥
/sbin/ip link set br0 up
查看
sudo brctl show
关闭
ifconfig br0 down
删除 (删除前先将网桥关闭)
sudo brctl delbr br0
在/etc/sysctl.conf下添加内容
vi /etc/sysctl.conf
将光标移至文字最后一行按o,右键选择粘贴
net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-arptables = 1 net.ipv4.ip_forward = 1 net.ipv4.ip_nonlocal_bind = 1 net.ipv4.conf.default.rp_filter = 0 net.ipv4.conf.all.rp_filter = 0 net.ipv4.conf.br0.rp_filter = 0
1.按esc 2.输入 : 3.然后输入 wq 回车
执行使生效
sysctl -p
如果报错:
sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-ip6tables: 没有那个文件或目录 sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-iptables: 没有那个文件或目录 sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-arptables: 没有那个文件或目录 执行 modprobe br_netfilter 再次执行sysctl -p
添加路由
/sbin/ip -f inet rule add fwmark 1 lookup 100 /sbin/ip -f inet route add local default dev lo table 100
添加规则前,首先查内是否有其他规则
iptables -t 表名 -L
如果有其他规则,将其删除
iptables -t 表名 -D 链名 第几条规则
添加规则
iptables -t nat -A PREROUTING -d 192.168.1.20 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.73:80
说明:
192.168.1.20 web服务器IP 80 web服务端口 192.168.1.73 网桥IP 8080 ShareWAF端口
保存规则
service iptables save
以上,系统透明模式需要的设置已完成。为实现断电或异常时Bypass功能,需向Bypass网卡或硬件厂家咨询、索取其Bypass网卡相关程序、资料,如:“喂狗程序”、脚本等。
透明网桥配成功后。
四、安装NodeJS
(ShareWAF运行依赖的环境)
yum -y install wget mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum clean all yum makecache wget https://nodejs.org/dist/v8.11.1/node-v8.11.1-linux-x64.tar.xz --no-check-certificate tar -xvf node-v8.11.1-linux-x64.tar.xz mv node-v8.11.1-linux-x64 node-v8.11.1 ln -s /root/node-v8.11.1/bin/node /usr/local/bin/node ln -s /root/node-v8.11.1/bin/npm /usr/local/bin/npm
也可以从nodejs官网直接下载。
也可以用以下的方法:
sudo apt-get install nodejs-legacy sudo apt-get install npm sudo npm install -g n sudo n stable
五、安装ShareWAF
从ShareWAF官网,获取ShareWAF程序包后,在其目录下执行:
npm install
如在安装过程中Express报错,运行:npm config set strict-ssl false
如在安装过程中Sqlite3报错,运行:npm install sqlite3 –unsafe-perm
六、启动ShareWAF
nodejs ShareWAF
或
sudo node ShareWAF
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK