Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/proto/ms_tds/channel.rb
27907 views
1
# -*- coding: binary -*-
2
3
require 'bindata'
4
5
module Rex::Proto::MsTds
6
class Channel
7
include Rex::IO::StreamAbstraction
8
9
attr_reader :params
10
11
# the socket that makes the outbound connection to the SQL server
12
attr_reader :sock
13
14
def initialize(opts = {})
15
@params = Rex::Socket::Parameters.from_hash(opts)
16
17
# it doesn't work this way so throw an exception so that's clear
18
raise RuntimeError.new('SSL incompatible with MsTds::Socket') if @params.ssl
19
raise ArgumentError.new('MsTds::Socket must be TCP') if @params.proto != 'tcp'
20
21
@sock = Rex::Socket.create_param(@params)
22
initialize_abstraction
23
24
lsock.initinfo(@sock.peerinfo, @sock.localinfo)
25
26
monitor_sock(@sock, sink: method(:_read_handler), name: 'MonitorLocal', on_exit: method(:_exit_handler))
27
end
28
29
def write(buf, opts = {})
30
if negotiating_ssl?
31
Rex::IO::RelayManager.io_write_all(self.sock, [18, 0x01, buf.length + 8, 0x0000, 0x00, 0x00].pack('CCnnCC') + buf) - 8
32
else
33
Rex::IO::RelayManager.io_write_all(self.sock, buf)
34
end
35
end
36
37
def starttls
38
self.lsock.starttls(params)
39
end
40
41
protected
42
43
def negotiating_ssl?
44
return false unless self.lsock.is_a?(Rex::Socket::SslTcp)
45
return false if self.lsock.sslsock.state.start_with?('SSLOK')
46
47
true
48
end
49
50
def _exit_handler
51
self.rsock.close
52
end
53
54
def _read_handler(buf)
55
if negotiating_ssl?
56
Rex::IO::RelayManager.io_write_all(self.rsock, buf[8..]) + 8
57
else
58
Rex::IO::RelayManager.io_write_all(self.rsock, buf)
59
end
60
end
61
end
62
end
63
64