Git Product home page Git Product logo

Comments (6)

Ullaakut avatar Ullaakut commented on August 15, 2024 1

Hey @TheSecEng!

Sorry about the delay. This looks like a good implementation! Thank you so much!

It seems like a good first step to provide utility to people who might want to use it, but I'm curious how people will end up integrating it into their apps.

Either way, I'd love to see it in the form of a PR and to work on it together!

from nmap.

TerminalFi avatar TerminalFi commented on August 15, 2024 1

Hey @TheSecEng!

Sorry about the delay. This looks like a good implementation! Thank you so much!

It seems like a good first step to provide utility to people who might want to use it, but I'm curious how people will end up integrating it into their apps.

Either way, I'd love to see it in the form of a PR and to work on it together!

I've actually got a use case right now that I will be using this for once its finished. So there is definitely reasons to get this running.

from nmap.

TerminalFi avatar TerminalFi commented on August 15, 2024

Have you had time to look at this more ? If not, I’ll start looking into how to add this properly. It’s be nice to have.

from nmap.

Ullaakut avatar Ullaakut commented on August 15, 2024

Hey @TheSecEng I didn't have time as of now, I've been working on other projects, but if you're interested in picking this one up I'd love to give you any help you need :)

from nmap.

TerminalFi avatar TerminalFi commented on August 15, 2024

@Ullaakut you got it!

from nmap.

TerminalFi avatar TerminalFi commented on August 15, 2024

@Ullaakut Thoughts? As I wasn't fully understanding of channels. This would be a solution I propose.

However, if this doesn't meet the full need. I'll need to research further into channels and take time to really understand them.

I think this method avoids some of the pains of using channels, but potentially at the sacrifice of easily putting the results into a private var then exposing it.

But then again, it would require Run() to be modified as well. Those updates being minimal.

// Scanner represents an Nmap scanner.
type Scanner struct {
	Cmd *exec.Cmd

	args       []string
	binaryPath string
	ctx        context.Context

	portFilter func(Port) bool
	hostFilter func(Host) bool

	Stderr, Stdout bufio.Scanner
}
// RunAsync runs nmap asynchronously and returns error.
func (s *Scanner) RunAsync() error {
	// Enable XML output
	s.args = append(s.args, "-oX")

	// Get XML output in stdout instead of writing it in a file
	s.args = append(s.args, "-")
	s.Cmd = exec.Command(s.binaryPath, s.args...)

	stderr, err := s.Cmd.StderrPipe()
	if err != nil {
		return err
	}

	stdout, err := s.Cmd.StdoutPipe()
	if err != nil {
		return err
	}

	s.Stdout = *bufio.NewScanner(stdout)
	s.Stderr = *bufio.NewScanner(stderr)

	if err := s.Cmd.Start(); err != nil {
		return err
	}

	go func() {
		select {
		case <-s.ctx.Done():
			s.Cmd.Process.Kill()
		}
	}()

	return nil
}

Example

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"strings"

	"github.com/Ullaakut/nmap"
)

func main() {

	var resultBytes []byte
	var errorBytes []byte
	s, err := nmap.NewScanner(
		nmap.WithTargets("google.com", "facebook.com", "youtube.co1m"),
		nmap.WithPorts("80,443,843"),
	)

	if err != nil {
		log.Fatalf("unable to create nmap scanner: %v", err)
	}

	if err := s.RunAsync(); err != nil {
		panic(err)
	}

	go func() {
		for s.Stdout.Scan() {
			fmt.Println(s.Stdout.Text())
			resultBytes = append(resultBytes, s.Stdout.Bytes()...)
		}
	}()

	go func() {
		for s.Stderr.Scan() {
			errorBytes = append(errorBytes, s.Stderr.Bytes()...)
		}
	}()

	if err := s.Cmd.Wait(); err != nil {
		panic(err)
	}

	results, err := nmap.Parse(resultBytes)
	results.NmapErrors = strings.Split(string(errorBytes), "\n")
	if err != nil {
		panic(err)
	}

	temp, err := json.MarshalIndent(results, "", "  ")
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", temp)
}

from nmap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.