diff --git a/module.go b/module.go index 7ceb76b6b..7e259bc02 100644 --- a/module.go +++ b/module.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net" + "reflect" "strconv" "time" @@ -38,6 +39,26 @@ type Scanner interface { GetDialerGroupConfig() *DialerGroupConfig } +// BaseScanner provides default implementations for common scanner methods +type BaseScanner struct { + protocol string + DialerGroupConfig *DialerGroupConfig +} + +// NewBaseScanner creates a new base scanner +func NewBaseScanner(protocol string) *BaseScanner { + return &BaseScanner{protocol: protocol} +} + +// Protocol returns the protocol identifier +func (scanner *BaseScanner) Protocol() string { + return scanner.protocol +} + +func (scanner *BaseScanner) GetDialerGroupConfig() *DialerGroupConfig { + return scanner.DialerGroupConfig +} + // TransportProtocol is an enum for the transport layer protocol of a module type TransportProtocol uint @@ -221,12 +242,52 @@ type ScanModule interface { Description() string } +// ModuleInfo holds metadata about a module +type ModuleInfo struct { + Protocol string + Description string + DefaultPort int + FlagsType reflect.Type +} + +// BaseModule provides default implementations for common module methods +type BaseModule struct { + info ModuleInfo +} + +// NewBaseModule creates a new base module with the given info +func NewBaseModule(protocol, description string, defaultPort int, flagsType reflect.Type) *BaseModule { + return &BaseModule{ + info: ModuleInfo{ + Protocol: protocol, + Description: description, + DefaultPort: defaultPort, + FlagsType: flagsType, + }, + } +} + +// NewFlags creates a new instance of the flags type +func (m *BaseModule) NewFlags() any { + return reflect.New(m.info.FlagsType).Interface() +} + +// Description returns the module description +func (m *BaseModule) Description() string { + return m.info.Description +} + +func (m *BaseModule) Protocol() string { + return m.info.Protocol +} + +func (m *BaseModule) DefaultPort() int { + return m.info.DefaultPort +} + // ScanFlags is an interface which must be implemented by all types sent to // the flag parser type ScanFlags interface { - // Help optionally returns any additional help text, e.g. specifying what empty defaults are interpreted as. - Help() string - // Validate enforces all command-line flags and positional arguments have valid values. Validate([]string) error } diff --git a/modules/amqp091/scanner.go b/modules/amqp091/scanner.go index 718671cd6..a832bddcb 100644 --- a/modules/amqp091/scanner.go +++ b/modules/amqp091/scanner.go @@ -131,11 +131,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, ok := flags.(*Flags) diff --git a/modules/bacnet/scanner.go b/modules/bacnet/scanner.go index 13c690c1f..5511956ee 100644 --- a/modules/bacnet/scanner.go +++ b/modules/bacnet/scanner.go @@ -65,11 +65,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/banner/scanner.go b/modules/banner/scanner.go index 4f60c648f..00264679d 100644 --- a/modules/banner/scanner.go +++ b/modules/banner/scanner.go @@ -15,6 +15,7 @@ import ( "io" "net" "os" + "reflect" "regexp" "strconv" "time" @@ -46,14 +47,27 @@ type Flags struct { // Module is the implementation of the zgrab2.Module interface. type Module struct { + *zgrab2.BaseModule +} + +// Create the module with metadata +func NewModule() *Module { + return &Module{ + BaseModule: zgrab2.NewBaseModule( + "banner", + "Fetch a raw banner by sending a static probe and checking the result against a regular expression", + 80, + reflect.TypeOf(Flags{}), + ), + } } // Scanner is the implementation of the zgrab2.Scanner interface. type Scanner struct { + *zgrab2.BaseScanner config *Flags regex *regexp.Regexp probe []byte - dialerGroupConfig *zgrab2.DialerGroupConfig } // ScanResults instances are returned by the module's Scan function. @@ -70,18 +84,14 @@ var ErrNoMatch = errors.New("pattern did not match") // RegisterModule is called by modules/banner.go to register the scanner. func RegisterModule() { - var m Module - _, err := zgrab2.AddCommand("banner", "Banner", m.Description(), 80, &m) + m := NewModule() + //TODO, just pass module to AddCommand. We don't here so I don't refactor ALL the modules at once + _, err := zgrab2.AddCommand(m.Protocol(), m.Protocol(), m.Description(), m.DefaultPort(), m) if err != nil { log.Fatal(err) } } -// NewFlags returns a new default flags object. -func (m *Module) NewFlags() any { - return new(Flags) -} - // GetName returns the Scanner name defined in the Flags. func (s *Scanner) GetName() string { return s.config.Name @@ -92,13 +102,11 @@ func (s *Scanner) GetTrigger() string { return s.config.Trigger } -// Protocol returns the protocol identifier of the scan. -func (s *Scanner) Protocol() string { - return "banner" -} - -func (scanner *Scanner) GetDialerGroupConfig() *zgrab2.DialerGroupConfig { - return scanner.dialerGroupConfig +// NewScanner returns a new Scanner instance. +func (module *Module) NewScanner() zgrab2.Scanner { + return &Scanner{ + BaseScanner: zgrab2.NewBaseScanner(module.Protocol()), + } } // InitPerSender initializes the scanner for a given sender. @@ -106,11 +114,6 @@ func (s *Scanner) InitPerSender(senderID int) error { return nil } -// NewScanner returns a new Scanner object. -func (m *Module) NewScanner() zgrab2.Scanner { - return new(Scanner) -} - // Validate validates the flags and returns nil on success. func (f *Flags) Validate(_ []string) error { if f.Probe != "\\n" && f.ProbeFile != "" { @@ -120,16 +123,6 @@ func (f *Flags) Validate(_ []string) error { return nil } -// Description returns an overview of this module. -func (m *Module) Description() string { - return "Fetch a raw banner by sending a static probe and checking the result against a regular expression" -} - -// Help returns the module's help string. -func (f *Flags) Help() string { - return "" -} - // Init initializes the Scanner with the command-line flags. func (s *Scanner) Init(flags zgrab2.ScanFlags) error { var err error @@ -151,13 +144,13 @@ func (s *Scanner) Init(flags zgrab2.ScanFlags) error { } s.probe = []byte(strProbe) } - s.dialerGroupConfig = &zgrab2.DialerGroupConfig{ + s.DialerGroupConfig = &zgrab2.DialerGroupConfig{ TransportAgnosticDialerProtocol: zgrab2.TransportTCP, BaseFlags: &f.BaseFlags, TLSEnabled: f.UseTLS, } if f.UseTLS { - s.dialerGroupConfig.TLSFlags = &f.TLSFlags + s.DialerGroupConfig.TLSFlags = &f.TLSFlags } return nil } diff --git a/modules/dnp3/scanner.go b/modules/dnp3/scanner.go index 54e591e29..87b4fe234 100644 --- a/modules/dnp3/scanner.go +++ b/modules/dnp3/scanner.go @@ -63,11 +63,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/fox/scanner.go b/modules/fox/scanner.go index f7e0b21b4..1fdb55e15 100644 --- a/modules/fox/scanner.go +++ b/modules/fox/scanner.go @@ -66,11 +66,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/ftp/scanner.go b/modules/ftp/scanner.go index ba87de7b0..be8201e9e 100644 --- a/modules/ftp/scanner.go +++ b/modules/ftp/scanner.go @@ -112,11 +112,6 @@ func (f *Flags) Validate(_ []string) (err error) { return } -// Help returns this module's help string. -func (f *Flags) Help() string { - return "" -} - // Protocol returns the protocol identifer for the scanner. func (scanner *Scanner) Protocol() string { return "ftp" diff --git a/modules/http/scanner.go b/modules/http/scanner.go index dbb711e01..7ada3fab9 100644 --- a/modules/http/scanner.go +++ b/modules/http/scanner.go @@ -147,11 +147,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns module-specific help -func (flags *Flags) Help() string { - return "" -} - // Protocol returns the protocol identifer for the scanner. func (scanner *Scanner) Protocol() string { return "http" diff --git a/modules/imap/scanner.go b/modules/imap/scanner.go index 345306630..978625fdd 100644 --- a/modules/imap/scanner.go +++ b/modules/imap/scanner.go @@ -114,11 +114,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/ipp/scanner.go b/modules/ipp/scanner.go index 2db6b92ec..f65f8c469 100644 --- a/modules/ipp/scanner.go +++ b/modules/ipp/scanner.go @@ -159,12 +159,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - //TODO: Write a help string - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/jarm/scanner.go b/modules/jarm/scanner.go index 635c24d22..19da842b9 100644 --- a/modules/jarm/scanner.go +++ b/modules/jarm/scanner.go @@ -94,11 +94,6 @@ func (f *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (f *Flags) Help() string { - return "" -} - // Init initializes the Scanner with the command-line flags. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/modbus/scanner.go b/modules/modbus/scanner.go index 2f9377b3a..3aed62867 100644 --- a/modules/modbus/scanner.go +++ b/modules/modbus/scanner.go @@ -88,11 +88,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/mongodb/scanner.go b/modules/mongodb/scanner.go index 9877e0bf3..7f5e35105 100644 --- a/modules/mongodb/scanner.go +++ b/modules/mongodb/scanner.go @@ -236,11 +236,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string -func (flags *Flags) Help() string { - return "" -} - // NewFlags provides an empty instance of the flags that will be filled in by the framework func (module *Module) NewFlags() any { return new(Flags) diff --git a/modules/mqtt/scanner.go b/modules/mqtt/scanner.go index 93710032c..33136cfa0 100644 --- a/modules/mqtt/scanner.go +++ b/modules/mqtt/scanner.go @@ -79,11 +79,6 @@ func (f *Flags) Validate(_ []string) error { return nil } -// Help returns this module's help string. -func (f *Flags) Help() string { - return "" -} - // Protocol returns the protocol identifier for the scanner. func (s *Scanner) Protocol() string { return "mqtt" diff --git a/modules/mssql/scanner.go b/modules/mssql/scanner.go index bddea0147..51fc56fa1 100644 --- a/modules/mssql/scanner.go +++ b/modules/mssql/scanner.go @@ -86,11 +86,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the help string for this module. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner instance with the given command-line flags. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/mysql/scanner.go b/modules/mysql/scanner.go index c8faa3d7c..49423b2dc 100644 --- a/modules/mysql/scanner.go +++ b/modules/mysql/scanner.go @@ -182,11 +182,6 @@ func (f *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (f *Flags) Help() string { - return "" -} - // Init initializes the Scanner with the command-line flags. func (s *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/ntp/scanner.go b/modules/ntp/scanner.go index c32ac94c4..4a0ba954c 100644 --- a/modules/ntp/scanner.go +++ b/modules/ntp/scanner.go @@ -823,11 +823,6 @@ func (cfg *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string -func (cfg *Flags) Help() string { - return "" -} - // Init initialized the scanner func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/oracle/scanner.go b/modules/oracle/scanner.go index ae06eda77..0f076bc5e 100644 --- a/modules/oracle/scanner.go +++ b/modules/oracle/scanner.go @@ -156,11 +156,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/pop3/scanner.go b/modules/pop3/scanner.go index bd81a88b4..677714d0b 100644 --- a/modules/pop3/scanner.go +++ b/modules/pop3/scanner.go @@ -129,11 +129,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/postgres/scanner.go b/modules/postgres/scanner.go index 81f48b0cf..d685f7ed3 100644 --- a/modules/postgres/scanner.go +++ b/modules/postgres/scanner.go @@ -295,11 +295,6 @@ func (f *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (f *Flags) Help() string { - return "" -} - // Init initializes the scanner with the given flags. func (s *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/pptp/scanner.go b/modules/pptp/scanner.go index 692436eaf..ca4e68c6c 100644 --- a/modules/pptp/scanner.go +++ b/modules/pptp/scanner.go @@ -69,11 +69,6 @@ func (f *Flags) Validate(_ []string) (err error) { return } -// Help returns this module's help string. -func (f *Flags) Help() string { - return "" -} - // Protocol returns the protocol identifier for the scanner. func (scanner *Scanner) Protocol() string { return "pptp" diff --git a/modules/redis/scanner.go b/modules/redis/scanner.go index 456fd745c..5ce3f02fe 100644 --- a/modules/redis/scanner.go +++ b/modules/redis/scanner.go @@ -193,11 +193,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string -func (flags *Flags) Help() string { - return "" -} - // Init initializes the scanner func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/siemens/scanner.go b/modules/siemens/scanner.go index ca7013ae2..9241cd365 100644 --- a/modules/siemens/scanner.go +++ b/modules/siemens/scanner.go @@ -61,11 +61,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/smb/scanner.go b/modules/smb/scanner.go index 484203cf0..fab1255b0 100644 --- a/modules/smb/scanner.go +++ b/modules/smb/scanner.go @@ -65,11 +65,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/smtp/scanner.go b/modules/smtp/scanner.go index d9ab307f2..c0ff29748 100644 --- a/modules/smtp/scanner.go +++ b/modules/smtp/scanner.go @@ -147,11 +147,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/modules/socks5/scanner.go b/modules/socks5/scanner.go index 3917f9d9c..67ce88ff2 100644 --- a/modules/socks5/scanner.go +++ b/modules/socks5/scanner.go @@ -76,11 +76,6 @@ func (f *Flags) Validate(_ []string) (err error) { return } -// Help returns this module's help string. -func (f *Flags) Help() string { - return "" -} - // Protocol returns the protocol identifier for the scanner. func (s *Scanner) Protocol() string { return "socks5" diff --git a/modules/telnet/scanner.go b/modules/telnet/scanner.go index 54046c393..d821eab61 100644 --- a/modules/telnet/scanner.go +++ b/modules/telnet/scanner.go @@ -70,11 +70,6 @@ func (flags *Flags) Validate(_ []string) error { return nil } -// Help returns the module's help string. -func (flags *Flags) Help() string { - return "" -} - // Init initializes the Scanner. func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error { f, _ := flags.(*Flags) diff --git a/multiple.ini b/multiple.ini index 8b1d99051..73e5475bf 100644 --- a/multiple.ini +++ b/multiple.ini @@ -7,3 +7,7 @@ port=123 trigger="http80" name="http80" port=80 + +[banner] +name="banner20" +port=80