-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
134 lines (116 loc) · 4 KB
/
scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Package smb provides a zgrab2 module that scans for smb.
// This was ported directly from zgrab.
package smb
import (
log "github.com/sirupsen/logrus"
"github.com/dmin12/zgrab_tls1.3"
"github.com/dmin12/zgrab_tls1.3/lib/smb/smb"
)
// Flags holds the command-line configuration for the smb scan module.
// Populated by the framework.
type Flags struct {
zgrab2.BaseFlags
// SetupSession tells the client to continue the handshake up to the point where credentials would be needed.
SetupSession bool `long:"setup-session" description:"After getting the response from the negotiation request, send a setup session packet."`
// Verbose requests more verbose logging / output.
Verbose bool `long:"verbose" description:"More verbose logging, include debug fields in the scan results"`
}
// Module implements the zgrab2.Module interface.
type Module struct {
}
// Scanner implements the zgrab2.Scanner interface.
type Scanner struct {
config *Flags
}
// RegisterModule registers the zgrab2 module.
func RegisterModule() {
var module Module
_, err := zgrab2.AddCommand("smb", "smb", module.Description(), 445, &module)
if err != nil {
log.Fatal(err)
}
}
// NewFlags returns a default Flags object.
func (module *Module) NewFlags() interface{} {
return new(Flags)
}
// NewScanner returns a new Scanner instance.
func (module *Module) NewScanner() zgrab2.Scanner {
return new(Scanner)
}
// Description returns an overview of this module.
func (module *Module) Description() string {
return "Probe for SMB servers (Windows filesharing / SAMBA)"
}
// Validate checks that the flags are valid.
// On success, returns nil.
// On failure, returns an error instance describing the error.
func (flags *Flags) Validate(args []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)
scanner.config = f
return nil
}
// InitPerSender initializes the scanner for a given sender.
func (scanner *Scanner) InitPerSender(senderID int) error {
return nil
}
// GetName returns the Scanner name defined in the Flags.
func (scanner *Scanner) GetName() string {
return scanner.config.Name
}
// GetTrigger returns the Trigger defined in the Flags.
func (scanner *Scanner) GetTrigger() string {
return scanner.config.Trigger
}
// Protocol returns the protocol identifier of the scan.
func (scanner *Scanner) Protocol() string {
return "smb"
}
// Scan performs the following:
// 1. Connect to the TCP port (default 445).
// 2. Send a negotiation packet with the default values:
// Dialects = { DialectSmb_2_1 },
// SecurityMode = SecurityModeSigningEnabled
// 3. Read response from server; on failure, exit with log = nil.
// If the server returns a protocol ID indicating support for version 1, set smbv1_support = true
// Pull out the relevant information from the response packet
// 4. If --setup-session is not set, exit with success.
// 5. Send a setup session packet to the server with appropriate values
// 6. Read the response from the server; on failure, exit with the log so far.
// 7. Return the log.
func (scanner *Scanner) Scan(target zgrab2.ScanTarget) (zgrab2.ScanStatus, interface{}, error) {
conn, err := target.Open(&scanner.config.BaseFlags)
if err != nil {
return zgrab2.TryGetScanStatus(err), nil, err
}
defer conn.Close()
var result *smb.SMBLog
setupSession := scanner.config.SetupSession
verbose := scanner.config.Verbose
result, err = smb.GetSMBLog(conn, setupSession, false, verbose)
if err != nil {
if result == nil {
conn.Close()
conn, err = target.Open(&scanner.config.BaseFlags)
if err != nil {
return zgrab2.TryGetScanStatus(err), nil, err
}
defer conn.Close()
result, err = smb.GetSMBLog(conn, setupSession, true, verbose)
if err != nil {
return zgrab2.TryGetScanStatus(err), result, err
}
} else {
return zgrab2.TryGetScanStatus(err), result, err
}
}
return zgrab2.SCAN_SUCCESS, result, nil
}