Skip to content

Commit

Permalink
add: 主机探测加入tcp-ping支持
Browse files Browse the repository at this point in the history
  • Loading branch information
XinRoom committed Mar 5, 2023
1 parent 67df464 commit 3cca40e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
11 changes: 9 additions & 2 deletions cmd/go-portScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
ipStr string
portStr string
pn bool
pt bool
sT bool
rate int
sV bool
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)",
Expand Down
36 changes: 32 additions & 4 deletions core/host/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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命令模式
Expand Down Expand Up @@ -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
}

0 comments on commit 3cca40e

Please sign in to comment.