Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/meterpreter/extensions/stdapi/net/socket.rb
Views: 11794
# -*- coding: binary -*-12require 'thread'3require 'rex/socket'4require 'rex/post/meterpreter/extensions/stdapi/tlv'5require 'rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_client_channel'6require 'rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_server_channel'7require 'rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/udp_channel'89module Rex10module Post11module Meterpreter12module Extensions13module Stdapi14module Net1516###17#18# This class provides an interface to interacting with sockets19# on the remote machine. It allows callers to open TCP, UDP,20# and other arbitrary socket-based connections as channels that21# can then be interacted with through the established22# meterpreter connection.23#24###25class Socket26TLV_PARAM_MAP = {27TLV_TYPE_CONNECT_RETRIES => 'Retries',28TLV_TYPE_LOCAL_HOST => 'LocalHost',29TLV_TYPE_LOCAL_PORT => 'LocalPort',30TLV_TYPE_PEER_HOST => 'PeerHost',31TLV_TYPE_PEER_PORT => 'PeerPort'32}3334##35#36# Constructor37#38##3940#41# Initialize the socket subsystem and start monitoring sockets as they come42# in.43#44def initialize(client)45self.client = client4647# register the inbound handler for the tcp server channel (allowing us to48# receive new client connections to a tcp server channel)49client.register_inbound_handler(Rex::Post::Meterpreter::Extensions::Stdapi::Net::SocketSubsystem::TcpServerChannel)5051end5253#54# Deregister the inbound handler for the tcp server channel55#56def shutdown57client.deregister_inbound_handler(Rex::Post::Meterpreter::Extensions::Stdapi::Net::SocketSubsystem::TcpServerChannel)58end5960#61# Process a response packet and extract TLVs that are relevant for updating62# socket parameters.63#64def self.parameters_from_response(response)65params = {}66TLV_PARAM_MAP.each do |tlv_type, param_key|67value = response.get_tlv_value(tlv_type)68next if value.nil?69params[param_key] = value70end71Rex::Socket::Parameters.from_hash(params)72end7374##75#76# Factory77#78##7980#81# Creates an arbitrary client socket channel using the information supplied82# in the socket parameters instance. The +params+ argument is expected to be83# of type Rex::Socket::Parameters.84#85def create(params)86res = nil8788if params.tcp?89if params.server?90res = create_tcp_server_channel(params)91else92res = create_tcp_client_channel(params)93end94elsif params.udp?95res = create_udp_channel(params)96end9798return res99end100101#102# Create a TCP server channel.103#104def create_tcp_server_channel(params)105begin106return SocketSubsystem::TcpServerChannel.open(client, params)107rescue ::Rex::Post::Meterpreter::RequestError => e108case e.code109when 10048110raise ::Rex::AddressInUse.new(params.localhost, params.localport)111when 10000 .. 10100112raise ::Rex::ConnectionError.new113end114raise e115end116end117118#119# Creates a TCP client channel.120#121def create_tcp_client_channel(params)122begin123channel = SocketSubsystem::TcpClientChannel.open(client, params)124if channel != nil125return channel.lsock126end127return nil128rescue ::Rex::Post::Meterpreter::RequestError => e129case e.code130when 10000 .. 10100131raise ::Rex::ConnectionError.new132end133raise e134end135end136137#138# Creates a UDP channel.139#140def create_udp_channel(params)141begin142channel = SocketSubsystem::UdpChannel.open(client, params)143if channel != nil144return channel.lsock145end146return nil147rescue ::Rex::Post::Meterpreter::RequestError => e148case e.code149when 10048150raise ::Rex::AddressInUse.new(params.localhost, params.localport)151when 10000 .. 10100152raise ::Rex::ConnectionError.new153end154raise e155end156end157158159protected160161attr_accessor :client # :nodoc:162163end164165end; end; end; end; end; end166167168169