added a readme and took the example from the go library

This commit is contained in:
Brendan McDevitt 2022-10-14 23:08:16 -05:00
parent ecfeda9ceb
commit 0c770a71fa
2 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,19 @@
# Nmap Go module:
### How to Build it:
```
# setup a go module pointing here:
go mod init git.mcdevitt.tech/bpmcdevitt/security_research/tools/nmap_scanning
# get the nmap library:
go get github.com/Ullaakut/nmap/v2
```
### Create and test a new scan example
```
# create a directory
mkdir basic_scan
cd basic_scan
# create a main.go file in that directory.
go build .
# run it
go run .
```

View file

@ -0,0 +1,47 @@
// taken from https://github.com/Ullaakut/nmap/blob/master/examples/basic_scan/main.go
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/Ullaakut/nmap/v2"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
// Equivalent to `/usr/local/bin/nmap -p 80,443,843 google.com facebook.com youtube.com`,
// with a 5 minute timeout.
scanner, err := nmap.NewScanner(
nmap.WithTargets("google.com", "facebook.com", "youtube.com"),
nmap.WithPorts("80,443,843"),
nmap.WithContext(ctx),
)
if err != nil {
log.Fatalf("unable to create nmap scanner: %v", err)
}
result, _, err := scanner.Run()
if err != nil {
log.Fatalf("unable to run nmap scan: %v", err)
}
// Use the results to print an example output
for _, host := range result.Hosts {
if len(host.Ports) == 0 || len(host.Addresses) == 0 {
continue
}
fmt.Printf("Host %q:\n", host.Addresses[0])
for _, port := range host.Ports {
fmt.Printf("\tPort %d/%s %s %s\n", port.ID, port.Protocol, port.State, port.Service.Name)
}
}
fmt.Printf("Nmap done: %d hosts up scanned in %.2f seconds\n", len(result.Hosts), result.Stats.Finished.Elapsed)
}