diff --git a/tools/nmap_scanning/README.md b/tools/nmap_scanning/README.md new file mode 100644 index 0000000..b6470e6 --- /dev/null +++ b/tools/nmap_scanning/README.md @@ -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 . +``` diff --git a/tools/nmap_scanning/basic_scan/main.go b/tools/nmap_scanning/basic_scan/main.go new file mode 100644 index 0000000..c13ef68 --- /dev/null +++ b/tools/nmap_scanning/basic_scan/main.go @@ -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) +}