rita icon indicating copy to clipboard operation
rita copied to clipboard

Add IPv6 support for single IPs

Open ethack opened this issue 6 years ago • 2 comments

The following line will attempt to append a /32 on any string configured in the Filtering section if it fails to parse. The rationale is that the user may simply have meant a single IP and forgot to append /32 to make it CIDR format. However, this also assumes IPv4 as IPv6 would have a suffix of /128. https://github.com/activecm/rita/blob/305-add-neverinclude-to-filtering/parser/filter.go#L66

Suggestions to fix this would be to see if the Golang net library has any functions that would help tell these apart. Or check for : characters to identify an IPv6 address. Side note: I don't think checking for . will work to detect IPv4 as I just saw an RFC where an IPv6 address could be written with a combination of : and ..

ethack avatar Jan 03 '19 22:01 ethack

This is the solution we are using over in IPFIX-RITA

func (f *filtering) parseSubnetList(netList []string) ([]net.IPNet, []error) {
	var errorList []error
	var nets []net.IPNet
	for j := range netList {
		//parse as network
		_, network, err := net.ParseCIDR(netList[j])
		if err != nil {
			//parse as IP
			ipAddr := net.ParseIP(netList[j])

			if ipAddr == nil {
				errorList = append(errorList, errors.WithStack(err))
				continue
			}

			network = f.ipToIPNet(ipAddr)
		}

		nets = append(nets, *network)
	}
	return nets, errorList
}

func (f *filtering) ipToIPNet(ipAddr net.IP) *net.IPNet {
	var netmask net.IPMask
	if ipAddr.To4() == nil {
		netmask = net.CIDRMask(32, 32)
	} else {
		netmask = net.CIDRMask(128, 128)
	}
	return &net.IPNet{
		IP:   ipAddr,
		Mask: netmask,
	}
}

Heres a stack overflow post thats worth a read: https://stackoverflow.com/questions/22751035/golang-distinguish-ipv4-ipv6

Zalgo2462 avatar Jan 15 '19 21:01 Zalgo2462

Hello Sir,

Is this issue still active? I would like to contribute to this repo :)

CypherpunkSamurai avatar Sep 19 '22 20:09 CypherpunkSamurai

Updated link to problem area in the code: https://github.com/activecm/rita/blob/master/util/ip.go#L39

Zalgo2462 avatar Jul 21 '23 20:07 Zalgo2462