diff --git a/README.md b/README.md index b5e09a8..23af77d 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ cat nmap.xml | nmap-formatter json | jq List all the found ports and count them: ```bash -nmap-formatter json [nmap.xml] | jq -r '.Host[]?.Ports?.Port[]?.PortID' | sort | uniq -c +nmap-formatter json [nmap.xml] | jq -r '.Host[]?.Port[]?.PortID' | sort | uniq -c ``` ``` @@ -76,7 +76,7 @@ nmap-formatter json [nmap.xml] | jq -r '.Host[]?.Ports?.Port[]?.PortID' | sort | another example where only those hosts are selected, which have port where some http service is running: ```bash -nmap-formatter json [nmap.xml] | jq '.Host[]? | . as $host | .Ports?.Port[]? | select(.Service.Name== "http") | $host.HostAddress.Address' | uniq -c +nmap-formatter json [nmap.xml] | jq '.Host[]? | . as $host | .Port[]? | select(.Service.Name== "http") | $host.HostAddress.Address' | uniq -c ``` ``` @@ -89,14 +89,14 @@ In this case `192.168.1.3` has 2 http services running (for example on ports 80 Another example where it is needed to display only filtered ports: -``` -nmap-formatter json [nmap.xml] | jq '.Host[]?.Ports?.Port[]? | select(.State.State == "filtered") | .PortID' +```bash +nmap-formatter json [nmap.xml] | jq '.Host[]?.Port[]? | select(.State.State == "filtered") | .PortID' ``` Display host IP addresses that have filtered ports: ```bash -nmap-formatter json [nmap.xml] | jq '.Host[]? | . as $host | .Ports?.Port[]? | select(.State.State == "filtered") | .PortID | $host.HostAddress.Address' +nmap-formatter json [nmap.xml] | jq '.Host[]? | . as $host | .Port[]? | select(.State.State == "filtered") | .PortID | $host.HostAddress.Address' ``` ### Flags diff --git a/formatter/formatter_csv.go b/formatter/formatter_csv.go index a4fe9f8..35e279a 100644 --- a/formatter/formatter_csv.go +++ b/formatter/formatter_csv.go @@ -26,8 +26,8 @@ func (f *CSVFormatter) convert(td *TemplateData) (data [][]string) { } address := fmt.Sprintf("%s (%s)", host.HostAddress.Address, host.Status.State) data = append(data, []string{address, "", "", "", "", "", "", "", ""}) - for j := range host.Ports.Port { - var port *Port = &host.Ports.Port[j] + for j := range host.Port { + var port *Port = &host.Port[j] data = append( data, []string{ diff --git a/formatter/formatter_csv_test.go b/formatter/formatter_csv_test.go index 28c0082..759222a 100644 --- a/formatter/formatter_csv_test.go +++ b/formatter/formatter_csv_test.go @@ -40,7 +40,7 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{}, HostNames: HostNames{}, Status: HostStatus{ @@ -107,7 +107,7 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "127.0.0.1", }, @@ -147,22 +147,20 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 80, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "http", - Product: "nginx", - Version: "1.21.1", - }, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 80, + State: PortState{ + State: "open", + Reason: "syn-ack", }, + Service: PortService{ + Name: "http", + Product: "nginx", + Version: "1.21.1", + }, + Script: []Script{}, }, }, HostAddress: HostAddress{ @@ -205,36 +203,34 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 80, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "http", - Product: "nginx", - Version: "1.21.1", - }, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 80, + State: PortState{ + State: "open", + Reason: "syn-ack", + }, + Service: PortService{ + Name: "http", + Product: "nginx", + Version: "1.21.1", + }, + Script: []Script{}, + }, + { + Protocol: "tcp", + PortID: 443, + State: PortState{ + State: "open", + Reason: "syn-ack", }, - { - Protocol: "tcp", - PortID: 443, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "http", - Product: "nginx", - Version: "1.21.1", - }, - Script: []Script{}, + Service: PortService{ + Name: "http", + Product: "nginx", + Version: "1.21.1", }, + Script: []Script{}, }, }, HostAddress: HostAddress{ @@ -278,36 +274,34 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 80, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "http", - Product: "nginx", - Version: "1.21.1", - }, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 80, + State: PortState{ + State: "open", + Reason: "syn-ack", + }, + Service: PortService{ + Name: "http", + Product: "nginx", + Version: "1.21.1", + }, + Script: []Script{}, + }, + { + Protocol: "tcp", + PortID: 443, + State: PortState{ + State: "open", + Reason: "syn-ack", }, - { - Protocol: "tcp", - PortID: 443, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "http", - Product: "nginx", - Version: "1.21.1", - }, - Script: []Script{}, + Service: PortService{ + Name: "http", + Product: "nginx", + Version: "1.21.1", }, + Script: []Script{}, }, }, HostAddress: HostAddress{ @@ -328,7 +322,7 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.1", }, @@ -370,36 +364,34 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 80, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "http", - Product: "nginx", - Version: "1.21.1", - }, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 80, + State: PortState{ + State: "open", + Reason: "syn-ack", }, - { - Protocol: "tcp", - PortID: 443, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "http", - Product: "nginx", - Version: "1.21.1", - }, - Script: []Script{}, + Service: PortService{ + Name: "http", + Product: "nginx", + Version: "1.21.1", }, + Script: []Script{}, + }, + { + Protocol: "tcp", + PortID: 443, + State: PortState{ + State: "open", + Reason: "syn-ack", + }, + Service: PortService{ + Name: "http", + Product: "nginx", + Version: "1.21.1", + }, + Script: []Script{}, }, }, HostAddress: HostAddress{ @@ -420,7 +412,7 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.1", }, @@ -463,36 +455,34 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 80, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "http", - Product: "nginx", - Version: "1.21.1", - }, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 80, + State: PortState{ + State: "open", + Reason: "syn-ack", }, - { - Protocol: "tcp", - PortID: 443, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "http", - Product: "nginx", - Version: "1.21.1", - }, - Script: []Script{}, + Service: PortService{ + Name: "http", + Product: "nginx", + Version: "1.21.1", }, + Script: []Script{}, + }, + { + Protocol: "tcp", + PortID: 443, + State: PortState{ + State: "open", + Reason: "syn-ack", + }, + Service: PortService{ + Name: "http", + Product: "nginx", + Version: "1.21.1", + }, + Script: []Script{}, }, }, HostAddress: HostAddress{ @@ -513,22 +503,20 @@ func TestCSVFormatter_convert(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 22, - State: PortState{ - State: "open", - Reason: "syn-ack", - }, - Service: PortService{ - Name: "ssh", - Product: "OpenSSH", - Version: "5.3p1 Debian 3ubuntu7", - }, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 22, + State: PortState{ + State: "open", + Reason: "syn-ack", + }, + Service: PortService{ + Name: "ssh", + Product: "OpenSSH", + Version: "5.3p1 Debian 3ubuntu7", }, + Script: []Script{}, }, }, HostAddress: HostAddress{ diff --git a/formatter/formatter_html_test.go b/formatter/formatter_html_test.go index 5c3f636..fe0de34 100644 --- a/formatter/formatter_html_test.go +++ b/formatter/formatter_html_test.go @@ -92,7 +92,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.1", }, @@ -111,7 +111,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.2", }, @@ -156,7 +156,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.1", }, @@ -175,7 +175,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.2", }, @@ -220,7 +220,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.1", }, @@ -239,7 +239,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.2", }, @@ -284,26 +284,24 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 80, - State: PortState{ - State: "open", - }, - Service: PortService{}, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 80, + State: PortState{ + State: "open", }, - { - Protocol: "tcp", - PortID: 443, - State: PortState{ - State: "up", - }, - Service: PortService{}, - Script: []Script{}, + Service: PortService{}, + Script: []Script{}, + }, + { + Protocol: "tcp", + PortID: 443, + State: PortState{ + State: "up", }, + Service: PortService{}, + Script: []Script{}, }, }, HostAddress: HostAddress{ @@ -324,17 +322,15 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 8080, - State: PortState{ - State: "open", - }, - Service: PortService{}, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 8080, + State: PortState{ + State: "open", }, + Service: PortService{}, + Script: []Script{}, }, }, HostAddress: HostAddress{}, @@ -376,7 +372,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.1", }, @@ -395,7 +391,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.2", }, @@ -414,7 +410,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.3", }, @@ -459,7 +455,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.1", }, @@ -478,7 +474,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.2", }, @@ -497,7 +493,7 @@ func TestHTMLFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.3", }, diff --git a/formatter/formatter_md_test.go b/formatter/formatter_md_test.go index d0aef98..d2430f4 100644 --- a/formatter/formatter_md_test.go +++ b/formatter/formatter_md_test.go @@ -166,7 +166,7 @@ func TestMarkdownFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.1", }, @@ -185,7 +185,7 @@ func TestMarkdownFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.2", }, @@ -204,7 +204,7 @@ func TestMarkdownFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.3", }, @@ -249,7 +249,7 @@ func TestMarkdownFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.1", }, @@ -268,7 +268,7 @@ func TestMarkdownFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.2", }, @@ -287,7 +287,7 @@ func TestMarkdownFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.3", }, @@ -332,26 +332,24 @@ func TestMarkdownFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 80, - State: PortState{ - State: "open", - }, - Service: PortService{}, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 80, + State: PortState{ + State: "open", }, - { - Protocol: "tcp", - PortID: 443, - State: PortState{ - State: "up", - }, - Service: PortService{}, - Script: []Script{}, + Service: PortService{}, + Script: []Script{}, + }, + { + Protocol: "tcp", + PortID: 443, + State: PortState{ + State: "up", }, + Service: PortService{}, + Script: []Script{}, }, }, HostAddress: HostAddress{ @@ -372,7 +370,7 @@ func TestMarkdownFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{}, + Port: []Port{}, HostAddress: HostAddress{ Address: "192.168.1.2", }, @@ -391,17 +389,15 @@ func TestMarkdownFormatter_Format(t *testing.T) { { StartTime: 0, EndTime: 0, - Ports: Ports{ - Port: []Port{ - { - Protocol: "tcp", - PortID: 8080, - State: PortState{ - State: "open", - }, - Service: PortService{}, - Script: []Script{}, + Port: []Port{ + { + Protocol: "tcp", + PortID: 8080, + State: PortState{ + State: "open", }, + Service: PortService{}, + Script: []Script{}, }, }, HostAddress: HostAddress{ diff --git a/formatter/nmap_host.go b/formatter/nmap_host.go index 3b9da7a..3b2e02b 100644 --- a/formatter/nmap_host.go +++ b/formatter/nmap_host.go @@ -4,7 +4,7 @@ package formatter type Host struct { StartTime int `xml:"starttime,attr"` EndTime int `xml:"endtime,attr"` - Ports Ports `xml:"ports"` + Port []Port `xml:"ports>port"` HostAddress HostAddress `xml:"address"` HostNames HostNames `xml:"hostnames"` Status HostStatus `xml:"status"` diff --git a/formatter/nmap_port.go b/formatter/nmap_port.go index fe999e1..a3c30b7 100644 --- a/formatter/nmap_port.go +++ b/formatter/nmap_port.go @@ -1,10 +1,5 @@ package formatter -// Ports has a list of Port structs. Ports itself is defined in Host struct -type Ports struct { - Port []Port `xml:"port"` -} - // Port record contains main information about port that was scanned type Port struct { Protocol string `xml:"protocol,attr"` diff --git a/formatter/resources/templates/markdown.tmpl b/formatter/resources/templates/markdown.tmpl index c60bf46..7338aa2 100644 --- a/formatter/resources/templates/markdown.tmpl +++ b/formatter/resources/templates/markdown.tmpl @@ -67,13 +67,13 @@ NMAP Scan Result: {{ .NMAPRun.StartStr }} | Port | State | Service | Reason | Product | Version | Extra Info | | ---- | ----- | ------- | ------ | ------- | ------- | ---------- | -{{ range .Ports.Port -}} +{{ range .Port -}} | {{ .PortID }} | {{ .Protocol }} | {{ .State.State }} | {{ .Service.Name }} | {{ .State.Reason }} | {{ .Service.Product }} | {{ .Service.Version }} | {{ .Service.ExtraInfo }} | -{{ end }}{{/* range .Ports.Port */}} +{{ end }}{{/* range .Port */}} {{ if not $skipPortScripts }} ### Scripts: -{{ range .Ports.Port -}} +{{ range .Port -}} {{- if .Script -}} #### PORT {{ .PortID }} {{ range .Script }} @@ -86,7 +86,7 @@ NMAP Scan Result: {{ .NMAPRun.StartStr }} {{- end -}}{{/* range .Script */}} {{- end -}}{{/* if .Script */}} {{ end -}}{{/* if not $skipPortScripts */}} -{{ end -}}{{/* range .Ports.Port */}} +{{ end -}}{{/* range .Port */}} {{- end -}}{{/* if eq .Status.State "up" */}} ---- diff --git a/formatter/resources/templates/simple-html.gohtml b/formatter/resources/templates/simple-html.gohtml index c2f22dd..7d5699b 100644 --- a/formatter/resources/templates/simple-html.gohtml +++ b/formatter/resources/templates/simple-html.gohtml @@ -41,15 +41,15 @@ .address-info-table > tbody > tr > td > ul { padding-left: 0; } - .ports-table > tbody > tr > td.port-open { + .port-table > tbody > tr > td.port-open { background-color: rgba(54, 182, 14, 0.18); border-color: rgba(62, 198, 16, 0.3); } - .ports-table > tbody > tr > td.port-closed { + .port-table > tbody > tr > td.port-closed { background-color: rgba(182, 2, 5, 0.18); border-color: rgba(253, 155, 157, 0.3); } - .ports-table > tbody > tr > td.port-filtered { + .port-table > tbody > tr > td.port-filtered { background-color: rgba(217, 63, 11, 0.18); border-color: rgba(247, 136, 100, 0.3); } @@ -216,7 +216,7 @@
Port | @@ -229,7 +229,7 @@|
---|---|
{{ .PortID }} | {{ .Protocol }} | @@ -249,7 +249,7 @@