2005-09-23 09:14:03 +00:00
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module SMB
|
|
|
|
class SimpleClient
|
|
|
|
|
|
|
|
require 'rex/text'
|
|
|
|
require 'rex/struct2'
|
|
|
|
require 'rex/proto/smb/constants'
|
|
|
|
require 'rex/proto/smb/exceptions'
|
|
|
|
require 'rex/proto/smb/evasions'
|
|
|
|
require 'rex/proto/smb/crypt'
|
|
|
|
require 'rex/proto/smb/utils'
|
|
|
|
require 'rex/proto/smb/client'
|
|
|
|
|
|
|
|
# Some short-hand class aliases
|
|
|
|
CONST = Rex::Proto::SMB::Constants
|
|
|
|
CRYPT = Rex::Proto::SMB::Crypt
|
|
|
|
UTILS = Rex::Proto::SMB::Utils
|
|
|
|
XCEPT = Rex::Proto::SMB::Exceptions
|
|
|
|
EVADE = Rex::Proto::SMB::Evasions
|
|
|
|
|
|
|
|
|
|
|
|
class OpenFile
|
|
|
|
attr_accessor :name, :tree_id, :file_id, :mode, :client, :chunk_size
|
|
|
|
|
|
|
|
def initialize(client, name, tree_id, file_id)
|
|
|
|
self.client = client
|
|
|
|
self.name = name
|
|
|
|
self.tree_id = tree_id
|
|
|
|
self.file_id = file_id
|
|
|
|
self.chunk_size = 48000
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
|
|
|
begin
|
|
|
|
self.close
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
self.client.delete(self.name, self.tree_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Close this open file
|
|
|
|
def close
|
|
|
|
self.client.close(self.file_id, self.tree_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Read data from the file
|
2006-04-26 16:59:44 +00:00
|
|
|
def read(length = nil, offset = 0)
|
2005-09-23 09:14:03 +00:00
|
|
|
if (length == nil)
|
|
|
|
data = ''
|
|
|
|
fptr = offset
|
|
|
|
ok = self.client.read(self.file_id, fptr, self.chunk_size)
|
|
|
|
while (ok['Payload'].v['DataLenLow'] > 0)
|
|
|
|
buff = ok.to_s.slice(
|
|
|
|
ok['Payload'].v['DataOffset'] + 4,
|
|
|
|
ok['Payload'].v['DataLenLow']
|
|
|
|
)
|
|
|
|
data << buff
|
2006-01-27 05:28:16 +00:00
|
|
|
if ok['Payload'].v['Remaining'] == 0
|
|
|
|
break
|
|
|
|
end
|
2005-09-23 09:14:03 +00:00
|
|
|
fptr += ok['Payload'].v['DataLenLow']
|
|
|
|
ok = self.client.read(self.file_id, fptr, self.chunk_size)
|
|
|
|
end
|
|
|
|
return data
|
|
|
|
else
|
|
|
|
ok = self.client.read(self.file_id, offset, length)
|
|
|
|
data = ok.to_s.slice(
|
|
|
|
ok['Payload'].v['DataOffset'] + 4,
|
|
|
|
ok['Payload'].v['DataLenLow']
|
|
|
|
)
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def << (data)
|
|
|
|
self.write(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Write data to the file
|
|
|
|
def write(data, offset = 0)
|
|
|
|
# Track our offset into the remote file
|
|
|
|
fptr = offset
|
|
|
|
|
|
|
|
# Duplicate the data so we can use slice!
|
|
|
|
data = data.dup
|
|
|
|
|
|
|
|
# Take our first chunk of bytes
|
|
|
|
chunk = data.slice!(0, self.chunk_size)
|
|
|
|
|
|
|
|
# Keep writing data until we run out
|
|
|
|
while (chunk.length > 0)
|
2006-06-01 15:18:53 +00:00
|
|
|
ok = self.client.write(self.file_id, fptr, chunk)
|
2005-09-23 09:14:03 +00:00
|
|
|
cl = ok['Payload'].v['CountLow']
|
|
|
|
|
|
|
|
# Partial write, push the failed data back into the queue
|
|
|
|
if (cl != chunk.length)
|
|
|
|
data = chunk.slice(cl - 1, chunk.length - cl) + data
|
|
|
|
end
|
|
|
|
|
|
|
|
# Increment our painter and grab the next chunk
|
|
|
|
fptr += cl
|
|
|
|
chunk = data.slice!(0, self.chunk_size)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-04-26 16:59:44 +00:00
|
|
|
class OpenPipe < OpenFile
|
|
|
|
|
|
|
|
# Valid modes are: 'trans' and 'rw'
|
|
|
|
attr_accessor :mode
|
|
|
|
|
2006-05-03 05:53:37 +00:00
|
|
|
def initialize(*args)
|
2006-04-26 16:59:44 +00:00
|
|
|
super(*args)
|
2006-05-30 16:11:00 +00:00
|
|
|
self.mode = 'rw'
|
2006-04-26 16:59:44 +00:00
|
|
|
@buff = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_buffer(length, offset=0)
|
2006-05-03 05:53:37 +00:00
|
|
|
length ||= @buff.length
|
2006-04-26 16:59:44 +00:00
|
|
|
@buff.slice!(0, length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(length = nil, offset = 0)
|
|
|
|
case self.mode
|
|
|
|
when 'trans'
|
|
|
|
read_buffer(length, offset)
|
|
|
|
when 'rw'
|
|
|
|
super(length, offset)
|
|
|
|
else
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(data, offset = 0)
|
|
|
|
case self.mode
|
|
|
|
|
|
|
|
when 'trans'
|
|
|
|
write_trans(data, offset)
|
|
|
|
when 'rw'
|
|
|
|
super(data, offset)
|
|
|
|
else
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_trans(data, offset=0)
|
2006-05-03 05:53:37 +00:00
|
|
|
# Payload is not being filled the the response !?!!?
|
2006-04-26 16:59:44 +00:00
|
|
|
ack = self.client.trans_named_pipe(self.file_id, data)
|
2006-05-03 05:53:37 +00:00
|
|
|
@buff << ack['Payload'].v['Payload']
|
2006-04-26 16:59:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-09-23 09:14:03 +00:00
|
|
|
|
|
|
|
# Public accessors
|
|
|
|
attr_accessor :last_error
|
|
|
|
|
|
|
|
# Private accessors
|
|
|
|
attr_accessor :socket, :client, :direct, :shares, :last_share
|
|
|
|
|
|
|
|
# Pass the socket object and a boolean indicating whether the socket is netbios or cifs
|
2006-04-30 19:49:27 +00:00
|
|
|
def initialize(socket, direct = false)
|
2005-09-23 09:14:03 +00:00
|
|
|
self.socket = socket
|
|
|
|
self.direct = direct
|
|
|
|
self.client = Rex::Proto::SMB::Client.new(socket)
|
|
|
|
self.shares = { }
|
|
|
|
end
|
|
|
|
|
2006-06-17 17:36:19 +00:00
|
|
|
def login(name = '', user = '', pass = '', domain = '')
|
|
|
|
|
2005-09-23 09:14:03 +00:00
|
|
|
begin
|
|
|
|
|
|
|
|
if (self.direct != true)
|
|
|
|
self.client.session_request(name)
|
|
|
|
end
|
2005-12-13 06:08:40 +00:00
|
|
|
|
2005-09-23 09:14:03 +00:00
|
|
|
self.client.negotiate
|
|
|
|
ok = self.client.session_setup(user, pass, domain)
|
2006-06-17 17:36:19 +00:00
|
|
|
rescue ::Exception
|
2005-09-23 09:14:03 +00:00
|
|
|
e = XCEPT::LoginError.new
|
2006-06-23 06:20:52 +00:00
|
|
|
e.source = $!.to_s
|
2005-09-23 09:14:03 +00:00
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2006-04-08 06:35:35 +00:00
|
|
|
def connect(share)
|
2005-09-23 09:14:03 +00:00
|
|
|
ok = self.client.tree_connect(share)
|
|
|
|
tree_id = ok['Payload']['SMB'].v['TreeID']
|
|
|
|
self.shares[share] = tree_id
|
|
|
|
self.last_share = share
|
|
|
|
end
|
|
|
|
|
2006-04-08 06:35:35 +00:00
|
|
|
def disconnect(share)
|
2005-09-23 09:14:03 +00:00
|
|
|
ok = self.client.tree_disconnect(self.shares[share])
|
|
|
|
self.shares.delete(share)
|
|
|
|
end
|
|
|
|
|
2006-04-08 06:35:35 +00:00
|
|
|
def open(path, perm)
|
2005-10-02 06:53:39 +00:00
|
|
|
mode = UTILS.open_mode_to_mode(perm)
|
|
|
|
access = UTILS.open_mode_to_access(perm)
|
|
|
|
|
|
|
|
ok = self.client.open(path, mode, access)
|
2005-09-23 09:14:03 +00:00
|
|
|
file_id = ok['Payload'].v['FileID']
|
|
|
|
|
|
|
|
fh = OpenFile.new(self.client, path, self.client.last_tree_id, file_id)
|
|
|
|
end
|
|
|
|
|
2006-04-08 06:35:35 +00:00
|
|
|
def delete(*args)
|
2005-09-23 09:14:03 +00:00
|
|
|
self.client.delete(*args)
|
|
|
|
end
|
|
|
|
|
2006-04-08 06:35:35 +00:00
|
|
|
def create_pipe(path, perm = 'c')
|
2005-10-03 13:51:05 +00:00
|
|
|
disposition = UTILS.create_mode_to_disposition(perm)
|
2006-04-30 19:49:27 +00:00
|
|
|
ok = self.client.create_pipe(path, disposition)
|
2005-10-03 13:51:05 +00:00
|
|
|
file_id = ok['Payload'].v['FileID']
|
2006-04-26 16:59:44 +00:00
|
|
|
fh = OpenPipe.new(self.client, path, self.client.last_tree_id, file_id)
|
2005-10-02 06:53:39 +00:00
|
|
|
end
|
|
|
|
|
2006-06-01 15:18:53 +00:00
|
|
|
def trans_pipe(fid, data, no_response = nil)
|
|
|
|
client.trans_named_pipe(fid, data, no_response)
|
2006-04-08 06:35:35 +00:00
|
|
|
end
|
|
|
|
|
2005-09-23 09:14:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|