Skip to content

Commit ec90a15

Browse files
committed
Tweak/separate host up func (#166)
* refactor: IsUp function for host * tweak: more variations for should-skip-host func * cicd: codeql fix
1 parent cd20686 commit ec90a15

File tree

8 files changed

+97
-22
lines changed

8 files changed

+97
-22
lines changed

.github/workflows/codeql-analysis.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ jobs:
3636
strategy:
3737
fail-fast: false
3838
matrix:
39-
language: ["go"]
39+
include:
40+
- language: go
41+
build-mode: autobuild
4042
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
4143
# Learn more:
4244
# 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

formatter/formatter_csv.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func (f *CSVFormatter) convert(td *TemplateData) (data [][]string) {
2020
data = append(data, []string{"IP", "Port", "Protocol", "State", "Service", "Reason", "Product", "Version", "Extra info"})
2121
for i := range td.NMAPRun.Host {
2222
var host *Host = &td.NMAPRun.Host[i]
23-
// Skipping hosts that are down
24-
if td.OutputOptions.CSVOptions.SkipDownHosts && host.Status.State != "up" {
23+
// shouldSkip := host.ShouldSkipHost(td.OutputOptions.CSVOptions.SkipDownHosts)
24+
if host.ShouldSkipHost(td.OutputOptions.CSVOptions.SkipDownHosts) {
2525
continue
2626
}
2727
address := fmt.Sprintf("%s (%s)", host.JoinedAddresses("/"), host.Status.State)

formatter/formatter_excel.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,24 @@ func (f *ExcelFormatter) writeHostRows(h []Host, cd *CellData) error {
7575
for i := range h {
7676
host := h[i]
7777

78-
// Skipping hosts that are down
79-
if f.config.OutputOptions.ExcelOptions.SkipDownHosts && host.Status.State != "up" {
78+
if host.ShouldSkipHost(f.config.OutputOptions.ExcelOptions.SkipDownHosts) {
8079
continue
8180
}
8281

8382
joinedAddresses := host.JoinedAddresses("/")
8483
joinedHostnames := host.JoinedHostNames("/")
84+
addressFormat := "%s [%s]"
8585
address := ""
8686

8787
if joinedHostnames == "" {
88-
address = joinedAddresses
88+
address = fmt.Sprintf(addressFormat, joinedAddresses, host.Status.State)
8989
} else {
90+
addressFormat = "%s (%s) [%s]"
9091
address = fmt.Sprintf(
91-
"%s (%s)",
92+
addressFormat,
9293
joinedAddresses,
9394
joinedHostnames,
95+
host.Status.State,
9496
)
9597
}
9698

@@ -126,6 +128,19 @@ func (f *ExcelFormatter) writeHostRows(h []Host, cd *CellData) error {
126128
func (f *ExcelFormatter) writePorts(p []Port, cd *CellData, row *int) error {
127129
var err error
128130

131+
// The case when there are no open ports
132+
if len(p) == 0 {
133+
err = cd.writeCell(
134+
fmt.Sprintf("%c%d", 'B', *row),
135+
"-",
136+
)
137+
if err != nil {
138+
return err
139+
}
140+
*row++
141+
return nil
142+
}
143+
129144
for i := range p {
130145
port := p[i]
131146

formatter/nmap_host.go

+18
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ func (h *Host) JoinedHostNames(delimiter string) string {
5050
return hostAddr
5151
}
5252

53+
// ShouldSkipHost returns true if host should be skipped in output
54+
// it's more convenient to use in loops, if we should skip the host
55+
// we use continue to the next host
56+
func (h *Host) ShouldSkipHost(skipDownHostsOption bool) bool {
57+
return skipDownHostsOption && !h.Status.IsUp()
58+
}
59+
60+
// ShouldNotSkipHost returns true if host should not be skipped in output
61+
// in some cases it's more convenient to write other way around (html, md templates)
62+
func (h *Host) ShouldNotSkipHost(skipDownHostsOption bool) bool {
63+
return !h.ShouldSkipHost(skipDownHostsOption)
64+
}
65+
5366
// TCPTSSequence describes all information related to `<tcptssequence>` node
5467
type TCPTSSequence struct {
5568
Class string `xml:"class,attr"`
@@ -86,6 +99,11 @@ type HostStatus struct {
8699
Reason string `xml:"reason,attr"`
87100
}
88101

102+
// IsUp returns true if the host is up and accessible
103+
func (hs *HostStatus) IsUp() bool {
104+
return hs.State == "up"
105+
}
106+
89107
// HostAddress struct contains the host address (IP) and type of it.
90108
type HostAddress struct {
91109
Address string `xml:"addr,attr"`

formatter/nmap_host_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,43 @@ func TestHost_JoinedHostNames(t *testing.T) {
218218
})
219219
}
220220
}
221+
222+
func TestHostStatus_IsUp(t *testing.T) {
223+
type fields struct {
224+
State string
225+
Reason string
226+
}
227+
tests := []struct {
228+
name string
229+
fields fields
230+
want bool
231+
}{
232+
{
233+
name: "Host is down",
234+
fields: fields{
235+
State: "down",
236+
Reason: "ping",
237+
},
238+
want: false,
239+
},
240+
{
241+
name: "Host is up",
242+
fields: fields{
243+
State: "up",
244+
Reason: "no reason",
245+
},
246+
want: true,
247+
},
248+
}
249+
for _, tt := range tests {
250+
t.Run(tt.name, func(t *testing.T) {
251+
hs := &HostStatus{
252+
State: tt.fields.State,
253+
Reason: tt.fields.Reason,
254+
}
255+
if got := hs.IsUp(); got != tt.want {
256+
t.Errorf("HostStatus.IsUp() = %v, want %v", got, tt.want)
257+
}
258+
})
259+
}
260+
}

formatter/resources/templates/graphviz.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ strict digraph "Scan" {
1212
{{ end }}
1313

1414
{{ range $key, $value := .NMAPRun.Host -}}
15-
{{ if eq $value.Status.State "up" -}}
15+
{{ if $value.Status.IsUp -}}
1616
srv{{ $key }} [label="{{ $value.JoinedAddresses "/" }}", tooltip="{{ $value.JoinedAddresses "/" }}", shape=hexagon, style=filled];
1717
{{ range $portKey, $portValue := $value.Port }}
1818
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 }}"];

formatter/resources/templates/markdown.tmpl

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
NMAP Scan Result: {{ .NMAPRun.StartStr }}
22
==========================================
3-
{{ $displayDownHosts := not .OutputOptions.MarkdownOptions.SkipDownHosts }}
3+
{{ $skipDownHosts := .OutputOptions.MarkdownOptions.SkipDownHosts }}
44
{{ $skipSummary := .OutputOptions.MarkdownOptions.SkipSummary }}
55
{{ $skipPortScripts := .OutputOptions.MarkdownOptions.SkipPortScripts }}
66
{{ $skipMetrics := .OutputOptions.MarkdownOptions.SkipMetrics }}
@@ -12,9 +12,9 @@ NMAP Scan Result: {{ .NMAPRun.StartStr }}
1212
* [Scan Summary](#scan-summary)
1313
{{- end }}{{/* if $skipSummary */}}
1414
{{- range .NMAPRun.Host -}}
15-
{{- if or ($displayDownHosts) (eq .Status.State "up") }}
15+
{{- if .ShouldNotSkipHost $skipDownHosts }}
1616
* [{{ md_title . }}](#{{ md_link . }})
17-
{{- end -}}{{/* if or ($displayDownHosts) (eq .Status.State "up") */}}
17+
{{- end -}}{{/* .ShouldNotSkipHost $skipDownHosts */}}
1818
{{- end -}}{{/* range .Host */}}
1919

2020
{{- if not $skipSummary }}
@@ -60,11 +60,11 @@ NMAP Scan Result: {{ .NMAPRun.StartStr }}
6060
----
6161

6262
{{ range .NMAPRun.Host -}}
63-
{{- if or ($displayDownHosts) (eq .Status.State "up") }}
63+
{{- if .ShouldNotSkipHost $skipDownHosts }}
6464

6565
## {{ md_title . }}
6666

67-
{{- if eq .Status.State "up" }}
67+
{{- if .ShouldNotSkipHost $skipDownHosts }}
6868
### Info:
6969

7070
| Name | Value |
@@ -134,10 +134,10 @@ NMAP Scan Result: {{ .NMAPRun.StartStr }}
134134
{{- end -}}{{/* if .Script */}}
135135
{{ end -}}{{/* if not $skipPortScripts */}}
136136
{{ end -}}{{/* range .Port */}}
137-
{{- end -}}{{/* if eq .Status.State "up" */}}
137+
{{- end -}}{{/* if .ShouldNotSkipHost $skipDownHosts */}}
138138

139139
----
140140

141-
{{- end -}}{{/* if or ($displayDownHosts) (eq .Status.State "up") */}}
141+
{{- end -}}{{/* if .ShouldNotSkipHost $skipDownHosts */}}
142142

143143
{{- end -}}{{/* range .NMAPRun.Host */}}

formatter/resources/templates/simple-html.gohtml

+7-7
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
</style>
105105
</head>
106106
<body>
107-
{{ $displayDownHosts := not .OutputOptions.HTMLOptions.SkipDownHosts }}
107+
{{ $skipDownHosts := .OutputOptions.HTMLOptions.SkipDownHosts }}
108108
{{ $skipSummary := .OutputOptions.HTMLOptions.SkipSummary }}
109109
{{ $skipTraceroute := .OutputOptions.HTMLOptions.SkipTraceroute }}
110110
{{ $skipMetrics := .OutputOptions.HTMLOptions.SkipMetrics }}
@@ -116,9 +116,9 @@
116116
<ul>
117117
{{ if not $skipSummary }}<li><a href="#scan-summary">Scan Summary</a></li>{{ end }}
118118
{{ range $index, $value := .NMAPRun.Host }}
119-
{{ if or ($displayDownHosts) (eq .Status.State "up") }}
119+
{{ if .ShouldNotSkipHost $skipDownHosts }}
120120
<li><a href="#{{ $index }}">{{ .JoinedAddresses "/" }}{{ range .HostNames.HostName }} / {{ .Name }}{{ end }}</a> ({{ .Status.State }})</li>
121-
{{ end }}{{/* if or ($displayDownHosts) (eq .Status.State "up") */}}
121+
{{ end }}{{/* if .ShouldNotSkipHost $skipDownHosts */}}
122122
{{ end }}{{/* range .Host */}}
123123
</ul>
124124
<hr>
@@ -215,10 +215,10 @@
215215
{{ end }}{{/* if .CustomOptions */}}
216216
<hr>
217217
{{ range $index, $value := .NMAPRun.Host }}
218-
{{ if or ($displayDownHosts) (eq .Status.State "up") }}
218+
{{ if .ShouldNotSkipHost $skipDownHosts }}
219219
<a id="{{ $index }}"></a>
220-
<h2 class="host-address-header{{ if eq .Status.State "up" }} host-up{{ else }} host-down{{ end }}">
221-
{{ .JoinedAddresses "/" }}{{ range .HostNames.HostName }} / {{ .Name }}{{ end }} {{ if eq .Status.State "up" }}(up){{ else }}(down){{ end }}
220+
<h2 class="host-address-header{{ if .Status.IsUp }} host-up{{ else }} host-down{{ end }}">
221+
{{ .JoinedAddresses "/" }}{{ range .HostNames.HostName }} / {{ .Name }}{{ end }} {{ if .Status.IsUp }}(up){{ else }}(down){{ end }}
222222
</h2>
223223
{{ if eq .Status.State "up" }}
224224
<h3>Info:</h3>
@@ -376,7 +376,7 @@
376376
{{ end }}{{/* if not $skipMetrics */}}
377377
<hr>
378378
{{ end }}{{/* if eq .Status.State "up" */}}
379-
{{ end }}{{/* if or ($displayDownHosts) (eq .Status.State "up") */}}
379+
{{ end }}{{/* if .ShouldNotSkipHost $skipDownHosts */}}
380380
{{ end }}{{/* range .Host */}}
381381
</body>
382382
</html>

0 commit comments

Comments
 (0)