Skip to content

Commit

Permalink
Tweak/separate host up func (#166)
Browse files Browse the repository at this point in the history
* refactor: IsUp function for host

* tweak: more variations for should-skip-host func

* cicd: codeql fix
  • Loading branch information
vdjagilev authored Apr 1, 2024
1 parent 3259deb commit 1705552
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions formatter/formatter_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 19 additions & 4 deletions formatter/formatter_excel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 79 in formatter/formatter_excel.go

View check run for this annotation

Codecov / codecov/patch

formatter/formatter_excel.go#L79

Added line #L79 was not covered by tests
}

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,
)
}

Expand Down Expand Up @@ -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

Check warning on line 138 in formatter/formatter_excel.go

View check run for this annotation

Codecov / codecov/patch

formatter/formatter_excel.go#L138

Added line #L138 was not covered by tests
}
*row++
return nil
}

for i := range p {
port := p[i]

Expand Down
18 changes: 18 additions & 0 deletions formatter/nmap_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<tcptssequence>` node
type TCPTSSequence struct {
Class string `xml:"class,attr"`
Expand Down Expand Up @@ -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"`
Expand Down
40 changes: 40 additions & 0 deletions formatter/nmap_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion formatter/resources/templates/graphviz.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"];
Expand Down
14 changes: 7 additions & 7 deletions formatter/resources/templates/markdown.tmpl
Original file line number Diff line number Diff line change
@@ -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 }}
Expand All @@ -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 }}
Expand Down Expand Up @@ -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 |
Expand Down Expand Up @@ -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 */}}
14 changes: 7 additions & 7 deletions formatter/resources/templates/simple-html.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
</style>
</head>
<body>
{{ $displayDownHosts := not .OutputOptions.HTMLOptions.SkipDownHosts }}
{{ $skipDownHosts := .OutputOptions.HTMLOptions.SkipDownHosts }}
{{ $skipSummary := .OutputOptions.HTMLOptions.SkipSummary }}
{{ $skipTraceroute := .OutputOptions.HTMLOptions.SkipTraceroute }}
{{ $skipMetrics := .OutputOptions.HTMLOptions.SkipMetrics }}
Expand All @@ -116,9 +116,9 @@
<ul>
{{ if not $skipSummary }}<li><a href="#scan-summary">Scan Summary</a></li>{{ end }}
{{ range $index, $value := .NMAPRun.Host }}
{{ if or ($displayDownHosts) (eq .Status.State "up") }}
{{ if .ShouldNotSkipHost $skipDownHosts }}
<li><a href="#{{ $index }}">{{ .JoinedAddresses "/" }}{{ range .HostNames.HostName }} / {{ .Name }}{{ end }}</a> ({{ .Status.State }})</li>
{{ end }}{{/* if or ($displayDownHosts) (eq .Status.State "up") */}}
{{ end }}{{/* if .ShouldNotSkipHost $skipDownHosts */}}
{{ end }}{{/* range .Host */}}
</ul>
<hr>
Expand Down Expand Up @@ -215,10 +215,10 @@
{{ end }}{{/* if .CustomOptions */}}
<hr>
{{ range $index, $value := .NMAPRun.Host }}
{{ if or ($displayDownHosts) (eq .Status.State "up") }}
{{ if .ShouldNotSkipHost $skipDownHosts }}
<a id="{{ $index }}"></a>
<h2 class="host-address-header{{ if eq .Status.State "up" }} host-up{{ else }} host-down{{ end }}">
{{ .JoinedAddresses "/" }}{{ range .HostNames.HostName }} / {{ .Name }}{{ end }} {{ if eq .Status.State "up" }}(up){{ else }}(down){{ end }}
<h2 class="host-address-header{{ if .Status.IsUp }} host-up{{ else }} host-down{{ end }}">
{{ .JoinedAddresses "/" }}{{ range .HostNames.HostName }} / {{ .Name }}{{ end }} {{ if .Status.IsUp }}(up){{ else }}(down){{ end }}
</h2>
{{ if eq .Status.State "up" }}
<h3>Info:</h3>
Expand Down Expand Up @@ -376,7 +376,7 @@
{{ end }}{{/* if not $skipMetrics */}}
<hr>
{{ end }}{{/* if eq .Status.State "up" */}}
{{ end }}{{/* if or ($displayDownHosts) (eq .Status.State "up") */}}
{{ end }}{{/* if .ShouldNotSkipHost $skipDownHosts */}}
{{ end }}{{/* range .Host */}}
</body>
</html>

0 comments on commit 1705552

Please sign in to comment.