@@ -289,33 +289,29 @@ def smb2_grooms(grooms, payload_hdr_pkt)
289
289
end
290
290
end
291
291
292
- def smb1_anonymous_connect_ipc ( )
292
+ def smb1_anonymous_connect_ipc
293
293
sock = connect ( false )
294
294
dispatcher = RubySMB ::Dispatcher ::Socket . new ( sock )
295
295
client = RubySMB ::Client . new ( dispatcher , smb1 : true , smb2 : false , username : '' , password : '' )
296
296
response_code = client . login
297
297
298
+ authed = false
298
299
unless response_code == ::WindowsError ::NTStatus ::STATUS_SUCCESS
299
- if datastore [ 'SMBUser' ] . present? && datastore [ 'SMBPass' ] . present?
300
- client = RubySMB ::Client . new (
301
- dispatcher ,
302
- smb1 : true ,
303
- smb2 : false ,
304
- username : datastore [ 'SMBUser' ] ,
305
- password : datastore [ 'SMBPass' ] ,
306
- domain : datastore [ 'SMBDomain' ]
307
- )
308
- response_code = client . login
309
-
310
- unless response_code == ::WindowsError ::NTStatus ::STATUS_SUCCESS
311
- raise RubySMB ::Error ::UnexpectedStatusCode , "Error with credentialed login: #{ response_code . to_s } "
312
- end
300
+ client = authenticated_login ( dispatcher )
301
+ authed = true
302
+ end
303
+ os = client . peer_native_os
304
+
305
+ begin
306
+ tree = client . tree_connect ( "\\ \\ #{ datastore [ 'RHOST' ] } \\ IPC$" )
307
+ rescue RubySMB ::Error ::UnexpectedStatusCode => e
308
+ if authed
309
+ raise e
313
310
else
314
- raise RubySMB ::Error ::UnexpectedStatusCode , "Error with anonymous login: #{ response_code . to_s } "
311
+ client = authenticated_login
312
+ tree = client . tree_connect ( "\\ \\ #{ datastore [ 'RHOST' ] } \\ IPC$" )
315
313
end
316
314
end
317
- os = client . peer_native_os
318
- tree = client . tree_connect ( "\\ \\ #{ datastore [ 'RHOST' ] } \\ IPC$" )
319
315
320
316
return client , tree , sock , os
321
317
end
@@ -782,4 +778,25 @@ def make_kernel_shellcode
782
778
783
779
end
784
780
781
+ def authenticated_login ( dispatcher )
782
+ if datastore [ 'SMBUser' ] . present? && datastore [ 'SMBPass' ] . present?
783
+ client = RubySMB ::Client . new (
784
+ dispatcher ,
785
+ smb1 : true ,
786
+ smb2 : false ,
787
+ username : datastore [ 'SMBUser' ] ,
788
+ password : datastore [ 'SMBPass' ] ,
789
+ domain : datastore [ 'SMBDomain' ]
790
+ )
791
+ response_code = client . login
792
+
793
+ unless response_code == ::WindowsError ::NTStatus ::STATUS_SUCCESS
794
+ raise RubySMB ::Error ::UnexpectedStatusCode , "Error with credentialed login: #{ response_code . to_s } "
795
+ end
796
+ else
797
+ raise RubySMB ::Error ::UnexpectedStatusCode , "Error with anonymous login: #{ response_code . to_s } "
798
+ end
799
+ client
800
+ end
801
+
785
802
end
0 commit comments