ruby_smb 2.0.0 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +3 -3
  3. data.tar.gz.sig +5 -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 +117 -53
  21. data/lib/ruby_smb/client/authentication.rb +7 -12
  22. data/lib/ruby_smb/client/echo.rb +2 -4
  23. data/lib/ruby_smb/client/negotiation.rb +31 -12
  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 +3 -2
  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 +9 -24
  77. data/lib/ruby_smb/smb1/pipe.rb +8 -6
  78. data/lib/ruby_smb/smb1/tree.rb +22 -9
  79. data/lib/ruby_smb/smb2/file.rb +46 -46
  80. data/lib/ruby_smb/smb2/packet/negotiate_response.rb +1 -1
  81. data/lib/ruby_smb/smb2/pipe.rb +9 -6
  82. data/lib/ruby_smb/smb2/tree.rb +30 -20
  83. data/lib/ruby_smb/version.rb +1 -1
  84. data/spec/lib/ruby_smb/client_spec.rb +248 -109
  85. data/spec/lib/ruby_smb/dcerpc/ndr_spec.rb +1396 -77
  86. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_authenticate3_request_spec.rb +69 -0
  87. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_authenticate3_response_spec.rb +53 -0
  88. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_password_set2_request_spec.rb +69 -0
  89. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_password_set2_response_spec.rb +37 -0
  90. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_req_challenge_request_spec.rb +45 -0
  91. data/spec/lib/ruby_smb/dcerpc/netlogon/netr_server_req_challenge_response_spec.rb +37 -0
  92. data/spec/lib/ruby_smb/dcerpc/rpc_security_attributes_spec.rb +161 -0
  93. data/spec/lib/ruby_smb/dcerpc/rrp_unicode_string_spec.rb +49 -12
  94. data/spec/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_request_spec.rb +191 -0
  95. data/spec/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_response_spec.rb +38 -0
  96. data/spec/lib/ruby_smb/dcerpc/svcctl/close_service_handle_request_spec.rb +30 -0
  97. data/spec/lib/ruby_smb/dcerpc/svcctl/close_service_handle_response_spec.rb +38 -0
  98. data/spec/lib/ruby_smb/dcerpc/svcctl/control_service_request_spec.rb +39 -0
  99. data/spec/lib/ruby_smb/dcerpc/svcctl/control_service_response_spec.rb +38 -0
  100. data/spec/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_request_spec.rb +78 -0
  101. data/spec/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_response_spec.rb +38 -0
  102. data/spec/lib/ruby_smb/dcerpc/svcctl/open_service_w_request_spec.rb +59 -0
  103. data/spec/lib/ruby_smb/dcerpc/svcctl/open_service_w_response_spec.rb +38 -0
  104. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_request_spec.rb +38 -0
  105. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_response_spec.rb +152 -0
  106. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_status_request_spec.rb +30 -0
  107. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_status_response_spec.rb +38 -0
  108. data/spec/lib/ruby_smb/dcerpc/svcctl/service_status_spec.rb +72 -0
  109. data/spec/lib/ruby_smb/dcerpc/svcctl/start_service_w_request_spec.rb +46 -0
  110. data/spec/lib/ruby_smb/dcerpc/svcctl/start_service_w_response_spec.rb +30 -0
  111. data/spec/lib/ruby_smb/dcerpc/svcctl_spec.rb +512 -0
  112. data/spec/lib/ruby_smb/dcerpc/winreg/create_key_request_spec.rb +110 -0
  113. data/spec/lib/ruby_smb/dcerpc/winreg/create_key_response_spec.rb +44 -0
  114. data/spec/lib/ruby_smb/dcerpc/winreg/enum_key_request_spec.rb +0 -4
  115. data/spec/lib/ruby_smb/dcerpc/winreg/enum_value_request_spec.rb +2 -2
  116. data/spec/lib/ruby_smb/dcerpc/winreg/enum_value_response_spec.rb +2 -2
  117. data/spec/lib/ruby_smb/dcerpc/winreg/open_root_key_request_spec.rb +9 -4
  118. data/spec/lib/ruby_smb/dcerpc/winreg/query_info_key_request_spec.rb +0 -4
  119. data/spec/lib/ruby_smb/dcerpc/winreg/query_value_request_spec.rb +17 -17
  120. data/spec/lib/ruby_smb/dcerpc/winreg/query_value_response_spec.rb +11 -23
  121. data/spec/lib/ruby_smb/dcerpc/winreg/save_key_request_spec.rb +57 -0
  122. data/spec/lib/ruby_smb/dcerpc/winreg/save_key_response_spec.rb +22 -0
  123. data/spec/lib/ruby_smb/dcerpc/winreg_spec.rb +227 -41
  124. data/spec/lib/ruby_smb/dispatcher/socket_spec.rb +12 -12
  125. data/spec/lib/ruby_smb/error_spec.rb +34 -5
  126. data/spec/lib/ruby_smb/field/stringz16_spec.rb +12 -0
  127. data/spec/lib/ruby_smb/generic_packet_spec.rb +7 -0
  128. data/spec/lib/ruby_smb/nbss/session_header_spec.rb +4 -11
  129. data/spec/lib/ruby_smb/smb1/file_spec.rb +1 -3
  130. data/spec/lib/ruby_smb/smb1/pipe_spec.rb +30 -5
  131. data/spec/lib/ruby_smb/smb1/tree_spec.rb +22 -0
  132. data/spec/lib/ruby_smb/smb2/file_spec.rb +73 -21
  133. data/spec/lib/ruby_smb/smb2/pipe_spec.rb +9 -5
  134. data/spec/lib/ruby_smb/smb2/tree_spec.rb +64 -7
  135. metadata +91 -2
  136. metadata.gz.sig +0 -0
@@ -13,9 +13,10 @@ module RubySMB
13
13
 
14
14
  rpc_hkey :hkey
15
15
  rrp_unicode_string :lp_value_name
16
- string :pad, length: -> { pad_length }
16
+ string :pad1, length: -> { pad_length(self.lp_value_name) }
17
17
  ndr_lp_dword :lp_type
18
- ndr_lp_byte :lp_data
18
+ ndr_lp_byte_array :lp_data
19
+ string :pad2, length: -> { pad_length(self.lp_data) }
19
20
  ndr_lp_dword :lpcb_data
20
21
  ndr_lp_dword :lpcb_len
21
22
 
@@ -24,10 +25,10 @@ module RubySMB
24
25
  @opnum = REG_QUERY_VALUE
25
26
  end
26
27
 
27
- # Determines the correct length for the padding in front of
28
- # #lp_type. It should always force a 4-byte alignment.
29
- def pad_length
30
- offset = (lp_value_name.abs_offset + lp_value_name.to_binary_s.length) % 4
28
+ # Determines the correct length for the padding, so that the next
29
+ # field is 4-byte aligned.
30
+ def pad_length(prev_element)
31
+ offset = (prev_element.abs_offset + prev_element.to_binary_s.length) % 4
31
32
  (4 - offset) % 4
32
33
  end
33
34
  end
@@ -9,22 +9,22 @@ module RubySMB
9
9
 
10
10
  endian :little
11
11
 
12
- ndr_lp_dword :lp_type
13
- ndr_lp_byte :lp_data
14
- string :pad, length: -> { pad_length }
15
- ndr_lp_dword :lpcb_data
16
- ndr_lp_dword :lpcb_len
17
- uint32 :error_status
12
+ ndr_lp_dword :lp_type
13
+ ndr_lp_byte_array :lp_data
14
+ string :pad, length: -> { pad_length(self.lp_data) }
15
+ ndr_lp_dword :lpcb_data
16
+ ndr_lp_dword :lpcb_len
17
+ uint32 :error_status
18
18
 
19
19
  def initialize_instance
20
20
  super
21
21
  @opnum = REG_QUERY_VALUE
22
22
  end
23
23
 
24
- # Determines the correct length for the padding in front of
25
- # #lpcb_data. It should always force a 4-byte alignment.
26
- def pad_length
27
- offset = (lp_data.abs_offset + lp_data.to_binary_s.length) % 4
24
+ # Determines the correct length for the padding, so that the next
25
+ # field is 4-byte aligned.
26
+ def pad_length(prev_element)
27
+ offset = (prev_element.abs_offset + prev_element.to_binary_s.length) % 4
28
28
  (4 - offset) % 4
29
29
  end
30
30
 
@@ -0,0 +1,37 @@
1
+ module RubySMB
2
+ module Dcerpc
3
+ module Winreg
4
+
5
+ class RpcHkey < Ndr::NdrContextHandle; end
6
+
7
+ # This class represents a BaseRegSaveKey Request Packet as defined in
8
+ # [3.1.5.20 BaseRegSaveKey (Opnum 20)](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rrp/f022247d-6ef1-4f46-b195-7f60654f4a0d)
9
+ class SaveKeyRequest < BinData::Record
10
+ attr_reader :opnum
11
+
12
+ endian :little
13
+
14
+ rpc_hkey :hkey
15
+ rrp_unicode_string :lp_file
16
+ string :pad, length: -> { pad_length(self.lp_file) }
17
+ prpc_security_attributes :lp_security_attributes
18
+
19
+ def initialize_instance
20
+ super
21
+ @opnum = REG_SAVE_KEY
22
+ end
23
+
24
+ # Determines the correct length for the padding, so that the next
25
+ # field is 4-byte aligned.
26
+ def pad_length(prev_element)
27
+ offset = (prev_element.abs_offset + prev_element.to_binary_s.length) % 4
28
+ (4 - offset) % 4
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+
@@ -0,0 +1,23 @@
1
+ module RubySMB
2
+ module Dcerpc
3
+ module Winreg
4
+
5
+ # This class represents a BaseRegSaveKey Response Packet as defined in
6
+ # [3.1.5.20 BaseRegSaveKey (Opnum 20)](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rrp/f022247d-6ef1-4f46-b195-7f60654f4a0d)
7
+ class SaveKeyResponse < BinData::Record
8
+ attr_reader :opnum
9
+
10
+ endian :little
11
+
12
+ uint32 :error_status
13
+
14
+ def initialize_instance
15
+ super
16
+ @opnum = REG_CREATE_KEY
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+
@@ -9,7 +9,7 @@ module RubySMB
9
9
  def nbss(packet)
10
10
  nbss = RubySMB::Nbss::SessionHeader.new
11
11
  nbss.session_packet_type = RubySMB::Nbss::SESSION_MESSAGE
12
- nbss.packet_length = packet.do_num_bytes
12
+ nbss.stream_protocol_length = packet.do_num_bytes
13
13
  nbss.to_binary_s
14
14
  end
15
15
 
@@ -61,19 +61,20 @@ module RubySMB
61
61
  # which are assumed to be the NetBiosSessionService header.
62
62
  # @raise [RubySMB::Error::CommunicationError] if the read timeout expires or an error occurs when reading the socket
63
63
  def recv_packet(full_response: false)
64
+ raise RubySMB::Error::CommunicationError, 'Connection has already been closed' if @tcp_socket.closed?
64
65
  if IO.select([@tcp_socket], nil, nil, @read_timeout).nil?
65
66
  raise RubySMB::Error::CommunicationError, "Read timeout expired when reading from the Socket (timeout=#{@read_timeout})"
66
67
  end
67
68
 
68
69
  begin
69
70
  nbss_data = @tcp_socket.read(4)
70
- raise IOError if nbss_data.nil?
71
+ raise RubySMB::Error::CommunicationError, 'Socket read returned nil' if nbss_data.nil?
71
72
  nbss_header = RubySMB::Nbss::SessionHeader.read(nbss_data)
72
73
  rescue IOError
73
74
  raise ::RubySMB::Error::NetBiosSessionService, 'NBSS Header is missing'
74
75
  end
75
76
 
76
- length = nbss_header.packet_length
77
+ length = nbss_header.stream_protocol_length
77
78
  data = full_response ? nbss_header.to_binary_s : ''
78
79
  if length > 0
79
80
  if IO.select([@tcp_socket], nil, nil, @read_timeout).nil?
@@ -16,23 +16,26 @@ module RubySMB
16
16
  # Raised when trying to parse raw binary into a Packet and the data
17
17
  # is invalid.
18
18
  class InvalidPacket < RubySMBError
19
+ attr_reader :status_code
19
20
  def initialize(args = nil)
20
21
  if args.nil?
21
22
  super
22
23
  elsif args.is_a? String
23
24
  super(args)
24
25
  elsif args.is_a? Hash
25
- expected_proto = args[:expected_proto] ? translate_protocol(args[:expected_proto]) : "???"
26
- expected_cmd = args[:expected_cmd] || "???"
27
- received_proto = args[:received_proto] ? translate_protocol(args[:received_proto]) : "???"
28
- received_cmd = args[:received_cmd] || "???"
26
+ expected_proto = args[:expected_proto] ? translate_protocol(args[:expected_proto]) : '???'
27
+ expected_cmd = args[:expected_cmd] || '???'
28
+ received_proto = args[:packet]&.packet_smb_version || '???'
29
+ received_cmd = get_cmd(args[:packet]) || '???'
30
+ @status_code = args[:packet]&.status_code
29
31
  super(
30
32
  "Expecting #{expected_proto} protocol "\
31
33
  "with command=#{expected_cmd}"\
32
34
  "#{(" (" + args[:expected_custom] + ")") if args[:expected_custom]}, "\
33
35
  "got #{received_proto} protocol "\
34
36
  "with command=#{received_cmd}"\
35
- "#{(" (" + args[:received_custom] + ")") if args[:received_custom]}"
37
+ "#{(" (" + args[:received_custom] + ")") if args[:received_custom]}"\
38
+ "#{(", Status: #{@status_code}") if @status_code}"
36
39
  )
37
40
  else
38
41
  raise ArgumentError, "InvalidPacket expects a String or a Hash, got a #{args.class}"
@@ -50,6 +53,19 @@ module RubySMB
50
53
  end
51
54
  end
52
55
  private :translate_protocol
56
+
57
+ def get_cmd(packet)
58
+ return nil unless packet
59
+ case packet.packet_smb_version
60
+ when 'SMB1'
61
+ packet.smb_header.command
62
+ when 'SMB2'
63
+ packet.smb2_header.command
64
+ else
65
+ nil
66
+ end
67
+ end
68
+ private :get_cmd
53
69
  end
54
70
 
55
71
  # Raised when a response packet has a NTStatus code that was unexpected.
@@ -25,12 +25,17 @@ module RubySMB
25
25
  # @see BinData::Stringz
26
26
  def read_and_return_value(io)
27
27
  max_length = eval_parameter(:max_length)
28
+ if max_length && max_length % 2 != 0
29
+ raise ArgumentError, "[Stringz16] #max_length should be a multiple of "\
30
+ "two, since it is Unicode (got #{max_length})"
31
+ end
28
32
  str = ''
29
33
  i = 0
30
34
  ch = nil
31
35
 
32
36
  # read until double NULL-byte or we have read in the max number of bytes
33
- while (ch != "\0\0") && (i != max_length)
37
+ loop do
38
+ break if ch == "\0\0" || (max_length && i == max_length)
34
39
  ch = io.readbytes(2)
35
40
  str << ch
36
41
  i += 2
@@ -46,6 +51,17 @@ module RubySMB
46
51
  def truncate_after_first_zero_byte!(str)
47
52
  str.sub!(/([^\0]*\0\0\0).*/, '\1')
48
53
  end
54
+
55
+ def trim_to!(str, max_length = nil)
56
+ if max_length
57
+ max_length = 2 if max_length < 2
58
+ str.slice!(max_length..-1)
59
+ if str.length == max_length && str[-2, 2] != "\0\0"
60
+ str[-2, 2] = "\0\0"
61
+ end
62
+ end
63
+ end
64
+
49
65
  end
50
66
  end
51
67
  end
@@ -49,7 +49,17 @@ module RubySMB
49
49
  when /SMB1/
50
50
  packet = RubySMB::SMB1::Packet::EmptyPacket.read(val)
51
51
  when /SMB2/
52
- packet = RubySMB::SMB2::Packet::ErrorPacket.read(val)
52
+ begin
53
+ packet = RubySMB::SMB2::Packet::ErrorPacket.read(val)
54
+ rescue RubySMB::Error::InvalidPacket
55
+ # Handle the case where an SMB2 error packet is expected, but the
56
+ # server sent an SMB1 empty packet instead. This behavior has been
57
+ # observed with older versions of Samba when something goes wrong
58
+ # on the server side. We just want to give it a chance and try to
59
+ # parse it as an SMB1 empty packet to keep information and avoid
60
+ # failing as much as possible.
61
+ packet = RubySMB::SMB1::Packet::EmptyPacket.read(val)
62
+ end
53
63
  else
54
64
  raise RubySMB::Error::InvalidPacket, 'Not a valid SMB packet'
55
65
  end
@@ -1,13 +1,13 @@
1
1
  module RubySMB
2
2
  module Nbss
3
3
  # Representation of the NetBIOS Session Service Header as defined in
4
- # [4.3.1 GENERAL FORMAT OF SESSION PACKETS](https://tools.ietf.org/html/rfc1002)
4
+ # SMB: [2.1 Transport](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb/f906c680-330c-43ae-9a71-f854e24aeee6)
5
+ # SMB2: [2.1 Transport](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/1dfacde4-b5c7-4494-8a14-a09d3ab4cc83)
5
6
  class SessionHeader < BinData::Record
6
7
  endian :big
7
8
 
8
- uint8 :session_packet_type, label: 'Session Packet Type'
9
- bit7 :flags, label: 'Flags', initial_value: 0
10
- bit17 :packet_length, label: 'Packet Length'
9
+ uint8 :session_packet_type, label: 'Session Packet Type', initial_value: 0
10
+ uint24 :stream_protocol_length, label: 'Stream Protocol Length'
11
11
  end
12
12
  end
13
13
  end
@@ -86,8 +86,7 @@ module RubySMB
86
86
  raise RubySMB::Error::InvalidPacket.new(
87
87
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
88
88
  expected_cmd: RubySMB::SMB1::Packet::CloseResponse::COMMAND,
89
- received_proto: response.smb_header.protocol,
90
- received_cmd: response.smb_header.command
89
+ packet: response
91
90
  )
92
91
  end
93
92
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
@@ -106,11 +105,7 @@ module RubySMB
106
105
  # @raise [RubySMB::Error::InvalidPacket] if the response packet is not valid
107
106
  # @raise [RubySMB::Error::UnexpectedStatusCode] if the response NTStatus is not STATUS_SUCCESS
108
107
  def read(bytes: @size, offset: 0)
109
- atomic_read_size = if bytes > @tree.client.max_buffer_size
110
- @tree.client.max_buffer_size
111
- else
112
- bytes
113
- end
108
+ atomic_read_size = [bytes, @tree.client.max_buffer_size].min
114
109
  remaining_bytes = bytes
115
110
  data = ''
116
111
 
@@ -122,8 +117,7 @@ module RubySMB
122
117
  raise RubySMB::Error::InvalidPacket.new(
123
118
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
124
119
  expected_cmd: RubySMB::SMB1::Packet::ReadAndxResponse::COMMAND,
125
- received_proto: response.smb_header.protocol,
126
- received_cmd: response.smb_header.command
120
+ packet: response
127
121
  )
128
122
  end
129
123
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
@@ -171,8 +165,7 @@ module RubySMB
171
165
  raise RubySMB::Error::InvalidPacket.new(
172
166
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
173
167
  expected_cmd: RubySMB::SMB1::Packet::ReadAndxResponse::COMMAND,
174
- received_proto: response.smb_header.protocol,
175
- received_cmd: response.smb_header.command
168
+ packet: response
176
169
  )
177
170
  end
178
171
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
@@ -193,8 +186,7 @@ module RubySMB
193
186
  raise RubySMB::Error::InvalidPacket.new(
194
187
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
195
188
  expected_cmd: RubySMB::SMB1::Packet::Trans2::SetFileInformationResponse::COMMAND,
196
- received_proto: response.smb_header.protocol,
197
- received_cmd: response.smb_header.command
189
+ packet: response
198
190
  )
199
191
  end
200
192
  response.status_code
@@ -227,11 +219,7 @@ module RubySMB
227
219
  total_bytes_written = 0
228
220
 
229
221
  loop do
230
- atomic_write_size = if bytes > @tree.client.max_buffer_size
231
- @tree.client.max_buffer_size
232
- else
233
- bytes
234
- end
222
+ atomic_write_size = [bytes, @tree.client.max_buffer_size].min
235
223
  write_request = write_packet(data: buffer.slice!(0, atomic_write_size), offset: offset)
236
224
  raw_response = @tree.client.send_recv(write_request)
237
225
  response = RubySMB::SMB1::Packet::WriteAndxResponse.read(raw_response)
@@ -239,8 +227,7 @@ module RubySMB
239
227
  raise RubySMB::Error::InvalidPacket.new(
240
228
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
241
229
  expected_cmd: RubySMB::SMB1::Packet::WriteAndxResponse::COMMAND,
242
- received_proto: response.smb_header.protocol,
243
- received_cmd: response.smb_header.command
230
+ packet: response
244
231
  )
245
232
  end
246
233
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
@@ -279,8 +266,7 @@ module RubySMB
279
266
  raise RubySMB::Error::InvalidPacket.new(
280
267
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
281
268
  expected_cmd: RubySMB::SMB1::Packet::WriteAndxResponse::COMMAND,
282
- received_proto: response.smb_header.protocol,
283
- received_cmd: response.smb_header.command
269
+ packet: response
284
270
  )
285
271
  end
286
272
  response.parameter_block.count_low
@@ -298,8 +284,7 @@ module RubySMB
298
284
  raise RubySMB::Error::InvalidPacket.new(
299
285
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
300
286
  expected_cmd: RubySMB::SMB1::Packet::Trans2::SetFileInformationResponse::COMMAND,
301
- received_proto: response.smb_header.protocol,
302
- received_cmd: response.smb_header.command
287
+ packet: response
303
288
  )
304
289
  end
305
290
  response.status_code
@@ -16,9 +16,13 @@ module RubySMB
16
16
  def initialize(tree:, response:, name:)
17
17
  raise ArgumentError, 'No Name Provided' if name.nil?
18
18
  case name
19
- when 'srvsvc'
19
+ when 'netlogon', '\\netlogon'
20
+ extend RubySMB::Dcerpc::Netlogon
21
+ when 'srvsvc', '\\srvsvc'
20
22
  extend RubySMB::Dcerpc::Srvsvc
21
- when 'winreg'
23
+ when 'svcctl', '\\svcctl'
24
+ extend RubySMB::Dcerpc::Svcctl
25
+ when 'winreg', '\\winreg'
22
26
  extend RubySMB::Dcerpc::Winreg
23
27
  end
24
28
  super(tree: tree, response: response, name: name)
@@ -41,8 +45,7 @@ module RubySMB
41
45
  raise RubySMB::Error::InvalidPacket.new(
42
46
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
43
47
  expected_cmd: RubySMB::SMB1::Packet::Trans::PeekNmpipeRequest::COMMAND,
44
- received_proto: response.smb_header.protocol,
45
- received_cmd: response.smb_header.command
48
+ packet: response
46
49
  )
47
50
  end
48
51
 
@@ -100,8 +103,7 @@ module RubySMB
100
103
  raise RubySMB::Error::InvalidPacket.new(
101
104
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
102
105
  expected_cmd: RubySMB::SMB1::Packet::Trans::TransactNmpipeResponse::COMMAND,
103
- received_proto: trans_nmpipe_response.smb_header.protocol,
104
- received_cmd: trans_nmpipe_response.smb_header.command
106
+ packet: trans_nmpipe_response
105
107
  )
106
108
  end
107
109
  unless [WindowsError::NTStatus::STATUS_SUCCESS,
@@ -49,13 +49,20 @@ module RubySMB
49
49
  raise RubySMB::Error::InvalidPacket.new(
50
50
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
51
51
  expected_cmd: RubySMB::SMB1::Packet::TreeDisconnectResponse::COMMAND,
52
- received_proto: response.smb_header.protocol,
53
- received_cmd: response.smb_header.command
52
+ packet: response
54
53
  )
55
54
  end
56
55
  response.status_code
57
56
  end
58
57
 
58
+ def open_pipe(opts)
59
+ # Make sure we don't modify the caller's hash options
60
+ opts = opts.dup
61
+ opts[:filename] = opts[:filename].dup
62
+ opts[:filename].prepend('\\') unless opts[:filename].start_with?('\\')
63
+ open_file(opts)
64
+ end
65
+
59
66
  # Open a file on the remote share.
60
67
  #
61
68
  # @example
@@ -123,18 +130,26 @@ module RubySMB
123
130
  raw_response = @client.send_recv(nt_create_andx_request)
124
131
  response = RubySMB::SMB1::Packet::NtCreateAndxResponse.read(raw_response)
125
132
  unless response.valid?
133
+ if response.is_a?(RubySMB::SMB1::Packet::EmptyPacket) &&
134
+ response.smb_header.protocol == RubySMB::SMB1::SMB_PROTOCOL_ID &&
135
+ response.smb_header.command == response.original_command
136
+ raise RubySMB::Error::InvalidPacket.new(
137
+ 'The response seems to be an SMB1 NtCreateAndxResponse but an '\
138
+ 'error occurs while parsing it. It is probably missing the '\
139
+ 'required extended information.'
140
+ )
141
+ end
126
142
  raise RubySMB::Error::InvalidPacket.new(
127
143
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
128
144
  expected_cmd: RubySMB::SMB1::Packet::NtCreateAndxResponse::COMMAND,
129
- received_proto: response.smb_header.protocol,
130
- received_cmd: response.smb_header.command
145
+ packet: response
131
146
  )
132
147
  end
133
148
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
134
149
  raise RubySMB::Error::UnexpectedStatusCode, response.status_code
135
150
  end
136
151
 
137
- case response.parameter_block.resource_type
152
+ case response.parameter_block.resource_type
138
153
  when RubySMB::SMB1::ResourceType::BYTE_MODE_PIPE, RubySMB::SMB1::ResourceType::MESSAGE_MODE_PIPE
139
154
  RubySMB::SMB1::Pipe.new(name: filename, tree: self, response: response)
140
155
  when RubySMB::SMB1::ResourceType::DISK
@@ -186,8 +201,7 @@ module RubySMB
186
201
  raise RubySMB::Error::InvalidPacket.new(
187
202
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
188
203
  expected_cmd: RubySMB::SMB1::Packet::Trans2::FindFirst2Response::COMMAND,
189
- received_proto: response.smb_header.protocol,
190
- received_cmd: response.smb_header.command
204
+ packet: response
191
205
  )
192
206
  end
193
207
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
@@ -221,8 +235,7 @@ module RubySMB
221
235
  raise RubySMB::Error::InvalidPacket.new(
222
236
  expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
223
237
  expected_cmd: RubySMB::SMB1::Packet::Trans2::FindNext2Response::COMMAND,
224
- received_proto: response.smb_header.protocol,
225
- received_cmd: response.smb_header.command
238
+ packet: response
226
239
  )
227
240
  end
228
241
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS