[渗透]Go语言实现masscan异步扫描

Github传送门

安装

go get github.com/zan8in/masscan

简单示例

package main

import (
	"fmt"
	"log"

	"github.com/zan8in/masscan"
)

// Example
func main() {
	scanner, err := masscan.NewScanner(
		masscan.SetParamTargets("146.56.202.100/24"),
		masscan.SetParamPorts("80"),
        masscan.EnableDebug(),
		masscan.SetParamWait(0),
		masscan.SetParamRate(10000),
	)
	if err != nil {
		log.Fatalf("unable to create masscan scanner: %v", err)
	}

	scanResult, _, err := scanner.Run()
	if err != nil {
		log.Fatalf("masscan encountered an error: %v", err)
	}

	if scanResult != nil {
		for i, v := range scanResult.Hosts {
			fmt.Printf("Host: %s Port: %v\n", v.IP, scanResult.Ports[i].Port)
		}
		fmt.Println("hosts len : ", len(scanResult.Hosts))
	}

}

输出结果

/usr/bin/masscan 146.56.202.100/24 -p 80 --wait=0 --rate=10000 -oJ -
Host: 146.56.202.15 Port: 80
Host: 146.56.202.251 Port: 80
Host: 146.56.202.112 Port: 80
...
...
Host: 146.56.202.17 Port: 80
Host: 146.56.202.209 Port: 80
Host: 146.56.202.190 Port: 80
Host: 146.56.202.222 Port: 80
Host: 146.56.202.207 Port: 80
hosts len :  37

异步扫描

package main

import (
	"fmt"
	"github.com/zan8in/masscan"
	"log"
)

func main() {
	var (
		scannerResult []masscan.ScannerResult
		errorBytes  []byte
	)

	scanner, err := masscan.NewScanner(
		masscan.SetParamTargets("146.56.202.100-146.56.202.200"),
		masscan.SetParamPorts("3306"),
		masscan.EnableDebug(),
		masscan.SetParamWait(0),
		masscan.SetParamRate(2000),
	)
	if err != nil {
		log.Fatalf("unable to create masscan scanner: %v", err)
	}

	if err := scanner.RunAsync(); err != nil {
		panic(err)
	}

	stdout := scanner.GetStdout()

	stderr := scanner.GetStderr()

	go func() {
		for stdout.Scan() {
			srs := masscan.ParseResult(stdout.Bytes())
			fmt.Println(srs.IP, srs.Port)
			scannerResult = append(scannerResult, srs)
		}
	}()

	go func() {
		for stderr.Scan() {
			fmt.Println("err: ", stderr.Text())
			errorBytes = append(errorBytes, stderr.Bytes()...)
		}
	}()

	if err := scanner.Wait(); err !=nil {
		panic(err)
	}

	fmt.Println("masscan result count : ", len(scannerResult))

}

输出结果

C:\masscan\masscan.exe 146.56.202.100-146.56.202.200 -p 3306 --wait=0 --rate=2000
err:  Starting masscan 1.3.2 (http://bit.ly/14GZzcT) at 2021-03-19 14:52:27 GMT
err:  Initiating SYN Stealth Scan
err:  Scanning 101 hosts [1 port/host]
146.56.202.115 3306
146.56.202.190 3306
146.56.202.188 3306
146.56.202.125 3306
146.56.202.185 3306
146.56.202.117 3306
146.56.202.112 3306
146.56.202.161 3306
146.56.202.165 3306
146.56.202.166 3306
                                                                             
masscan result count :  10

Process finished with exit code 0
上一篇:Linux 一条命令杀死占用端口的所有进程


下一篇:nacos 连不上数据库的一个坑