2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-09-23 09:14:03 +00:00
|
|
|
|
|
|
|
$:.unshift(File.join(File.dirname(__FILE__), '..', '..', '..'))
|
|
|
|
|
2005-12-13 06:08:40 +00:00
|
|
|
require 'rex/test'
|
2005-09-23 09:14:03 +00:00
|
|
|
require 'rex/proto/smb'
|
|
|
|
require 'rex/proto/dcerpc'
|
|
|
|
require 'rex/socket'
|
|
|
|
|
2005-12-02 02:35:30 +00:00
|
|
|
class Rex::Proto::SMB::SimpleClient::UnitTest < Test::Unit::TestCase
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-09-23 09:14:03 +00:00
|
|
|
Klass = Rex::Proto::SMB::SimpleClient
|
|
|
|
|
|
|
|
# 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-10-03 13:51:05 +00:00
|
|
|
XCEPT = Rex::Proto::SMB::Exceptions
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-09-23 09:14:03 +00:00
|
|
|
FILE_CREATE = 0x10
|
|
|
|
FILE_TRUNC = 0x02
|
|
|
|
FILE_OPEN = 0x01
|
2009-11-02 18:14:57 +00:00
|
|
|
|
|
|
|
|
2005-09-23 09:14:03 +00:00
|
|
|
def test_smb_open_share
|
|
|
|
user = 'SMBTest'
|
|
|
|
pass = 'SMBTest'
|
|
|
|
share = 'C$'
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-10-02 06:53:39 +00:00
|
|
|
write_data = ('A' * (1024 * 8))
|
2005-09-23 09:14:03 +00:00
|
|
|
filename = 'smb_tester.txt'
|
2006-01-27 05:28:16 +00:00
|
|
|
begin
|
2009-11-02 18:14:57 +00:00
|
|
|
Timeout.timeout($_REX_TEST_TIMEOUT) {
|
2005-09-23 09:14:03 +00:00
|
|
|
s = Rex::Socket.create_tcp(
|
2005-12-13 06:08:40 +00:00
|
|
|
'PeerHost' => $_REX_TEST_SMB_HOST,
|
|
|
|
'PeerPort' => 445
|
2005-09-23 09:14:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
c = Klass.new(s, true)
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
begin
|
2005-12-13 06:08:40 +00:00
|
|
|
c.login('*SMBSERVER', user, pass)
|
2006-01-27 05:28:16 +00:00
|
|
|
rescue XCEPT::LoginError
|
|
|
|
flunk('login failure')
|
|
|
|
end
|
2005-12-13 06:08:40 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
c.connect(share)
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
f = c.open(filename, 'rwct')
|
2005-12-13 06:08:40 +00:00
|
|
|
f << write_data
|
|
|
|
f.close
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
f = c.open(filename, 'ro')
|
2005-12-13 06:08:40 +00:00
|
|
|
d = f.read()
|
|
|
|
f.close
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-12-13 06:08:40 +00:00
|
|
|
c.delete(filename)
|
|
|
|
c.disconnect(share)
|
2005-12-02 02:35:30 +00:00
|
|
|
|
2009-11-02 18:14:57 +00:00
|
|
|
s.close
|
2006-01-27 05:28:16 +00:00
|
|
|
}
|
|
|
|
rescue Timeout::Error
|
|
|
|
flunk('timeout')
|
|
|
|
end
|
2005-12-02 02:35:30 +00:00
|
|
|
end
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
def test_smb_dcerpc
|
|
|
|
begin
|
2009-11-02 18:14:57 +00:00
|
|
|
Timeout.timeout($_REX_TEST_TIMEOUT) {
|
2005-12-02 02:35:30 +00:00
|
|
|
s = Rex::Socket.create_tcp(
|
2005-12-13 06:08:40 +00:00
|
|
|
'PeerHost' => $_REX_TEST_SMB_HOST,
|
|
|
|
'PeerPort' => 445
|
2005-12-02 02:35:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
c = Klass.new(s, true)
|
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
user = ''
|
|
|
|
pass = ''
|
2005-12-02 02:35:30 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
begin
|
|
|
|
c.login('*SMBSERVER', user, pass)
|
|
|
|
rescue XCEPT::LoginError
|
|
|
|
flunk('login failure')
|
|
|
|
end
|
2005-12-13 06:08:40 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
c.connect('IPC$')
|
2005-12-13 06:08:40 +00:00
|
|
|
f = c.create_pipe('\BROWSER')
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
bind, ctx = DCERPCPacket.make_bind_fake_multi(
|
|
|
|
'4b324fc8-1670-01d3-1278-5a47bf6ee188', '3.0',
|
2009-11-02 18:14:57 +00:00
|
|
|
10,
|
2006-01-27 05:28:16 +00:00
|
|
|
4
|
|
|
|
)
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-12-13 06:27:34 +00:00
|
|
|
# Evasion techniques:
|
2005-12-13 06:08:40 +00:00
|
|
|
# 1) Write the bind out a few bytes at a time with a random offset
|
|
|
|
# 2) Read the response back a few bytes at a time with a random offset
|
2005-12-13 06:27:34 +00:00
|
|
|
|
2005-12-13 06:08:40 +00:00
|
|
|
# Write the bind request out in random chunk sizes
|
|
|
|
while (bind.length > 0)
|
|
|
|
f.write( bind.slice!(0, (rand(20)+5)), rand(1024)+1 )
|
2006-01-27 05:28:16 +00:00
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-12-13 06:08:40 +00:00
|
|
|
d = ''
|
|
|
|
# Read the response back a few bytes a time
|
|
|
|
begin
|
|
|
|
while(true)
|
|
|
|
t = (f.read((rand(20)+5), rand(1024)+1))
|
|
|
|
last if ! t.length
|
2006-01-27 05:28:16 +00:00
|
|
|
d << t
|
|
|
|
end
|
|
|
|
rescue XCEPT::NoReply
|
|
|
|
end
|
2005-09-23 09:14:03 +00:00
|
|
|
|
2006-01-27 05:28:16 +00:00
|
|
|
r = DCERPCResponse.new(d)
|
2005-12-13 06:08:40 +00:00
|
|
|
assert_equal(r.type, 12)
|
|
|
|
assert_equal(r.ack_result[ctx-0], 0)
|
|
|
|
assert_equal(r.ack_result[ctx-1], 2)
|
|
|
|
|
2009-11-02 18:14:57 +00:00
|
|
|
s.close
|
2006-01-27 05:28:16 +00:00
|
|
|
}
|
|
|
|
rescue Timeout::Error
|
|
|
|
flunk('timeout')
|
|
|
|
end
|
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
end
|
|
|
|
|