zgrab2/module.go

96 lines
2.6 KiB
Go
Raw Normal View History

package zgrab2
import (
2017-09-26 18:02:27 +00:00
"fmt"
"log"
"time"
)
2017-09-26 18:02:27 +00:00
type Scanner interface {
// Init runs once for this module at library init time
Init(flags ScanFlags) error
2017-09-26 18:02:27 +00:00
// 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
2017-09-26 18:02:27 +00:00
// Scan connects to a host. The result should be JSON-serializable
Scan(t ScanTarget, port uint) (interface{}, error)
}
2017-09-26 18:02:27 +00:00
type ScanModule interface {
// Called by the framework to pass to the argument parser. The parsed flags will be passed
// to the scanner created by NewScanner().
NewFlags() interface{}
// 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
}
2017-09-26 18:02:27 +00:00
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
}
2017-09-26 18:02:27 +00:00
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"`
}
2017-09-26 18:02:27 +00:00
func (b *BaseFlags) GetName() string {
return b.Name
}
func GetModule(name string) *ScanModule {
return modules[name]
}
var modules map[string]*ScanModule
2017-09-26 18:02:27 +00:00
var scanners map[string]*Scanner
var orderedScanners []string
func init() {
2017-09-26 18:02:27 +00:00
scanners = make(map[string]*Scanner)
modules = make(map[string]*ScanModule)
}
func RegisterScan(name string, s Scanner) {
//add to list and map
2017-09-26 18:02:27 +00:00
if scanners[name] != nil {
log.Fatalf("name: %s already used", name)
}
2017-09-26 18:02:27 +00:00
orderedScanners = append(orderedScanners, name)
scanners[name] = &s
fmt.Println("Registered: ", name, s)
}
func PrintScanners() {
for k, v := range scanners {
fmt.Println(k, v)
}
}
2017-09-26 18:02:27 +00:00
func RunModule(s Scanner, mon *Monitor, target ScanTarget) (string, ScanResponse) {
t := time.Now()
2017-09-26 18:02:27 +00:00
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 {
2017-09-26 18:02:27 +00:00
mon.statusesChan <- moduleStatus{name: s.GetName(), st: status_success}
err = nil
} else {
2017-09-26 18:02:27 +00:00
mon.statusesChan <- moduleStatus{name: s.GetName(), st: status_failure}
err = &e
}
2017-09-26 18:02:27 +00:00
resp := ScanResponse{Result: res, Error: err, Time: t.Format(time.RFC3339)}
return s.GetName(), resp
}