44# including protocol negotiation and authentication.
55
66require 'bundler/setup'
7+ require 'optparse'
78require 'ruby_smb'
89
9- def run_authentication ( address , smb1 , smb2 , smb3 , username , password )
10+ def run_authentication ( address , smb1 , smb2 , smb3 , username , password , domain )
1011 # Create our socket and add it to the dispatcher
1112 sock = TCPSocket . new address , 445
1213 dispatcher = RubySMB ::Dispatcher ::Socket . new ( sock )
1314
14- client = RubySMB ::Client . new ( dispatcher , smb1 : smb1 , smb2 : smb2 , smb3 : smb3 , username : username , password : password )
15+ client = RubySMB ::Client . new ( dispatcher , smb1 : smb1 , smb2 : smb2 , smb3 : smb3 , username : username , password : password , domain : domain )
1516 protocol = client . negotiate
1617 status = client . authenticate
1718 puts "#{ protocol } : #{ status } "
@@ -28,17 +29,64 @@ def run_authentication(address, smb1, smb2, smb3, username, password)
2829 puts "OS Version: #{ client . os_version } "
2930end
3031
31- address = ARGV [ 0 ]
32- username = ARGV [ 1 ]
33- password = ARGV [ 2 ]
34-
35- # Negotiate with SMB1, SMB2 and SMB3 enabled on the client
36- run_authentication ( address , true , true , true , username , password )
37- # Negotiate with both SMB1 and SMB2 enabled on the client
38- run_authentication ( address , true , true , false , username , password )
39- # Negotiate with only SMB1 enabled
40- run_authentication ( address , true , false , false , username , password )
41- # Negotiate with only SMB2 enabled
42- run_authentication ( address , false , true , false , username , password )
43- # Negotiate with only SMB3 enabled
44- run_authentication ( address , false , false , true , username , password )
32+ args = ARGV . dup
33+ options = {
34+ domain : '.' ,
35+ username : '' ,
36+ password : '' ,
37+ smbv1 : true ,
38+ smbv2 : true ,
39+ smbv3 : true ,
40+ target : nil
41+ }
42+ options [ :target ] = args . pop
43+ optparser = OptionParser . new do |opts |
44+ opts . banner = "Usage: #{ File . basename ( __FILE__ ) } [options] target"
45+ opts . on ( "--[no-]smbv1" , "Enable or disable SMBv1 (default: #{ options [ :smbv1 ] ? 'Enabled' : 'Disabled' } )" ) do |smbv1 |
46+ options [ :smbv1 ] = smbv1
47+ end
48+ opts . on ( "--[no-]smbv2" , "Enable or disable SMBv2 (default: #{ options [ :smbv2 ] ? 'Enabled' : 'Disabled' } )" ) do |smbv2 |
49+ options [ :smbv2 ] = smbv2
50+ end
51+ opts . on ( "--[no-]smbv3" , "Enable or disable SMBv3 (default: #{ options [ :smbv3 ] ? 'Enabled' : 'Disabled' } )" ) do |smbv3 |
52+ options [ :smbv3 ] = smbv3
53+ end
54+ opts . on ( "--username USERNAME" , "The account's username (default: #{ options [ :username ] } )" ) do |username |
55+ if username . include? ( '\\' )
56+ options [ :domain ] , options [ :username ] = username . split ( '\\' , 2 )
57+ else
58+ options [ :username ] = username
59+ end
60+ end
61+ opts . on ( "--password PASSWORD" , "The account's password (default: #{ options [ :password ] } )" ) do |password |
62+ options [ :password ] = password
63+ end
64+ end
65+ optparser . parse! ( args )
66+
67+ if options [ :target ] == '-h' || options [ :target ] == '--help'
68+ puts optparser . help
69+ exit
70+ end
71+
72+ if options [ :target ] . nil?
73+ abort ( optparser . help )
74+ end
75+
76+ # (smb1, smb2, smb3) combinations to exercise — filtered by the user's
77+ # --[no-]smbv{1,2,3} flags so that any combo requiring a disabled version
78+ # is skipped.
79+ combinations = [
80+ [ true , true , true ] , # SMB1, SMB2 and SMB3 enabled
81+ [ true , true , false ] , # SMB1 and SMB2 enabled
82+ [ true , false , false ] , # only SMB1 enabled
83+ [ false , true , false ] , # only SMB2 enabled
84+ [ false , false , true ] # only SMB3 enabled
85+ ]
86+
87+ combinations . each do |smb1 , smb2 , smb3 |
88+ next if smb1 && !options [ :smbv1 ]
89+ next if smb2 && !options [ :smbv2 ]
90+ next if smb3 && !options [ :smbv3 ]
91+ run_authentication ( options [ :target ] , smb1 , smb2 , smb3 , options [ :username ] , options [ :password ] , options [ :domain ] )
92+ end
0 commit comments