diff --git a/cmd/parse.go b/cmd/parse.go index f5066ad..d7346db 100644 --- a/cmd/parse.go +++ b/cmd/parse.go @@ -7,6 +7,7 @@ import ( "net" "os" "strconv" + "strings" "time" "github.com/docopt/docopt-go" @@ -48,7 +49,7 @@ type O struct { } func parse() *O { - args, err := docopt.Parse(usage, nil, true, "cookiescan 2.2.0", false) + args, err := docopt.Parse(usage, nil, true, "cookiescan 2.3.0", false) if err != nil { log.Fatalf("Error parsing usage. Error: %s\n", err.Error()) } @@ -123,7 +124,7 @@ func parse() *O { return o } -// linesToIPList processes a list of IP addresses or networks in CIDR format. +// linesToIPList processes a list of IP addresses or networks in CIDR or dash format. // Returning a list of all possible IP addresses. func linesToIPList(lines []string) ([]string, error) { ipList := []string{} @@ -134,13 +135,39 @@ func linesToIPList(lines []string) ([]string, error) { for ip := ip.Mask(network.Mask); network.Contains(ip); increaseIP(ip) { ipList = append(ipList, ip.String()) } + } else if strings.Contains(line, "-") { + splitIP := strings.SplitN(line, "-", 2) + ip := net.ParseIP(splitIP[0]) + endIP := net.ParseIP(splitIP[1]) + if endIP != nil { + if !isStartingIPLower(ip, endIP) { + return ipList, fmt.Errorf("%s is greater than %s", ip.String(), endIP.String()) + } + ipList = append(ipList, ip.String()) + for !ip.Equal(endIP) { + increaseIP(ip) + ipList = append(ipList, ip.String()) + } + } else { + ipOct := strings.SplitN(ip.String(), ".", 4) + endIP := net.ParseIP(ipOct[0] + "." + ipOct[1] + "." + ipOct[2] + "." + splitIP[1]) + if endIP != nil { + if !isStartingIPLower(ip, endIP) { + return ipList, fmt.Errorf("%s is greater than %s", ip.String(), endIP.String()) + } + ipList = append(ipList, ip.String()) + for !ip.Equal(endIP) { + increaseIP(ip) + ipList = append(ipList, ip.String()) + } + } else { + return ipList, fmt.Errorf("%s is not an IP Address or CIDR Network", line) + } + } + } else if ip, err := net.LookupIP(line); err == nil { + ipList = append(ipList, ip[0].String()) } else { return ipList, fmt.Errorf("%s is not an IP Address or CIDR Network", line) - ips, err := net.LookupIP(line) - if err != nil { - return ipList, fmt.Errorf("%s is not a valid hostname", line) - } - ipList = append(ipList, ips[0].String()) } } return ipList, nil @@ -156,6 +183,18 @@ func increaseIP(ip net.IP) { } } +func isStartingIPLower(start, end net.IP) bool { + if len(start) != len(end) { + return false + } + for i := range start { + if start[i] > end[i] { + return false + } + } + return true +} + // readFileLines returns all the lines in a file. func readFileLines(path string) ([]string, error) { file, err := os.Open(path)