Ubuntu ufw + Python3 add / remove port-rule

Ubuntu ufw + Python3 add / remove port-rule

bash 复制代码
# Ubuntu 24.04
# 查看防火墙状态
~$ sudo ufw status
# 启用ufw防火墙
~$ sudo ufw enable
# 关闭ufw防火墙
~$ sudo ufw disable
# 重载ufw防火墙
~$ sudo ufw reload
# 防火墙禁止接收http数据包
# http端口号为80
~$ sudo ufw reject http
# 防火墙禁止发送http数据包
~$ sudo ufw reject out http
# 防火墙禁止接收https数据包
# https端口号为443
~$ sudo ufw reject https
# 防火墙禁止发送https数据包
~$ sudo ufw reject out https
# 显示ufw防火墙状态和带编号策略
~$ sudo ufw status numbered
# 删除第1条策略
~$ sudo ufw delete 1
# sudo ufw status numbered | grep '80'
# sudo ufw status numbered | grep '443'
# 显示有关80或443端口的ufw防火墙策略
sudo ufw status numbered | grep '80\|443'

一键添加禁止http/https数据包的策略,保存为addhttpsrule.sh,Ubuntu下使用命令行source addhttpsrule.shsh addhttpsrule.sh执行

bash 复制代码
#
# File: addhttpsrule.sh
# Author: Nega
# Created: 11/5/24 Tue.
# 
#!/bin/bash
echo 'addrule reject-http-in..'
sudo ufw reject in http
echo 'addrule reject-http-out..'
sudo ufw reject out http
echo 'addrule reject-https-out..'
sudo ufw reject out https
echo 'addrule reject-https-in..'
sudo ufw reject in https
sudo ufw status numbered | grep '80\|443'

Python代码删除ufw防火墙禁止http/https的策略:

python 复制代码
'''
@file: removehttpsrule.py
@author: Nega
@created: 11/5/24 Tue.
@description: None
'''
#!/bin/python3

import os
import re

def __auto_remove_https_rules(port=80):
    p = os.popen('sudo ufw status numbered | grep ' + str(port))
    if p is None:
        print('[TEST] aborted, with no return')
        return
    r = p.read()
    if r is None:
        print('[TEST] no read data')
        return
    r = r.strip()
    if '\n' in r:
        r = r.replace('\n', '')
    if r == '':
        print('[SKIP] no rule matching port ' + str(port))
    MAX_TRIES = 4
    count = 0
    while r != '' and count < MAX_TRIES:
        try:
            count += 1
            did = re.findall('\\[[ ]?([0-9]+)\\]', r, re.M | re.I | re.S)[0]
            print('sudo ufw delete [' + did + '] port=' + str(port))
            os.system('sudo ufw delete ' + did)
            p = os.popen('sudo ufw status numbered | grep ' + str(port))
            r = p.read()
            r = r.strip().replace('\n', '')
        except Exception as err:
            r = ''
            print('[ERR] error occurred, ' + str(err))

if __name__ == '__main__':
    __auto_remove_https_rules(80)
    __auto_remove_https_rules(443)
    os.system('sudo ufw status numbered | grep "80\\|443"')

removehttpsrule.sh

bash 复制代码
#
# File: removehttpsrule.sh
# Author: Nega
# Created: 11/5/24 Tue.
# 
#!/bin/bash
if test -f /usr/bin/python3;then
python3 removehttpsrule.py
else
echo 'no python3'
fi
相关推荐
Dawn-bit43 分钟前
Linux日志处理三剑客之基础篇:(基础正则+扩展正则)
linux·运维·服务器·正则表达式·云计算·运维开发
☆凡尘清心☆1 小时前
Linux运维故障排查速查命令清单
linux·运维·服务器
小泊客1 小时前
友善R5C刷OpenWrt后RTL8822CE无线网卡显示“禁用”或“未激活”的完整解决方案
linux·github·运维开发
mounter6251 小时前
跨内核实时更新:如何实现 hugetlbfs 巨页的无缝保留与恢复?
linux·linux kernel·kernel·kexec·liveupdate·kho
qeen871 小时前
【Linux】make/Makefile 自动化工具的介绍
linux·运维·服务器·自动化
funnycoffee1231 小时前
Cisco ip access-list序号重排方法
linux·网络·tcp/ip
艾莉丝努力练剑2 小时前
【Linux:动静态库】Linux 动静态库与可执行文件
linux·运维·服务器·学习·面试·文件系统·动静态库
乌萨奇也要立志学C++2 小时前
【Linux】传输层协议UDP与TCP详解
linux·tcp/ip·udp
殷忆枫2 小时前
Linux 4G模块驱动适配实战:从手动绑定到自动识别
linux·运维·服务器
独隅2 小时前
CLion 在 Linux 上的完整安装与配置使用指南
linux·运维·服务器·c语言·c++·ide