diff --git a/cmd/go-portScan.go b/cmd/go-portScan.go index 95c988e..73625f3 100644 --- a/cmd/go-portScan.go +++ b/cmd/go-portScan.go @@ -24,6 +24,7 @@ var ( ipStr string portStr string pn bool + pt bool sT bool rate int sV bool @@ -46,6 +47,7 @@ func parseFlag(c *cli.Context) { devices = c.Bool("devices") pn = c.Bool("Pn") rateP = c.Int("rateP") + pt = c.Bool("PT") rate = c.Int("rate") sT = c.Bool("sT") sV = c.Bool("sV") @@ -117,7 +119,7 @@ func run(c *cli.Context) error { poolIpsLive, _ := ants.NewPoolWithFunc(rateP, func(ip interface{}) { _ip := ip.([]net.IP) for _, ip2 := range _ip { - if host.IsLive(ip2.String()) { + if host.IsLive(ip2.String(), pt, time.Duration(tcp.DefaultTcpOption.Timeout)*time.Millisecond) { myLog.Printf("[+] %s is live\n", ip2.String()) break } @@ -297,7 +299,7 @@ func run(c *cli.Context) error { // Pool - ping and port scan poolPing, _ := ants.NewPoolWithFunc(rateP, func(ip interface{}) { _ip := ip.(net.IP) - if host.IsLive(_ip.String()) { + if host.IsLive(_ip.String(), pt, time.Duration(option.Timeout)*time.Millisecond) { portScan(_ip) } wgPing.Done() @@ -362,6 +364,11 @@ func main() { Usage: "concurrent num when ping probe each ip", Value: 300, }, + &cli.BoolFlag{ + Name: "PT", + Usage: "use TCP-PING mode", + Value: false, + }, &cli.BoolFlag{ Name: "sT", Usage: "TCP-mode(support IPv4 and IPv6)", diff --git a/core/host/ping.go b/core/host/ping.go index ff1293d..ee5ed15 100644 --- a/core/host/ping.go +++ b/core/host/ping.go @@ -2,29 +2,38 @@ package host import ( "bytes" + "fmt" "github.com/go-ping/ping" + "net" "os/exec" "runtime" "strings" + "sync" "time" ) var CanIcmp bool +var TcpPingPorts = []uint16{80, 22, 445, 23, 443, 81, 111, 3389, 8080, 8081} + // 判断是否支持发送icmp包 func init() { - if IcmpOK("localhost") { + if IcmpOK("127.0.0.1") { CanIcmp = true } } // IsLive 判断ip是否存活 -func IsLive(ip string) bool { +func IsLive(ip string, tcpPing bool, tcpTimeout time.Duration) (ok bool) { if CanIcmp { - return IcmpOK(ip) + ok = IcmpOK(ip) } else { - return PingOk(ip) + ok = PingOk(ip) + } + if !ok && tcpPing { + ok = TcpPing(ip, TcpPingPorts, tcpTimeout) } + return } // PingOk Ping命令模式 @@ -75,3 +84,22 @@ func IcmpOK(host string) bool { } return false } + +// TcpPing 指定默认常见端口进行存活探测 +func TcpPing(host string, ports []uint16, timeout time.Duration) (ok bool) { + var wg sync.WaitGroup + for _, port := range ports { + time.Sleep(10 * time.Millisecond) + wg.Add(1) + go func(_port uint16) { + conn, _ := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, _port), timeout) + if conn != nil { + conn.Close() + ok = true + } + wg.Done() + }(port) + } + wg.Wait() + return +}