用 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 有可执行的属性,剩下的就简单了,不再赘述

相关推荐

  1. ipset iptables 保护 sip 端口

    2024-04-03 12:14:04       42 阅读
  2. iptables命令总结

    2024-04-03 12:14:04       29 阅读
  3. 端口复用的SPI控制

    2024-04-03 12:14:04       69 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-03 12:14:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-03 12:14:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-03 12:14:04       87 阅读
  4. Python语言-面向对象

    2024-04-03 12:14:04       96 阅读

热门阅读

  1. TCP

    TCP

    2024-04-03 12:14:04      44 阅读
  2. Docker入门

    2024-04-03 12:14:04       37 阅读
  3. Node.js入门:常用命令一览

    2024-04-03 12:14:04       30 阅读
  4. FPGA的串口的收发程序设计

    2024-04-03 12:14:04       41 阅读
  5. 每日2题(面试)2024.4.2

    2024-04-03 12:14:04       32 阅读
  6. 数学分析复习:等价量的概念

    2024-04-03 12:14:04       42 阅读
  7. RGB到Lab的转换原理及例程

    2024-04-03 12:14:04       37 阅读
  8. SQL简单优化思路

    2024-04-03 12:14:04       40 阅读
  9. docker 应用部署

    2024-04-03 12:14:04       37 阅读
  10. WebGL BabylonJ 如何改变相机移动方式(WASD移动)

    2024-04-03 12:14:04       34 阅读