forked from michenriksen/aquatone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmap.go
83 lines (70 loc) · 1.67 KB
/
nmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package parsers
import (
"io"
"io/ioutil"
"github.com/michenriksen/aquatone/core"
"github.com/lair-framework/go-nmap"
)
type NmapParser struct{}
func NewNmapParser() *NmapParser {
return &NmapParser{}
}
func (p *NmapParser) Parse(r io.Reader) ([]string, error) {
var targets []string
bytes, err := ioutil.ReadAll(r)
if err != nil {
return targets, err
}
scan, err := nmap.Parse(bytes)
if err != nil {
return targets, err
}
for _, host := range scan.Hosts {
urls := p.hostToURLs(host)
for _, url := range urls {
targets = append(targets, url)
}
}
return targets, nil
}
func (p *NmapParser) isHTTPPort(port int) bool {
for _, p := range core.XLargePortList {
if p == port {
return true
}
}
return false
}
func (p *NmapParser) hostToURLs(host nmap.Host) []string {
var urls []string
for _, port := range host.Ports {
if port.State.State != "open" {
continue
}
var protocol string
if port.Service.Name == "ssl" {
protocol = "https"
} else if port.Service.Tunnel == "ssl" && (port.Service.Name != "smtp" && port.Service.Name != "imap" && port.Service.Name != "pop3") {
protocol = "https"
} else if port.Service.Name == "http" || port.Service.Name == "http-alt" {
protocol = "http"
} else {
if !p.isHTTPPort(port.PortId) {
continue
}
}
if len(host.Hostnames) > 0 {
for _, hostname := range host.Hostnames {
urls = append(urls, core.HostAndPortToURL(hostname.Name, port.PortId, protocol))
}
} else {
for _, address := range host.Addresses {
if address.AddrType == "mac" {
continue
}
urls = append(urls, core.HostAndPortToURL(address.Addr, port.PortId, protocol))
}
}
}
return urls
}