Files
s41r4j 0f03d49514
CI / contracts (push) Canceled after 0s
Website / deploy (push) Canceled after 0s
CI / test (macos-latest) (push) Canceled after 0s
CI / test (ubuntu-24.04) (push) Canceled after 0s
CI / test (ubuntu-24.04-arm) (push) Canceled after 0s
CI / test (windows-latest) (push) Canceled after 0s
CI / Compact real-tool fixtures (ubuntu-24.04) (push) Canceled after 0s
CI / Compact real-tool fixtures (ubuntu-24.04-arm) (push) Canceled after 0s
CI / Code analysis fixtures (ubuntu-24.04) (push) Canceled after 0s
CI / Code analysis fixtures (ubuntu-24.04-arm) (push) Canceled after 0s
CI / Web engine fixtures (ubuntu-24.04) (push) Canceled after 0s
CI / Web engine fixtures (ubuntu-24.04-arm) (push) Canceled after 0s
CI / Chromium operation fixtures (ubuntu-24.04) (push) Canceled after 0s
CI / Chromium operation fixtures (ubuntu-24.04-arm) (push) Canceled after 0s
CI / operation-readiness (push) Canceled after 0s
CI / Supply chain fixtures (ubuntu-24.04) (push) Canceled after 0s
CI / Supply chain fixtures (ubuntu-24.04-arm) (push) Canceled after 0s
Initial commit
2026-11-01 09:14:18 +05:30

84 lines
2.3 KiB
Go

package capability
import (
"encoding/xml"
"fmt"
"net"
"strings"
"github.com/s41r4j/cybot/pkg/schema"
)
// parseNmap matches the XML selected by nmapArgs (-oX -).
func parseNmap(data []byte, _ schema.RunRequest) (schema.Result, error) {
var report struct {
XMLName xml.Name `xml:"nmaprun"`
Hosts []struct {
Status struct {
State string `xml:"state,attr"`
} `xml:"status"`
Addresses []struct {
Addr string `xml:"addr,attr"`
Type string `xml:"addrtype,attr"`
} `xml:"address"`
Names []struct {
Name string `xml:"name,attr"`
} `xml:"hostnames>hostname"`
Ports []struct {
ID int `xml:"portid,attr"`
Protocol string `xml:"protocol,attr"`
State struct {
State string `xml:"state,attr"`
} `xml:"state"`
Service struct {
Name string `xml:"name,attr"`
Product string `xml:"product,attr"`
Version string `xml:"version,attr"`
} `xml:"service"`
} `xml:"ports>port"`
} `xml:"host"`
Finished struct {
Exit string `xml:"exit,attr"`
Error string `xml:"errormsg,attr"`
} `xml:"runstats>finished"`
}
r := emptyResult()
if err := xml.Unmarshal(data, &report); err != nil {
return r, fmt.Errorf("nmap XML: %w", err)
}
if report.Finished.Exit == "error" {
return r, fmt.Errorf("nmap failed: %s", report.Finished.Error)
}
for _, host := range report.Hosts {
if host.Status.State == "down" {
continue
}
address := ""
for _, a := range host.Addresses {
if (a.Type != "ipv4" && a.Type != "ipv6") || net.ParseIP(a.Addr) == nil {
continue
}
if address == "" {
address = a.Addr
}
r.Assets = append(r.Assets, schema.Asset{Value: a.Addr, Type: a.Type, Source: "nmap", Confidence: .9})
}
for _, n := range host.Names {
if validateHostname(n.Name) == nil {
r.Assets = append(r.Assets, schema.Asset{Value: n.Name, Type: "hostname", Source: "nmap", Confidence: .85})
}
}
for _, p := range host.Ports {
if p.State.State != "open" || address == "" {
continue
}
if p.ID < 1 || p.ID > 65535 {
return r, fmt.Errorf("nmap returned invalid port %d", p.ID)
}
r.Services = append(r.Services, schema.Service{Host: address, Port: p.ID, Protocol: p.Protocol, Technology: strings.TrimSpace(p.Service.Name + " " + p.Service.Product + " " + p.Service.Version), Confidence: .85})
}
}
deduplicateResult(&r)
return r, nil
}