Path: blob/master/lib/rex/proto/ms_tds/channel.rb
27907 views
# -*- coding: binary -*-12require 'bindata'34module Rex::Proto::MsTds5class Channel6include Rex::IO::StreamAbstraction78attr_reader :params910# the socket that makes the outbound connection to the SQL server11attr_reader :sock1213def initialize(opts = {})14@params = Rex::Socket::Parameters.from_hash(opts)1516# it doesn't work this way so throw an exception so that's clear17raise RuntimeError.new('SSL incompatible with MsTds::Socket') if @params.ssl18raise ArgumentError.new('MsTds::Socket must be TCP') if @params.proto != 'tcp'1920@sock = Rex::Socket.create_param(@params)21initialize_abstraction2223lsock.initinfo(@sock.peerinfo, @sock.localinfo)2425monitor_sock(@sock, sink: method(:_read_handler), name: 'MonitorLocal', on_exit: method(:_exit_handler))26end2728def write(buf, opts = {})29if negotiating_ssl?30Rex::IO::RelayManager.io_write_all(self.sock, [18, 0x01, buf.length + 8, 0x0000, 0x00, 0x00].pack('CCnnCC') + buf) - 831else32Rex::IO::RelayManager.io_write_all(self.sock, buf)33end34end3536def starttls37self.lsock.starttls(params)38end3940protected4142def negotiating_ssl?43return false unless self.lsock.is_a?(Rex::Socket::SslTcp)44return false if self.lsock.sslsock.state.start_with?('SSLOK')4546true47end4849def _exit_handler50self.rsock.close51end5253def _read_handler(buf)54if negotiating_ssl?55Rex::IO::RelayManager.io_write_all(self.rsock, buf[8..]) + 856else57Rex::IO::RelayManager.io_write_all(self.rsock, buf)58end59end60end61end626364