107 lines
3.2 KiB
Go
107 lines
3.2 KiB
Go
package zgrab2
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
// Scanner is an interface that represents all functions necessary to run a scan
|
|
type Scanner interface {
|
|
// Init runs once for this module at library init time
|
|
Init(flags ScanFlags) error
|
|
|
|
// InitPerSender runs once per Goroutine. A single Goroutine will scan some non-deterministics
|
|
// subset of the input scan targets
|
|
InitPerSender(senderID int) error
|
|
|
|
// Returns the name passed at init
|
|
GetName() string
|
|
|
|
// Scan connects to a host. The result should be JSON-serializable
|
|
Scan(t ScanTarget, port uint) (interface{}, error)
|
|
}
|
|
|
|
// ScanModule is an interface which represents a module that the framework can
|
|
// manipulate
|
|
type ScanModule interface {
|
|
// NewFlags is called by the framework to pass to the argument parser. The parsed flags will be passed
|
|
// to the scanner created by NewScanner().
|
|
NewFlags() interface{}
|
|
|
|
// NewScanner is called by the framework for each time an individual scan is specified in the config or on
|
|
// the command-line. The framework will then call scanner.Init(name, flags).
|
|
NewScanner() Scanner
|
|
}
|
|
|
|
// 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(args []string) error
|
|
}
|
|
|
|
// BaseFlags contains the options that every flags type must embed
|
|
type BaseFlags struct {
|
|
Port uint `short:"p" long:"port" description:"Specify port to grab on"`
|
|
Name string `short:"n" long:"name" description:"Specify name for output json, only necessary if scanning multiple modules"`
|
|
Timeout uint `short:"t" long:"timeout" description:"Set connection timeout in seconds"`
|
|
}
|
|
|
|
// GetName returns the name of the respective scanner
|
|
func (b *BaseFlags) GetName() string {
|
|
return b.Name
|
|
}
|
|
|
|
// GetModule returns the registered module that corresponds to the given name
|
|
// or nil otherwise
|
|
func GetModule(name string) *ScanModule {
|
|
return modules[name]
|
|
}
|
|
|
|
var modules map[string]*ScanModule
|
|
var scanners map[string]*Scanner
|
|
var orderedScanners []string
|
|
|
|
func init() {
|
|
scanners = make(map[string]*Scanner)
|
|
modules = make(map[string]*ScanModule)
|
|
}
|
|
|
|
// RegisterScan registers each individual scanner to be ran by the framework
|
|
func RegisterScan(name string, s Scanner) {
|
|
//add to list and map
|
|
if scanners[name] != nil {
|
|
log.Fatalf("name: %s already used", name)
|
|
}
|
|
orderedScanners = append(orderedScanners, name)
|
|
scanners[name] = &s
|
|
}
|
|
|
|
// PrintScanners prints all registered scanners
|
|
func PrintScanners() {
|
|
for k, v := range scanners {
|
|
fmt.Println(k, v)
|
|
}
|
|
}
|
|
|
|
// this should be renamed?
|
|
func RunModule(s Scanner, mon *Monitor, target ScanTarget) (string, ScanResponse) {
|
|
t := time.Now()
|
|
res, e := s.Scan(target, uint(22))
|
|
var err *error //nil pointers are null in golang, which is not nil and not empty
|
|
if e == nil {
|
|
mon.statusesChan <- moduleStatus{name: s.GetName(), st: statusSuccess}
|
|
err = nil
|
|
} else {
|
|
mon.statusesChan <- moduleStatus{name: s.GetName(), st: statusFailure}
|
|
err = &e
|
|
}
|
|
resp := ScanResponse{Result: res, Error: err, Time: t.Format(time.RFC3339)}
|
|
return s.GetName(), resp
|
|
}
|