diff --git a/Plugins/portscan.go b/Plugins/portscan.go index 36637d6..efa54c1 100644 --- a/Plugins/portscan.go +++ b/Plugins/portscan.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/shadow1ng/fscan/common" "net" + "sort" "strconv" "sync" "time" @@ -19,22 +20,21 @@ func PortScan(hostslist []string, ports string, timeout int64) []string { probePorts := common.ParsePort(ports) noPorts := common.ParsePort(common.NoPorts) if len(noPorts) > 0 { - var tmpPorts []int + temp := map[int]struct{}{} for _, port := range probePorts { - var flag bool - nport: - for _, noport := range noPorts { - if port == noport { - flag = true - break nport - } - } - if flag { - continue - } - tmpPorts = append(tmpPorts, port) + temp[port] = struct{}{} } - probePorts = tmpPorts + + for _, port := range noPorts { + delete(temp, port) + } + + var newDatas []int + for port, _ := range temp { + newDatas = append(newDatas, port) + } + probePorts = newDatas + sort.Ints(probePorts) } workers := common.Threads Addrs := make(chan Addr, len(hostslist)*len(probePorts)) diff --git a/common/ParseIP.go b/common/ParseIP.go index 94f5065..1bf40b2 100644 --- a/common/ParseIP.go +++ b/common/ParseIP.go @@ -7,6 +7,7 @@ import ( "net" "os" "regexp" + "sort" "strconv" "strings" ) @@ -21,35 +22,54 @@ var ParseIPErr = errors.New(" host parsing error\n" + "192.168.1.1-192.168.255.255\n" + "192.168.1.1-255") -func ParseIP(ip string, filename string) (hosts []string, err error) { - +func ParseIP(ip string, filename string, nohost string) (hosts []string, err error) { if ip != "" { - hosts, err = ParseIPs(ip) + hosts = ParseIPs(ip) } if filename != "" { var filehost []string filehost, _ = Readipfile(filename) hosts = append(hosts, filehost...) } + + if nohost != "" { + nohosts := ParseIPs(nohost) + if len(nohosts) > 0 { + temp := map[string]struct{}{} + for _, host := range hosts { + temp[host] = struct{}{} + } + + for _, host := range nohosts { + delete(temp, host) + } + + var newDatas []string + for host, _ := range temp { + newDatas = append(newDatas, host) + } + hosts = newDatas + sort.Strings(hosts) + } + } hosts = RemoveDuplicate(hosts) return hosts, err } -func ParseIPs(ip string) (hosts []string, err error) { +func ParseIPs(ip string) (hosts []string) { + var err error if strings.Contains(ip, ",") { IPList := strings.Split(ip, ",") var ips []string for _, ip := range IPList { ips, err = ParseIPone(ip) - CheckErr(ip, err) hosts = append(hosts, ips...) } - return hosts, err } else { hosts, err = ParseIPone(ip) - CheckErr(ip, err) - return hosts, err } + CheckErr(ip, err) + return hosts } func ParseIPone(ip string) ([]string, error) { @@ -189,8 +209,7 @@ func Readipfile(filename string) ([]string, error) { for scanner.Scan() { text := strings.TrimSpace(scanner.Text()) if text != "" { - host, err := ParseIPs(text) - CheckErr(text, err) + host := ParseIPs(text) content = append(content, host...) } } @@ -198,7 +217,7 @@ func Readipfile(filename string) ([]string, error) { } func RemoveDuplicate(old []string) []string { - result := make([]string, 0, len(old)) + result := []string{} temp := map[string]struct{}{} for _, item := range old { if _, ok := temp[item]; !ok { diff --git a/common/ParsePort.go b/common/ParsePort.go index c8e4645..c9cb328 100644 --- a/common/ParsePort.go +++ b/common/ParsePort.go @@ -5,8 +5,10 @@ import ( "strings" ) -func ParsePort(ports string) []int { - var scanPorts []int +func ParsePort(ports string) (scanPorts []int) { + if ports == "" { + return + } slices := strings.Split(ports, ",") for _, port := range slices { port = strings.Trim(port, " ") @@ -39,7 +41,7 @@ func ParsePort(ports string) []int { } func removeDuplicate(old []int) []int { - result := make([]int, 0, len(old)) + result := []int{} temp := map[int]struct{}{} for _, item := range old { if _, ok := temp[item]; !ok { diff --git a/common/config.go b/common/config.go index ef62973..3075ade 100644 --- a/common/config.go +++ b/common/config.go @@ -92,5 +92,6 @@ var ( UrlFile string Urls []string NoPorts string + NoHosts string SC string ) diff --git a/common/flag.go b/common/flag.go index f0f6467..2387e71 100644 --- a/common/flag.go +++ b/common/flag.go @@ -19,6 +19,7 @@ func Banner() { func Flag(Info *HostInfo) { Banner() flag.StringVar(&Info.Host, "h", "", "IP address of the host you want to scan,for example: 192.168.11.11 | 192.168.11.11-255 | 192.168.11.11,192.168.11.12") + flag.StringVar(&NoHosts, "hn", "", "the hosts no scan,as: -hn 192.168.1.1/24") flag.StringVar(&Info.Ports, "p", DefaultPorts, "Select a port,for example: 22 | 1-65535 | 22,80,3306") flag.StringVar(&NoPorts, "pn", "", "the ports no scan,as: -pn 445") flag.StringVar(&Info.Command, "c", "", "exec command (ssh)")