Skip to content
Draft
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
67 changes: 64 additions & 3 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"reflect"
"strconv"
"time"

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

Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 0 additions & 5 deletions modules/amqp091/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions modules/bacnet/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 25 additions & 32 deletions modules/banner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"io"
"net"
"os"
"reflect"
"regexp"
"strconv"
"time"
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -92,25 +102,18 @@ 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.
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 != "" {
Expand All @@ -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
Expand All @@ -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
}
Expand Down
5 changes: 0 additions & 5 deletions modules/dnp3/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions modules/fox/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions modules/ftp/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 0 additions & 5 deletions modules/http/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 0 additions & 5 deletions modules/imap/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions modules/ipp/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions modules/jarm/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions modules/modbus/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions modules/mongodb/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions modules/mqtt/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 0 additions & 5 deletions modules/mssql/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions modules/mysql/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions modules/ntp/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading
Loading