Skip to content

Commit

Permalink
closes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
leesoh committed May 9, 2022
1 parent ac34f05 commit a95f364
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 76 deletions.
81 changes: 7 additions & 74 deletions internal/scan/nmap.go → internal/scan/nmap/nmap.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package scan
package nmap

import (
"encoding/xml"
"net"
"strconv"

"github.com/leesoh/np/internal/result"
"fmt"
)

// Thanks to lair-framework/go-nmap for making this simpler
Expand Down Expand Up @@ -127,75 +124,11 @@ type Times struct {
To string `xml:"to,attr"`
}

func (s *Scan) IsNmap() bool {
if err := s.unmarshalNmap(); err != nil {
return false
}
// TODO: I don't think we need this
if s.Nmap.Scanner == "nmap" {
return true
}
return false
}

func (s *Scan) unmarshalNmap() error {
err := xml.Unmarshal(s.Bytes, &s.Nmap)
func Parse(scan []byte) (*NmapScan, error) {
s := &NmapScan{}
err := xml.Unmarshal(scan, s)
if err != nil {
s.Logger.Errorf("error unmarshaling Nmap: %v", err)
return err
}
return nil
}

func (s *Scan) ParseNmap() {
for _, hh := range s.Nmap.Hosts {
h := &result.Host{
Name: s.getNmapHostname(hh),
IP: s.getNmapIP(hh),
TCPPorts: s.getNmapPorts(hh, "tcp"),
UDPPorts: s.getNmapPorts(hh, "udp"),
}
s.Result.AddHost(h)
s.Logger.Debugf("added host: %v", h.IP)
}
}

func (s *Scan) getNmapHostname(h Host) string {
for _, hh := range h.Hostnames {
if hh.Type == "user" {
s.Logger.Debugf("found hostname: %v", hh.Name)
return hh.Name
}
}
return ""
}

func (s *Scan) getNmapIP(h Host) net.IP {
ip := net.ParseIP(h.Address.Addr)
if ip != nil {
s.Logger.Debugf("added IP: %v", ip)
return ip
}
return nil
}

func (s *Scan) getNmapPorts(h Host, protocol string) map[int]*result.Port {
ports := make(map[int]*result.Port)
for _, pp := range h.Ports {
if pp.State.State == "open" && pp.Protocol == protocol {
number, err := strconv.Atoi(pp.Portid)
if err != nil {
s.Logger.Errorf("error casting port: %v", pp.Portid)
}
port := &result.Port{
Name: pp.Service.Name,
Product: pp.Service.Product,
Version: pp.Service.Version,
ExtraInfo: pp.Service.Extrainfo,
}
ports[number] = port
s.Logger.Debugf("found port: %v/%v", number, protocol)
}
return nil, fmt.Errorf("error unmarshaling Nmap: %v", err)
}
return ports
return s, nil
}
2 changes: 1 addition & 1 deletion internal/scan/np.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *Scan) IsNP() bool {
func (s *Scan) ParseNP() {
var hosts []*result.Host
// We're importing an old np session so just unpack into results
err := json.Unmarshal(s.Bytes, hosts)
err := json.Unmarshal(s.Bytes, &hosts)
if err != nil {
s.Logger.Errorf("error unmarshaling np results: %v", err)
}
Expand Down
69 changes: 68 additions & 1 deletion internal/scan/scan.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package scan

import (
"net"
"strconv"

"github.com/Masterminds/log-go/impl/cli"
"github.com/leesoh/np/internal/result"
"github.com/leesoh/np/internal/scan/nmap"
)

type Scan struct {
Bytes []byte
Logger *cli.Logger
Nmap NmapScan
Result *result.Result
}

Expand All @@ -19,3 +22,67 @@ func New(b []byte, logger *cli.Logger, r *result.Result) *Scan {
Result: r,
}
}

func (s *Scan) IsNmap() bool {
if _, err := nmap.Parse(s.Bytes); err != nil {
return false
}
return true
}

func (s *Scan) ParseNmap() {
ns, err := nmap.Parse(s.Bytes)
if err != nil {
s.Logger.Errorf("error parsing Nmap scan: %v", err)
}
for _, hh := range ns.Hosts {
h := &result.Host{
Name: s.getNmapHostname(hh),
IP: s.getNmapIP(hh),
TCPPorts: s.getNmapPorts(hh, "tcp"),
UDPPorts: s.getNmapPorts(hh, "udp"),
}
s.Result.AddHost(h)
s.Logger.Debugf("added host: %v", h.IP)
}
}

func (s *Scan) getNmapHostname(h nmap.Host) string {
for _, hh := range h.Hostnames {
if hh.Type == "user" {
s.Logger.Debugf("found hostname: %v", hh.Name)
return hh.Name
}
}
return ""
}

func (s *Scan) getNmapIP(h nmap.Host) net.IP {
ip := net.ParseIP(h.Address.Addr)
if ip != nil {
s.Logger.Debugf("added IP: %v", ip)
return ip
}
return nil
}

func (s *Scan) getNmapPorts(h nmap.Host, protocol string) map[int]*result.Port {
ports := make(map[int]*result.Port)
for _, pp := range h.Ports {
if pp.State.State == "open" && pp.Protocol == protocol {
number, err := strconv.Atoi(pp.Portid)
if err != nil {
s.Logger.Errorf("error casting port: %v", pp.Portid)
}
port := &result.Port{
Name: pp.Service.Name,
Product: pp.Service.Product,
Version: pp.Service.Version,
ExtraInfo: pp.Service.Extrainfo,
}
ports[number] = port
s.Logger.Debugf("found port: %v/%v", number, protocol)
}
}
return ports
}

0 comments on commit a95f364

Please sign in to comment.