Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nmap error handling #21

Merged
merged 15 commits into from Aug 26, 2019
Merged

Nmap error handling #21

merged 15 commits into from Aug 26, 2019

Conversation

TerminalFi
Copy link
Collaborator

@TerminalFi TerminalFi commented Aug 26, 2019

Added a NmapErrors Struct for holding errors encountered during the Nmap Scan. Since these errors are specific to nmap, we shouldn't cancel an entire multi target scan just because one host is unresolvable or causing issues.

Additionally, added a utils file that holds a RemoveDuplicates to remove Duplicate nmap errors. Nmap can under certain circumstances return duplicate errors.

This could potentially be further improved to remove duplicates of specific errors.

Example Usage:

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/theseceng/nmap"
)

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("facebook.com", "Bad.Domain.Com.Dsads"),
		nmap.WithPorts("80"),
		nmap.WithContext(ctx),
	)
	if err != nil {
		log.Fatalf("unable to create nmap scanner: %v", err)
	}

	result, err := scanner.Run()
	if err != nil {
		fmt.Println("Errors encountered")
	}

	for _, e := range result.NmapErrors {
		fmt.Printf("[ ERROR ] - %v\n", e.Error)
	}

	// 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("[ SUCCESS ] - Host %q:\n", host.Addresses[0])

		for _, port := range host.Ports {
			fmt.Printf("\t\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)
}

utils.go Outdated Show resolved Hide resolved
Copy link
Owner

@Ullaakut Ullaakut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @TheSecEng!

Congratulations on your PR, I'm very excited about your contribution :) I have a few suggestions, mostly related to style, but that should be addressed before the PR can be merged 👍

Thanks a lot and let me know if you have any questions!

nmap.go Outdated Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
xml.go Outdated Show resolved Hide resolved
xml.go Outdated Show resolved Hide resolved
xml.go Outdated Show resolved Hide resolved
@Ullaakut Ullaakut added the enhancement New feature or request label Aug 26, 2019
TerminalFi and others added 4 commits August 26, 2019 13:17
Removing Plurals as suggest @Ullaakut

Co-Authored-By: Brendan Le Glaunec <brendan.le-glaunec@epitech.eu>
Removing Plurals as suggest @Ullaakut

Co-Authored-By: Brendan Le Glaunec <brendan.le-glaunec@epitech.eu>
Removing Plurals as suggest @Ullaakut

Co-Authored-By: Brendan Le Glaunec <brendan.le-glaunec@epitech.eu>
Updated from bool to struct{} as suggested.

Co-Authored-By: Brendan Le Glaunec <brendan.le-glaunec@epitech.eu>
xml.go Outdated Show resolved Hide resolved
xml.go Outdated Show resolved Hide resolved
nmap.go Outdated Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
TerminalFi and others added 5 commits August 26, 2019 13:19
@TerminalFi
Copy link
Collaborator Author

Hi @TheSecEng!

Congratulations on your PR, I'm very excited about your contribution :) I have a few suggestions, mostly related to style, but that should be addressed before the PR can be merged 👍

Thanks a lot and let me know if you have any questions!

All changes have been made...This is my first PR on a project ! What a learning experience.

@Ullaakut
Copy link
Owner

Hey @TheSecEng !

Well you did a great job for a first PR! 👏 I'll just checkout the branch and test it out on my machine for a few minutes, and then it will be merged :) Thanks again for your contribution friend!

@Ullaakut
Copy link
Owner

Ah, the unit tests need an update it seems!

# github.com/Ullaakut/nmap [github.com/Ullaakut/nmap.test]
./xml_test.go:1069:24: not enough arguments in call to Parse
	have ([]byte)
	want ([]byte, []NmapError)

If you are not familiar with Go unit tests, let me know and I'll handle it :)

@TerminalFi
Copy link
Collaborator Author

Ah, the unit tests need an update it seems!

# github.com/Ullaakut/nmap [github.com/Ullaakut/nmap.test]
./xml_test.go:1069:24: not enough arguments in call to Parse
	have ([]byte)
	want ([]byte, []NmapError)

If you are not familiar with Go unit tests, let me know and I'll handle it :)

I am unfamiliar with those. I'd love to see what you have to change to make this work. As a learning experience.

@Ullaakut
Copy link
Owner

Ullaakut commented Aug 26, 2019

Okay so after looking into it, I think it's better for Parse not to take an extra nmapErrors arguments, since it isn't really used for parsing at all, it's simply put into the Run structure, so instead let's delegate this responsibility to the Scanner.Run method by simply setting result.NmapErrors = nmapErrors after the parsing :) This means we don't even need to update the tests 🎉

If we kept it, the way the tests would have been changed in this case would have simply been to pass a nil value for the nmapResults, since they are not used. If there was some logic to test with the nmapResults however, we'd have needed a brand new test to ensure that it behaves as expected :)

EDIT: I'll push a commit that makes those changes in a few minutes and we'll merge the PR :)

@TerminalFi
Copy link
Collaborator Author

e it isn't really used for parsing

Ahh you know, originally when I had this written out I had additional logic in the Parse() function. Since I removed that, you are right, its not needed.

Commited.

nmap.go Outdated Show resolved Hide resolved
@Ullaakut Ullaakut merged commit fb38fc9 into Ullaakut:master Aug 26, 2019
@TerminalFi TerminalFi deleted the nmap-error-handling branch September 6, 2019 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants