用 ipset 和 iptables 保护 sip 端口

这里先假定 sip 端口是 5060 和 5080

cat china.sh,and ./china.sh

#!/bin/bash

apt install -y ipset

ipset destroy china

ipset create china hash:net maxelem 65536

ipset flush china

wget --no-check-certificate -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /etc/china.txt

while read ip; do

/sbin/ipset add china $ip

done < /etc/china.txt

ipset add china 192.168.0.0/16

ipset add china 172.16.0.0/12

ipset add china 10.0.0.0/8

ipset save china > ./china.conf

设置下面几条 iptables 规则:

iptables -A INPUT -p udp --dport 5060:5080 -m set --match-set china src -j ACCEPT

iptables -A INPUT -p udp --dport 5060:5080 -j DROP

iptables -A INPUT -p tcp --dport 5060:5080 -m set --match-set china src -j ACCEPT

iptables -A INPUT -p tcp --dport 5060:5080 -j DROP

iptables-save > ./iprule.conf

最后一个问题是开机后自动生效

cat /lib/systemd/system/rc-local.service

该服务的生效条件为 /etc/rc.local 有可执行的属性,剩下的就简单了,不再赘述

相关推荐
无名38721 天前
SIPp uac.xml 之我见
通讯
酒茶白开水2 个月前
React二组件进阶
react.js·生命周期·context·通讯·props·render-props·高阶组件
佳途自动化学院3 个月前
三菱FX3U进阶课程-运动控制讲解
自动化·plc·工控·通讯·三菱
无名3873 个月前
简单介绍 Kamailio cfg_rpc 模块
通讯
无名3874 个月前
MicroSIP 使用笔记
通讯
无名3875 个月前
Kamailio openssl 3.0.x 需要注意的事项
通讯
h6play5 个月前
如何优雅的实现 iframe 多层级嵌套通讯
npm·vue·bridge·iframe·通讯·连接·嵌套iframe
无名3875 个月前
rtpengine 的端点学习模式
通讯
无名3876 个月前
测试 rtpengine 的 sip-source 参数
通讯
无名3876 个月前
Debian11 下源码编译 rtpengine11.1
通讯