CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/lib/rex/post/meterpreter/channels/socket_abstraction.rb
Views: 1904
# -*- coding: binary -*-12require 'rex/post/channel'3require 'rex/post/meterpreter/channel'45module Rex6module Post7module Meterpreter89###10#11# Abstraction12# ------13#14# This class represents a channel that is streaming. This means15# that sequential data is flowing in either one or both directions.16#17###18module SocketAbstraction1920include Rex::Post::Channel::SocketAbstraction2122#23# Simple mixin for lsock in order to help avoid a ruby interpreter issue with ::Socket.pair24# Instead of writing to the lsock, reading from the rsock and then writing to the channel,25# we use this mixin to directly write to the channel.26#27# Note: This does not work with OpenSSL as OpenSSL is implemented natively and requires a real28# socket to write to and we cant intercept the sockets syswrite at a native level.29#30# Note: The deadlock only seems to effect the Ruby build for cygwin.31#32module DirectChannelWrite3334def syswrite(buf)35channel._write(buf)36end3738attr_accessor :channel39end4041##42#43# Constructor44#45##4647#48# Passes the initialization information up to the base class49#50def initialize(client, cid, type, flags, packet, **_)51# sf: initialize_abstraction() before super() as we can get a scenario where dio_write_handler() is called52# with data to write to the rsock but rsock has not yet been initialized. This happens if the channel53# is registered (client.add_channel(self) in Channel.initialize) to a session and a COMMAND_ID_CORE_CHANNEL_WRITE54# request comes in before we have called self.initialize_abstraction()55initialize_abstraction56super(client, cid, type, flags, packet)57end5859##60#61# Remote I/O handlers62#63##6465#66# Performs a write operation on the right side of the local stream.67#68def dio_write_handler(packet, data)69rv = Rex::ThreadSafe.select(nil, [rsock], nil, 0.01)70if(rv)71rsock.syswrite(data)72return true73else74return false75end76end7778#79# Performs a close operation on the right side of the local stream.80#81def dio_close_handler(packet)82rsock.close8384return super(packet)85end8687#88# Cleans up the stream abstraction.89#90def cleanup91super9293cleanup_abstraction94end9596#97# Wrap the _write() call in order to catch some common, but harmless Windows exceptions98#99def _write(*args)100begin101super(*args)102rescue ::Rex::Post::Meterpreter::RequestError => e103case e.code104when 10000 .. 10100105raise ::Rex::ConnectionError.new106end107end108end109110end111112end; end; end113114115