ruby_smb 2.0.8 → 2.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/workflows/verify.yml +5 -15
  4. data/examples/auth_capture.rb +71 -0
  5. data/lib/ruby_smb/client/negotiation.rb +9 -11
  6. data/lib/ruby_smb/client.rb +30 -25
  7. data/lib/ruby_smb/dialect.rb +45 -0
  8. data/lib/ruby_smb/dispatcher/base.rb +1 -1
  9. data/lib/ruby_smb/gss/provider/authenticator.rb +42 -0
  10. data/lib/ruby_smb/gss/provider/ntlm.rb +303 -0
  11. data/lib/ruby_smb/gss/provider.rb +35 -0
  12. data/lib/ruby_smb/gss.rb +56 -63
  13. data/lib/ruby_smb/ntlm.rb +45 -0
  14. data/lib/ruby_smb/server/server_client/negotiation.rb +156 -0
  15. data/lib/ruby_smb/server/server_client/session_setup.rb +82 -0
  16. data/lib/ruby_smb/server/server_client.rb +162 -0
  17. data/lib/ruby_smb/server.rb +54 -0
  18. data/lib/ruby_smb/signing.rb +59 -0
  19. data/lib/ruby_smb/smb1/packet/negotiate_response.rb +11 -11
  20. data/lib/ruby_smb/smb1/packet/negotiate_response_extended.rb +1 -1
  21. data/lib/ruby_smb/smb1/packet/session_setup_request.rb +1 -1
  22. data/lib/ruby_smb/smb1/tree.rb +1 -1
  23. data/lib/ruby_smb/smb2/negotiate_context.rb +18 -2
  24. data/lib/ruby_smb/smb2/packet/negotiate_request.rb +9 -0
  25. data/lib/ruby_smb/smb2/packet/negotiate_response.rb +0 -1
  26. data/lib/ruby_smb/smb2/packet/session_setup_response.rb +2 -2
  27. data/lib/ruby_smb/smb2/packet/tree_connect_request.rb +1 -1
  28. data/lib/ruby_smb/smb2/tree.rb +1 -1
  29. data/lib/ruby_smb/smb2.rb +3 -1
  30. data/lib/ruby_smb/version.rb +1 -1
  31. data/lib/ruby_smb.rb +2 -1
  32. data/spec/lib/ruby_smb/client_spec.rb +24 -16
  33. data/spec/lib/ruby_smb/gss/provider/ntlm/account_spec.rb +32 -0
  34. data/spec/lib/ruby_smb/gss/provider/ntlm/authenticator_spec.rb +101 -0
  35. data/spec/lib/ruby_smb/gss/provider/ntlm/os_version_spec.rb +32 -0
  36. data/spec/lib/ruby_smb/gss/provider/ntlm_spec.rb +113 -0
  37. data/spec/lib/ruby_smb/server/server_client_spec.rb +156 -0
  38. data/spec/lib/ruby_smb/server_spec.rb +32 -0
  39. data/spec/lib/ruby_smb/smb1/tree_spec.rb +4 -4
  40. data/spec/lib/ruby_smb/smb2/negotiate_context_spec.rb +2 -2
  41. data/spec/lib/ruby_smb/smb2/tree_spec.rb +5 -5
  42. data.tar.gz.sig +0 -0
  43. metadata +25 -3
  44. metadata.gz.sig +0 -0
  45. data/lib/ruby_smb/client/signing.rb +0 -64
@@ -0,0 +1,303 @@
1
+ require 'ruby_smb/ntlm'
2
+
3
+ module RubySMB
4
+ module Gss
5
+ module Provider
6
+ #
7
+ # A GSS provider that authenticates clients via the NT LAN Manager (NTLM) Security Support Provider (NTLMSSP)
8
+ # protocol.
9
+ #
10
+ class NTLM < Base
11
+ include RubySMB::NTLM
12
+
13
+ # An account representing an identity for which this provider will accept authentication attempts.
14
+ Account = Struct.new(:username, :password, :domain) do
15
+ def to_s
16
+ "#{domain}\\#{username}"
17
+ end
18
+ end
19
+
20
+ class Authenticator < Authenticator::Base
21
+ def reset!
22
+ super
23
+ @server_challenge = nil
24
+ end
25
+
26
+ def process(request_buffer=nil)
27
+ if request_buffer.nil?
28
+ # this is only NTLMSSP (as opposed to SPNEGO + NTLMSSP)
29
+ buffer = OpenSSL::ASN1::ASN1Data.new([
30
+ Gss::OID_SPNEGO,
31
+ OpenSSL::ASN1::ASN1Data.new([
32
+ OpenSSL::ASN1::Sequence.new([
33
+ OpenSSL::ASN1::ASN1Data.new([
34
+ OpenSSL::ASN1::Sequence.new([
35
+ Gss::OID_NTLMSSP
36
+ ])
37
+ ], 0, :CONTEXT_SPECIFIC),
38
+ OpenSSL::ASN1::ASN1Data.new([
39
+ OpenSSL::ASN1::ASN1Data.new([
40
+ OpenSSL::ASN1::ASN1Data.new([
41
+ OpenSSL::ASN1::GeneralString.new('not_defined_in_RFC4178@please_ignore')
42
+ ], 0, :CONTEXT_SPECIFIC)
43
+ ], 16, :UNIVERSAL)
44
+ ], 3, :CONTEXT_SPECIFIC)
45
+ ])
46
+ ], 0, :CONTEXT_SPECIFIC)
47
+ ], 0, :APPLICATION).to_der
48
+ return Result.new(buffer, WindowsError::NTStatus::STATUS_SUCCESS)
49
+ end
50
+
51
+ begin
52
+ gss_api = OpenSSL::ASN1.decode(request_buffer)
53
+ rescue OpenSSL::ASN1::ASN1Error
54
+ return
55
+ end
56
+
57
+ if gss_api&.tag == 0 && gss_api&.tag_class == :APPLICATION
58
+ result = process_gss_type1(gss_api)
59
+ elsif gss_api&.tag == 1 && gss_api&.tag_class == :CONTEXT_SPECIFIC
60
+ result = process_gss_type3(gss_api)
61
+ end
62
+
63
+ result
64
+ end
65
+
66
+ #
67
+ # Process the NTLM type 1 message and build a type 2 response message.
68
+ #
69
+ # @param [Net::NTLM::Message::Type1] type1_msg the NTLM type 1 message received by the client that should be
70
+ # processed
71
+ # @return [Net::NTLM::Message::Type2] the NTLM type 2 response message with which to reply to the client
72
+ def process_ntlm_type1(type1_msg)
73
+ type2_msg = Net::NTLM::Message::Type2.new.tap do |msg|
74
+ msg.target_name = 'LOCALHOST'.encode('UTF-16LE').b
75
+ msg.flag = 0
76
+ %i{ KEY56 KEY128 KEY_EXCHANGE UNICODE TARGET_INFO VERSION_INFO }.each do |flag|
77
+ msg.flag |= NTLM::NEGOTIATE_FLAGS.fetch(flag)
78
+ end
79
+
80
+ @server_challenge = @provider.generate_server_challenge
81
+ msg.challenge = @server_challenge.unpack1('Q<') # 64-bit unsigned, little endian (uint64_t)
82
+ target_info = Net::NTLM::TargetInfo.new('')
83
+ target_info.av_pairs.merge!({
84
+ Net::NTLM::TargetInfo::MSV_AV_NB_DOMAIN_NAME => @provider.netbios_domain.encode('UTF-16LE').b,
85
+ Net::NTLM::TargetInfo::MSV_AV_NB_COMPUTER_NAME => @provider.netbios_hostname.encode('UTF-16LE').b,
86
+ Net::NTLM::TargetInfo::MSV_AV_DNS_DOMAIN_NAME => @provider.dns_domain.encode('UTF-16LE').b,
87
+ Net::NTLM::TargetInfo::MSV_AV_DNS_COMPUTER_NAME => @provider.dns_hostname.encode('UTF-16LE').b,
88
+ Net::NTLM::TargetInfo::MSV_AV_TIMESTAMP => [(Time.now.to_i + Net::NTLM::TIME_OFFSET) * Field::FileTime::NS_MULTIPLIER].pack('Q')
89
+ })
90
+ msg.target_info = target_info.to_s
91
+ msg.enable(:target_info)
92
+ msg.context = 0
93
+ msg.enable(:context)
94
+ msg.os_version = NTLM::OSVersion.new(major: 6, minor: 3).to_binary_s
95
+ msg.enable(:os_version)
96
+ end
97
+
98
+ type2_msg
99
+ end
100
+
101
+ #
102
+ # Process the NTLM type 3 message and either accept or reject the authentication attempt.
103
+ #
104
+ # @param [Net::NTLM::Message::Type3] type3_msg the NTLM type 3 message received by the client that should be
105
+ # processed
106
+ # @return [WindowsError::ErrorCode] an NT Status error code representing the operations outcome where
107
+ # STATUS_SUCCESS is a successful authentication attempt and anything else is a failure
108
+ def process_ntlm_type3(type3_msg)
109
+ if type3_msg.user == '' && type3_msg.domain == ''
110
+ if @provider.allow_anonymous
111
+ return WindowsError::NTStatus::STATUS_SUCCESS
112
+ end
113
+
114
+ return WindowsError::NTStatus::STATUS_LOGON_FAILURE
115
+ end
116
+
117
+ account = @provider.get_account(
118
+ type3_msg.user,
119
+ domain: type3_msg.domain
120
+ )
121
+ return WindowsError::NTStatus::STATUS_LOGON_FAILURE if account.nil?
122
+
123
+ matches = false
124
+ case type3_msg.ntlm_version
125
+ when :ntlmv1
126
+ my_ntlm_response = Net::NTLM::ntlm_response(
127
+ ntlm_hash: Net::NTLM::ntlm_hash(account.password.encode('UTF-16LE'), unicode: true),
128
+ challenge: @server_challenge
129
+ )
130
+ matches = my_ntlm_response == type3_msg.ntlm_response
131
+ when :ntlmv2
132
+ digest = OpenSSL::Digest::MD5.new
133
+ their_nt_proof_str = type3_msg.ntlm_response[0...digest.digest_length]
134
+ their_blob = type3_msg.ntlm_response[digest.digest_length..-1]
135
+
136
+ ntlmv2_hash = Net::NTLM.ntlmv2_hash(
137
+ account.username.encode('UTF-16LE'),
138
+ account.password.encode('UTF-16LE'),
139
+ type3_msg.domain.encode('UTF-16LE'), # don't use the account domain because of the special '.' value
140
+ {client_challenge: their_blob[16...24], unicode: true}
141
+ )
142
+
143
+ my_nt_proof_str = OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, ntlmv2_hash, @server_challenge + their_blob)
144
+ matches = my_nt_proof_str == their_nt_proof_str
145
+ if matches
146
+ user_session_key = OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, ntlmv2_hash, my_nt_proof_str)
147
+ if type3_msg.flag & NTLM::NEGOTIATE_FLAGS[:KEY_EXCHANGE] == NTLM::NEGOTIATE_FLAGS[:KEY_EXCHANGE] && type3_msg.session_key.length == 16
148
+ rc4 = OpenSSL::Cipher.new('rc4')
149
+ rc4.decrypt
150
+ rc4.key = user_session_key
151
+ @session_key = rc4.update type3_msg.session_key
152
+ @session_key << rc4.final
153
+ else
154
+ @session_key = user_session_key
155
+ end
156
+ end
157
+ else
158
+ # the only other value Net::NTLM will return for this is ntlm_session
159
+ raise NotImplementedError, "authentication via ntlm version #{type3_msg.ntlm_version} is not supported"
160
+ end
161
+
162
+ return WindowsError::NTStatus::STATUS_LOGON_FAILURE unless matches
163
+
164
+ WindowsError::NTStatus::STATUS_SUCCESS
165
+ end
166
+
167
+ attr_accessor :server_challenge
168
+
169
+ private
170
+
171
+ # take the GSS blob, extract the NTLM type 1 message and pass it to the process method to build the response
172
+ # which is then put back into a new GSS reply-blob
173
+ def process_gss_type1(gss_api)
174
+ unless Gss.asn1dig(gss_api, 1, 0, 0, 0, 0)&.value == Gss::OID_NTLMSSP.value
175
+ return
176
+ end
177
+
178
+ raw_type1_msg = Gss.asn1dig(gss_api, 1, 0, 1, 0)&.value
179
+ return unless raw_type1_msg
180
+
181
+ type1_msg = Net::NTLM::Message.parse(raw_type1_msg)
182
+ if type1_msg.flag & NTLM::NEGOTIATE_FLAGS[:UNICODE] == NTLM::NEGOTIATE_FLAGS[:UNICODE]
183
+ type1_msg.domain.force_encoding('UTF-16LE')
184
+ type1_msg.workstation.force_encoding('UTF-16LE')
185
+ end
186
+ type2_msg = process_ntlm_type1(type1_msg)
187
+
188
+ Result.new(Gss.gss_type2(type2_msg.serialize), WindowsError::NTStatus::STATUS_MORE_PROCESSING_REQUIRED)
189
+ end
190
+
191
+ # take the GSS blob, extract the NTLM type 3 message and pass it to the process method to build the response
192
+ # which is then put back into a new GSS reply-blob
193
+ def process_gss_type3(gss_api)
194
+ neg_token_init = Hash[RubySMB::Gss.asn1dig(gss_api, 0).value.map { |obj| [obj.tag, obj.value[0].value] }]
195
+ raw_type3_msg = neg_token_init[2]
196
+
197
+ type3_msg = Net::NTLM::Message.parse(raw_type3_msg)
198
+ if type3_msg.flag & NTLM::NEGOTIATE_FLAGS[:UNICODE] == NTLM::NEGOTIATE_FLAGS[:UNICODE]
199
+ type3_msg.domain.force_encoding('UTF-16LE')
200
+ type3_msg.user.force_encoding('UTF-16LE')
201
+ type3_msg.workstation.force_encoding('UTF-16LE')
202
+ end
203
+
204
+ nt_status = process_ntlm_type3(type3_msg)
205
+ buffer = identity = nil
206
+
207
+ case nt_status
208
+ when WindowsError::NTStatus::STATUS_SUCCESS
209
+ buffer = OpenSSL::ASN1::ASN1Data.new([
210
+ OpenSSL::ASN1::Sequence.new([
211
+ OpenSSL::ASN1::ASN1Data.new([
212
+ OpenSSL::ASN1::Enumerated.new(OpenSSL::BN.new(0)),
213
+ ], 0, :CONTEXT_SPECIFIC)
214
+ ])
215
+ ], 1, :CONTEXT_SPECIFIC).to_der
216
+
217
+ account = @provider.get_account(
218
+ type3_msg.user,
219
+ domain: type3_msg.domain
220
+ )
221
+ if account.nil?
222
+ if @provider.allow_anonymous
223
+ identity = IDENTITY_ANONYMOUS
224
+ end
225
+ else
226
+ identity = account.to_s
227
+ end
228
+ end
229
+
230
+ Result.new(buffer, nt_status, identity)
231
+ end
232
+ end
233
+
234
+ # @param [Boolean] allow_anonymous whether or not to allow anonymous authentication attempts
235
+ # @param [String] default_domain the default domain to use for authentication, unless specified 'WORKGROUP' will
236
+ # be used
237
+ def initialize(allow_anonymous: false, default_domain: 'WORKGROUP')
238
+ raise ArgumentError, 'Must specify a default domain' unless default_domain
239
+
240
+ @allow_anonymous = allow_anonymous
241
+ @default_domain = default_domain
242
+ @accounts = []
243
+ @generate_server_challenge = -> { SecureRandom.bytes(8) }
244
+
245
+ @dns_domain = @netbios_domain = 'LOCALDOMAIN'
246
+ @dns_hostname = @netbios_hostname = 'LOCALHOST'
247
+ end
248
+
249
+ #
250
+ # Generate the 8-byte server challenge. If a block is specified, it's used as the challenge generation routine
251
+ # and should return an 8-byte value.
252
+ #
253
+ # @return [String] an 8-byte challenge value
254
+ def generate_server_challenge(&block)
255
+ if block.nil?
256
+ @generate_server_challenge.call
257
+ else
258
+ @generate_server_challenge = block
259
+ end
260
+ end
261
+
262
+ def new_authenticator(server_client)
263
+ # build and return an instance that can process and track stateful information for a particular connection but
264
+ # that's backed by this particular provider
265
+ Authenticator.new(self, server_client)
266
+ end
267
+
268
+ #
269
+ # Lookup and return an account based on the username and optionally, the domain. If no domain is specified or
270
+ # or it is the special value '.', the default domain will be used. The username and domain values are case
271
+ # insensitive.
272
+ #
273
+ # @param [String] username the username of the account to fetch.
274
+ # @param [String, nil] domain the domain in which the account to fetch exists.
275
+ # @return [Account, nil] the account if it was found
276
+ def get_account(username, domain: nil)
277
+ # the username and password values should use the native encoding for the comparison in the #find operation
278
+ username = username.downcase
279
+ domain = @default_domain if domain.nil? || domain == '.'.encode(domain.encoding)
280
+ domain = domain.downcase
281
+ @accounts.find { |account| account.username.encode(username.encoding).downcase == username && account.domain.encode(domain.encoding).downcase == domain }
282
+ end
283
+
284
+ #
285
+ # Add an account to the database.
286
+ #
287
+ # @param [String] username the username of the account to add
288
+ # @param [String] password either the plaintext password or the NTLM hash of the account to add
289
+ # @param [String] domain the domain of the account to add, if not specified, the @default_domain will be used
290
+ def put_account(username, password, domain: nil)
291
+ domain = @default_domain if domain.nil? || domain == '.'.encode(domain.encoding)
292
+ @accounts << Account.new(username, password, domain)
293
+ end
294
+
295
+ #
296
+ # The default domain value to use for accounts which do not have one specified or use the special '.' value.
297
+ attr_reader :default_domain
298
+
299
+ attr_accessor :dns_domain, :dns_hostname, :netbios_domain, :netbios_hostname
300
+ end
301
+ end
302
+ end
303
+ end
@@ -0,0 +1,35 @@
1
+ module RubySMB
2
+ module Gss
3
+ #
4
+ # This module provides GSS based authentication.
5
+ #
6
+ module Provider
7
+ # A special constant implying that the authenticated user is anonymous.
8
+ IDENTITY_ANONYMOUS = :anonymous
9
+ # The result of a processed GSS request.
10
+ Result = Struct.new(:buffer, :nt_status, :identity)
11
+
12
+ #
13
+ # The base class for a GSS authentication provider. This class defines a common interface and is not usable as a
14
+ # provider on its own.
15
+ #
16
+ class Base
17
+ # Create a new, client-specific authenticator instance. This new instance is then able to track the unique state
18
+ # of a particular client / connection.
19
+ #
20
+ # @param [Server::ServerClient] server_client the client instance that this the authenticator will be for
21
+ def new_authenticator(server_client)
22
+ raise NotImplementedError
23
+ end
24
+
25
+ #
26
+ # Whether or not anonymous authentication attempts should be permitted.
27
+ #
28
+ attr_accessor :allow_anonymous
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ require 'ruby_smb/gss/provider/authenticator'
35
+ require 'ruby_smb/gss/provider/ntlm'
data/lib/ruby_smb/gss.rb CHANGED
@@ -2,6 +2,27 @@ module RubySMB
2
2
  # module containing methods required for using the [GSS-API](http://www.rfc-editor.org/rfc/rfc2743.txt)
3
3
  # for Secure Protected Negotiation(SPNEGO) in SMB Authentication.
4
4
  module Gss
5
+ require 'ruby_smb/gss/provider'
6
+
7
+ OID_SPNEGO = OpenSSL::ASN1::ObjectId.new('1.3.6.1.5.5.2')
8
+ OID_NEGOEX = OpenSSL::ASN1::ObjectId.new('1.3.6.1.4.1.311.2.2.30')
9
+ OID_NTLMSSP = OpenSSL::ASN1::ObjectId.new('1.3.6.1.4.1.311.2.2.10')
10
+
11
+ # Allow safe navigation of a decoded ASN.1 data structure. Similar to Ruby's
12
+ # builtin Hash#dig method but using the #value attribute of each ASN object.
13
+ #
14
+ # @param asn The ASN object to apply the traversal path on.
15
+ # @param [Array] path The path to traverse, each element is passed to the
16
+ # ASN object's #value's #[] operator.
17
+ def self.asn1dig(asn, *path)
18
+ path.each do |part|
19
+ return nil unless asn&.value
20
+ asn = asn.value[part]
21
+ end
22
+
23
+ asn
24
+ end
25
+
5
26
  # Cargo culted from Rex. Hacked Together ASN1 encoding that works for our GSS purposes
6
27
  # @todo Document these magic numbers
7
28
  def self.asn1encode(str = '')
@@ -25,78 +46,50 @@ module RubySMB
25
46
  end
26
47
 
27
48
  # Create a GSS Security Blob of an NTLM Type 1 Message.
28
- # This code has been cargo culted and needs to be researched
29
- # and refactored into something better later.
30
- # @todo Refactor this into non-magical code
31
49
  def self.gss_type1(type1)
32
- "\x60".force_encoding('binary') + asn1encode(
33
- "\x06".force_encoding('binary') + asn1encode(
34
- "\x2b\x06\x01\x05\x05\x02".force_encoding('binary')
35
- ) +
36
- "\xa0".force_encoding('binary') + asn1encode(
37
- "\x30".force_encoding('binary') + asn1encode(
38
- "\xa0".force_encoding('binary') + asn1encode(
39
- "\x30".force_encoding('binary') + asn1encode(
40
- "\x06".force_encoding('binary') + asn1encode(
41
- "\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a".force_encoding('binary')
42
- )
43
- )
44
- ) +
45
- "\xa2".force_encoding('binary') + asn1encode(
46
- "\x04".force_encoding('binary') + asn1encode(
47
- type1
48
- )
49
- )
50
- )
51
- )
52
- )
50
+ OpenSSL::ASN1::ASN1Data.new([
51
+ OID_SPNEGO,
52
+ OpenSSL::ASN1::ASN1Data.new([
53
+ OpenSSL::ASN1::Sequence.new([
54
+ OpenSSL::ASN1::ASN1Data.new([
55
+ OpenSSL::ASN1::Sequence.new([
56
+ OID_NTLMSSP
57
+ ])
58
+ ], 0, :CONTEXT_SPECIFIC),
59
+ OpenSSL::ASN1::ASN1Data.new([
60
+ OpenSSL::ASN1::OctetString.new(type1)
61
+ ], 2, :CONTEXT_SPECIFIC)
62
+ ])
63
+ ], 0, :CONTEXT_SPECIFIC)
64
+ ], 0, :APPLICATION).to_der
53
65
  end
54
66
 
55
67
  # Create a GSS Security Blob of an NTLM Type 2 Message.
56
- # This code has been cargo culted and needs to be researched
57
- # and refactored into something better later.
58
68
  def self.gss_type2(type2)
59
- blob =
60
- "\xa1" + asn1encode(
61
- "\x30" + asn1encode(
62
- "\xa0" + asn1encode(
63
- "\x0a" + asn1encode(
64
- "\x01"
65
- )
66
- ) +
67
- "\xa1" + asn1encode(
68
- "\x06" + asn1encode(
69
- "\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a"
70
- )
71
- ) +
72
- "\xa2" + asn1encode(
73
- "\x04" + asn1encode(
74
- type2
75
- )
76
- )
77
- )
78
- )
79
-
80
- blob
69
+ OpenSSL::ASN1::ASN1Data.new([
70
+ OpenSSL::ASN1::Sequence.new([
71
+ OpenSSL::ASN1::ASN1Data.new([
72
+ OpenSSL::ASN1::Enumerated.new(OpenSSL::BN.new(1))
73
+ ], 0, :CONTEXT_SPECIFIC),
74
+ OpenSSL::ASN1::ASN1Data.new([
75
+ OID_NTLMSSP
76
+ ], 1, :CONTEXT_SPECIFIC),
77
+ OpenSSL::ASN1::ASN1Data.new([
78
+ OpenSSL::ASN1::OctetString.new(type2)
79
+ ], 2, :CONTEXT_SPECIFIC)
80
+ ])
81
+ ], 1, :CONTEXT_SPECIFIC).to_der
81
82
  end
82
83
 
83
84
  # Create a GSS Security Blob of an NTLM Type 3 Message.
84
- # This code has been cargo culted and needs to be researched
85
- # and refactored into something better later.
86
- # @todo Refactor this into non-magical code
87
85
  def self.gss_type3(type3)
88
- gss =
89
- "\xa1".force_encoding('binary') + asn1encode(
90
- "\x30".force_encoding('binary') + asn1encode(
91
- "\xa2".force_encoding('binary') + asn1encode(
92
- "\x04".force_encoding('binary') + asn1encode(
93
- type3
94
- )
95
- )
96
- )
97
- )
98
-
99
- gss
86
+ OpenSSL::ASN1::ASN1Data.new([
87
+ OpenSSL::ASN1::Sequence.new([
88
+ OpenSSL::ASN1::ASN1Data.new([
89
+ OpenSSL::ASN1::OctetString.new(type3)
90
+ ], 2, :CONTEXT_SPECIFIC)
91
+ ])
92
+ ], 1, :CONTEXT_SPECIFIC).to_der
100
93
  end
101
94
  end
102
95
  end
@@ -0,0 +1,45 @@
1
+ module RubySMB
2
+ module NTLM
3
+ # [[MS-NLMP] 2.2.2.5](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/99d90ff4-957f-4c8a-80e4-5bfe5a9a9832)
4
+ NEGOTIATE_FLAGS = {
5
+ :UNICODE => 1 << 0,
6
+ :OEM => 1 << 1,
7
+ :REQUEST_TARGET => 1 << 2,
8
+ :SIGN => 1 << 4,
9
+ :SEAL => 1 << 5,
10
+ :DATAGRAM => 1 << 6,
11
+ :LAN_MANAGER_KEY => 1 << 7,
12
+ :NTLM => 1 << 9,
13
+ :NT_ONLY => 1 << 10,
14
+ :ANONYMOUS => 1 << 11,
15
+ :OEM_DOMAIN_SUPPLIED => 1 << 12,
16
+ :OEM_WORKSTATION_SUPPLIED => 1 << 13,
17
+ :ALWAYS_SIGN => 1 << 15,
18
+ :TARGET_TYPE_DOMAIN => 1 << 16,
19
+ :TARGET_TYPE_SERVER => 1 << 17,
20
+ :TARGET_TYPE_SHARE => 1 << 18,
21
+ :EXTENDED_SECURITY => 1 << 19,
22
+ :IDENTIFY => 1 << 20,
23
+ :NON_NT_SESSION => 1 << 22,
24
+ :TARGET_INFO => 1 << 23,
25
+ :VERSION_INFO => 1 << 25,
26
+ :KEY128 => 1 << 29,
27
+ :KEY_EXCHANGE => 1 << 30,
28
+ :KEY56 => 1 << 31
29
+ }.freeze
30
+
31
+ class OSVersion < BinData::Record
32
+ endian :big
33
+
34
+ uint8 :major
35
+ uint8 :minor
36
+ uint16 :build
37
+ uint32 :ntlm_revision, initial_value: 15
38
+
39
+ def to_s
40
+ "Version #{major}.#{minor} (Build #{build}); NTLM Current Revision #{ntlm_revision}"
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,156 @@
1
+ require 'securerandom'
2
+
3
+ module RubySMB
4
+ class Server
5
+ class ServerClient
6
+ module Negotiation
7
+ #
8
+ # Handle an SMB negotiation request. Once negotiation is complete, the state will be updated to :session_setup.
9
+ # At this point the @dialect will have been set along with other dialect-specific values.
10
+ #
11
+ # @param [String] raw_request the negotiation request to process
12
+ def handle_negotiate(raw_request)
13
+ response = nil
14
+ case raw_request[0...4].unpack1('L>')
15
+ when RubySMB::SMB1::SMB_PROTOCOL_ID
16
+ request = SMB1::Packet::NegotiateRequest.read(raw_request)
17
+ response = do_negotiate_smb1(request) if request.is_a?(SMB1::Packet::NegotiateRequest)
18
+ when RubySMB::SMB2::SMB2_PROTOCOL_ID
19
+ request = SMB2::Packet::NegotiateRequest.read(raw_request)
20
+ response = do_negotiate_smb2(request) if request.is_a?(SMB2::Packet::NegotiateRequest)
21
+ end
22
+
23
+ if response.nil?
24
+ disconnect!
25
+ else
26
+ send_packet(response)
27
+ end
28
+
29
+ nil
30
+ end
31
+
32
+ def do_negotiate_smb1(request)
33
+ client_dialects = request.dialects.map(&:dialect_string).map(&:value)
34
+
35
+ if client_dialects.include?(Client::SMB1_DIALECT_SMB2_WILDCARD) && \
36
+ @server.dialects.any? { |dialect| Dialect[dialect].order == Dialect::ORDER_SMB2 }
37
+ response = SMB2::Packet::NegotiateResponse.new
38
+ response.smb2_header.credits = 1
39
+ response.security_mode.signing_enabled = 1
40
+ response.dialect_revision = SMB2::SMB2_WILDCARD_REVISION
41
+ response.server_guid = @server.guid
42
+
43
+ response.max_transact_size = 0x800000
44
+ response.max_read_size = 0x800000
45
+ response.max_write_size = 0x800000
46
+ response.system_time.set(Time.now)
47
+ response.security_buffer_offset = response.security_buffer.abs_offset
48
+ response.security_buffer = process_gss.buffer
49
+ return response
50
+ end
51
+
52
+ server_dialects = @server.dialects.select { |dialect| Dialect[dialect].order == Dialect::ORDER_SMB1 }
53
+ dialect = (server_dialects & client_dialects).first
54
+ if dialect.nil?
55
+ # 'NT LM 0.12' is currently the only supported dialect
56
+ # see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cifs/80850595-e301-4464-9745-58e4945eb99b
57
+ response = SMB1::Packet::NegotiateResponse.new
58
+ response.parameter_block.word_count = 1
59
+ response.parameter_block.dialect_index = 0xffff
60
+ response.data_block.byte_count = 0
61
+ return response
62
+ end
63
+
64
+ response = SMB1::Packet::NegotiateResponseExtended.new
65
+ response.parameter_block.dialect_index = client_dialects.index(dialect)
66
+ response.parameter_block.max_mpx_count = 50
67
+ response.parameter_block.max_number_vcs = 1
68
+ response.parameter_block.max_buffer_size = 16644
69
+ response.parameter_block.max_raw_size = 65536
70
+ server_time = Time.now
71
+ response.parameter_block.system_time.set(server_time)
72
+ response.parameter_block.server_time_zone = server_time.utc_offset
73
+ response.data_block.server_guid = @server.guid
74
+ response.data_block.security_blob = process_gss.buffer
75
+
76
+ @state = :session_setup
77
+ @dialect = dialect
78
+ response
79
+ end
80
+
81
+ def do_negotiate_smb2(request)
82
+ client_dialects = request.dialects.map { |d| "0x%04x" % d }
83
+ server_dialects = @server.dialects.select { |dialect| Dialect[dialect].order == Dialect::ORDER_SMB2 }
84
+ dialect = (server_dialects & client_dialects).first
85
+
86
+ response = SMB2::Packet::NegotiateResponse.new
87
+ response.smb2_header.credits = 1
88
+ response.smb2_header.message_id = request.smb2_header.message_id
89
+ response.security_mode.signing_enabled = 1
90
+ response.server_guid = @server.guid
91
+ response.max_transact_size = 0x800000
92
+ response.max_read_size = 0x800000
93
+ response.max_write_size = 0x800000
94
+ response.system_time.set(Time.now)
95
+ if dialect.nil?
96
+ # see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/b39f253e-4963-40df-8dff-2f9040ebbeb1
97
+ # > If a common dialect is not found, the server MUST fail the request with STATUS_NOT_SUPPORTED.
98
+ response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_NOT_SUPPORTED.value
99
+ return response
100
+ end
101
+
102
+ contexts = []
103
+ hash_algorithm = hash_value = nil
104
+ if dialect == '0x0311'
105
+ # see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/b39f253e-4963-40df-8dff-2f9040ebbeb1
106
+ nc = request.find_negotiate_context(SMB2::NegotiateContext::SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
107
+ hash_algorithm = SMB2::PreauthIntegrityCapabilities::HASH_ALGORITM_MAP[nc&.data&.hash_algorithms&.first]
108
+ hash_value = "\x00" * 64
109
+ unless hash_algorithm
110
+ response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_INVALID_PARAMETER.value
111
+ return response
112
+ end
113
+
114
+ contexts << SMB2::NegotiateContext.new(
115
+ context_type: SMB2::NegotiateContext::SMB2_PREAUTH_INTEGRITY_CAPABILITIES,
116
+ data: {
117
+ hash_algorithms: [ SMB2::PreauthIntegrityCapabilities::SHA_512 ],
118
+ salt: SecureRandom.random_bytes(32)
119
+ }
120
+ )
121
+
122
+ nc = request.find_negotiate_context(SMB2::NegotiateContext::SMB2_ENCRYPTION_CAPABILITIES)
123
+ cipher = nc&.data&.ciphers&.first
124
+ cipher = 0 unless SMB2::EncryptionCapabilities::ENCRYPTION_ALGORITHM_MAP.include? cipher
125
+ contexts << SMB2::NegotiateContext.new(
126
+ context_type: SMB2::NegotiateContext::SMB2_ENCRYPTION_CAPABILITIES,
127
+ data: {
128
+ ciphers: [ cipher ]
129
+ }
130
+ )
131
+ end
132
+
133
+ # the order in which the response is built is important to ensure it is valid
134
+ response.dialect_revision = dialect.to_i(16)
135
+ response.security_buffer_offset = response.security_buffer.abs_offset
136
+ response.security_buffer = process_gss.buffer
137
+ if dialect == '0x0311'
138
+ response.negotiate_context_offset = response.negotiate_context_list.abs_offset
139
+ contexts.each { |nc| response.add_negotiate_context(nc) }
140
+ end
141
+ @preauth_integrity_hash_algorithm = hash_algorithm
142
+ @preauth_integrity_hash_value = hash_value
143
+
144
+ if dialect == '0x0311'
145
+ update_preauth_hash(request)
146
+ update_preauth_hash(response)
147
+ end
148
+
149
+ @state = :session_setup
150
+ @dialect = dialect
151
+ response
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end