ruby_smb 2.0.1 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -1
  4. data/examples/anonymous_auth.rb +3 -3
  5. data/examples/append_file.rb +10 -8
  6. data/examples/authenticate.rb +9 -5
  7. data/examples/delete_file.rb +8 -6
  8. data/examples/enum_registry_key.rb +5 -4
  9. data/examples/enum_registry_values.rb +5 -4
  10. data/examples/list_directory.rb +8 -6
  11. data/examples/negotiate_with_netbios_service.rb +9 -5
  12. data/examples/net_share_enum_all.rb +6 -4
  13. data/examples/pipes.rb +11 -12
  14. data/examples/query_service_status.rb +64 -0
  15. data/examples/read_file.rb +8 -6
  16. data/examples/read_registry_key_value.rb +6 -5
  17. data/examples/rename_file.rb +9 -7
  18. data/examples/tree_connect.rb +7 -5
  19. data/examples/write_file.rb +9 -7
  20. data/lib/ruby_smb/client.rb +81 -48
  21. data/lib/ruby_smb/client/authentication.rb +5 -10
  22. data/lib/ruby_smb/client/echo.rb +2 -4
  23. data/lib/ruby_smb/client/negotiation.rb +21 -14
  24. data/lib/ruby_smb/client/tree_connect.rb +2 -4
  25. data/lib/ruby_smb/client/utils.rb +16 -10
  26. data/lib/ruby_smb/client/winreg.rb +1 -1
  27. data/lib/ruby_smb/dcerpc.rb +4 -0
  28. data/lib/ruby_smb/dcerpc/error.rb +3 -0
  29. data/lib/ruby_smb/dcerpc/ndr.rb +306 -44
  30. data/lib/ruby_smb/dcerpc/netlogon.rb +101 -0
  31. data/lib/ruby_smb/dcerpc/netlogon/netr_server_authenticate3_request.rb +28 -0
  32. data/lib/ruby_smb/dcerpc/netlogon/netr_server_authenticate3_response.rb +26 -0
  33. data/lib/ruby_smb/dcerpc/netlogon/netr_server_password_set2_request.rb +27 -0
  34. data/lib/ruby_smb/dcerpc/netlogon/netr_server_password_set2_response.rb +23 -0
  35. data/lib/ruby_smb/dcerpc/netlogon/netr_server_req_challenge_request.rb +25 -0
  36. data/lib/ruby_smb/dcerpc/netlogon/netr_server_req_challenge_response.rb +24 -0
  37. data/lib/ruby_smb/dcerpc/request.rb +19 -0
  38. data/lib/ruby_smb/dcerpc/rpc_security_attributes.rb +34 -0
  39. data/lib/ruby_smb/dcerpc/rrp_unicode_string.rb +9 -6
  40. data/lib/ruby_smb/dcerpc/svcctl.rb +479 -0
  41. data/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_request.rb +48 -0
  42. data/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_response.rb +26 -0
  43. data/lib/ruby_smb/dcerpc/svcctl/close_service_handle_request.rb +25 -0
  44. data/lib/ruby_smb/dcerpc/svcctl/close_service_handle_response.rb +26 -0
  45. data/lib/ruby_smb/dcerpc/svcctl/control_service_request.rb +26 -0
  46. data/lib/ruby_smb/dcerpc/svcctl/control_service_response.rb +26 -0
  47. data/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_request.rb +35 -0
  48. data/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_response.rb +23 -0
  49. data/lib/ruby_smb/dcerpc/svcctl/open_service_w_request.rb +31 -0
  50. data/lib/ruby_smb/dcerpc/svcctl/open_service_w_response.rb +23 -0
  51. data/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_request.rb +25 -0
  52. data/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_response.rb +44 -0
  53. data/lib/ruby_smb/dcerpc/svcctl/query_service_status_request.rb +23 -0
  54. data/lib/ruby_smb/dcerpc/svcctl/query_service_status_response.rb +27 -0
  55. data/lib/ruby_smb/dcerpc/svcctl/service_status.rb +25 -0
  56. data/lib/ruby_smb/dcerpc/svcctl/start_service_w_request.rb +27 -0
  57. data/lib/ruby_smb/dcerpc/svcctl/start_service_w_response.rb +25 -0
  58. data/lib/ruby_smb/dcerpc/winreg.rb +98 -17
  59. data/lib/ruby_smb/dcerpc/winreg/create_key_request.rb +73 -0
  60. data/lib/ruby_smb/dcerpc/winreg/create_key_response.rb +36 -0
  61. data/lib/ruby_smb/dcerpc/winreg/enum_key_request.rb +1 -1
  62. data/lib/ruby_smb/dcerpc/winreg/enum_value_request.rb +1 -1
  63. data/lib/ruby_smb/dcerpc/winreg/enum_value_response.rb +1 -1
  64. data/lib/ruby_smb/dcerpc/winreg/open_root_key_request.rb +4 -4
  65. data/lib/ruby_smb/dcerpc/winreg/query_info_key_request.rb +1 -1
  66. data/lib/ruby_smb/dcerpc/winreg/query_value_request.rb +7 -6
  67. data/lib/ruby_smb/dcerpc/winreg/query_value_response.rb +10 -10
  68. data/lib/ruby_smb/dcerpc/winreg/save_key_request.rb +37 -0
  69. data/lib/ruby_smb/dcerpc/winreg/save_key_response.rb +23 -0
  70. data/lib/ruby_smb/dispatcher/base.rb +1 -1
  71. data/lib/ruby_smb/dispatcher/socket.rb +1 -1
  72. data/lib/ruby_smb/error.rb +21 -5
  73. data/lib/ruby_smb/field/stringz16.rb +17 -1
  74. data/lib/ruby_smb/generic_packet.rb +11 -1
  75. data/lib/ruby_smb/nbss/session_header.rb +4 -4
  76. data/lib/ruby_smb/smb1/file.rb +10 -25
  77. data/lib/ruby_smb/smb1/packet/trans2/find_first2_response.rb +0 -1
  78. data/lib/ruby_smb/smb1/packet/trans2/find_next2_response.rb +0 -1
  79. data/lib/ruby_smb/smb1/packet/trans2/open2_response.rb +1 -2
  80. data/lib/ruby_smb/smb1/packet/trans2/set_file_information_response.rb +1 -13
  81. data/lib/ruby_smb/smb1/pipe.rb +8 -6
  82. data/lib/ruby_smb/smb1/tree.rb +13 -9
  83. data/lib/ruby_smb/smb2/file.rb +33 -33
  84. data/lib/ruby_smb/smb2/pipe.rb +9 -6
  85. data/lib/ruby_smb/smb2/tree.rb +21 -11
  86. data/lib/ruby_smb/version.rb +1 -1
  87. data/spec/lib/ruby_smb/client_spec.rb +195 -101
  88. data/spec/lib/ruby_smb/dcerpc/ndr_spec.rb +1396 -77
  89. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_authenticate3_request_spec.rb +69 -0
  90. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_authenticate3_response_spec.rb +53 -0
  91. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_password_set2_request_spec.rb +69 -0
  92. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_password_set2_response_spec.rb +37 -0
  93. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_req_challenge_request_spec.rb +45 -0
  94. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_req_challenge_response_spec.rb +37 -0
  95. data/spec/lib/ruby_smb/dcerpc/rpc_security_attributes_spec.rb +161 -0
  96. data/spec/lib/ruby_smb/dcerpc/rrp_unicode_string_spec.rb +49 -12
  97. data/spec/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_request_spec.rb +191 -0
  98. data/spec/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_response_spec.rb +38 -0
  99. data/spec/lib/ruby_smb/dcerpc/svcctl/close_service_handle_request_spec.rb +30 -0
  100. data/spec/lib/ruby_smb/dcerpc/svcctl/close_service_handle_response_spec.rb +38 -0
  101. data/spec/lib/ruby_smb/dcerpc/svcctl/control_service_request_spec.rb +39 -0
  102. data/spec/lib/ruby_smb/dcerpc/svcctl/control_service_response_spec.rb +38 -0
  103. data/spec/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_request_spec.rb +78 -0
  104. data/spec/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_response_spec.rb +38 -0
  105. data/spec/lib/ruby_smb/dcerpc/svcctl/open_service_w_request_spec.rb +59 -0
  106. data/spec/lib/ruby_smb/dcerpc/svcctl/open_service_w_response_spec.rb +38 -0
  107. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_request_spec.rb +38 -0
  108. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_response_spec.rb +152 -0
  109. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_status_request_spec.rb +30 -0
  110. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_status_response_spec.rb +38 -0
  111. data/spec/lib/ruby_smb/dcerpc/svcctl/service_status_spec.rb +72 -0
  112. data/spec/lib/ruby_smb/dcerpc/svcctl/start_service_w_request_spec.rb +46 -0
  113. data/spec/lib/ruby_smb/dcerpc/svcctl/start_service_w_response_spec.rb +30 -0
  114. data/spec/lib/ruby_smb/dcerpc/svcctl_spec.rb +512 -0
  115. data/spec/lib/ruby_smb/dcerpc/winreg/create_key_request_spec.rb +110 -0
  116. data/spec/lib/ruby_smb/dcerpc/winreg/create_key_response_spec.rb +44 -0
  117. data/spec/lib/ruby_smb/dcerpc/winreg/enum_key_request_spec.rb +0 -4
  118. data/spec/lib/ruby_smb/dcerpc/winreg/enum_value_request_spec.rb +2 -2
  119. data/spec/lib/ruby_smb/dcerpc/winreg/enum_value_response_spec.rb +2 -2
  120. data/spec/lib/ruby_smb/dcerpc/winreg/open_root_key_request_spec.rb +9 -4
  121. data/spec/lib/ruby_smb/dcerpc/winreg/query_info_key_request_spec.rb +0 -4
  122. data/spec/lib/ruby_smb/dcerpc/winreg/query_value_request_spec.rb +17 -17
  123. data/spec/lib/ruby_smb/dcerpc/winreg/query_value_response_spec.rb +11 -23
  124. data/spec/lib/ruby_smb/dcerpc/winreg/save_key_request_spec.rb +57 -0
  125. data/spec/lib/ruby_smb/dcerpc/winreg/save_key_response_spec.rb +22 -0
  126. data/spec/lib/ruby_smb/dcerpc/winreg_spec.rb +227 -41
  127. data/spec/lib/ruby_smb/dispatcher/socket_spec.rb +10 -10
  128. data/spec/lib/ruby_smb/error_spec.rb +34 -5
  129. data/spec/lib/ruby_smb/field/stringz16_spec.rb +12 -0
  130. data/spec/lib/ruby_smb/generic_packet_spec.rb +7 -0
  131. data/spec/lib/ruby_smb/nbss/session_header_spec.rb +4 -11
  132. data/spec/lib/ruby_smb/smb1/file_spec.rb +2 -4
  133. data/spec/lib/ruby_smb/smb1/packet/trans2/find_first2_response_spec.rb +0 -1
  134. data/spec/lib/ruby_smb/smb1/packet/trans2/find_next2_response_spec.rb +0 -1
  135. data/spec/lib/ruby_smb/smb1/packet/trans2/open2_response_spec.rb +0 -5
  136. data/spec/lib/ruby_smb/smb1/packet/trans2/set_file_information_response_spec.rb +0 -6
  137. data/spec/lib/ruby_smb/smb1/pipe_spec.rb +30 -5
  138. data/spec/lib/ruby_smb/smb1/tree_spec.rb +22 -0
  139. data/spec/lib/ruby_smb/smb2/file_spec.rb +61 -9
  140. data/spec/lib/ruby_smb/smb2/pipe_spec.rb +9 -5
  141. data/spec/lib/ruby_smb/smb2/tree_spec.rb +58 -1
  142. metadata +91 -2
  143. metadata.gz.sig +0 -0
@@ -9,18 +9,20 @@
9
9
  require 'bundler/setup'
10
10
  require 'ruby_smb'
11
11
 
12
- address = ARGV[0]
13
- username = ARGV[1]
14
- password = ARGV[2]
15
- share = ARGV[3]
16
- file = ARGV[4]
17
- data = ARGV[5]
12
+ address = ARGV[0]
13
+ username = ARGV[1]
14
+ password = ARGV[2]
15
+ share = ARGV[3]
16
+ file = ARGV[4]
17
+ data = ARGV[5]
18
+ smb_versions = ARGV[6]&.split(',') || ['1','2','3']
19
+
18
20
  path = "\\\\#{address}\\#{share}"
19
21
 
20
22
  sock = TCPSocket.new address, 445
21
23
  dispatcher = RubySMB::Dispatcher::Socket.new(sock)
22
24
 
23
- client = RubySMB::Client.new(dispatcher, smb1: true, smb2: true, username: username, password: password)
25
+ client = RubySMB::Client.new(dispatcher, smb1: smb_versions.include?('1'), smb2: smb_versions.include?('2'), smb3: smb_versions.include?('3'), username: username, password: password)
24
26
  protocol = client.negotiate
25
27
  status = client.authenticate
26
28
 
@@ -160,9 +160,16 @@ module RubySMB
160
160
 
161
161
  # The UID set in SMB1
162
162
  # @!attribute [rw] user_id
163
- # @return [String]
163
+ # @return [Integer]
164
164
  attr_accessor :user_id
165
165
 
166
+ # The Process ID set in SMB1
167
+ # It is randomly generated during the client initialization, but can
168
+ # be modified using the accessor.
169
+ # @!attribute [rw] pid
170
+ # @return [Integer]
171
+ attr_accessor :pid
172
+
166
173
  # The maximum size SMB message that the Client accepts (in bytes)
167
174
  # The default value is equal to {MAX_BUFFER_SIZE}.
168
175
  # @!attribute [rw] max_buffer_size
@@ -258,6 +265,14 @@ module RubySMB
258
265
  # @return [Integer] the negotiated SMB version
259
266
  attr_accessor :negotiated_smb_version
260
267
 
268
+ # Whether or not the server supports multi-credit operations. It is
269
+ # reported by the LARGE_MTU capabiliy as part of the negotiation process
270
+ # (SMB 2.x and 3.x).
271
+ # @!attribute [rw] server_supports_multi_credit
272
+ # @return [Boolean] true if the server supports multi-credit operations,
273
+ # false otherwise
274
+ attr_accessor :server_supports_multi_credit
275
+
261
276
  # @param dispatcher [RubySMB::Dispatcher::Socket] the packet dispatcher to use
262
277
  # @param smb1 [Boolean] whether or not to enable SMB1 support
263
278
  # @param smb2 [Boolean] whether or not to enable SMB2 support
@@ -268,6 +283,7 @@ module RubySMB
268
283
  raise ArgumentError, 'You must enable at least one Protocol'
269
284
  end
270
285
  @dispatcher = dispatcher
286
+ @pid = rand(0xFFFF)
271
287
  @domain = domain
272
288
  @local_workstation = local_workstation
273
289
  @password = password.encode('utf-8') || ''.encode('utf-8')
@@ -285,6 +301,7 @@ module RubySMB
285
301
  @server_max_read_size = RubySMB::SMB2::File::MAX_PACKET_SIZE
286
302
  @server_max_write_size = RubySMB::SMB2::File::MAX_PACKET_SIZE
287
303
  @server_max_transact_size = RubySMB::SMB2::File::MAX_PACKET_SIZE
304
+ @server_supports_multi_credit = false
288
305
 
289
306
  # SMB 3.x options
290
307
  @session_encrypt_data = always_encrypt
@@ -344,7 +361,7 @@ module RubySMB
344
361
  # @param packet [RubySMB::GenericPacket] the packet to set the message id for
345
362
  # @return [RubySMB::GenericPacket] the modified packet
346
363
  def increment_smb_message_id(packet)
347
- packet.smb2_header.message_id = smb2_message_id
364
+ packet.smb2_header.message_id = self.smb2_message_id
348
365
  self.smb2_message_id += 1
349
366
  packet
350
367
  end
@@ -394,8 +411,7 @@ module RubySMB
394
411
  raise RubySMB::Error::InvalidPacket.new(
395
412
  expected_proto: RubySMB::SMB2::SMB2_PROTOCOL_ID,
396
413
  expected_cmd: RubySMB::SMB2::Packet::LogoffResponse::COMMAND,
397
- received_proto: response.smb2_header.protocol,
398
- received_cmd: response.smb2_header.command
414
+ packet: response
399
415
  )
400
416
  end
401
417
  else
@@ -406,8 +422,7 @@ module RubySMB
406
422
  raise RubySMB::Error::InvalidPacket.new(
407
423
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
408
424
  expected_cmd: RubySMB::SMB1::Packet::LogoffResponse::COMMAND,
409
- received_proto: response.smb_header.protocol,
410
- received_cmd: response.smb_header.command
425
+ packet: response
411
426
  )
412
427
  end
413
428
  end
@@ -427,7 +442,8 @@ module RubySMB
427
442
  version = packet.packet_smb_version
428
443
  case version
429
444
  when 'SMB1'
430
- packet.smb_header.uid = user_id if user_id
445
+ packet.smb_header.uid = self.user_id if self.user_id
446
+ packet.smb_header.pid_low = self.pid if self.pid
431
447
  packet = smb1_sign(packet)
432
448
  when 'SMB2'
433
449
  packet = increment_smb_message_id(packet)
@@ -443,35 +459,38 @@ module RubySMB
443
459
  packet = packet
444
460
  end
445
461
 
462
+ encrypt_data = false
446
463
  if can_be_encrypted?(packet) && encryption_supported? && (@session_encrypt_data || encrypt)
447
- send_encrypt(packet)
448
- raw_response = recv_encrypt
449
- loop do
450
- break unless is_status_pending?(raw_response)
451
- sleep 1
452
- raw_response = recv_encrypt
453
- end
454
- else
455
- dispatcher.send_packet(packet)
456
- raw_response = dispatcher.recv_packet
457
- loop do
458
- break unless is_status_pending?(raw_response)
459
- sleep 1
460
- raw_response = dispatcher.recv_packet
461
- end unless version == 'SMB1'
464
+ encrypt_data = true
462
465
  end
466
+ send_packet(packet, encrypt: encrypt_data)
467
+ raw_response = recv_packet(encrypt: encrypt_data)
468
+ smb2_header = nil
469
+ loop do
470
+ smb2_header = RubySMB::SMB2::SMB2Header.read(raw_response)
471
+ break unless is_status_pending?(smb2_header)
472
+ sleep 1
473
+ raw_response = recv_packet(encrypt: encrypt_data)
474
+ rescue IOError
475
+ # We're expecting an SMB2 packet, but the server sent an SMB1 packet
476
+ # instead. This behavior has been observed with older versions of Samba
477
+ # when something goes wrong on the server side. So, we just ignore it
478
+ # and expect the caller to handle this wrong response packet.
479
+ break
480
+ end unless version == 'SMB1'
463
481
 
464
482
  self.sequence_counter += 1 if signing_required && !session_key.empty?
483
+ # update the SMB2 message ID according to the received Credit Charged
484
+ self.smb2_message_id += smb2_header.credit_charge - 1 if smb2_header && self.server_supports_multi_credit
465
485
  raw_response
466
486
  end
467
487
 
468
488
  # Check if the response is an asynchronous operation with STATUS_PENDING
469
489
  # status code.
470
490
  #
471
- # @param raw_response [String] the raw response packet
491
+ # @param smb2_header [String] the response packet SMB2 header
472
492
  # @return [Boolean] true if it is a status pending operation, false otherwise
473
- def is_status_pending?(raw_response)
474
- smb2_header = RubySMB::SMB2::SMB2Header.read(raw_response)
493
+ def is_status_pending?(smb2_header)
475
494
  value = smb2_header.nt_status.value
476
495
  status_code = WindowsError::NTStatus.find_by_retval(value).first
477
496
  status_code == WindowsError::NTStatus::STATUS_PENDING &&
@@ -497,37 +516,50 @@ module RubySMB
497
516
  ['0x0300', '0x0302', '0x0311'].include?(@dialect)
498
517
  end
499
518
 
500
- # Encrypt and send a packet
501
- def send_encrypt(packet)
502
- begin
503
- transform_request = smb3_encrypt(packet.to_binary_s)
504
- rescue RubySMB::Error::RubySMBError => e
505
- raise RubySMB::Error::EncryptionError, "Error while encrypting #{packet.class.name} packet (SMB #{@dialect}): #{e}"
519
+ # Encrypt (if required) and send a packet.
520
+ #
521
+ # @param encrypt [Boolean] true if the packet should be encrypted, false
522
+ # otherwise
523
+ def send_packet(packet, encrypt: false)
524
+ if encrypt
525
+ begin
526
+ packet = smb3_encrypt(packet.to_binary_s)
527
+ rescue RubySMB::Error::RubySMBError => e
528
+ raise RubySMB::Error::EncryptionError, "Error while encrypting #{packet.class.name} packet (SMB #{@dialect}): #{e}"
529
+ end
506
530
  end
507
- dispatcher.send_packet(transform_request)
531
+ dispatcher.send_packet(packet)
508
532
  end
509
533
 
510
- # Receives the raw response through the Dispatcher and decrypt the packet.
534
+ # Receives the raw response through the Dispatcher and decrypt the packet (if required).
511
535
  #
536
+ # @param encrypt [Boolean] true if the packet is encrypted, false otherwise
512
537
  # @return [String] the raw unencrypted packet
513
- def recv_encrypt
538
+ def recv_packet(encrypt: false)
514
539
  begin
515
540
  raw_response = dispatcher.recv_packet
516
541
  rescue RubySMB::Error::CommunicationError => e
517
- raise RubySMB::Error::EncryptionError, "Communication error with the "\
518
- "remote host: #{e.message}. The server supports encryption but was "\
519
- "not able to handle the encrypted request."
520
- end
521
- begin
522
- transform_response = RubySMB::SMB2::Packet::TransformHeader.read(raw_response)
523
- rescue IOError
524
- raise RubySMB::Error::InvalidPacket, 'Not a SMB2 TransformHeader packet'
542
+ if encrypt
543
+ raise RubySMB::Error::EncryptionError, "Communication error with the "\
544
+ "remote host: #{e.message}. The server supports encryption but was "\
545
+ "not able to handle the encrypted request."
546
+ else
547
+ raise e
548
+ end
525
549
  end
526
- begin
527
- smb3_decrypt(transform_response)
528
- rescue RubySMB::Error::RubySMBError => e
529
- raise RubySMB::Error::EncryptionError, "Error while decrypting #{transform_response.class.name} packet (SMB #@dialect}): #{e}"
550
+ if encrypt
551
+ begin
552
+ transform_response = RubySMB::SMB2::Packet::TransformHeader.read(raw_response)
553
+ rescue IOError
554
+ raise RubySMB::Error::InvalidPacket, 'Not a SMB2 TransformHeader packet'
555
+ end
556
+ begin
557
+ raw_response = smb3_decrypt(transform_response)
558
+ rescue RubySMB::Error::RubySMBError => e
559
+ raise RubySMB::Error::EncryptionError, "Error while decrypting #{transform_response.class.name} packet (SMB #@dialect}): #{e}"
560
+ end
530
561
  end
562
+ raw_response
531
563
  end
532
564
 
533
565
  # Connects to the supplied share
@@ -551,7 +583,7 @@ module RubySMB
551
583
  # @param [String] host
552
584
  def net_share_enum_all(host)
553
585
  tree = tree_connect("\\\\#{host}\\IPC$")
554
- named_pipe = tree.open_file(filename: "srvsvc", write: true, read: true)
586
+ named_pipe = tree.open_pipe(filename: "srvsvc", write: true, read: true)
555
587
  named_pipe.net_share_enum_all(host)
556
588
  end
557
589
 
@@ -568,6 +600,7 @@ module RubySMB
568
600
  self.smb2_message_id = 0
569
601
  self.client_encryption_key = nil
570
602
  self.server_encryption_key = nil
603
+ self.server_supports_multi_credit = false
571
604
  end
572
605
 
573
606
  # Requests a NetBIOS Session Service using the provided name.
@@ -605,7 +638,7 @@ module RubySMB
605
638
  session_request.session_header.session_packet_type = RubySMB::Nbss::SESSION_REQUEST
606
639
  session_request.called_name = called_name
607
640
  session_request.calling_name = calling_name
608
- session_request.session_header.packet_length =
641
+ session_request.session_header.stream_protocol_length =
609
642
  session_request.num_bytes - session_request.session_header.num_bytes
610
643
  session_request
611
644
  end
@@ -60,8 +60,7 @@ module RubySMB
60
60
  raise RubySMB::Error::InvalidPacket.new(
61
61
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
62
62
  expected_cmd: RubySMB::SMB1::Packet::SessionSetupLegacyResponse::COMMAND,
63
- received_proto: packet.smb_header.protocol,
64
- received_cmd: packet.smb_header.command
63
+ packet: packet
65
64
  )
66
65
  end
67
66
  packet
@@ -154,8 +153,7 @@ module RubySMB
154
153
  raise RubySMB::Error::InvalidPacket.new(
155
154
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
156
155
  expected_cmd: RubySMB::SMB1::Packet::SessionSetupResponse::COMMAND,
157
- received_proto: packet.smb_header.protocol,
158
- received_cmd: packet.smb_header.command
156
+ packet: packet
159
157
  )
160
158
  end
161
159
  packet
@@ -168,8 +166,7 @@ module RubySMB
168
166
  raise RubySMB::Error::InvalidPacket.new(
169
167
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
170
168
  expected_cmd: RubySMB::SMB1::Packet::SessionSetupResponse::COMMAND,
171
- received_proto: packet.smb_header.protocol,
172
- received_cmd: packet.smb_header.command
169
+ packet: packet
173
170
  )
174
171
  end
175
172
 
@@ -236,8 +233,7 @@ module RubySMB
236
233
  raise RubySMB::Error::InvalidPacket.new(
237
234
  expected_proto: RubySMB::SMB2::SMB2_PROTOCOL_ID,
238
235
  expected_cmd: RubySMB::SMB2::Packet::SessionSetupResponse::COMMAND,
239
- received_proto: packet.smb2_header.protocol,
240
- received_cmd: packet.smb2_header.command
236
+ packet: packet
241
237
  )
242
238
  end
243
239
 
@@ -251,8 +247,7 @@ module RubySMB
251
247
  raise RubySMB::Error::InvalidPacket.new(
252
248
  expected_proto: RubySMB::SMB2::SMB2_PROTOCOL_ID,
253
249
  expected_cmd: RubySMB::SMB2::Packet::SessionSetupResponse::COMMAND,
254
- received_proto: packet.smb2_header.protocol,
255
- received_cmd: packet.smb2_header.command
250
+ packet: packet
256
251
  )
257
252
  end
258
253
 
@@ -21,8 +21,7 @@ module RubySMB
21
21
  raise RubySMB::Error::InvalidPacket.new(
22
22
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
23
23
  expected_cmd: RubySMB::SMB1::Packet::EchoResponse::COMMAND,
24
- received_proto: response.smb_header.protocol,
25
- received_cmd: response.smb_header.command
24
+ packet: response
26
25
  )
27
26
  end
28
27
  response
@@ -40,8 +39,7 @@ module RubySMB
40
39
  raise RubySMB::Error::InvalidPacket.new(
41
40
  expected_proto: RubySMB::SMB2::SMB2_PROTOCOL_ID,
42
41
  expected_cmd: RubySMB::SMB2::Packet::EchoResponse::COMMAND,
43
- received_proto: response.smb2_header.protocol,
44
- received_cmd: response.smb2_header.command
42
+ packet: response
45
43
  )
46
44
  end
47
45
  response
@@ -18,11 +18,9 @@ module RubySMB
18
18
  # This is only valid for SMB1.
19
19
  response_packet.dialects = request_packet.dialects if response_packet.respond_to? :dialects=
20
20
  version = parse_negotiate_response(response_packet)
21
- case @dialect
22
- when '0x0300', '0x0302'
23
- @encryption_algorithm = RubySMB::SMB2::EncryptionCapabilities::ENCRYPTION_ALGORITHM_MAP[RubySMB::SMB2::EncryptionCapabilities::AES_128_CCM]
24
- when '0x0311'
25
- parse_smb3_encryption_data(request_packet, response_packet)
21
+ if @dialect == '0x0311'
22
+ update_preauth_hash(request_packet)
23
+ update_preauth_hash(response_packet)
26
24
  end
27
25
 
28
26
  # If the response contains the SMB2 wildcard revision number dialect;
@@ -85,16 +83,14 @@ module RubySMB
85
83
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
86
84
  expected_cmd: RubySMB::SMB1::Packet::NegotiateResponseExtended::COMMAND,
87
85
  expected_custom: "extended_security=1",
88
- received_proto: packet.smb_header.protocol,
89
- received_cmd: packet.smb_header.command,
86
+ packet: packet,
90
87
  received_custom: "extended_security=#{extended_security}"
91
88
  )
92
89
  elsif packet.packet_smb_version == 'SMB2'
93
90
  raise RubySMB::Error::InvalidPacket.new(
94
91
  expected_proto: RubySMB::SMB2::SMB2_PROTOCOL_ID,
95
92
  expected_cmd: RubySMB::SMB2::Packet::NegotiateResponse::COMMAND,
96
- received_proto: packet.smb2_header.protocol,
97
- received_cmd: packet.smb2_header.command
93
+ packet: packet
98
94
  )
99
95
  else
100
96
  raise RubySMB::Error::InvalidPacket, 'Unknown SMB protocol version'
@@ -123,14 +119,13 @@ module RubySMB
123
119
  # protocol overhead every time.
124
120
  self.server_max_buffer_size = packet.parameter_block.max_buffer_size - 260
125
121
  self.negotiated_smb_version = 1
122
+ self.session_encrypt_data = false
126
123
  'SMB1'
127
124
  when RubySMB::SMB2::Packet::NegotiateResponse
128
125
  self.smb1 = false
129
126
  unless packet.dialect_revision.to_i == 0x02ff
130
127
  self.smb2 = packet.dialect_revision.to_i >= 0x0200 && packet.dialect_revision.to_i < 0x0300
131
128
  self.smb3 = packet.dialect_revision.to_i >= 0x0300 && packet.dialect_revision.to_i < 0x0400
132
- # Only enable session encryption if the server supports it
133
- @session_encrypt_data = self.smb3 && @session_encrypt_data && packet.capabilities.encryption == 1
134
129
  end
135
130
  self.signing_required = packet.security_mode.signing_required == 1 if self.smb2 || self.smb3
136
131
  self.dialect = "0x%04x" % packet.dialect_revision
@@ -143,6 +138,20 @@ module RubySMB
143
138
  self.server_guid = packet.server_guid
144
139
  self.server_start_time = packet.server_start_time.to_time if packet.server_start_time != 0
145
140
  self.server_system_time = packet.system_time.to_time if packet.system_time != 0
141
+ self.server_supports_multi_credit = self.dialect != '0x0202' && packet&.capabilities&.large_mtu == 1
142
+ case self.dialect
143
+ when '0x02ff'
144
+ when '0x0300', '0x0302'
145
+ if packet&.capabilities&.encryption == 1
146
+ self.encryption_algorithm = RubySMB::SMB2::EncryptionCapabilities::ENCRYPTION_ALGORITHM_MAP[RubySMB::SMB2::EncryptionCapabilities::AES_128_CCM]
147
+ end
148
+ self.session_encrypt_data = self.session_encrypt_data && !self.encryption_algorithm.nil?
149
+ when '0x0311'
150
+ parse_smb3_capabilities(packet)
151
+ self.session_encrypt_data = self.session_encrypt_data && !self.encryption_algorithm.nil?
152
+ else
153
+ self.session_encrypt_data = false
154
+ end
146
155
  return "SMB#{self.negotiated_smb_version}"
147
156
  else
148
157
  error = 'Unable to negotiate with remote host'
@@ -155,7 +164,7 @@ module RubySMB
155
164
  end
156
165
  end
157
166
 
158
- def parse_smb3_encryption_data(request_packet, response_packet)
167
+ def parse_smb3_capabilities(response_packet)
159
168
  nc = response_packet.find_negotiate_context(
160
169
  RubySMB::SMB2::NegotiateContext::SMB2_PREAUTH_INTEGRITY_CAPABILITIES
161
170
  )
@@ -185,8 +194,6 @@ module RubySMB
185
194
  'Unable to retrieve the encryption cipher list supported by the server from the Negotiate response'
186
195
  )
187
196
  end
188
- update_preauth_hash(request_packet)
189
- update_preauth_hash(response_packet)
190
197
 
191
198
  nc = response_packet.find_negotiate_context(
192
199
  RubySMB::SMB2::NegotiateContext::SMB2_COMPRESSION_CAPABILITIES
@@ -33,8 +33,7 @@ module RubySMB
33
33
  raise RubySMB::Error::InvalidPacket.new(
34
34
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
35
35
  expected_cmd: RubySMB::SMB1::Packet::TreeConnectResponse::COMMAND,
36
- received_proto: response.smb_header.protocol,
37
- received_cmd: response.smb_header.command
36
+ packet: response
38
37
  )
39
38
  end
40
39
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
@@ -73,8 +72,7 @@ module RubySMB
73
72
  raise RubySMB::Error::InvalidPacket.new(
74
73
  expected_proto: RubySMB::SMB2::SMB2_PROTOCOL_ID,
75
74
  expected_cmd: RubySMB::SMB2::Packet::TreeConnectResponse::COMMAND,
76
- received_proto: response.smb2_header.protocol,
77
- received_cmd: response.smb2_header.command
75
+ packet: response
78
76
  )
79
77
  end
80
78
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
@@ -24,19 +24,25 @@ module RubySMB
24
24
  last_tree.id
25
25
  end
26
26
 
27
- def open(path, disposition=RubySMB::Dispositions::FILE_OPEN, write: false, read: true)
28
- file = last_tree.open_file(filename: path.sub(/^\\/, ''), write: write, read: read, disposition: disposition)
29
- @last_file_id = if file.respond_to?(:guid)
30
- file.guid.to_binary_s
31
- elsif file.respond_to?(:fid)
32
- file.fid.to_binary_s
33
- end
34
- @open_files[@last_file_id] = file
35
- @last_file_id
27
+ def open(path, disposition=RubySMB::Dispositions::FILE_OPEN, write: false, read: true, pipe: false)
28
+ if pipe
29
+ file = last_tree.open_pipe(filename: path, write: write, read: read, disposition: disposition)
30
+ else
31
+ file = last_tree.open_file(filename: path, write: write, read: read, disposition: disposition)
32
+ end
33
+ @last_file_id = if file.respond_to?(:guid)
34
+ # SMB2 uses guid
35
+ file.guid.to_binary_s
36
+ elsif file.respond_to?(:fid)
37
+ # SMB1 uses fid
38
+ file.fid.to_binary_s
39
+ end
40
+ @open_files[@last_file_id] = file
41
+ @last_file_id
36
42
  end
37
43
 
38
44
  def create_pipe(path, disposition=RubySMB::Dispositions::FILE_OPEN_IF)
39
- open(path.gsub(/\\/, ''), disposition, write: true, read: true)
45
+ open(path, disposition, write: true, read: true, pipe: true)
40
46
  end
41
47
 
42
48
  #Writes data to an open file handle