Git Product home page Git Product logo

masscan's Introduction

masscan

Just I love it Masscan is a golang library to run masscan scans, parse scan results.

What is masscan

Masscan is an Internet-scale port scanner. It can scan the entire Internet in under 5 minutes, transmitting 10 million packets per second, from a single machine.

Its usage (parameters, output) is similar to nmap, the most famous port scanner. When in doubt, try one of those features -- features that support widespread scanning of many machines are supported, while in-depth scanning of single machines aren't.

Internally, it uses asynchronous transmission, similar to port scanners like scanrand, unicornscan, and ZMap. It's more flexible, allowing arbitrary port and address ranges.

Installation

go get github.com/zan8in/masscan

to install the package

import "github.com/zan8in/masscan"

Dependencies

Table of content

  • masscan run timeout setting
  • Get process ID (PID)
  • Select network interface automatically

TODO

  • Support more parameters

Simple example

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))
	}

}

The program above outputs:

/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

Async scan example

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/zan8in/masscan"
)

func main() {
	context, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
	defer cancel()

	var (
		scannerResult []masscan.ScannerResult
		errorBytes    []byte
	)

	scanner, err := masscan.NewScanner(
		masscan.SetParamTargets("60.10.116.10"),
		masscan.SetParamPorts("80"),
		masscan.EnableDebug(),
		masscan.SetParamWait(0),
		masscan.SetParamRate(50),
		masscan.WithContext(context),
	)

	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), " PID : ", scanner.GetPid())

}

The program above outputs:

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

The development soul comes from

Ullaakut

Special thanks

李雪松 XueSong Lee

masscan's People

Contributors

gamingdiamond982 avatar loockeeer avatar vert3xo avatar zan8in avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

masscan's Issues

[ Suggestion ] Map multiple targets to multiple ports separated by commas

masscan.SetParamTargets("82.208.17.0/26, 89.203.193.100/26, 89.187.158.0/24"),
masscan.SetParamPorts("27000-27999, 20000-60000, 30000-60000"),

Basically add functionality where you have multiple targets and ports separated by commas and third target corresponds to third port etc.

So you can have multiple target and each target can have it's own assined port range

Not getting any output?

Running the example provided in the README, modified to target my local network returns no results. If I run the command manually I get JSON output, but the library gives no output.

I also tried looking at the stdout it received, and it appeared to be empty.

unable to create masscan scanner: masscan binary was not found

我在goland里面运行,提示如下:

unable to create masscan scanner: masscan binary was not found

但在命令行模式下可以正常使用。(这里将masscan添加到了环境变量path里)

C:\Users>masscan 127.0.0.1/24 -p 80 --wait=0 --rate=10000 -oJ -

Starting masscan 1.0.3 (http://bit.ly/14GZzcT) at 2021-12-16 12:27:17 GMT
 -- forced options: -sS -Pn -n --randomize-hosts -v --send-eth
Initiating SYN Stealth Scan
Scanning 256 hosts [1 port/host]
{   "ip": "127.0.0.1",   "timestamp": "1639657638", "ports": [ {"port": 80, "proto": "tcp", "status": "open", "reason": "syn-ack", "ttl": 51} ] },
{   "ip": "127.0.0.1",   "timestamp": "1639657638", "ports": [ {"port": 80, "proto": "tcp", "status": "open", "reason": "syn-ack", "ttl": 52} ] },
{   "ip": "127.0.0.1",   "timestamp": "1639657638", "ports": [ {"port": 80, "proto": "tcp", "status": "open", "reason": "syn-ack", "ttl": 52} ] },

unable to parse masscan output

I use your demo, wen I run it, it's raise "unable to parse masscan output"
what should I do ?

scanResult:  <nil>
2021/03/14 09:29:58  masscan encountered an error: unable to parse masscan output, see warning for details
exit status 1

Why keep structs weirdly?

Why do you have ports listed in the masscanresult when you already have it in hosts? :/

	MasscanResult struct {
		Hosts []Hosts `json:"hosts"`
		Ports []Ports `json:"ports"`
	}

	// Hosts  masscan hosts output struct
	Hosts struct {
		IP        string  `json:"ip"`
		Ports     []Ports `json:"ports"`
		Timestamp string  `json:"timestamp"`
	}

	// Ports  masscan ports output struct
	Ports struct {
		Port   int    `json:"port"`
		Proto  string `json:"proto"`
		Status string `json:"status"`
		Reason string `json:"reason"`
		TTL    int    `json:"ttl"`
	}

)

why not just do this?

	MasscanResult struct {
		Hosts []Hosts `json:"hosts"`
	}

	// Hosts  masscan hosts output struct
	Hosts struct {
		IP        string  `json:"ip"`
		Ports     []Ports `json:"ports"`
		Timestamp string  `json:"timestamp"`
	}

	// Ports  masscan ports output struct
	Ports struct {
		Port   int    `json:"port"`
		Proto  string `json:"proto"`
		Status string `json:"status"`
		Reason string `json:"reason"`
		TTL    int    `json:"ttl"`
	}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.