diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index a41a915..4166c1a 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -36,7 +36,9 @@ jobs: strategy: fail-fast: false matrix: - language: ["go"] + include: + - language: go + build-mode: autobuild # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] # Learn more: # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed diff --git a/formatter/formatter_csv.go b/formatter/formatter_csv.go index f03a029..451479b 100644 --- a/formatter/formatter_csv.go +++ b/formatter/formatter_csv.go @@ -20,8 +20,8 @@ func (f *CSVFormatter) convert(td *TemplateData) (data [][]string) { data = append(data, []string{"IP", "Port", "Protocol", "State", "Service", "Reason", "Product", "Version", "Extra info"}) for i := range td.NMAPRun.Host { var host *Host = &td.NMAPRun.Host[i] - // Skipping hosts that are down - if td.OutputOptions.CSVOptions.SkipDownHosts && host.Status.State != "up" { + // shouldSkip := host.ShouldSkipHost(td.OutputOptions.CSVOptions.SkipDownHosts) + if host.ShouldSkipHost(td.OutputOptions.CSVOptions.SkipDownHosts) { continue } address := fmt.Sprintf("%s (%s)", host.JoinedAddresses("/"), host.Status.State) diff --git a/formatter/formatter_excel.go b/formatter/formatter_excel.go index 0521b05..d18e285 100644 --- a/formatter/formatter_excel.go +++ b/formatter/formatter_excel.go @@ -75,22 +75,24 @@ func (f *ExcelFormatter) writeHostRows(h []Host, cd *CellData) error { for i := range h { host := h[i] - // Skipping hosts that are down - if f.config.OutputOptions.ExcelOptions.SkipDownHosts && host.Status.State != "up" { + if host.ShouldSkipHost(f.config.OutputOptions.ExcelOptions.SkipDownHosts) { continue } joinedAddresses := host.JoinedAddresses("/") joinedHostnames := host.JoinedHostNames("/") + addressFormat := "%s [%s]" address := "" if joinedHostnames == "" { - address = joinedAddresses + address = fmt.Sprintf(addressFormat, joinedAddresses, host.Status.State) } else { + addressFormat = "%s (%s) [%s]" address = fmt.Sprintf( - "%s (%s)", + addressFormat, joinedAddresses, joinedHostnames, + host.Status.State, ) } @@ -126,6 +128,19 @@ func (f *ExcelFormatter) writeHostRows(h []Host, cd *CellData) error { func (f *ExcelFormatter) writePorts(p []Port, cd *CellData, row *int) error { var err error + // The case when there are no open ports + if len(p) == 0 { + err = cd.writeCell( + fmt.Sprintf("%c%d", 'B', *row), + "-", + ) + if err != nil { + return err + } + *row++ + return nil + } + for i := range p { port := p[i] diff --git a/formatter/nmap_host.go b/formatter/nmap_host.go index 013ea88..1745db6 100644 --- a/formatter/nmap_host.go +++ b/formatter/nmap_host.go @@ -50,6 +50,19 @@ func (h *Host) JoinedHostNames(delimiter string) string { return hostAddr } +// ShouldSkipHost returns true if host should be skipped in output +// it's more convenient to use in loops, if we should skip the host +// we use continue to the next host +func (h *Host) ShouldSkipHost(skipDownHostsOption bool) bool { + return skipDownHostsOption && !h.Status.IsUp() +} + +// ShouldNotSkipHost returns true if host should not be skipped in output +// in some cases it's more convenient to write other way around (html, md templates) +func (h *Host) ShouldNotSkipHost(skipDownHostsOption bool) bool { + return !h.ShouldSkipHost(skipDownHostsOption) +} + // TCPTSSequence describes all information related to `` node type TCPTSSequence struct { Class string `xml:"class,attr"` @@ -86,6 +99,11 @@ type HostStatus struct { Reason string `xml:"reason,attr"` } +// IsUp returns true if the host is up and accessible +func (hs *HostStatus) IsUp() bool { + return hs.State == "up" +} + // HostAddress struct contains the host address (IP) and type of it. type HostAddress struct { Address string `xml:"addr,attr"` diff --git a/formatter/nmap_host_test.go b/formatter/nmap_host_test.go index 662b750..66239fe 100644 --- a/formatter/nmap_host_test.go +++ b/formatter/nmap_host_test.go @@ -218,3 +218,43 @@ func TestHost_JoinedHostNames(t *testing.T) { }) } } + +func TestHostStatus_IsUp(t *testing.T) { + type fields struct { + State string + Reason string + } + tests := []struct { + name string + fields fields + want bool + }{ + { + name: "Host is down", + fields: fields{ + State: "down", + Reason: "ping", + }, + want: false, + }, + { + name: "Host is up", + fields: fields{ + State: "up", + Reason: "no reason", + }, + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + hs := &HostStatus{ + State: tt.fields.State, + Reason: tt.fields.Reason, + } + if got := hs.IsUp(); got != tt.want { + t.Errorf("HostStatus.IsUp() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/formatter/resources/templates/graphviz.tmpl b/formatter/resources/templates/graphviz.tmpl index f214fb3..5af6fb2 100644 --- a/formatter/resources/templates/graphviz.tmpl +++ b/formatter/resources/templates/graphviz.tmpl @@ -12,7 +12,7 @@ strict digraph "Scan" { {{ end }} {{ range $key, $value := .NMAPRun.Host -}} - {{ if eq $value.Status.State "up" -}} + {{ if $value.Status.IsUp -}} srv{{ $key }} [label="{{ $value.JoinedAddresses "/" }}", tooltip="{{ $value.JoinedAddresses "/" }}", shape=hexagon, style=filled]; {{ range $portKey, $portValue := $value.Port }} srv{{ $key }}_port_{{ $portValue.Protocol }}_{{ $portValue.PortID }} [label="{{ $portValue.Protocol }}/{{ $portValue.PortID }} ({{ $portValue.State.State }})", tooltip="{{ $portValue.Protocol }}/{{ $portValue.PortID }} ({{ $portValue.State.State }})", shape=underline, width=.12, height=.12, fontsize=8, color="{{ port_state_color $portValue }}"]; diff --git a/formatter/resources/templates/markdown.tmpl b/formatter/resources/templates/markdown.tmpl index 2452f27..c92f95b 100644 --- a/formatter/resources/templates/markdown.tmpl +++ b/formatter/resources/templates/markdown.tmpl @@ -1,6 +1,6 @@ NMAP Scan Result: {{ .NMAPRun.StartStr }} ========================================== -{{ $displayDownHosts := not .OutputOptions.MarkdownOptions.SkipDownHosts }} +{{ $skipDownHosts := .OutputOptions.MarkdownOptions.SkipDownHosts }} {{ $skipSummary := .OutputOptions.MarkdownOptions.SkipSummary }} {{ $skipPortScripts := .OutputOptions.MarkdownOptions.SkipPortScripts }} {{ $skipMetrics := .OutputOptions.MarkdownOptions.SkipMetrics }} @@ -12,9 +12,9 @@ NMAP Scan Result: {{ .NMAPRun.StartStr }} * [Scan Summary](#scan-summary) {{- end }}{{/* if $skipSummary */}} {{- range .NMAPRun.Host -}} -{{- if or ($displayDownHosts) (eq .Status.State "up") }} +{{- if .ShouldNotSkipHost $skipDownHosts }} * [{{ md_title . }}](#{{ md_link . }}) -{{- end -}}{{/* if or ($displayDownHosts) (eq .Status.State "up") */}} +{{- end -}}{{/* .ShouldNotSkipHost $skipDownHosts */}} {{- end -}}{{/* range .Host */}} {{- if not $skipSummary }} @@ -60,11 +60,11 @@ NMAP Scan Result: {{ .NMAPRun.StartStr }} ---- {{ range .NMAPRun.Host -}} -{{- if or ($displayDownHosts) (eq .Status.State "up") }} +{{- if .ShouldNotSkipHost $skipDownHosts }} ## {{ md_title . }} -{{- if eq .Status.State "up" }} +{{- if .ShouldNotSkipHost $skipDownHosts }} ### Info: | Name | Value | @@ -134,10 +134,10 @@ NMAP Scan Result: {{ .NMAPRun.StartStr }} {{- end -}}{{/* if .Script */}} {{ end -}}{{/* if not $skipPortScripts */}} {{ end -}}{{/* range .Port */}} -{{- end -}}{{/* if eq .Status.State "up" */}} +{{- end -}}{{/* if .ShouldNotSkipHost $skipDownHosts */}} ---- -{{- end -}}{{/* if or ($displayDownHosts) (eq .Status.State "up") */}} +{{- end -}}{{/* if .ShouldNotSkipHost $skipDownHosts */}} {{- end -}}{{/* range .NMAPRun.Host */}} diff --git a/formatter/resources/templates/simple-html.gohtml b/formatter/resources/templates/simple-html.gohtml index 3b1abfe..d457f08 100644 --- a/formatter/resources/templates/simple-html.gohtml +++ b/formatter/resources/templates/simple-html.gohtml @@ -104,7 +104,7 @@ - {{ $displayDownHosts := not .OutputOptions.HTMLOptions.SkipDownHosts }} + {{ $skipDownHosts := .OutputOptions.HTMLOptions.SkipDownHosts }} {{ $skipSummary := .OutputOptions.HTMLOptions.SkipSummary }} {{ $skipTraceroute := .OutputOptions.HTMLOptions.SkipTraceroute }} {{ $skipMetrics := .OutputOptions.HTMLOptions.SkipMetrics }} @@ -116,9 +116,9 @@
@@ -215,10 +215,10 @@ {{ end }}{{/* if .CustomOptions */}}
{{ range $index, $value := .NMAPRun.Host }} - {{ if or ($displayDownHosts) (eq .Status.State "up") }} + {{ if .ShouldNotSkipHost $skipDownHosts }} -

- {{ .JoinedAddresses "/" }}{{ range .HostNames.HostName }} / {{ .Name }}{{ end }} {{ if eq .Status.State "up" }}(up){{ else }}(down){{ end }} +

+ {{ .JoinedAddresses "/" }}{{ range .HostNames.HostName }} / {{ .Name }}{{ end }} {{ if .Status.IsUp }}(up){{ else }}(down){{ end }}

{{ if eq .Status.State "up" }}

Info:

@@ -376,7 +376,7 @@ {{ end }}{{/* if not $skipMetrics */}}
{{ end }}{{/* if eq .Status.State "up" */}} - {{ end }}{{/* if or ($displayDownHosts) (eq .Status.State "up") */}} + {{ end }}{{/* if .ShouldNotSkipHost $skipDownHosts */}} {{ end }}{{/* range .Host */}}