-
Notifications
You must be signed in to change notification settings - Fork 14.2k
/
Copy pathsmb_version.rb
309 lines (269 loc) · 9.79 KB
/
smb_version.rb
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'recog'
class MetasploitModule < Msf::Auxiliary
# Exploit mixins should be called first
include Msf::Exploit::Remote::DCERPC
include Msf::Exploit::Remote::SMB::Client
# Scanner mixin should be near last
include Msf::Auxiliary::Scanner
include Msf::Auxiliary::Report
SMB2_DIALECT_STRINGS = {
'0x0202' => 'SMB 2.0.2',
'0x0210' => 'SMB 2.1',
'0x0300' => 'SMB 3.0',
'0x0302' => 'SMB 3.0.2',
'0x0311' => 'SMB 3.1.1',
'0x02ff' => 'SMB 2.???'
}.freeze
def initialize
super(
'Name' => 'SMB Version Detection',
'Description' => %q{
Fingerprint and display version information about SMB servers. Protocol
information and host operating system (if available) will be reported.
Host operating system detection requires the remote server to support
version 1 of the SMB protocol. Compression and encryption capability
negotiation is only present in version 3.1.1.
},
'Author' => ['hdm', 'Spencer McIntyre', 'Christophe De La Fuente'],
'License' => MSF_LICENSE
)
deregister_options('RPORT', 'SMBDIRECT', 'SMB::ProtocolVersion')
end
def rport
@smb_port
end
def smb_direct
(@smb_port == 445)
end
def seconds_to_timespan(seconds)
timespan = []
[
['w', 60 * 60 * 24 * 7], # weeks
['d', 60 * 60 * 24], # days
['h', 60 * 60], # hours
['m', 60], # minutes
['s', 1] # seconds
].each do |spec, span|
if seconds > span || !timespan.empty?
timespan << "#{(seconds / span).floor}#{spec}"
seconds %= span
end
end
timespan.join(' ')
end
def smb_proto_info
info = {
capabilities: {},
versions: []
}
(1..3).each do |version|
begin
simple = connect(false, versions: [version])
protocol = simple.client.negotiate
rescue Rex::Proto::SMB::Exceptions::Error, RubySMB::Error::RubySMBError
next
rescue Errno::ECONNRESET
next
rescue ::Exception => e # rubocop:disable Lint/RescueException
vprint_error("#{rhost}: #{e.class} #{e}")
next
end
preferred_dialect = simple.client.dialect
if simple.client.is_a? RubySMB::Client
if preferred_dialect == '0x0311'
info[:capabilities][:compression] = simple.client.server_encryption_algorithms.map do |algorithm|
RubySMB::SMB2::CompressionCapabilities::COMPRESSION_ALGORITHM_MAP[algorithm]
end
info[:capabilities][:encryption] = simple.client.server_encryption_algorithms.map do |algorithm|
RubySMB::SMB2::EncryptionCapabilities::ENCRYPTION_ALGORITHM_MAP[algorithm]
end
end
# assume that if the server supports multiple versions, the preferred
# dialect will correspond to the latest version
preferred_dialect = SMB2_DIALECT_STRINGS[preferred_dialect]
if simple.client.server_start_time && simple.client.server_system_time
uptime = simple.client.server_system_time - simple.client.server_start_time
info[:uptime] = seconds_to_timespan(uptime)
end
info[:server_guid] = simple.client.server_guid
unless info.key? :auth_domain
begin
simple.client.authenticate
rescue RubySMB::RubySMBError
info[:auth_domain] = nil
else
info[:auth_domain] = simple.client.default_domain
end
end
end
info[:preferred_dialect] = preferred_dialect
info[:versions] << version unless protocol.nil?
end
info
end
def smb_os_description(res, nd_smb_fingerprint)
#
# Create the note hash for fingerprint.match
#
nd_fingerprint_match = {}
#
# Create a descriptive string for service.info
#
desc = res['os'].dup
if !res['edition'].to_s.empty?
desc << " #{res['edition']}"
nd_smb_fingerprint[:os_edition] = res['edition']
nd_fingerprint_match['os.edition'] = res['edition']
end
if !res['sp'].to_s.empty?
desc << " #{res['sp'].downcase.gsub('service pack ', 'SP')}"
nd_smb_fingerprint[:os_sp] = res['sp']
nd_fingerprint_match['os.version'] = res['sp']
end
if !res['build'].to_s.empty?
desc << " (build:#{res['build']})"
nd_smb_fingerprint[:os_build] = res['build']
nd_fingerprint_match['os.build'] = res['build']
end
if !res['lang'].to_s.empty? && res['lang'] != 'Unknown'
desc << " (language:#{res['lang']})"
nd_smb_fingerprint[:os_lang] = res['lang']
nd_fingerprint_match['os.language'] = nd_smb_fingerprint[:os_lang]
end
if simple.client.default_name
desc << " (name:#{simple.client.default_name})"
nd_smb_fingerprint[:SMBName] = simple.client.default_name
nd_fingerprint_match['host.name'] = nd_smb_fingerprint[:SMBName]
end
{ text: desc, fingerprint_match: nd_fingerprint_match, smb_fingerprint: nd_smb_fingerprint }
end
#
# Fingerprint a single host
#
def run_host(ip)
smb_ports = [445, 139]
lines = [] # defer status output to the very end to group lines together by host
smb_ports.each do |pnum|
@smb_port = pnum
self.simple = nil
begin
res = smb_fingerprint
info = smb_proto_info
desc = "SMB Detected (versions:#{info[:versions].join(', ')}) (preferred dialect:#{info[:preferred_dialect]})"
info[:capabilities].each do |name, values|
desc << " (#{name} capabilities:#{values.join(', ')})"
end
if simple.client.peer_require_signing
desc << ' (signatures:required)'
else
desc << ' (signatures:optional)'
report_vuln({
host: ip,
port: rport,
proto: 'tcp',
name: 'SMB Signing Is Not Required',
refs: [
SiteReference.new('URL', 'https://support.microsoft.com/en-us/help/161372/how-to-enable-smb-signing-in-windows-nt'),
SiteReference.new('URL', 'https://support.microsoft.com/en-us/help/887429/overview-of-server-message-block-signing'),
]
})
end
desc << " (uptime:#{info[:uptime]})" if info[:uptime]
desc << " (guid:#{Rex::Text.to_guid(info[:server_guid])})" if info[:server_guid]
desc << " (authentication domain:#{info[:auth_domain]})" if info[:auth_domain]
lines << { type: :status, message: desc }
#
# Create the note hash for smb.fingerprint
#
nd_smb_fingerprint = {
native_os: res['native_os'],
native_lm: res['native_lm']
}
if res['os'] && res['os'] != 'Unknown'
description = smb_os_description(res, nd_smb_fingerprint)
desc = description[:text]
nd_fingerprint_match = description[:fingerprint_match]
nd_smb_fingerprint = description[:smb_fingerprint]
if simple.client.default_domain
if simple.client.default_domain.encoding.name == 'UTF-8'
desc << " (domain:#{simple.client.default_domain})"
else
# Workgroup names are in ANSI, but may contain invalid characters
# Go through each char and convert/check
temp_workgroup = simple.client.default_domain.dup
desc << ' (workgroup:'
temp_workgroup.each_char do |i|
begin
desc << i.encode('UTF-8')
rescue Encoding::UndefinedConversionError # rubocop:disable Metrics/BlockNesting
desc << '?'
end
end
desc << ')'
end
nd_smb_fingerprint[:SMBDomain] = simple.client.default_domain
nd_fingerprint_match['host.domain'] = nd_smb_fingerprint[:SMBDomain]
end
lines << { type: :good, message: " Host is running #{desc}" }
# Report the service with a friendly banner
report_service(
host: ip,
port: rport,
proto: 'tcp',
name: 'smb',
info: desc
)
# Report a fingerprint.match hash for name, domain, and language
# Ignore OS fields, as those are handled via smb.fingerprint
report_note(
host: ip,
port: rport,
proto: 'tcp',
ntype: 'fingerprint.match',
data: nd_fingerprint_match
)
elsif res['native_os'] || res['native_lm']
desc = "#{res['native_os']} (#{res['native_lm']})"
report_service(host: ip, port: rport, name: 'smb', info: desc)
lines << { type: :status, message: " Host could not be identified: #{desc}" }
else
lines << { type: :status, message: ' Host could not be identified', verbose: true }
end
# Report a smb.fingerprint hash of attributes for OS fingerprinting
report_note(
host: ip,
port: rport,
proto: 'tcp',
ntype: 'smb.fingerprint',
data: nd_smb_fingerprint
)
disconnect
break
rescue ::Rex::Proto::SMB::Exceptions::NoReply
next
rescue ::Rex::Proto::SMB::Exceptions::ErrorCode
next
rescue ::Rex::Proto::SMB::Exceptions::LoginError => e
# Vista has 139 open but doesnt like *SMBSERVER
next if e.to_s =~ /server refused our NetBIOS/
break
rescue ::Timeout::Error
next
rescue ::Rex::ConnectionError
next
rescue ::Exception => e # rubocop:disable Lint/RescueException
print_error("#{rhost}: #{e.class} #{e}")
ensure
disconnect
lines.each do |line|
send "#{ line[:verbose] ? 'v' : '' }print_#{line[:type]}", line[:message]
end
lines = []
end
end
end
end