2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-09-16 09:27:41 +00:00
|
|
|
|
|
|
|
$:.unshift(File.join(File.dirname(__FILE__), '..', '..', '..'))
|
|
|
|
|
2005-12-13 06:08:40 +00:00
|
|
|
require 'rex/test'
|
2005-09-16 09:27:41 +00:00
|
|
|
require 'rex/proto/smb/constants'
|
2005-09-22 09:04:40 +00:00
|
|
|
require 'rex/proto/smb/exceptions'
|
2005-09-16 09:27:41 +00:00
|
|
|
require 'rex/proto/smb/utils'
|
|
|
|
require 'rex/proto/smb/client'
|
2005-09-22 09:04:40 +00:00
|
|
|
require 'rex/proto/dcerpc'
|
2005-09-19 23:35:51 +00:00
|
|
|
require 'rex/socket'
|
2005-09-16 09:27:41 +00:00
|
|
|
|
|
|
|
class Rex::Proto::SMB::Client::UnitTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
Klass = Rex::Proto::SMB::Client
|
|
|
|
|
2005-09-22 09:04:40 +00:00
|
|
|
# Alias over the Rex DCERPC protocol modules
|
|
|
|
DCERPCPacket = Rex::Proto::DCERPC::Packet
|
|
|
|
DCERPCClient = Rex::Proto::DCERPC::Client
|
|
|
|
DCERPCResponse = Rex::Proto::DCERPC::Response
|
|
|
|
DCERPCUUID = Rex::Proto::DCERPC::UUID
|
|
|
|
|
2005-09-23 03:43:04 +00:00
|
|
|
def test_smb_open_share
|
|
|
|
|
|
|
|
share = 'C$'
|
|
|
|
|
2005-09-23 06:27:22 +00:00
|
|
|
write_data = ('A' * 256)
|
2005-09-23 03:43:04 +00:00
|
|
|
filename = 'smb_test.txt'
|
2005-12-13 06:08:40 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
begin
|
|
|
|
timeout($_REX_TEST_TIMEOUT) {
|
2005-09-23 03:43:04 +00:00
|
|
|
s = Rex::Socket.create_tcp(
|
2005-12-13 06:08:40 +00:00
|
|
|
'PeerHost' => $_REX_TEST_SMB_HOST,
|
|
|
|
'PeerPort' => 139
|
2005-09-23 03:43:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
c = Klass.new(s)
|
|
|
|
|
|
|
|
# Request a SMB session over NetBIOS
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Requesting a SMB session over NetBIOS..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.session_request()
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
|
|
|
# Check for a positive session response
|
|
|
|
# A negative response is 0x83
|
|
|
|
assert_equal(ok.v['Type'], 0x82)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Negotiating SMB dialects..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.negotiate()
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Authenticating with NTLMv2..."
|
2006-06-01 15:30:26 +00:00
|
|
|
ok = c.session_setup_ntlmv2($_REX_TEXT_SMB_USER, $_REX_TEXT_SMB_PASS)
|
2005-09-23 03:43:04 +00:00
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
assert_not_equal(c.auth_user_id, 0)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Connecting to the share..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.tree_connect(share)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
assert_not_equal(c.last_tree_id, 0)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Opening a file for write..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.open(filename)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
assert_not_equal(c.last_file_id, 0)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Writing data to the test file..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.write(c.last_file_id, 0, write_data)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
assert_equal(ok['Payload'].v['CountLow'], write_data.length)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Closing the test file..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.close(c.last_file_id)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Opening a file for read..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.open(filename, 1)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
assert_not_equal(c.last_file_id, 0)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Reading data from the test file..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.read(c.last_file_id, 0, write_data.length)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
assert_equal(ok['Payload'].v['DataLenLow'], write_data.length)
|
|
|
|
|
|
|
|
read_data = ok.to_s.slice(
|
|
|
|
ok['Payload'].v['DataOffset'] + 4,
|
|
|
|
ok['Payload'].v['DataLenLow']
|
|
|
|
)
|
|
|
|
assert_equal(read_data, write_data)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Closing the test file..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.close(c.last_file_id)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Disconnecting from the tree..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.tree_disconnect
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
|
|
|
s.close
|
|
|
|
|
|
|
|
|
|
|
|
# Reconnect and delete the file
|
2005-09-19 23:35:51 +00:00
|
|
|
s = Rex::Socket.create_tcp(
|
2005-12-13 06:08:40 +00:00
|
|
|
'PeerHost' => $_REX_TEST_SMB_HOST,
|
|
|
|
'PeerPort' => 139
|
2005-09-19 23:35:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
c = Klass.new(s)
|
2005-09-16 09:27:41 +00:00
|
|
|
|
2005-09-19 23:35:51 +00:00
|
|
|
# Request a SMB session over NetBIOS
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Requesting a SMB session over NetBIOS..."
|
2005-09-19 23:35:51 +00:00
|
|
|
ok = c.session_request()
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
|
|
|
# Check for a positive session response
|
|
|
|
# A negative response is 0x83
|
|
|
|
assert_equal(ok.v['Type'], 0x82)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Negotiating SMB dialects..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.negotiate()
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Authenticating with NTLMv2..."
|
2006-06-01 15:30:26 +00:00
|
|
|
ok = c.session_setup_ntlmv2($_REX_TEXT_SMB_USER, $_REX_TEXT_SMB_PASS)
|
2005-09-23 03:43:04 +00:00
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
assert_not_equal(c.auth_user_id, 0)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Connecting to the share..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.tree_connect(share)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
assert_not_equal(c.last_tree_id, 0)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Deleting the test file..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.delete(filename)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Diconnecting from the tree..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.tree_disconnect
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
|
|
|
s.close
|
2006-01-27 05:28:16 +00:00
|
|
|
}
|
|
|
|
rescue Timeout::Error
|
|
|
|
flunk('timeout')
|
|
|
|
end
|
2005-09-23 03:43:04 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_smb_session_request
|
2006-01-27 05:28:16 +00:00
|
|
|
begin
|
|
|
|
timeout($_REX_TEST_TIMEOUT) {
|
2005-09-23 03:43:04 +00:00
|
|
|
s = Rex::Socket.create_tcp(
|
2005-12-13 06:08:40 +00:00
|
|
|
'PeerHost' => $_REX_TEST_SMB_HOST,
|
|
|
|
'PeerPort' => 139
|
2005-09-23 03:43:04 +00:00
|
|
|
)
|
2005-09-19 23:35:51 +00:00
|
|
|
|
2005-09-23 03:43:04 +00:00
|
|
|
c = Klass.new(s)
|
|
|
|
|
|
|
|
# Request a SMB session over NetBIOS
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Requesting a SMB session over NetBIOS..."
|
2005-09-23 03:43:04 +00:00
|
|
|
ok = c.session_request()
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
|
|
|
# Check for a positive session response
|
|
|
|
# A negative response is 0x83
|
|
|
|
assert_equal(ok.v['Type'], 0x82)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Negotiating SMB dialects..."
|
2005-09-19 23:35:51 +00:00
|
|
|
ok = c.negotiate()
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Authenticating with NTLMv2..."
|
2005-09-19 23:35:51 +00:00
|
|
|
ok = c.session_setup_ntlmv2
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
2005-09-22 04:04:06 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Authenticating with NTLMv1..."
|
2005-09-22 04:04:06 +00:00
|
|
|
ok = c.session_setup_ntlmv1
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Authenticating with clear text passwords..."
|
2005-09-23 03:43:04 +00:00
|
|
|
begin
|
|
|
|
ok = c.session_setup_clear
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
rescue Rex::Proto::SMB::Exceptions::ErrorCode
|
|
|
|
if ($!.error_code != 0x00010002)
|
|
|
|
raise $!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Connecting to IPC$..."
|
2005-09-22 04:04:06 +00:00
|
|
|
ok = c.tree_connect
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
2005-09-23 03:43:04 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Opening the \BROWSER pipe..."
|
2006-05-30 16:11:00 +00:00
|
|
|
ok = c.create_pipe('\BROWSER')
|
2005-09-22 09:04:40 +00:00
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
|
|
|
vers = DCERPCUUID.vers_by_name('SRVSVC')
|
|
|
|
uuid = DCERPCUUID.uuid_by_name('SRVSVC')
|
2005-09-23 03:43:04 +00:00
|
|
|
bind, ctx = DCERPCPacket.make_bind_fake_multi(uuid, vers)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
# puts "[*] Binding to the Server Service..."
|
2005-09-22 09:04:40 +00:00
|
|
|
ok = c.trans_named_pipe(c.last_file_id, bind)
|
|
|
|
assert_kind_of(Rex::Struct2::CStruct, ok)
|
|
|
|
|
|
|
|
data = ok.to_s.slice(
|
|
|
|
ok['Payload'].v['DataOffset'] + 4,
|
|
|
|
ok['Payload'].v['DataCount']
|
|
|
|
)
|
2005-09-23 03:43:04 +00:00
|
|
|
assert_not_equal(data, nil)
|
2005-09-22 09:04:40 +00:00
|
|
|
|
2005-09-23 03:43:04 +00:00
|
|
|
resp = DCERPCResponse.new(data)
|
2005-09-22 09:04:40 +00:00
|
|
|
assert_equal(resp.type, 12)
|
2006-01-27 05:28:16 +00:00
|
|
|
}
|
|
|
|
rescue Timeout::Error
|
|
|
|
flunk('timeout')
|
|
|
|
end
|
2005-09-16 09:27:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|