vulnebify/cidre

GitHub: vulnebify/cidre

从区域互联网注册机构自动获取按国家分类的 CIDR 数据,并直接对接 UFW/iptables 实现基于地理位置的访问控制。

Stars: 28 | Forks: 2

# CIDRE **CIDRe** 是一个 CLI 工具,它从 **区域互联网注册机构** 获取**每日更新的 IP 分配**,将其编译成基于国家的 CIDR 文件,并提供便捷的**防火墙管理**功能。每日自动 CIDR 更新**[位于仓库中](https://github.com/vulnebify/cidre/blob/main/output/cidr)。** [![编译 CIDR](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/5e8e469385122550.svg)](https://github.com/vulnebify/cidre/actions/workflows/compile_cidrs.yml) [![发布到 PyPI](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/104c66e508122551.svg)](https://github.com/vulnebify/cidre/actions/workflows/pypi_release.yml) [![asciicast](https://asciinema.org/a/727926.svg)](https://asciinema.org/a/727926) ## 快速开始 ### **安装 CIDRE** ``` pip install cidre-cli ``` ### **拉取与合并 CIDR 范围** ``` cidre cidr pull --merge ``` - 从 RIR 下载最新的 CIDR 分配。 - 合并重叠的 IP 范围以提高效率。 ### **屏蔽特定国家** ``` # UFW 更适合小型 CIDR 输入 cidre firewall deny ir kp --firewall ufw ``` - 在 UFW 中屏蔽 **伊朗 (IR) 和朝鲜 (KP)**。 - 需要安装 **ufw** (`sudo apt install ufw`)。 ``` # iptables 更适合大型 CIDR 输入 cidre firewall deny ru ir kp --firewall iptables ``` - 使用 ipset 在 iptables 中屏蔽 **俄罗斯 (RU)、伊朗 (IR) 和朝鲜 (KP)**。 - 需要安装 **ipset 和 iptables** (`sudo apt install ipset iptables`)。 ## 安装 ### 从 PyPI ``` pip install cidre-cli ``` ### 从 Release 从 [Releases](https://github.com/vulnebify/cidre/releases) 页面下载最新的预编译二进制文件: ``` chmod +x cidre && ./cidre ``` ### 从源码 ``` git clone https://github.com/vulnebify/cidre.git && cd cidre && python3 -m venv .venv && source .venv/bin/activate && pip install . ``` ## 命令 ### `cidr pull` | 命令 | 描述 | | ----------------------------------- | ------------------------------------------------------------------- | | `cidre cidr pull` | 从所有 RIR 获取最新的 IP 分配数据 | | `cidre cidr pull --merge` | 合并重叠的 IP 范围以提高效率。可选。 | | `cidre cidr pull --proxy PROXY` | 使用代理连接到 RIR。可选。 | | `cidre cidr pull --cidr-store PATH` | 指定 CIDR 的自定义存储目录。默认值:`./output/cidr` | ### `cidr count` | 命令 | 描述 | | ------------------------ | -------------------------------------------------------------- | | `cidre cidr count` | 统计每个国家的 IP 数量 | | `cidre cidr count US CN` | 按国家代码 (ISO 3166-1 alpha-2 代码) 统计 IP 数量 | | `cidre cidr count --cidr-store PATH` | 指定 CIDR 的自定义存储目录。默认值:`./output/cidr` | ### `firewall allow|deny|reject` | 命令 | 描述 | | ----------------------------------------- | ------------------------------------------------------------------- | | `cidre firewall allow` | 对指定防火墙应用允许规则 | | `cidre firewall deny` | 对指定防火墙应用拒绝规则 | | `cidre firewall reject` | 对指定防火墙应用拒绝规则 | | `cidre firewall reject --firewall ufw` | 要应用规则的防火墙。选项:`ufw`、`iptables`。默认值:`ufw` | | `cidre firewall reject --cidr-store PATH` | 指定 CIDR 的自定义存储目录。默认值:`./output/cidr` | **⚠️ 注意:iptables 防火墙默认不持久化规则** 为了确保 iptables 和 IPSet 规则在重启后依然有效,请执行以下步骤: ``` # 基于 firewall 方法保存规则: # - 对于 iptables + IPSet: sudo ipset save > /etc/ipset.rules sudo iptables-save > /etc/iptables/rules.v4 sudo ip6tables-save > /etc/iptables/rules.v6 # 在启动时恢复 firewall 规则: # - 对于 iptables + IPSet: sudo bash -c 'echo "ipset restore < /etc/ipset.rules" >> /etc/rc.local' sudo chmod +x /etc/rc.local # 重启并验证: sudo reboot sudo ipset list sudo iptables -L -v -n ``` ## 许可证 本项目基于 **MIT 许可证**授权。 ## 灵感来源 CIDRE 的灵感来源于 **[herrbischoff/country-ip-blocks](https://github.com/herrbischoff/country-ip-blocks)**,旨在提供一种带有防火墙集成的自动化替代方案。 ## 贡献 欢迎提交 PR!随时**复刻仓库**并提交 Pull Request。 ## 联系方式 如有任何疑问,请开启一个 issue 或通过 GitHub Discussions 联系我们。
标签:CIDR, IPSet, iptables, IP 地址批量处理, IP地址管理, IP封禁, Python, RIR, Streamlit, UFW, 二进制发布, 地理封锁, 开源工具, 文档结构分析, 无后门, 网络安全, 自动化运维, 访问控制, 逆向工具, 防火墙管理, 隐私保护