2017-08-01 23:13:28 +00:00
|
|
|
package zgrab2
|
|
|
|
|
|
|
|
import (
|
2017-08-03 19:55:15 +00:00
|
|
|
"net"
|
2017-08-03 20:06:48 +00:00
|
|
|
"strings"
|
2017-08-03 19:55:15 +00:00
|
|
|
|
|
|
|
"github.com/ajholland/zflags"
|
2017-08-01 23:13:28 +00:00
|
|
|
)
|
|
|
|
|
2017-08-03 19:55:15 +00:00
|
|
|
var parser *flags.Parser
|
2017-08-01 23:13:28 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-08-03 19:55:15 +00:00
|
|
|
parser = flags.NewParser(&config, flags.Default)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddCommand(command string, shortDescription string, longDescription string, data interface{}) (*flags.Command, error) {
|
|
|
|
return parser.AddCommand(command, shortDescription, longDescription, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseFlags() ([]string, error) {
|
|
|
|
return parser.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseInput takes input and parses it into either a list of IP addresses, domain name, or errors
|
|
|
|
func ParseInput(s string) ([]net.IP, string, error) {
|
2017-08-03 20:06:48 +00:00
|
|
|
i := strings.IndexByte(s, '/')
|
2017-08-03 19:55:15 +00:00
|
|
|
if i < 0 {
|
|
|
|
//not cidr
|
|
|
|
if ip := net.ParseIP(s); ip != nil {
|
2017-08-03 20:06:48 +00:00
|
|
|
return []net.IP{ip}, "", nil //single ip
|
2017-08-03 19:55:15 +00:00
|
|
|
} else {
|
|
|
|
return nil, s, nil //domain address
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//is cidr
|
2017-08-03 20:06:48 +00:00
|
|
|
ip, ipnet, err := net.ParseCIDR(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ips []net.IP
|
|
|
|
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
|
|
|
|
ips = append(ips, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, "", nil
|
2017-08-03 19:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func inc(ip net.IP) {
|
2017-08-03 20:06:48 +00:00
|
|
|
for j := len(ip) - 1; j >= 0; j-- {
|
|
|
|
ip[j]++
|
|
|
|
if ip[j] > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 23:13:28 +00:00
|
|
|
}
|