Micsoftvn
  • 😙Micsoftvn
  • Use Cases
    • For Hacking
      • Kiểm thử mạng
      • Tor - Sock - Proxy
      • Poc
        • POC -draytek-vigor2960 ( CVE-2024-12987 )
    • For Security
      • Security website with htacess file
      • Incident Response
        • Cli AWS - Incident
        • Command line
      • Add basic Authen with Cloudflare
      • Haderning Apache
      • Thiết lập ANTT cho TLS
      • Check network traffic ( Ddos )
      • Tools
        • Tools for AWS
        • Fail2Ban Cheat Sheet
      • Các lỗi thường bảo mật với Websocket
    • For Engineering
      • Thiết lập cấu hình CMD log
      • Cấu hình CLI kết nối đến AWS
      • Sử dụng PET
      • 🔧 Gom Nhóm IP Thành Subnet CIDR Tối Ưu
      • PAC Proxy: Tự Động Cấu Hình Proxy Trong Môi Trường Doanh Nghiệp
      • Sử dụng Podman tạo base images Pentest
      • Tạo YUM Local Repository Trong Container CentOS 7 Sử Dụng Podman
      • Cài Đặt Và Cấu Hình dnsmasq Trên Ubuntu
    • For SysAdmin
      • Scripts
        • Bash Script Gen SSH key
        • Health check System
      • Install Oracle Java JDK 18 in Ubuntu 20.04
      • Run script on startup on Ubuntu 22.04
      • Remove Snap from Ubuntu
      • Config Network on Ubuntu Server
      • View Wifi Network Connection
      • Add user can access network interfaces
      • USB drive with QEMU
      • INSTALL AND MANAGE MULTIPLE JAVA JDK AND JRE VERSIONS ON UBUNTU
      • Export Windows Config
      • Auto Install Openvpn
      • Install Nginx Centos 7 or Docker
      • Install Mkdocs
      • Cheat Sheet
        • Cheat sheet Postgres
      • Cài Đặt Fluent Bit Trên Amazon Linux 2023 & Tạo Repository Offline
    • Installations
      • Install Helm on Ubuntu
  • Extras
    • Keyboard Shortcuts
Powered by GitBook
On this page
  • 💡 Ví dụ đầu vào:
  • 🧠 Kết quả sau khi tối ưu subnet:
  • 🛠 Code Python tự động gom subnet
  • 📦 Cài đặt thư viện cần thiết:
  • 🧩 Ứng dụng thực tế:

Was this helpful?

  1. Use Cases
  2. For Engineering

🔧 Gom Nhóm IP Thành Subnet CIDR Tối Ưu

Trong quá trình cấu hình firewall, proxy, WAF (ModSecurity), hay security group trên các hệ thống cloud/on-premise, việc phải liệt kê từng địa chỉ IP riêng biệt là cực kỳ tốn thời gian và dễ nhầm lẫn.

🎯 Mục tiêu

Tự động gom các IP rời rạc thành các subnet nhỏ nhất có thể sử dụng CIDR notation – giúp:

  • Giảm dòng cấu hình cần gõ

  • Tối ưu rule firewall

  • Dễ kiểm soát, bảo trì về sau


💡 Ví dụ đầu vào:

Danh sách IP ban đầu:

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.10
192.168.1.11
192.168.2.100
192.168.2.101
192.168.2.102
192.168.2.103
192.168.2.200

🧠 Kết quả sau khi tối ưu subnet:

192.168.1.1/32

192.168.1.1

192.168.1.2/31

192.168.1.2 – 192.168.1.3

192.168.1.4/32

192.168.1.4

192.168.1.10/31

192.168.1.10 – 192.168.1.11

192.168.2.100/30

192.168.2.100 – 192.168.2.10

192.168.2.200/32

192.168.2.200

🛠 Code Python tự động gom subnet

from netaddr import IPAddress, IPSet, cidr_merge

ip_list = [
    '192.168.1.1',
    '192.168.1.2',
    '192.168.1.3',
    '192.168.1.4',
    '192.168.1.10',
    '192.168.1.11',
    '192.168.2.100',
    '192.168.2.101',
    '192.168.2.102',
    '192.168.2.103',
    '192.168.2.200'
]

ip_set = IPSet(IPAddress(ip) for ip in ip_list)
merged_subnets = cidr_merge(ip_set.iter_cidrs())

print("Các subnet tối ưu:")
for subnet in merged_subnets:
    print(subnet)

📦 Cài đặt thư viện cần thiết:

pip install netaddr

🧩 Ứng dụng thực tế:

  • Tạo whitelist IP cho NGINX, Apache, iptables

  • Nhập nhanh security group vào AWS/GCP/Azure

  • Giảm tải khi audit hệ thống


PreviousSử dụng PETNextPAC Proxy: Tự Động Cấu Hình Proxy Trong Môi Trường Doanh Nghiệp

Last updated 1 month ago

Was this helpful?