nmap

package module
v0.0.0-...-37a76f7 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2019 License: MIT Imports: 7 Imported by: 0

README

go-nmap

go-nmap is a golang library to run nmap scans, parse scan results.

Installation

go get github.com/RyunosukeA/go-nmap

to install the package

import "github.com/RyunosukeA/go-nmap"

Example

package main

import (
	"fmt"
	"strconv"

	"github.com/RyunosukeA/go-nmap"
)

func main() {

	n := nmap.New()

	// nmap可执行文件路径,默认不需要设置
	//m.SetSystemPath("/usr/local/nmap/bin/nmap")

	// 设置nmap扫描参数
	args := []string{"-sV", "-n", "-O", "-T4", "--open"}
	n.SetArgs(args...)

	// 设置扫描端口范围
	n.SetPorts("0-65535")

	// 设置扫描目标
	n.SetHosts("127.0.0.1")

	// 隔离扫描名单
	//n.SetExclude("127.0.0.1")

	// 开始扫描
	err := n.Run()
	if err != nil {
		fmt.Println("scanner failed:", err)
		return
	}

	// 解析扫描结果
	result, err := n.Parse()
	if err != nil {
		fmt.Println("Parse scanner result:", err)
		return
	}

	var (
		osName     string
		osAccuracy = 0
	)
	for _, host := range result.Hosts {
		if host.Status.State == "up" {

			for _, osMatch := range host.Os.OsMatches {
				tempOsAccuracy, _ := strconv.Atoi(osMatch.Accuracy)
				if tempOsAccuracy >= osAccuracy {
					osName = osMatch.Name
					osAccuracy = tempOsAccuracy
				}
			}

			ipAddr := host.Addresses[0].Addr
			for _, port := range host.Ports {
				portStr := strconv.Itoa(port.PortId)
				servicesStr := port.Service.Name
				fmt.Println(ipAddr, portStr, servicesStr, osName)
			}
		}
	}

}

Thanks

https://github.com/lair-framework/go-nmap/blob/master/nmap.go

Documentation

Overview

Package nmap parses Nmap XML data into a similary formed struct.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

type Address struct {
	Addr     string `xml:"addr,attr" json:"addr"`
	AddrType string `xml:"addrtype,attr" json:"addrtype"`
	Vendor   string `xml:"vendor,attr" json:"vendor"`
}

Address contains a IPv4 or IPv6 address for a Host.

type CPE

type CPE string

CPE (Common Platform Enumeration) is a standardized way to name software applications, operating systems, and hardware platforms.

type Debugging

type Debugging struct {
	Level int `xml:"level,attr" json:"level"`
}

Debugging contains the debugging level for the Nmap scan.

type Distance

type Distance struct {
	Value int `xml:"value,attr" json:"value"`
}

Distance is the amount of hops to a particular host.

type ExtraPorts

type ExtraPorts struct {
	State   string   `xml:"state,attr" json:"state"`
	Count   int      `xml:"count,attr" json:"count"`
	Reasons []Reason `xml:"extrareasons" json:"reasons"`
}

ExtraPorts contains the information about the closed|filtered ports.

type Finished

type Finished struct {
	Time     Timestamp `xml:"time,attr" json:"time"`
	TimeStr  string    `xml:"timestr,attr" json:"timestr"`
	Elapsed  float32   `xml:"elapsed,attr" json:"elapsed"`
	Summary  string    `xml:"summary,attr" json:"summary"`
	Exit     string    `xml:"exit,attr" json:"exit"`
	ErrorMsg string    `xml:"errormsg,attr" json:"errormsg"`
}

Finished contains detailed statistics regarding a finished Nmap scan.

type Hop

type Hop struct {
	TTL    float32 `xml:"ttl,attr" json:"ttl"`
	RTT    float32 `xml:"rtt,attr" json:"rtt"`
	IPAddr string  `xml:"ipaddr,attr" json:"ipaddr"`
	Host   string  `xml:"host,attr" json:"host"`
}

Hop is a ip hop to a Host.

type Host

type Host struct {
	StartTime     Timestamp     `xml:"starttime,attr" json:"starttime"`
	EndTime       Timestamp     `xml:"endtime,attr" json:"endtime"`
	Comment       string        `xml:"comment,attr" json:"comment"`
	Status        Status        `xml:"status" json:"status"`
	Addresses     []Address     `xml:"address" json:"addresses"`
	Hostnames     []Hostname    `xml:"hostnames>hostname" json:"hostnames"`
	Smurfs        []Smurf       `xml:"smurf" json:"smurfs"`
	Ports         []Port        `xml:"ports>port" json:"ports"`
	ExtraPorts    []ExtraPorts  `xml:"ports>extraports" json:"extraports"`
	Os            Os            `xml:"os" json:"os"`
	Distance      Distance      `xml:"distance" json:"distance"`
	Uptime        Uptime        `xml:"uptime" json:"uptime"`
	TcpSequence   TcpSequence   `xml:"tcpsequence" json:"tcpsequence"`
	IpIdSequence  IpIdSequence  `xml:"ipidsequence" json:"ipidsequence"`
	TcpTsSequence TcpTsSequence `xml:"tcptssequence" json:"tcptssequence"`
	HostScripts   []Script      `xml:"hostscript>script" json:"hostscripts"`
	Trace         Trace         `xml:"trace" json:"trace"`
	Times         Times         `xml:"times" json:"times"`
}

Host contains all information about a single host.

type HostStats

type HostStats struct {
	Up    int `xml:"up,attr" json:"up"`
	Down  int `xml:"down,attr" json:"down"`
	Total int `xml:"total,attr" json:"total"`
}

HostStats contains the amount of up and down hosts and the total count.

type Hostname

type Hostname struct {
	Name string `xml:"name,attr" json:"name"`
	Type string `xml:"type,attr" json:"type"`
}

Hostname is a single name for a Host.

type IpIdSequence

type IpIdSequence Sequence

type Nmap

type Nmap struct {
	SystemPath    string
	Args          []string
	Ports         string
	Hosts         string
	MaxRttTimeOut string
	HostTimeOut   string
	Exclude       string
	MaxRetries    string
	Result        []byte
}

func New

func New() *Nmap

func (*Nmap) Parse

func (n *Nmap) Parse() (*NmapRun, error)

Parse takes a byte array of nmap xml data and unmarshals it into an NmapRun struct. All elements are returned as strings, it is up to the caller to check and cast them to the proper type.

func (*Nmap) Run

func (n *Nmap) Run() error

func (*Nmap) SetArgs

func (n *Nmap) SetArgs(arg ...string)

func (*Nmap) SetExclude

func (n *Nmap) SetExclude(exclude string)

排除扫描IP/IP段

func (*Nmap) SetHostTimeOut

func (n *Nmap) SetHostTimeOut(hostTimeOut string)

func (*Nmap) SetHosts

func (n *Nmap) SetHosts(hosts string)

func (*Nmap) SetMaxRetries

func (n *Nmap) SetMaxRetries(maxRetries string)

func (*Nmap) SetMaxRttTimeOut

func (n *Nmap) SetMaxRttTimeOut(rttTimeOut string)

func (*Nmap) SetPorts

func (n *Nmap) SetPorts(ports string)

func (*Nmap) SetSystemPath

func (n *Nmap) SetSystemPath(systemPath string)

type NmapRun

type NmapRun struct {
	Scanner          string         `xml:"scanner,attr" json:"scanner"`
	Args             string         `xml:"args,attr" json:"args"`
	Start            Timestamp      `xml:"start,attr" json:"start"`
	StartStr         string         `xml:"startstr,attr" json:"startstr"`
	Version          string         `xml:"version,attr" json:"version"`
	ProfileName      string         `xml:"profile_name,attr" json:"profile_name"`
	XMLOutputVersion string         `xml:"xmloutputversion,attr" json:"xmloutputversion"`
	ScanInfo         ScanInfo       `xml:"scaninfo" json:"scaninfo"`
	Verbose          Verbose        `xml:"verbose" json:"verbose"`
	Debugging        Debugging      `xml:"debugging" json:"debugging"`
	TaskBegin        []Task         `xml:"taskbegin" json:"taskbegin"`
	TaskProgress     []TaskProgress `xml:"taskprogress" json:"taskprogress"`
	TaskEnd          []Task         `xml:"taskend" json:"taskend"`
	PreScripts       []Script       `xml:"prescript>script" json:"prescripts"`
	PostScripts      []Script       `xml:"postscript>script" json:"postscripts"`
	Hosts            []Host         `xml:"host" json:"hosts"`
	Targets          []Target       `xml:"target" json:"targets"`
	RunStats         RunStats       `xml:"runstats" json:"runstats"`
}

NmapRun is contains all the data for a single nmap scan.

type Os

type Os struct {
	PortsUsed      []PortUsed      `xml:"portused" json:"portsused"`
	OsMatches      []OsMatch       `xml:"osmatch" json:"osmatches"`
	OsFingerprints []OsFingerprint `xml:"osfingerprint" json:"osfingerprints"`
}

Os contains the fingerprinted operating system for a Host.

type OsClass

type OsClass struct {
	Vendor   string `xml:"vendor,attr" json:"vendor"`
	OsGen    string `xml:"osgen,attr"`
	Type     string `xml:"type,attr" json:"type"`
	Accuracy string `xml:"accurancy,attr" json:"accurancy"`
	OsFamily string `xml:"osfamily,attr" json:"osfamily"`
	CPEs     []CPE  `xml:"cpe" json:"cpes"`
}

OsClass contains vendor information for an Os.

type OsFingerprint

type OsFingerprint struct {
	Fingerprint string `xml:"fingerprint,attr" json:"fingerprint"`
}

OsFingerprint is the actual fingerprint string.

type OsMatch

type OsMatch struct {
	Name      string    `xml:"name,attr" json:"name"`
	Accuracy  string    `xml:"accuracy,attr" json:"accuracy"`
	Line      string    `xml:"line,attr" json:"line"`
	OsClasses []OsClass `xml:"osclass" json:"osclasses"`
}

OsMatch contains detailed information regarding a Os fingerprint.

type Owner

type Owner struct {
	Name string `xml:"name,attr" json:"name"`
}

Owner contains the name of Port.Owner.

type Port

type Port struct {
	Protocol string   `xml:"protocol,attr" json:"protocol"`
	PortId   int      `xml:"portid,attr" json:"id"`
	State    State    `xml:"state" json:"state"`
	Owner    Owner    `xml:"owner" json:"owner"`
	Service  Service  `xml:"service" json:"service"`
	Scripts  []Script `xml:"script" json:"scripts"`
}

Port contains all the information about a scanned port.

type PortUsed

type PortUsed struct {
	State  string `xml:"state,attr" json:"state"`
	Proto  string `xml:"proto,attr" json:"proto"`
	PortId int    `xml:"portid,attr" json:"portid"`
}

PortsUsed is the port used to fingerprint a Os.

type Reason

type Reason struct {
	Reason string `xml:"reason,attr" json:"reason"`
	Count  int    `xml:"count,attr" json:"count"`
}

type RunStats

type RunStats struct {
	Finished Finished  `xml:"finished" json:"finished"`
	Hosts    HostStats `xml:"hosts" json:"hosts"`
}

RunStats contains statistics for a finished Nmap scan.

type ScanInfo

type ScanInfo struct {
	Type        string `xml:"type,attr" json:"type"`
	Protocol    string `xml:"protocol,attr" json:"protocol"`
	NumServices int    `xml:"numservices,attr" json:"numservices"`
	Services    string `xml:"services,attr" json:"services"`
	ScanFlags   string `xml:"scanflags,attr" json:"scanflags"`
}

ScanInfo contains informational regarding how the scan was run.

type Script

type Script struct {
	Id     string  `xml:"id,attr" json:"id"`
	Output string  `xml:"output,attr" json:"output"`
	Tables []Table `xml:"table" json:"tables"`
}

Script contains information from Nmap Scripting Engine.

type Sequence

type Sequence struct {
	Class  string `xml:"class,attr" json:"class"`
	Values string `xml:"values,attr" json:"values"`
}

Sequence contains information regarding the detected X sequence.

type Service

type Service struct {
	Name       string `xml:"name,attr" json:"name"`
	Conf       int    `xml:"conf,attr" json:"conf"`
	Method     string `xml:"method,attr" json:"method"`
	Version    string `xml:"version,attr" json:"version"`
	Product    string `xml:"product,attr" json:"product"`
	ExtraInfo  string `xml:"extrainfo,attr" json:"extrainfo"`
	Tunnel     string `xml:"tunnel,attr" json:"tunnel"`
	Proto      string `xml:"proto,attr" json:"proto"`
	Rpcnum     string `xml:"rpcnum,attr" json:"rpcnum"`
	Lowver     string `xml:"lowver,attr" json:"lowver"`
	Highver    string `xml:"hiver,attr" json:"hiver"`
	Hostname   string `xml:"hostname,attr" json:"hostname"`
	OsType     string `xml:"ostype,attr" json:"ostype"`
	DeviceType string `xml:"devicetype,attr" json:"devicetype"`
	ServiceFp  string `xml:"servicefp,attr" json:"servicefp"`
	CPEs       []CPE  `xml:"cpe" json:"cpes"`
}

Service contains detailed information about a Port's service details.

type Smurf

type Smurf struct {
	Responses string `xml:"responses,attr" json:"responses"`
}

Smurf contains repsonses from a smurf attack. I think. Smurf attacks, really?

type State

type State struct {
	State     string  `xml:"state,attr" json:"state"`
	Reason    string  `xml:"reason,attr" json:"reason"`
	ReasonTTL float32 `xml:"reason_ttl,attr" json:"reason_ttl"`
	ReasonIP  string  `xml:"reason_ip,attr" json:"reason_ip"`
}

State contains information about a given ports status. State will be open, closed, etc.

type Status

type Status struct {
	State     string  `xml:"state,attr" json:"state"`
	Reason    string  `xml:"reason,attr" json:"reason"`
	ReasonTTL float32 `xml:"reason_ttl,attr" json:"reason_ttl"`
}

Status is the host's status. Up, down, etc.

type Table

type Table struct {
	Key      string   `xml:"key,attr" json:"key"`
	Elements []string `xml:"elem" json:"elements"`
}

Table contains the output of the script in a more parse-able form. ToDo: This should be a map[string][]string

type Target

type Target struct {
	Specification string `xml:"specification,attr" json:"specification"`
	Status        string `xml:"status,attr" json:"status"`
	Reason        string `xml:"reason,attr" json:"reason"`
}

Target is found in the Nmap xml spec. I have no idea what it actually is.

type Task

type Task struct {
	Task      string    `xml:"task,attr" json:"task"`
	Time      Timestamp `xml:"time,attr" json:"time"`
	ExtraInfo string    `xml:"extrainfo,attr" json:"extrainfo"`
}

Task contains information about started and stopped Nmap tasks.

type TaskProgress

type TaskProgress struct {
	Task      string    `xml:"task,attr" json:"task"`
	Time      Timestamp `xml:"time,attr" json:"time"`
	Percent   float32   `xml:"percent,attr" json:"percent"`
	Remaining int       `xml:"remaining,attr" json:"remaining"`
	Etc       Timestamp `xml:"etc,attr" json:"etc"`
}

TaskProgress contains information about the progression of a Task.

type TcpSequence

type TcpSequence struct {
	Index      int    `xml:"index,attr" json:"index"`
	Difficulty string `xml:"difficulty,attr" json:"difficulty"`
	Values     string `xml:"vaules,attr" json:"vaules"`
}

TcpSequence contains information regarding the detected tcp sequence.

type TcpTsSequence

type TcpTsSequence Sequence

type Times

type Times struct {
	SRTT string `xml:"srtt,attr" json:"srtt"`
	RTT  string `xml:"rttvar,attr" json:"rttv"`
	To   string `xml:"to,attr" json:"to"`
}

Times contains time statistics for an Nmap scan.

type Timestamp

type Timestamp time.Time

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

func (*Timestamp) MarshalXMLAttr

func (t *Timestamp) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

func (Timestamp) UnmarshalJSON

func (t Timestamp) UnmarshalJSON(b []byte) error

func (*Timestamp) UnmarshalXMLAttr

func (t *Timestamp) UnmarshalXMLAttr(attr xml.Attr) (err error)

type Trace

type Trace struct {
	Proto string `xml:"proto,attr" json:"proto"`
	Port  int    `xml:"port,attr" json:"port"`
	Hops  []Hop  `xml:"hop" json:"hops"`
}

Trace contains the hops to a Host.

type Uptime

type Uptime struct {
	Seconds  int    `xml:"seconds,attr" json:"seconds"`
	Lastboot string `xml:"lastboot,attr" json:"lastboot"`
}

Uptime is the amount of time the host has been up.

type Verbose

type Verbose struct {
	Level int `xml:"level,attr" json:"level"`
}

Verbose contains the verbosity level for the Nmap scan.

Jump to

Keyboard shortcuts

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