2017-08-01 23:13:28 +00:00
package zgrab2
import (
2017-08-29 01:23:16 +00:00
"errors"
2017-08-03 19:55:15 +00:00
"net"
2017-09-26 18:02:27 +00:00
"strconv"
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 )
}
2017-08-31 21:24:05 +00:00
// AddCommand adds a module to the parser and returns a pointer to a flags.command object or an error
2017-09-26 18:02:27 +00:00
func AddCommand ( command string , shortDescription string , longDescription string , port int , m ScanModule ) ( * flags . Command , error ) {
cmd , err := parser . AddCommand ( command , shortDescription , longDescription , m )
if err != nil {
return nil , err
}
cmd . FindOptionByLongName ( "port" ) . Default = [ ] string { strconv . FormatUint ( uint64 ( port ) , 10 ) }
cmd . FindOptionByLongName ( "name" ) . Default = [ ] string { command }
return cmd , nil
2017-08-03 19:55:15 +00:00
}
2017-08-31 21:24:05 +00:00
// ParseFlags abstracts away the parser and validates the framework configuration (global options) immediately after parsing
2017-08-03 19:55:15 +00:00
func ParseFlags ( ) ( [ ] string , error ) {
2017-08-09 18:56:05 +00:00
r , err := parser . Parse ( )
if err == nil {
validateFrameworkConfiguration ( )
}
return r , err
2017-08-03 19:55:15 +00:00
}
2017-08-31 21:24:05 +00:00
// ParseInput takes input as a string and parses it into either an IPNet (may have empty mask and just contain IP) , domain name, or errors, may return both IPNet and domain name
func ParseInput ( s string ) ( * net . IPNet , string , error ) {
2017-08-14 18:13:18 +00:00
i := strings . IndexByte ( s , ',' )
j := strings . IndexByte ( s , '/' )
switch {
case i == - 1 && j == - 1 :
//just ip or domain
2017-08-03 19:55:15 +00:00
if ip := net . ParseIP ( s ) ; ip != nil {
2017-08-31 21:24:05 +00:00
return & net . IPNet { IP : ip } , "" , nil
2017-08-03 19:55:15 +00:00
} else {
2017-08-29 01:23:16 +00:00
ips , err := net . LookupIP ( s )
if err != nil {
return nil , "" , err
}
2017-08-31 21:24:05 +00:00
return & net . IPNet { IP : ips [ 0 ] } , s , nil //only return first IP after a lookup
2017-08-03 19:55:15 +00:00
}
2017-08-14 18:13:18 +00:00
case i == - 1 :
//cidr block
2017-08-31 21:24:05 +00:00
_ , ipnet , err := net . ParseCIDR ( s )
2017-08-03 20:06:48 +00:00
if err != nil {
return nil , "" , err
}
2017-08-31 21:24:05 +00:00
return ipnet , "" , nil
2017-08-14 18:13:18 +00:00
case j == - 1 :
//ip,domain
str := strings . Split ( s , "," )
if len ( str ) != 2 {
2017-08-29 01:23:16 +00:00
return nil , "" , errors . New ( "malformed input" )
2017-08-14 18:13:18 +00:00
}
if ip := net . ParseIP ( str [ 0 ] ) ; ip != nil {
2017-08-31 21:24:05 +00:00
return & net . IPNet { IP : ip } , str [ 1 ] , nil
2017-08-14 18:13:18 +00:00
}
return nil , str [ 1 ] , nil
2017-08-03 19:55:15 +00:00
}
2017-08-14 18:13:18 +00:00
return nil , "" , nil
2017-08-03 19:55:15 +00:00
}
2017-08-09 18:56:05 +00:00
func incrementIP ( 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
}
2017-08-04 20:20:32 +00:00
2017-08-09 18:56:05 +00:00
func duplicateIP ( ip net . IP ) net . IP {
2017-08-04 20:20:32 +00:00
dup := make ( net . IP , len ( ip ) )
copy ( dup , ip )
return dup
}