From 1152e76731de4a122c8aac6994d2e33d8eb26892 Mon Sep 17 00:00:00 2001 From: XinRoom <32238570+XinRoom@users.noreply.github.com> Date: Tue, 11 Oct 2022 15:46:01 +0800 Subject: [PATCH] add: Detect live C-class networks eg: -ip 192.168.0.0/16,172.16.0.0/12,10.0.0.0/8 -netLive --- README.md | 7 ++++--- cmd/go-portScan.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 358760c..81c264a 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ go build cmd/go-portScan.go `.\go-portScan.exe -ip 1.1.1.1/30 [-p str] [-Pn] [-sT] [-sV] [-rate num] [-rateP num] [-timeout num(ms)]` ``` - NAME: +NAME: PortScan - A new cli application USAGE: @@ -187,16 +187,17 @@ COMMANDS: GLOBAL OPTIONS: --ip value target ip, eg: "1.1.1.1/30,1.1.1.1-1.1.1.2,1.1.1.1-2" --iL value target ip file, eg: "ips.txt" - --port value, -p value eg: "top1000,5612,65120" (default: "top1000") + --port value, -p value eg: "top1000,5612,65120,-" (default: "top1000") --Pn no ping probe (default: false) --rateP value, --rp value concurrent num when ping probe each ip (default: 300) --sT TCP-mode(support IPv4 and IPv6) (default: false) --timeout value, --to value TCP-mode SYN-mode timeout. unit is ms. (default: 800) --sS Use SYN-mode(Only IPv4) (default: true) --dev value specified pcap dev name - --rate value, -r value number of packets sent per second. If set -1, TCP-mode is 1000, SYN-mode is 2000(SYN-mode is restricted by the network adapter, 2000=1M) (default: -1) + --rate value, -r value number of packets sent per second. If set -1, TCP-mode is 1000, SYN-mode is 2000(SYN-mode is restricted by the network adapter, 2000=1M) (default: -1) --devices, --ld list devices name (default: false) --sV port service identify (default: false) --httpx http server identify (default: false) + --netLive Detect live C-class networks, eg: -ip 192.168.0.0/16,172.16.0.0/12,10.0.0.0/8 (default: false) --help, -h show help (default: false) ``` \ No newline at end of file diff --git a/cmd/go-portScan.go b/cmd/go-portScan.go index 130806a..fb89d16 100644 --- a/cmd/go-portScan.go +++ b/cmd/go-portScan.go @@ -13,6 +13,7 @@ import ( "github.com/panjf2000/ants/v2" "github.com/urfave/cli/v2" "log" + "math/rand" "net" "os" "strings" @@ -33,6 +34,7 @@ var ( devices bool dev string httpx bool + netLive bool ) func parseFlag(c *cli.Context) { @@ -48,6 +50,7 @@ func parseFlag(c *cli.Context) { sV = c.Bool("sV") timeout = c.Int("timeout") httpx = c.Bool("httpx") + netLive = c.Bool("netLive") } func run(c *cli.Context) error { @@ -104,6 +107,43 @@ func run(c *cli.Context) error { } ipRangeGroup = append(ipRangeGroup, it) } + + // netLive + var wgIpsLive sync.WaitGroup + // Pool - ipsLive + poolIpsLive, _ := ants.NewPoolWithFunc(rateP, func(ip interface{}) { + _ip := ip.([]net.IP) + for _, ip2 := range _ip { + if host.IsLive(ip2.String()) { + fmt.Printf("[+] %s is live\n", ip2.String()) + break + } + } + wgIpsLive.Done() + }) + defer poolIpsLive.Release() + + if netLive { + // 按c段探测 + for _, ir := range ipRangeGroup { // ip group + for i := uint64(0); i < ir.TotalNum(); i = i + 256 { // ip index + ip := make(net.IP, len(ir.GetIpByIndex(0))) + copy(ip, ir.GetIpByIndex(i)) // Note: dup copy []byte when concurrent (GetIpByIndex not to do dup copy) + ipLastByte := []byte{1, 2, 254, 253, byte(100 + rand.Intn(20)), byte(200 + rand.Intn(20))} + ips2 := make([]net.IP, 6) + for j := 0; j < 6; j++ { + ips2[j] = make(net.IP, len(ip)) + ip[3] = ipLastByte[j] + copy(ips2[j], ip) + } + wgIpsLive.Add(1) + poolIpsLive.Invoke(ips2) + } + } + wgIpsLive.Wait() + return nil + } + // port parse ports, err := port.ShuffleParseAndMergeTopPorts(portStr) if err != nil { @@ -346,6 +386,11 @@ func main() { Usage: "http server identify", Value: false, }, + &cli.BoolFlag{ + Name: "netLive", + Usage: "Detect live C-class networks, eg: -ip 192.168.0.0/16,172.16.0.0/12,10.0.0.0/8", + Value: false, + }, }, }