mapCIDR

repository·main·Indexed 22 days ago

https://github.com/projectdiscovery/mapcidr

A high-performance utility and Go library for subnet operations, including CIDR expansion, slicing by CIDR or host count, aggregation, and IP filtering. It supports converting between IPv4 and IPv6, transforming IPs into various formats (hex, decimal, binary, etc.), and calculating host counts. Designed to facilitate load distribution for mass scanning operations, it provides both a CLI tool and a Go package with functions like SplitN, SplitByNumber, and CoalesceCIDRs.

Tokens
5K
Snippets
17
Records
40
Agent score
79%

What's inside mapcidr

  1. Match or Filter IPs from CIDR ranges

    main
    Use -mi (match-ip) to only output IPs from the input CIDR that exist in the provided match list. Use -fi (filter-ip) to exclude IPs from the input CIDR that exist in the provided filter list. Both flags accept comma-separated strings or file paths.
  2. Slice CIDRs by Host count

    main

    Use the -sbh flag to slice a CIDR into multiple subnets, each containing approximately the specified number of hosts. The tool attempts to find the best split strategy if a perfect power-of-two split is not possible.

    mapcidr -cidr 173.0.84.0/16 -sbh 20000 -silent
  3. Convert between IPv4 and IPv6

    main
    Convert IPv4-mapped IPv6 addresses to IPv4 using -t4 or convert IPv4 addresses to IPv6 notation using -t6. Note that not all IPv6 addresses can be converted to IPv4; only valid IPv4-represented IPv6 addresses are supported.
  4. Aggregate IPs and CIDRs

    main

    Aggregate multiple CIDR ranges or individual IPs into the minimum possible subnet blocks using -a or -aggregate.

    For sparse IP groups (IPv4 only), use -aa or -aggregate-approx to perform approximated aggregation, which may result in a final interval containing contiguous IPs that were not in the original input.

  5. Authenticate with ProjectDiscovery Cloud (PDCP)

    main

    mapCIDR supports authentication with ProjectDiscovery Cloud (PDCP). Authentication follows this hierarchy:

    1. --auth CLI flag (if set to true, it triggers credential validation).
    2. PDCP_API_KEY environment variable.
    3. Credentials stored in the .pdcp/credential file.

    If you provide a 36-character string via the --auth flag, it is treated as the API key. If no local credentials exist, the tool will attempt to validate the key against the PDCP API server (configured via PDCP_API_SERVER) and save it if valid.

  6. Use mapCIDR CLI for CIDR and IP processing

    main

    mapCIDR is a tool designed to ease load distribution for mass scanning operations. It can process CIDRs, individual IPs, IP ranges, and ASNs. It supports various operations including slicing, aggregation, filtering, and format conversion.

    Input Methods:

    • Use the -cidr or -cl flag to provide a list of CIDRs/IPs or a file containing them.
    • Pipe input via stdin.

    Common Operations:

    • Slicing: Split a CIDR into multiple subnets using -sbc (by CIDR count) or -sbh (by host count).
    • Aggregation: Combine IPs/CIDRs into the minimum possible subnet using -aggregate or approximated subnets using -aggregate-approx.
    • Filtering: Filter for IPv4 (-f4) or IPv6 (-f6), or use -match-ip to include only specific IPs/CIDRs or -filter-ip to exclude them.
    • Conversion: Convert CIDRs to IP ranges (-range), or convert IPs to IPv4 (-t4) or IPv6 (-t6) formats.
  7. Use mapCIDR as a Go library

    main

    You can import github.com/projectdiscovery/mapcidr to use its core functionality directly in your Go applications. Key functions include SplitN for slicing by CIDR count, SplitByNumber for slicing by host count, and IPAddresses to list all IPs in a CIDR.

    package main
    
    import (
    	"fmt"
    
    	"github.com/projectdiscovery/mapcidr"
    )
    
    func main() {
    	// Divide the CIDR into two subnets
    	subnets1 := mapcidr.SplitN("192.168.1.0/24", 2)
    	for _, subnet := range subnets1 {
    		fmt.Println(subnet)
    	}
    	// Divide the CIDR into two subnets containing 128 hosts each
    	subnets2 := mapcidr.SplitByNumber("192.168.1.0/24", 128)
    	for _, subnet := range subnets2 {
    		fmt.Println(subnet)
    	}
    
    	// List all IPs in the CIDR
    	ips, _ := mapcidr.IPAddresses("192.168.1.0/24")
    	for _, ip := range ips {
    		fmt.Println(ip)
    	}
    }