nmap

package module
v0.0.0-...-7081d96 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2021 License: GPL-3.0 Imports: 8 Imported by: 1

README

NMap library for Go

GoDoc

Package nmap is a wrapper library for nmap.

Why

NMap is a great tool, but it's very hard to script when trying to use Go. This project is meant to make life easier when trying to do network scanning in Go. The package is meant to be very easy to use, and extremely intuitive to learn.

Install nmap

You will need to have the binary nmap installed on your computer, so visit their website http://nmap.org and install nmap.

  • For macos, you can install nmap by using Homebrew
  • For Linux systems, use your package manager to install nmap.
  • For Windows, visit http://nmap.org

Install library

go get github.com/t94j0/nmap

Examples

See GoDoc

Documentation

Overview

Package nmap is a wrapper library for nmap.

Why

NMap is a great tool, but it's very hard to script when using Go. This project is meant to make life easier when doing network scanning in Go. The package is meant to be very easy to use, and extremely intuitive to learn.

Install

You will need to have the binary `nmap` installed on your computer, so visit their website http://nmap.org and install nmap.

For macos, you can install nmap by using Homebrew

for Linux systems, use your package manager to install nmap.

For Windows, visit http://nmap.org

Index

Examples

Constants

This section is empty.

Variables

View Source
var DisallowedFlags = []string{"-oN", "-oX", "-oG", "-oA"}

DisallowedFlags is a list of flags that will break the nmap library's ability to parse the output

Functions

This section is empty.

Types

type DisallowedFlagError

type DisallowedFlagError struct {
	Flag string
}

DisallowedFlagError is thrown when a disallowed flag is used. A list of disallowed flags can be seen in the `DisallowedFlags` variable

func (*DisallowedFlagError) Error

func (f *DisallowedFlagError) Error() string

Error returns the flag string

type Element

type Element struct {
	Key   string
	Value string
}

Element are returned from NSE scripts

type Host

type Host struct {
	State       string
	Address     string
	AddressType string
	Hostnames   []Hostname
	Ports       []Port
	// contains filtered or unexported fields
}

Host declares host information

func (Host) Diff

func (h Host) Diff(altHost Host) (added []Port, removed []Port)

Diff gets the difference between the the target host and the argument host. The first returned value is the added ports and the second returned value is the removed ports.

Example
// Scan maxh.io one
scan, _ := Init().AddHosts("maxh.io").Run()
firstHost, _ := scan.GetHost("maxh.io")

// Wait 100 seconds
time.Sleep(100 * time.Second)

// Rescan maxh.io
scan, _ = scan.Run()
secondHost, _ := scan.GetHost("maxh.io")

// Get list of added and removed ports
added, removed := firstHost.Diff(secondHost)
fmt.Println(added, removed)
Output:

func (Host) Rescan

func (h Host) Rescan() (scan Scan)

Rescan the target. Normally used for finding differences between scans at two points in time.

func (Host) ToString

func (h Host) ToString() (out string)

ToString converts the host into a nicely formatted string

type Hostname

type Hostname struct {
	Name string
	Type string
}

Hostname declares the hostname and type

type Port

type Port struct {
	Protocol string
	// ID is the port number
	ID      uint32
	State   string
	Service string
	Method  string
	Scripts []Script
}

Port represents nmap port information

func (Port) ToString

func (p Port) ToString() (out string)

ToString returns port information in a pretty-printed format

type Scan

type Scan struct {
	DisplayArgs string
	Hosts       map[string]Host
	// contains filtered or unexported fields
}

Scan holds one nmap scan. It can be rescanned, diff'ed, and parsed for hosts

func Init

func Init() Scan

Init initializes a scan object. This is the easiest way to create a Scan object. If you are trying to create a Scan object by hand, make sure to instantiate the Hosts map

func (Scan) AddFlags

func (s Scan) AddFlags(flags ...string) Scan

AddFlags adds a list of flags to be used by nmap. Seperate flags by new arguments. The order of the flag is kept, so when using flags that require file names, seperate it by using multiple arguments.

Use the DisallowedFlags variable to guide you on which flags are not allowed to be used.

func (Scan) AddHosts

func (s Scan) AddHosts(hosts ...string) Scan

AddHosts adds a list of hosts to the list of hosts to be scanned

func (Scan) AddPortRange

func (s Scan) AddPortRange(lPort, hPort uint16) Scan

AddPortRange adds a list of ports where the first argument is the low bound (inclusive) on the range and the second argument is the upper bound (exclusive)

E.x. AddPortRange(0, 1025) adds ports 1-1024 to the list TODO(t94j0): Make into actual nmap ranges. (-p1-1024)

func (Scan) AddPorts

func (s Scan) AddPorts(ports ...uint16) Scan

AddPorts appends a list of ports to the list of ports to be scanned

func (Scan) AddTCPPorts

func (s Scan) AddTCPPorts(ports ...uint16) Scan

AddTCPPorts adds TCP-only ports. Similar to using `-pT:<port1>,<port2>...`

func (Scan) AddUDPPorts

func (s Scan) AddUDPPorts(ports ...uint16) Scan

AddUDPPorts adds UDP-only ports. Similar to using `-pU:<port1>,<port2>...`

func (Scan) CreateNmapArgs

func (s Scan) CreateNmapArgs() ([]string, error)

CreateNmapArgs takes a Scan object and returns a list of strings that map to arguments for an nmap scan.

func (Scan) GetHost

func (s Scan) GetHost(hostTarget string) (target Host, exists bool)

GetHost will get a specified host by either hostname or ip. The first return value is the host, if it was found. The second return value is the wether the host was found or not

Example
// All online hosts are added to the `scan` object
scan, _ := Init().AddPorts(80).AddHosts("example.com").Run()
// GetHost allows you to select one host from the list
targetHost, _ := scan.GetHost("93.184.216.34")
fmt.Println(targetHost.ToString())
// It also searches hostnames
targetHost, _ = scan.GetHost("maxh.io")
fmt.Println(targetHost.ToString())
Output:

func (Scan) Intense

func (s Scan) Intense() Scan

Intense sets the options to use an "intense" scan. These are the same options as used in Zenmap's intense scan.

Example
scan, _ := Init().AddHosts("localhost").Intense().Run()
fmt.Println(scan.ToString())
Output:

func (Scan) IntenseAllTCPPorts

func (s Scan) IntenseAllTCPPorts() Scan

IntenseAllTCPPorts does an intense scan, but adds all TCP ports

func (Scan) Ping

func (s Scan) Ping() Scan

Ping sets the `-sn` flag to only do a ping scan

func (Scan) Quick

func (s Scan) Quick() Scan

Quick does a scan with timing at max and with the `-F` option

func (Scan) Run

func (s Scan) Run() (output Scan, err error)

Run is used to scan hosts. The Scan object should be configured using specified Add* Set* functions.

BUG(t94j0): The scan will sometimes segfault and theres no reason why

Example
scan, _ := Init().
	AddHosts("maxh.io", "192.168.0.1").
	AddPorts(80, 445).
	AddFlags("-A").
	Run()
fmt.Println(scan.ToString())
Output:

func (Scan) SetFlags

func (s Scan) SetFlags(flags ...string) Scan

func (Scan) SetHosts

func (s Scan) SetHosts(hosts ...string) Scan

SetHosts sets the hosts that will be scanned

func (Scan) SetPorts

func (s Scan) SetPorts(ports ...uint16) Scan

SetPorts sets the ports that wil be used

func (Scan) SetTCPPorts

func (s Scan) SetTCPPorts(ports ...uint16) Scan

SetTCPPorts sets which TCP-only ports are used to scan

func (Scan) SetUDPPorts

func (s Scan) SetUDPPorts(ports ...uint16) Scan

SetUDPPort sets which TCP-only ports are used to scan

func (Scan) ToString

func (s Scan) ToString() (out string)

ToString returns the list of hosts into a pretty-printed format

type Script

type Script struct {
	Name     string
	Output   string
	Elements []Element
}

Script are used for gathering nmap NSE script information

Notes

Bugs

  • The scan will sometimes segfault and theres no reason why

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL