Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: other: prepend filters for skip-down-hosts #171

Merged
merged 5 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ More examples can be found on [Usage Wiki page](https://github.com/vdjagilev/nma

### Flags

* `-f, --file [filename]` outputs result to the file (by default output goes to STDOUT)
* `--help` display help message
* `--version` display version (also can be used: `./nmap-formatter version`)
- `-f, --file [filename]` outputs result to the file (by default output goes to STDOUT)
- `--help` display help message
- `--version` display version (also can be used: `./nmap-formatter version`)
- `--skip-down-hosts` skip hosts that are down (by default `true`)

It's also possible to change various output options. More examples on [Usage Wiki Page - Flags](https://github.com/vdjagilev/nmap-formatter/wiki/Usage#flags-and-output-options).

Expand Down
12 changes: 7 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var config = formatter.Config{
},
ShowVersion: false,
CurrentVersion: VERSION,
SkipDownHosts: true,
FilterExpressions: []string{},
}

Expand Down Expand Up @@ -83,10 +84,10 @@ func init() {

// Some options related to the output
// Skip hosts that are down, so they won't be listed in the output
rootCmd.Flags().BoolVar(&config.OutputOptions.HTMLOptions.SkipDownHosts, "html-skip-down-hosts", true, "--html-skip-down-hosts=false, would print all hosts that are offline in HTML output")
rootCmd.Flags().BoolVar(&config.OutputOptions.MarkdownOptions.SkipDownHosts, "md-skip-down-hosts", true, "--md-skip-down-hosts=false, would print all hosts that are offline in Markdown output")
rootCmd.Flags().BoolVar(&config.OutputOptions.CSVOptions.SkipDownHosts, "csv-skip-down-hosts", true, "--csv-skip-down-hosts=false, would print all hosts that are offline in CSV output")
rootCmd.Flags().BoolVar(&config.OutputOptions.ExcelOptions.SkipDownHosts, "excel-skip-down-hosts", true, "--excel-skip-down-hosts=false, would print all hosts that are offline in Excel file")
// rootCmd.Flags().BoolVar(&config.OutputOptions.HTMLOptions.SkipDownHosts, "html-skip-down-hosts", true, "--html-skip-down-hosts=false, would print all hosts that are offline in HTML output")
// rootCmd.Flags().BoolVar(&config.OutputOptions.MarkdownOptions.SkipDownHosts, "md-skip-down-hosts", true, "--md-skip-down-hosts=false, would print all hosts that are offline in Markdown output")
// rootCmd.Flags().BoolVar(&config.OutputOptions.CSVOptions.SkipDownHosts, "csv-skip-down-hosts", true, "--csv-skip-down-hosts=false, would print all hosts that are offline in CSV output")
// rootCmd.Flags().BoolVar(&config.OutputOptions.ExcelOptions.SkipDownHosts, "excel-skip-down-hosts", true, "--excel-skip-down-hosts=false, would print all hosts that are offline in Excel file")

// Skip header information (overall meta information from the scan)
rootCmd.Flags().BoolVar(&config.OutputOptions.HTMLOptions.SkipHeader, "html-skip-header", false, "--html-skip-header, skips header in HTML output")
Expand Down Expand Up @@ -124,7 +125,8 @@ func init() {
rootCmd.Flags().StringVar(&config.OutputOptions.SqliteOutputOptions.ScanIdentifier, "scan-id", "", "--scan-id abc123")

// Configs related to D2 language
rootCmd.Flags().BoolVar(&config.OutputOptions.D2LangOptions.SkipDownHosts, "d2-skip-down-hosts", true, "--d2-skip-down-hosts=false, would print all hosts that are offline in D2 language output")
// rootCmd.Flags().BoolVar(&config.OutputOptions.D2LangOptions.SkipDownHosts, "d2-skip-down-hosts", true, "--d2-skip-down-hosts=false, would print all hosts that are offline in D2 language output")
rootCmd.Flags().BoolVar(&config.SkipDownHosts, "skip-down-hosts", false, "--skip-down-hosts=true, skips hosts that are offline")

// Multiple filter expressions supported
rootCmd.Flags().StringArrayVar(&config.FilterExpressions, "filter", []string{}, "--filter '.Status.State == \"up\" && any(.Port, { .PortID in [80,443] })'")
Expand Down
2 changes: 1 addition & 1 deletion cmd/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// validate is checking input from the command line
func validate(config formatter.Config) error {
if !config.OutputFormat.IsValid() {
return fmt.Errorf("not valid format: %s, please choose html/json/md/csv/excel/sqlite", config.OutputFormat)
return fmt.Errorf("not valid format: %s, please choose html/json/md/csv/excel/sqlite/d2", config.OutputFormat)
}

err := validateIOFiles(config)
Expand Down
1 change: 1 addition & 0 deletions formatter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
TemplatePath string
CustomOptions []string
CurrentVersion string
SkipDownHosts bool
FilterExpressions []string
}

Expand Down
4 changes: 0 additions & 4 deletions formatter/formatter_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ 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]
// 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)
data = append(data, []string{address, "", "", "", "", "", "", "", ""})
for j := range host.Port {
Expand Down
103 changes: 36 additions & 67 deletions formatter/formatter_csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,24 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
{
name: "1 Host is down (skip down hosts)",
name: "1 Host is down",
f: &CSVFormatter{},
args: args{
td: &TemplateData{
NMAPRun: NMAPRun{
Host: []Host{
{
StartTime: 0,
EndTime: 0,
Port: []Port{},
HostAddress: []HostAddress{},
HostNames: HostNames{},
StartTime: 0,
EndTime: 0,
Port: []Port{},
HostAddress: []HostAddress{
{
Address: "127.0.0.1",
AddressType: "ipv4",
Vendor: "",
},
},
HostNames: HostNames{},
Status: HostStatus{
State: "down",
},
Expand All @@ -57,48 +63,17 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
OutputOptions: OutputOptions{
CSVOptions: CSVOutputOptions{
SkipDownHosts: true,
},
},
},
},
wantData: [][]string{
header,
},
},
{
name: "2 Hosts are down (skip down hosts)",
f: &CSVFormatter{},
args: args{
td: &TemplateData{
NMAPRun: NMAPRun{
Host: []Host{
{
Status: HostStatus{
State: "down",
},
},
{
Status: HostStatus{
State: "down",
},
},
},
},
OutputOptions: OutputOptions{
CSVOptions: CSVOutputOptions{
SkipDownHosts: true,
},
CSVOptions: CSVOutputOptions{},
},
},
},
wantData: [][]string{
header,
{"127.0.0.1 (down)", "", "", "", "", "", "", "", ""},
},
},
{
name: "1 host is down (do not skip down hosts)",
name: "1 host is down",
f: &CSVFormatter{},
args: args{
td: &TemplateData{
Expand Down Expand Up @@ -128,9 +103,7 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
OutputOptions: OutputOptions{
CSVOptions: CSVOutputOptions{
SkipDownHosts: false,
},
CSVOptions: CSVOutputOptions{},
},
},
},
Expand Down Expand Up @@ -185,9 +158,7 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
OutputOptions: OutputOptions{
CSVOptions: CSVOutputOptions{
SkipDownHosts: true,
},
CSVOptions: CSVOutputOptions{},
},
},
},
Expand Down Expand Up @@ -257,9 +228,7 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
OutputOptions: OutputOptions{
CSVOptions: CSVOutputOptions{
SkipDownHosts: true,
},
CSVOptions: CSVOutputOptions{},
},
},
},
Expand All @@ -271,7 +240,7 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
{
name: "1 host up 2 ports, 1 host down (skip-down-hosts=true)",
name: "1 host up 2 ports, 1 host down",
f: &CSVFormatter{},
args: args{
td: &TemplateData{
Expand Down Expand Up @@ -351,9 +320,7 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
OutputOptions: OutputOptions{
CSVOptions: CSVOutputOptions{
SkipDownHosts: true,
},
CSVOptions: CSVOutputOptions{},
},
},
},
Expand All @@ -362,11 +329,14 @@ func TestCSVFormatter_convert(t *testing.T) {
{"127.0.0.1 (up)", "", "", "", "", "", "", "", ""},
{"", "80", "tcp", "open", "http", "syn-ack", "nginx", "1.21.1", ""},
{"", "443", "tcp", "open", "http", "syn-ack", "nginx", "1.21.1", ""},
{"192.168.1.1 (down)", "", "", "", "", "", "", "", ""},
},
},
{
name: "1 host up 2 ports, 1 host down (skip-down-hosts=false)",
f: &CSVFormatter{},
name: "1 host up 2 ports, 1 host down",
f: &CSVFormatter{
config: &Config{},
},
args: args{
td: &TemplateData{
NMAPRun: NMAPRun{
Expand Down Expand Up @@ -445,9 +415,7 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
OutputOptions: OutputOptions{
CSVOptions: CSVOutputOptions{
SkipDownHosts: false,
},
CSVOptions: CSVOutputOptions{},
},
},
},
Expand All @@ -460,8 +428,12 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
{
name: "2 host up (2+1 ports) (skip-down-hosts=true)",
f: &CSVFormatter{},
name: "2 hosts (2+1 ports)",
f: &CSVFormatter{
config: &Config{
SkipDownHosts: true,
},
},
args: args{
td: &TemplateData{
NMAPRun: NMAPRun{
Expand Down Expand Up @@ -555,9 +527,7 @@ func TestCSVFormatter_convert(t *testing.T) {
},
},
OutputOptions: OutputOptions{
CSVOptions: CSVOutputOptions{
SkipDownHosts: false,
},
CSVOptions: CSVOutputOptions{},
},
},
},
Expand Down Expand Up @@ -596,7 +566,8 @@ func TestCSVFormatter_Format(t *testing.T) {
name: "Successful header write",
f: &CSVFormatter{
config: &Config{
Writer: writer,
Writer: writer,
SkipDownHosts: true,
},
},
args: args{
Expand All @@ -605,9 +576,7 @@ func TestCSVFormatter_Format(t *testing.T) {
Host: []Host{},
},
OutputOptions: OutputOptions{
CSVOptions: CSVOutputOptions{
SkipDownHosts: true,
},
CSVOptions: CSVOutputOptions{},
},
},
},
Expand Down
4 changes: 0 additions & 4 deletions formatter/formatter_d2.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ func (f *D2LangFormatter) Format(td *TemplateData, templateContent string) (err
host := &td.NMAPRun.Host[i]
fnv := fnv.New128()

if host.ShouldSkipHost(td.OutputOptions.D2LangOptions.SkipDownHosts) {
continue
}

address := host.JoinedAddresses("/")
hostnames := host.JoinedHostNames("/")
hostLabel := address
Expand Down
3 changes: 2 additions & 1 deletion formatter/formatter_dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ type DotFormatter struct {
config *Config
}

//go:embed resources/templates/graphviz.tmpl
// DotTemplate variable is used to store contents of graphviz template
//
//go:embed resources/templates/graphviz.tmpl
var DotTemplate string

const (
Expand Down
5 changes: 0 additions & 5 deletions formatter/formatter_excel.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ func (f *ExcelFormatter) writeHostRows(h []Host, cd *CellData) error {

for i := range h {
host := h[i]

if host.ShouldSkipHost(f.config.OutputOptions.ExcelOptions.SkipDownHosts) {
continue
}

joinedAddresses := host.JoinedAddresses("/")
joinedHostnames := host.JoinedHostNames("/")
addressFormat := "%s [%s]"
Expand Down
3 changes: 2 additions & 1 deletion formatter/formatter_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ type HTMLFormatter struct {
config *Config
}

//go:embed resources/templates/simple-html.gohtml
// HTMLSimpleTemplate variable is used to store embedded HTML template content
//
//go:embed resources/templates/simple-html.gohtml
var HTMLSimpleTemplate string

// Format the data and output it to appropriate io.Writer
Expand Down
Loading
Loading