CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/post/channel/socket_abstraction.rb
Views: 11783
1
# -*- coding: binary -*-
2
3
module Rex
4
module Post
5
module Channel
6
module SocketAbstraction
7
###
8
#
9
# This interface is meant to be included by channelized sockets. It updates
10
# their getname methods to correctly report the information based on the
11
# channel object (which must have a `#params` attribute).
12
#
13
###
14
module SocketInterface
15
include Rex::Socket
16
17
def getsockname
18
return super unless channel
19
20
# Find the first host in our chain (our address)
21
hops = 0
22
csock = channel.client.sock
23
while csock.respond_to?('channel')
24
csock = csock.channel.client.sock
25
hops += 1
26
end
27
_address_family, caddr, _cport = csock.getsockname
28
address_family, raddr, _rport = csock.getpeername_as_array
29
_maddr = channel.params.localhost
30
mport = channel.params.localport
31
[ address_family, "#{caddr}#{(hops > 0) ? "-_#{hops}_" : ''}-#{raddr}", mport ]
32
end
33
34
def getpeername
35
return super if !channel
36
37
maddr = channel.params.peerhost
38
mport = channel.params.peerport
39
::Socket.sockaddr_in(mport, maddr)
40
end
41
42
%i[localhost localport peerhost peerport].map do |meth|
43
define_method(meth) do
44
return super if !channel
45
46
channel.params.send(meth)
47
end
48
end
49
50
def close
51
super
52
channel.cleanup_abstraction
53
channel.close
54
end
55
56
attr_accessor :channel
57
end
58
end
59
end
60
end
61
end
62
63