mapcidr

package module
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2022 License: MIT Imports: 10 Imported by: 113

README

mapCIDR

A utility program to perform multiple operations for a given subnet/cidr ranges.

Features • Install • Usage • Library • Join Discord


mapCIDR is developed to ease load distribution for mass scanning operations, it can be used both as a library and as independent CLI tool.

Features

mapCIDR

  • CIDR expansion support.
  • CIDR slicing support.
  • CIDR/IP aggregation support.
  • CIDR based IP filter support.
  • IP/PORT shuffling support.
  • STD IN/OUT support.

Installation

go install -v github.com/projectdiscovery/mapcidr/cmd/mapcidr@latest

Usage

mapcidr -h

This will display help for the tool. Here are all the switches it supports.

INPUT:
   -cidr string          CIDR to process
   -l, -list string      File containing list of CIDRs to process
   -il, -ip-list string  File containing list of IPs to process

PROCESS:
   -sbc int                   Slice CIDRs by given CIDR count
   -sbh int                   Slice CIDRs by given HOST count
   -agg, -aggregate           Aggregate IPs/CIDRs into the minimum subnet
   -sip, -shuffle-ip          Shuffle input ip
   -sp, -shuffle-port string  Shuffle input ip:port

OUTPUT:
   -o, -output string  File to write output to
   -silent             Silent mode
   -version            Show version
   -skip-base          Skip base IPs (ending in .0) in output
   -skip-broadcast     Skip broadcast IPs (ending in .255) in output

Running mapCIDR

In order to get list of IPs for a give CIDR, use the following command.

CIDR expansion
mapcidr -cidr 173.0.84.0/24
                   ____________  ___    
  __ _  ___ ____  / ___/  _/ _ \/ _ \
 /  ' \/ _ '/ _ \/ /___/ // // / , _/   
/_/_/_/\_,_/ .__/\___/___/____/_/|_| v0.5
          /_/                                                     	 

		projectdiscovery.io

[WRN] Use with caution. You are responsible for your actions
[WRN] Developers assume no liability and are not responsible for any misuse or damage.

173.0.84.0
173.0.84.1
173.0.84.2
173.0.84.3
173.0.84.4
173.0.84.5
173.0.84.13
173.0.84.14
173.0.84.15
173.0.84.16
CIDR Slicing by CIDR

In order to slice given CIDR or list of CIDR by CIDR count or slice into multiple and equal smaller subnets, use the following command.

mapcidr -cidr 173.0.84.0/24 -sbc 10 -silent
173.0.84.0/27
173.0.84.32/27
173.0.84.64/27
173.0.84.96/27
173.0.84.128/27
173.0.84.160/27
173.0.84.208/28
173.0.84.192/28
173.0.84.240/28
173.0.84.224/28
CIDR slicing by HOST

In order to slice given CIDR for equal number of host count in each CIDR, use the following command.

mapcidr -cidr 173.0.84.0/16 -sbh 20000 -silent
173.0.0.0/18
173.0.64.0/18
173.0.128.0/18
173.0.192.0/18

Note: it's possible to obtain a perfect split only when the desired amount of slices or hosts per subnet is a powers of two. Otherwise, the tool will attempt to automatically find the best split strategy to obtain the desired outcome.

CIDR/IP aggregation

In order to merge multiple CIDR ranges into smaller subnet block, use the following command.

mapcidr -l cidrs.txt -silent -aggregate

In order to list CIDR blocks for given list of IPs, use the following command.

mapcidr -l ips.txt -silent -aggregate
CIDR based IP filtering

In order to filter IPs from the given list of CIDR ranges, use the following command.

mapcidr -ips ip-list.txt -l cirds.txt

Use mapCIDR as a library

It's possible to use the library directly in your go programs. The following code snippets outline how to divide a cidr into subnets, and how to divide the same into subnets containing a certain number of hosts

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)
	}
}

mapCDIR is made with 🖤 by the projectdiscovery team.

Documentation

Overview

Package mapcidr implements methods to allow working with CIDRs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddressCount

func AddressCount(cidr string) (uint64, error)

AddressCount returns the number of IP addresses in a range

func AddressCountIpnet

func AddressCountIpnet(network *net.IPNet) uint64

AddressCountIpnet returns the number of IP addresses in an IPNet structure

func AddressRange

func AddressRange(network *net.IPNet) (firstIP, lastIP net.IP, err error)

AddressRange returns the first and last addresses in the given CIDR range.

func AsIPV4CIDR added in v0.0.7

func AsIPV4CIDR(ipv4 string) *net.IPNet

AsIPV4CIDR converts ipv4 address to cidr representation

func CIDRsAsIPNET added in v0.0.7

func CIDRsAsIPNET(cidrs []string) (ipnets []*net.IPNet)

CIDRsAsIPNET converts a list of cidrs to ipnet

func CoalesceCIDRs added in v0.0.6

func CoalesceCIDRs(cidrs []*net.IPNet) (coalescedIPV4, coalescedIPV6 []*net.IPNet)

CoalesceCIDRs transforms the provided list of CIDRs into the most-minimal equivalent set of IPv4 and IPv6 CIDRs. It removes CIDRs that are subnets of other CIDRs in the list, and groups together CIDRs that have the same mask size into a CIDR of the same mask size provided that they share the same number of most significant mask-size bits.

Note: this algorithm was ported from the Python library netaddr. https://github.com/drkjam/netaddr .

func CountIPsInCIDR added in v0.0.6

func CountIPsInCIDR(ipnet *net.IPNet) *big.Int

CountIPsInCIDR takes a RFC4632/RFC4291-formatted IPv4/IPv6 CIDR and determines how many IP addresses reside within that CIDR. Returns 0 if the input CIDR cannot be parsed.

func GetCIDRPrefixesFromIPs added in v0.0.6

func GetCIDRPrefixesFromIPs(ips []net.IP) []*net.IPNet

GetCIDRPrefixesFromIPs returns all of the ips as a slice of *net.IPNet.

func GetNextIP added in v0.0.6

func GetNextIP(ip net.IP) net.IP

GetNextIP returns the next IP from the given IP address. If the given IP is the last IP of a v4 or v6 range, the same IP is returned.

func IPAddresses added in v0.0.2

func IPAddresses(cidr string) ([]string, error)

IPAddresses returns all the IP addresses in a CIDR

func IPAddressesAsStream added in v0.0.8

func IPAddressesAsStream(cidr string) (chan string, error)

func IPAddressesIPnet added in v0.0.2

func IPAddressesIPnet(ipnet *net.IPNet) (ips []string)

IPAddressesIPnet returns all IP addresses in an IPNet.

func IPToInteger added in v0.0.2

func IPToInteger(ip net.IP) (*big.Int, int, error)

IPToInteger converts an IP address to its integer representation. It supports both IPv4 as well as IPv6 addresses.

func IPToPrefix added in v0.0.6

func IPToPrefix(ip net.IP) *net.IPNet

IPToPrefix returns the corresponding IPNet for the given IP.

func Inet_aton added in v0.0.6

func Inet_aton(ipnr net.IP) int64

Inet_aton convert net.IP to int64

func Inet_ntoa added in v0.0.6

func Inet_ntoa(ipnr int64) net.IP

Inet_ntoa convert uint to net.IP

func IntegerToIP added in v0.0.2

func IntegerToIP(ipInt *big.Int, bits int) net.IP

IntegerToIP converts an Integer IP address to net.IP format.

func IsBaseIP added in v0.0.9

func IsBaseIP(IP string) bool

func IsBroadcastIP added in v0.0.9

func IsBroadcastIP(IP string) bool

func IsExcluded added in v0.0.6

func IsExcluded(excludeList []net.IP, ip net.IP) bool

IsExcluded returns whether a given IP is must be excluded due to coming from blacklisted device.

func IsIPv4 added in v0.0.6

func IsIPv4(ip net.IP) bool

IsIPv4 returns true if the given IP is an IPv4

func KeepUniqueIPs added in v0.0.6

func KeepUniqueIPs(ips []net.IP) []net.IP

KeepUniqueIPs transforms the provided multiset of IPs into a single set, lexicographically sorted via a byte-wise comparison of the IP slices (i.e. IPv4 addresses show up before IPv6). The slice is manipulated in-place destructively.

1- Sort the slice by comparing the IPs as bytes 2- For every unseen unique IP in the sorted slice, move it to the end of the return slice. Note that the slice is always large enough and, because it is sorted, we will not overwrite a valid element with another. To overwrite an element i with j, i must have come before j AND we decided it was a duplicate of the element at i-1.

func PickIP added in v0.0.7

func PickIP(cidrs []*net.IPNet, index int64) string

PickIP takes an ip from a list of subnets

func PickPort added in v0.0.7

func PickPort(ports []int, index int) int

PickPort takes a port from a list

func PickSubnetIP added in v0.0.7

func PickSubnetIP(network *net.IPNet, index int64) string

PickSubnetIP takes an ip from a subnet

func RemoveCIDRs added in v0.0.6

func RemoveCIDRs(allowCIDRs, removeCIDRs []*net.IPNet) ([]*net.IPNet, error)

RemoveCIDRs removes the specified CIDRs from another set of CIDRs. If a CIDR to remove is not contained within the CIDR, the CIDR to remove is ignored. A slice of CIDRs is returned which contains the set of CIDRs provided minus the set of CIDRs which were removed. Both input slices may be modified by calling this function.

func ShuffleCidrsWithPortsAndSeed added in v0.0.7

func ShuffleCidrsWithPortsAndSeed(cidrs []*net.IPNet, ports []int, seed int64) chan Item

ShuffleCidrsWithPortsAndSeed uses blackrock to visit all ips and ports combinations in random order

func ShuffleCidrsWithSeed added in v0.0.7

func ShuffleCidrsWithSeed(cidrs []*net.IPNet, seed int64) chan Item

ShuffleCidrsWithSeed uses blackrock to visit all ips in random order

func SplitByNumber

func SplitByNumber(iprange string, number int) ([]*net.IPNet, error)

SplitByNumber splits the given cidr into subnets with the closest number of hosts per subnet.

func SplitIPNetByNumber added in v0.0.2

func SplitIPNetByNumber(ipnet *net.IPNet, number int) ([]*net.IPNet, error)

SplitIPNetByNumber splits an IPNet into subnets with the closest n umber of hosts per subnet.

func SplitIPNetIntoN added in v0.0.2

func SplitIPNetIntoN(iprange *net.IPNet, n int) ([]*net.IPNet, error)

SplitIPNetIntoN attempts to split a ipnet in the exact number of subnets

func SplitN

func SplitN(iprange string, n int) ([]*net.IPNet, error)

SplitN attempts to split a cidr in the exact number of subnets

func TotalIPSInCidrs added in v0.0.7

func TotalIPSInCidrs(cidrs []*net.IPNet) (totalIPs uint64)

TotalIPSInCidrs calculates the number of ips in the diven cidrs

Types

type Item added in v0.0.7

type Item struct {
	IP   string
	Port int
}

Item represent a combination of ip:port

func (Item) String added in v0.0.7

func (i Item) String() string

String returns the item as ip:port

type NetsByMask added in v0.0.6

type NetsByMask []*net.IPNet

NetsByMask is used to sort a list of IP networks by the size of their masks. Implements sort.Interface.

func (NetsByMask) Len added in v0.0.6

func (s NetsByMask) Len() int

func (NetsByMask) Less added in v0.0.6

func (s NetsByMask) Less(i, j int) bool

func (NetsByMask) Swap added in v0.0.6

func (s NetsByMask) Swap(i, j int)

type NetsByRange added in v0.0.6

type NetsByRange []*netWithRange

NetsByRange is used to sort a list of ranges, first by their last IPs, then by their first IPs Implements sort.Interface.

func (NetsByRange) Len added in v0.0.6

func (s NetsByRange) Len() int

func (NetsByRange) Less added in v0.0.6

func (s NetsByRange) Less(i, j int) bool

func (NetsByRange) Swap added in v0.0.6

func (s NetsByRange) Swap(i, j int)

Directories

Path Synopsis
cmd
mapcidr command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL