rapid7 / ruby_smb

A native Ruby implementation of the SMB Protocol Family
Other
81 stars 83 forks source link

Fix File Read Operations #193

Closed zeroSteiner closed 2 years ago

zeroSteiner commented 2 years ago

Since commit 6cca27c, the open file operations no longer remove the \ prefix on file paths. This broke file read operations as used by Metasploit. This resulted in execution failures when Metasploit's psexec module was used with the Command target which needs to read the file output. This readds the logic to remove the unnecessary prefix when opening files. This change has been tested using the newly updated read_file example on SMB versions 1, 2, 3 with and without a path with a \ prefix.

Demo

This example shows reading a file from the temp directory that was created using psexec. SMB3 is used, but can be disabled using the CLI options on the example. Note that the file path contains a \ prefix.

ruby read_file.rb --username smcintyre --password Password1 192.168.159.96 C$ "\\Windows\\Temp\\uDDauZxRI.txt"
SMB3 : (0x00000000) STATUS_WAIT_0: The caller specified WaitAny for WaitType and one of the dispatcher objects in the Object array has been set to the signaled state.
Connected to \\192.168.159.96\C$ successfully!

User accounts for \\

-------------------------------------------------------------------------------
$431000-ERJAFULIQUSS     Administrator            aliddle                  
Guest                    HealthMailbox0896988     HealthMailbox23c9938     
HealthMailbox256d6c9     HealthMailbox3dd648f     HealthMailbox3ef3d8d     
HealthMailbox8710d04     HealthMailbox8c45655     HealthMailboxa04271a     
HealthMailboxcad83a9     HealthMailboxde93ebe     HealthMailboxf4602f9     
jdoe                     krbtgt                   SM_2e9af84fe4c6473ea     
SM_3879f1f6de9b49818     SM_3c6a2d0a4c8648c9b     SM_5784a59a716948118     
SM_97642fc203314e6f8     SM_9db3f69793764fde9     SM_ac24b3f263bb427f9     
SM_c2d9a2c8d3844cd29     SM_f5faf24b3b1c46efa     smcintyre                
The command completed with one or more errors.
PSexec bug output Using the "Command" target. ``` [*] 192.168.159.96:445 - Connecting to the server... [*] 192.168.159.96:445 - Authenticating to 192.168.159.96:445 as user 'smcintyre'... [+] 192.168.159.96:445 - Service start timed out, OK if running a command or non-service executable... [-] 192.168.159.96:445 - Unable to get handle: The server responded with an unexpected status code: STATUS_INVALID_PARAMETER [-] 192.168.159.96:445 - Command seems to still be executing. Try increasing RETRY and DELAY [*] 192.168.159.96:445 - Getting the command output... [-] 192.168.159.96:445 - Unable to read file \Windows\Temp\uDDauZxRI.txt. RubySMB::Error::UnexpectedStatusCode: The server responded with an unexpected status code: STATUS_INVALID_PARAMETER. [-] 192.168.159.96:445 - Error getting command output [*] 192.168.159.96:445 - Executing cleanup... [-] 192.168.159.96:445 - Unable to cleanup \Windows\Temp\HvXgAIlbtIRw.bat. Error: The server responded with an unexpected status code: STATUS_INVALID_PARAMETER [+] 192.168.159.96:445 - Cleanup was successful [*] Exploit completed, but no session was created. ```
cdelafuente-r7 commented 2 years ago

I tested against multiple SMB servers and found out Samba v3.5.4 and 3.6.6 returned STATUS_OBJECT_NAME_NOT_FOUND when opening a named pipe with SMBv1. Everything else works perfectly while opening files and pipes with SMBv1/2/3.

It looks like the issue that the https://github.com/rapid7/ruby_smb/commit/6cca27ce3f00d7ea9b5225c2f8143776a80ed45f commit fixed came back. Maybe a keyword argument could be added to SMBv1 open_file that indicates if the leading backslash should be removed? Since open_pipe calls open_file, this flag could be set only for named pipes.

zeroSteiner commented 2 years ago

Oh yeah I see the issue you're referring to. What if instead of adding a new keyword argument though, I moved the bulk of the logic from open_file into a new function that both open_pipe and open_file called? Then open_(file|pipe) could update the filename parameter as appropriate before dispatching to the new function that would contain almost all the code currently in open_file that just wouldn't modify the filename parameter? Maybe a private _open method?

cdelafuente-r7 commented 2 years ago

Sounds perfect to me! go for it, please! Also, would it be possible to do it for both SMBv1 and SMBv2? I know there is no issue with SMBv2 and backslashes, but just for consistency.

cdelafuente-r7 commented 2 years ago

Thanks for updating this! I retested and everything is working as expected now. Specs will be added in a separate PR, as we discussed offline. I'll go ahead and land it.