知乎专栏 |
起因是这样的,10年前,我们的IDC部署大量的CentOS,当时是6.x版本,后面升级到7.x版本。对于新版CentOS 大家都不太适应它携带的 firewalld 防火墙,另一个原因是IDC的服务器几乎只做 INPUT(进入)规则,极少服务器有OUTPUT(访问)和FORWARD(转发)规则。firewalld 更适合做企业防火墙,并不适合做IDC防火墙,把简单的事情复杂化了。
使用下面命令查看区域设置:
[root@localhost ~]# firewall-cmd --get-zones block dmz drop external home internal nm-shared public trusted work
当我尝试删除的时候失败,于是我便使用Python开发了一款防火墙,用来替代 firewalld。
[root@localhost ~]# for zone in $(firewall-cmd --get-zones);do firewall-cmd --delete-zone=$zone --permanent; done Error: BUILTIN_ZONE: 'block' is built-in zone Error: BUILTIN_ZONE: 'dmz' is built-in zone Error: BUILTIN_ZONE: 'drop' is built-in zone Error: BUILTIN_ZONE: 'external' is built-in zone
防火墙设计与想法
面向哪些用户:运维人员和开发人员。
运维人员:运维人员可以像使用其他软件一样使用这款防火墙软件。容易安装,容易学习,容易使用,解决运维面临的痛点。
开发人员:能通过开发包,做防火墙的二次开发。
准备一台 CentOS 8 (CentOS Stream),在 root 用户下运行下面命令安装
pip 安装
[root@localhost firewall]# pip3 install netkiller-firewall WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead. Collecting netkiller-firewall Using cached https://files.pythonhosted.org/packages/d2/82/f0d7cc6646447e2560702415606b9aa668b0dc7536e24944a2d0823db7ff/netkiller_firewall-0.0.1-py3-none-any.whl Installing collected packages: netkiller-firewall Successfully installed netkiller-firewall-0.0.1 [root@localhost firewall]#
源码安装
[root@localhost ~]# cd /usr/local/src/ [root@localhost ~]# dnf install -y git python38 [root@localhost ~]# git clone https://github.com/netkiller/firewall.git [root@localhost ~]# cd firewall [root@localhost ~]# bash install.sh
如何使用该防火墙
启用防火墙: systemctl enable firewall 启动防火墙: systemctl start firewall 停止防火墙: systemctl stop firewall 查看iptables规则: iptables -L
修改 /etc/sysconfig/firewall 文件设置/切换默认规则
[root@localhost ~]# vim /etc/sysconfig/firewall ############################################## LIBEXEC=/srv/firewall/libexec RULE=www ##############################################
LIBEXEC 是规则库的位置
RULE 是使用规则库 LIBEXEC 中的那个规则
规则库的目录是 /srv/firewall/libexec/ 默认提供几个常用的规则:Web 服务器,邮件服务器,数据库服务器。
[root@localhost ~]# cd /srv/firewall/libexec/ [root@localhost ~]# ls db.py smtp.py www.py
默认防火墙规则 /srv/firewall/libexec/www.py
$ sudo cat /srv/firewall/libexec/www.py #!/usr/bin/env python3 # -*- coding: utf-8 -*- from firewall import * ######################################## # Web Application ######################################## www = Firewall() www.flush() www.policy(www.INPUT,www.ACCEPT) www.policy(www.OUTPUT,www.ACCEPT) www.policy(www.FORWARD,www.ACCEPT) www.input().state(('RELATED','ESTABLISHED')).accept() www.input().protocol('icmp').accept() www.input().interface('-i','lo').accept() www.input().protocol('tcp').dport('22').state('NEW').accept() www.input().protocol('tcp').dport(('443','80')).state('NEW').accept() www.output().protocol('tcp').dport(('20','21')).reject() #www.input().protocol('tcp').inbound('eth0').dport('80').recent('HTTP',2,20).drop() #www.input().protocol('tcp').inbound('eth0').dport('80').connlimit(30).drop() #www.input().protocol('tcp').inbound('eth0').dport('80').recent('HTTP').accept() # DDOS #www.input().proto('tcp').dport("80").string('XXDD0S').drop() www.input().reject('--reject-with icmp-host-prohibited') www.forward().reject('--reject-with icmp-host-prohibited') def start(): www.start() def stop(): www.stop() def restart(): www.stop() www.start() def show(): www.show() def status(): www.status() def main(): show() return( 0 ) if __name__ == '__main__': main()