Class: RubySMB::Client
- Inherits:
-
Object
- Object
- RubySMB::Client
- Includes:
- Authentication, Echo, Negotiation, Signing, TreeConnect
- Defined in:
- lib/ruby_smb/client.rb,
lib/ruby_smb/client/echo.rb,
lib/ruby_smb/client/signing.rb,
lib/ruby_smb/client/negotiation.rb,
lib/ruby_smb/client/tree_connect.rb,
lib/ruby_smb/client/authentication.rb
Overview
Represents an SMB client capable of talking to SMB1 or SMB2 servers and handling all end-user client functionality.
Defined Under Namespace
Modules: Authentication, Echo, Negotiation, Signing, TreeConnect
Constant Summary collapse
- SMB1_DIALECT_SMB1_DEFAULT =
The Default SMB1 Dialect string used in an SMB1 Negotiate Request
"NT LM 0.12"- SMB1_DIALECT_SMB2_DEFAULT =
The Default SMB2 Dialect string used in an SMB1 Negotiate Request
"SMB 2.002"- SMB2_DIALECT_DEFAULT =
Dialect value for SMB2 Default (Version 2.02)
0x0202
Instance Attribute Summary collapse
- #dispatcher ⇒ RubySMB::Dispatcher::Socket
- #domain ⇒ String
- #host ⇒ String
- #local_workstation ⇒ String
- #ntlm_client ⇒ String
- #password ⇒ String
- #peer_native_os ⇒ String
- #sequence_counter ⇒ Integer
- #session_id ⇒ Integer
- #signing_enabled ⇒ Boolean
-
#signing_required ⇒ Object
Whether or not the Server requires signing.
- #smb1 ⇒ Boolean
- #smb2 ⇒ Boolean
- #smb2_message_id ⇒ Integer
- #user_id ⇒ String
- #username ⇒ String
Attributes included from Signing
Instance Method Summary collapse
-
#disconnect! ⇒ void
Logs off any currently open session on the server and closes the TCP socket connection.
-
#echo(count: 1, data: '') ⇒ WindowsError::ErrorCode
Sends an Echo request to the server and returns the NTStatus of the last response packet received.
-
#increment_smb_message_id(packet) ⇒ RubySMB::GenericPacket
Sets the message id field in an SMB2 packet's header to the one tracked by the client.
-
#initialize(dispatcher, smb1: true, smb2: true, username:, password:, domain: '.', local_workstation: 'WORKSTATION') ⇒ Client
constructor
A new instance of Client.
-
#login(username: self.username, password: self.password, domain: self.domain, local_workstation: self.local_workstation) ⇒ Object
Performs protocol negotiation and session setup.
-
#logoff! ⇒ WindowsError::ErrorCode
Sends a LOGOFF command to the remote server to terminate the session.
-
#send_recv(packet) ⇒ String
Sends a packet and receives the raw response through the Dispatcher.
-
#tree_connect(share) ⇒ RubySMB::SMB1::Tree, RubySMB::SMB2::Tree
Connects to the supplied share.
-
#wipe_state! ⇒ void
Resets all of the session state on the client, setting it back to scratch.
Methods included from Echo
Methods included from TreeConnect
#smb1_tree_connect, #smb1_tree_from_response, #smb2_tree_connect, #smb2_tree_from_response
Methods included from Signing
Methods included from Authentication
#authenticate, #smb1_anonymous_auth, #smb1_anonymous_auth_request, #smb1_anonymous_auth_response, #smb1_authenticate, #smb1_ntlmssp_auth_packet, #smb1_ntlmssp_authenticate, #smb1_ntlmssp_challenge_packet, #smb1_ntlmssp_final_packet, #smb1_ntlmssp_negotiate, #smb1_ntlmssp_negotiate_packet, #smb1_type2_message, #smb2_authenticate, #smb2_ntlmssp_auth_packet, #smb2_ntlmssp_authenticate, #smb2_ntlmssp_challenge_packet, #smb2_ntlmssp_final_packet, #smb2_ntlmssp_negotiate, #smb2_ntlmssp_negotiate_packet, #smb2_type2_message
Methods included from Negotiation
#negotiate, #negotiate_request, #negotiate_response, #parse_negotiate_response, #smb1_negotiate_request, #smb2_negotiate_request
Constructor Details
#initialize(dispatcher, smb1: true, smb2: true, username:, password:, domain: '.', local_workstation: 'WORKSTATION') ⇒ Client
Returns a new instance of Client.
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 |
# File 'lib/ruby_smb/client.rb', line 107 def initialize(dispatcher, smb1: true, smb2: true, username:,password:, domain:'.', local_workstation:'WORKSTATION') raise ArgumentError, 'No Dispatcher provided' unless dispatcher.kind_of? RubySMB::Dispatcher::Base if smb1 == false && smb2 == false raise ArgumentError, 'You must enable at least one Protocol' end @dispatcher = dispatcher @domain = domain @local_workstation = local_workstation @password = password.encode("utf-8") || ''.encode("utf-8") @sequence_counter = 0 @session_id = 0x00 @session_key = '' @signing_required = false @smb1 = smb1 @smb2 = smb2 @username = username.encode("utf-8") || ''.encode("utf-8") @host = dispatcher.tcp_socket.peeraddr[2] @ntlm_client = Net::NTLM::Client.new( @username, @password, workstation: @local_workstation, domain: @domain ) = 0 end |
Instance Attribute Details
#dispatcher ⇒ RubySMB::Dispatcher::Socket
29 30 31 |
# File 'lib/ruby_smb/client.rb', line 29 def dispatcher @dispatcher end |
#domain ⇒ String
34 35 36 |
# File 'lib/ruby_smb/client.rb', line 34 def domain @domain end |
#host ⇒ String
39 40 41 |
# File 'lib/ruby_smb/client.rb', line 39 def host @host end |
#local_workstation ⇒ String
44 45 46 |
# File 'lib/ruby_smb/client.rb', line 44 def local_workstation @local_workstation end |
#ntlm_client ⇒ String
49 50 51 |
# File 'lib/ruby_smb/client.rb', line 49 def ntlm_client @ntlm_client end |
#password ⇒ String
54 55 56 |
# File 'lib/ruby_smb/client.rb', line 54 def password @password end |
#peer_native_os ⇒ String
60 61 62 |
# File 'lib/ruby_smb/client.rb', line 60 def peer_native_os @peer_native_os end |
#sequence_counter ⇒ Integer
67 68 69 |
# File 'lib/ruby_smb/client.rb', line 67 def sequence_counter @sequence_counter end |
#session_id ⇒ Integer
72 73 74 |
# File 'lib/ruby_smb/client.rb', line 72 def session_id @session_id end |
#signing_enabled ⇒ Boolean
77 |
# File 'lib/ruby_smb/client.rb', line 77 attr_accessor :signing_required |
#signing_required ⇒ Object
Whether or not the Server requires signing
77 78 79 |
# File 'lib/ruby_smb/client.rb', line 77 def signing_required @signing_required end |
#smb1 ⇒ Boolean
82 83 84 |
# File 'lib/ruby_smb/client.rb', line 82 def smb1 @smb1 end |
#smb2 ⇒ Boolean
87 88 89 |
# File 'lib/ruby_smb/client.rb', line 87 def smb2 @smb2 end |
#smb2_message_id ⇒ Integer
92 93 94 |
# File 'lib/ruby_smb/client.rb', line 92 def end |
#user_id ⇒ String
102 103 104 |
# File 'lib/ruby_smb/client.rb', line 102 def user_id @user_id end |
#username ⇒ String
97 98 99 |
# File 'lib/ruby_smb/client.rb', line 97 def username @username end |
Instance Method Details
#disconnect! ⇒ void
This method returns an undefined value.
Logs off any currently open session on the server and closes the TCP socket connection.
139 140 141 142 143 144 145 146 |
# File 'lib/ruby_smb/client.rb', line 139 def disconnect! begin logoff! rescue wipe_state! end dispatcher.tcp_socket.close end |
#echo(count: 1, data: '') ⇒ WindowsError::ErrorCode
Sends an Echo request to the server and returns the NTStatus of the last response packet received.
154 155 156 157 158 159 160 161 |
# File 'lib/ruby_smb/client.rb', line 154 def echo(count: 1, data: '' ) if smb2 response = smb2_echo else response = smb1_echo(count:count, data:data) end response.status_code end |
#increment_smb_message_id(packet) ⇒ RubySMB::GenericPacket
Sets the message id field in an SMB2 packet's header to the one tracked by the client. It then increments the counter on the client.
169 170 171 172 173 174 175 |
# File 'lib/ruby_smb/client.rb', line 169 def (packet) if packet.smb2_header. == 0 && self. != 0 packet.smb2_header. = self. self. += 1 end packet end |
#login(username: self.username, password: self.password, domain: self.domain, local_workstation: self.local_workstation) ⇒ Object
Performs protocol negotiation and session setup. It defaults to using the credentials supplied during initialization, but can take a new set of credentials if needed.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/ruby_smb/client.rb', line 179 def login(username: self.username, password: self.password, domain: self.domain, local_workstation: self.local_workstation ) @domain = domain @local_workstation = local_workstation @password = password.encode("utf-8") || ''.encode("utf-8") @username = username.encode("utf-8") || ''.encode("utf-8") @ntlm_client = Net::NTLM::Client.new( @username, @password, workstation: @local_workstation, domain: @domain ) negotiate authenticate end |
#logoff! ⇒ WindowsError::ErrorCode
Sends a LOGOFF command to the remote server to terminate the session
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/ruby_smb/client.rb', line 199 def logoff! if smb2 request = RubySMB::SMB2::Packet::LogoffRequest.new raw_response = send_recv(request) response = RubySMB::SMB2::Packet::LogoffResponse.read(raw_response) else request = RubySMB::SMB1::Packet::LogoffRequest.new raw_response = send_recv(request) response = RubySMB::SMB1::Packet::LogoffResponse.read(raw_response) end wipe_state! response.status_code end |
#send_recv(packet) ⇒ String
Sends a packet and receives the raw response through the Dispatcher. It will also sign the packet if neccessary.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/ruby_smb/client.rb', line 218 def send_recv(packet) case packet.packet_smb_version when 'SMB1' if self.user_id packet.smb_header.uid = self.user_id end packet = smb1_sign(packet) when 'SMB2' packet = (packet) packet.smb2_header.session_id = self.session_id unless packet.is_a?(RubySMB::SMB2::Packet::SessionSetupRequest) packet = smb2_sign(packet) end else packet = packet end dispatcher.send_packet(packet) raw_response = dispatcher.recv_packet if self.signing_required && !self.session_key.empty? self.sequence_counter += 1 end raw_response end |
#tree_connect(share) ⇒ RubySMB::SMB1::Tree, RubySMB::SMB2::Tree
Connects to the supplied share
248 249 250 251 252 253 254 |
# File 'lib/ruby_smb/client.rb', line 248 def tree_connect(share) if smb2 smb2_tree_connect(share) else smb1_tree_connect(share) end end |
#wipe_state! ⇒ void
This method returns an undefined value.
Resets all of the session state on the client, setting it back to scratch. Should only be called when a session is no longer valid.
261 262 263 264 265 266 267 |
# File 'lib/ruby_smb/client.rb', line 261 def wipe_state! self.session_id = 0x00 self.user_id = 0x00 self.session_key = '' self.sequence_counter = 0 self. = 0 end |