gonmap

package module
v0.0.0-...-9ba53e9 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2018 License: BSD-3-Clause Imports: 5 Imported by: 0

README

gonmap - Wrapper around Nmap

Copyright (c) 2017, 2018, DCSO Deutsche Cyber-Sicherheitsorganisation GmbH

gonmap is a wrapper around the Nmap tool. It uses the XML output capability of Nmap to retrieve results and make them available in Go.

Implemented capabilities

  • Simple port scanner for 1 host using either TCP or UDP.

Dependencies

gonmap requires the Nmap tool to be available in the user's $PATH.

Quick start

import (
	"fmt"
	"os"

	"github.com/DCSO/gonmap"
)

func main() {
	scan, err := gonmap.NewPortScan("localhost", []string{"tcp"})
	if err != nil {
		fmt.Printf("nmap failed: %s", err)
		os.Exit(1)
	}
	scan.Run()

	f := "%5d/%s %-15s %s\n"
	ft := "%9s %-15s %s\n"
	for _, host := range scan.Result().Hosts {
		fmt.Printf("Nmap scan report for %s\n", host.Address.Address)
		fmt.Printf(ft, "PORT", "STATE", "SERVICE")
		for _, p := range host.Ports {
			fmt.Printf(f, p.Port, p.Protocol, p.Status.State, p.Service.Name)
		}
	}
}

Possible output of the above example:

Nmap scan report for 127.0.0.1
     PORT STATE           SERVICE
   22/tcp open            ssh
   80/tcp open            http
 3306/tcp open            mysql
 5000/tcp open            upnp
 5432/tcp open            postgresql

License

This project is licensed under a 3-clause BSD-like license.

Documentation

Overview

Package gonmap is a wrapper around the Nmap tool. It uses the XML output capability of Nmap to retrieve results and make them available in Go.

Package gonmap port scanning is implemented through the PortScan type.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Nmap

type Nmap interface {
	Result() NmapRun
	Run() error
	Target() string
}

type NmapRun

type NmapRun struct {
	XMLName xml.Name      `xml:"nmaprun"`
	Version string        `xml:"version"`
	Args    string        `xml:"args,attr"`
	Start   int64         `xml:"start,attr"`
	Hosts   []NmapRunHost `xml:"host"`
	Stats   NmapRunStats  `xml:"runstats"`
}

type NmapRunHost

type NmapRunHost struct {
	XMLName xml.Name           `xml:"host"`
	Ports   []NmapRunPort      `xml:"ports>port"`
	Address NmapRunHostAddress `xml:"address"`
	Status  NmapRunStatus      `xml:"status"`
}

type NmapRunHostAddress

type NmapRunHostAddress struct {
	XMLName xml.Name `xml:"address"`
	Address string   `xml:"addr,attr"`
	Type    string   `xml:"addrtype,attr"`
}

type NmapRunPort

type NmapRunPort struct {
	XMLName  xml.Name           `xml:"port"`
	Protocol string             `xml:"protocol,attr"`
	Port     int                `xml:"portid,attr"`
	Status   NmapRunState       `xml:"state"`
	Service  NmapRunPortService `xml:"service"`
}

type NmapRunPortService

type NmapRunPortService struct {
	XMLName xml.Name `xml:"service"`
	Name    string   `xml:"name,attr"`
	Method  string   `xml:"method,attr"`
	Conf    int      `xml:"conf,attr"`
}

type NmapRunState

type NmapRunState struct {
	XMLName   xml.Name `xml:"state"`
	State     string   `xml:"state,attr"`
	Reason    string   `xml:"reason,attr"`
	ReasonTTL int      `xml:"reason_ttl,attr"`
}

type NmapRunStats

type NmapRunStats struct {
	XMLName  xml.Name             `xml:"runstats"`
	Finished NmapRunStatsFinished `xml:"finished"`
}

type NmapRunStatsFinished

type NmapRunStatsFinished struct {
	XMLName xml.Name `xml:"finished"`
	Time    int64    `xml:"time,attr"`
	Elapsed float32  `xml:"elapsed,attr"`
}

type NmapRunStatus

type NmapRunStatus struct {
	XMLName   xml.Name `xml:"status"`
	State     string   `xml:"state,attr"`
	Reason    string   `xml:"reason,attr"`
	ReasonTTL int      `xml:"reason_ttl,attr"`
}

type PortScan

type PortScan struct {
	Nmap
	// contains filtered or unexported fields
}

PortScan holds information for running the port scan and provides functionality to run and get the result.

Example
package main

import (
	"fmt"
	"os"

	"github.com/DCSO/gonmap"
)

func main() {
	scan, err := gonmap.NewPortScan("localhost", []string{"tcp"})
	if err != nil {
		fmt.Printf("nmap failed: %s", err)
		os.Exit(1)
	}
	scan.Run()

	f := "%5d/%s %-15s %s\n"
	ft := "%9s %-15s %s\n"
	for _, host := range scan.Result().Hosts {
		fmt.Printf("Nmap scan report for %s\n", host.Address.Address)
		fmt.Printf(ft, "PORT", "STATE", "SERVICE")
		for _, p := range host.Ports {
			fmt.Printf(f, p.Port, p.Protocol, p.Status.State, p.Service.Name)
		}
	}
}
Output:

func NewPortScan

func NewPortScan(target string, protocols []string) (*PortScan, error)

NewPortScan creates a new PortScan using a target and protocols. A target can be either an IP address or a hostname. `protocols` should be a slice of strings containing 'tcp' or 'udp' or both.

func (*PortScan) Protocols

func (n *PortScan) Protocols() []string

Protocols returns a slice of strings containing the protocols used for performing the port scan.

func (*PortScan) Result

func (n *PortScan) Result() NmapRun

Result returns the results after running the port scan.

func (*PortScan) Run

func (n *PortScan) Run() error

Run executes the port scan. The result is stored and can be retrieved using the `Result()` function.

func (*PortScan) SetProtocols

func (n *PortScan) SetProtocols(protocols []string) error

SetProtocols sets the protocol or protocols for performing the port scan.

func (*PortScan) Target

func (n *PortScan) Target() string

Target returns the target.

Jump to

Keyboard shortcuts

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