Skip to content

Commit 5a4abeb

Browse files
committed
make Rex UDPSocket.send work just like the real thing
1 parent 4f0569c commit 5a4abeb

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

lib/rex/post/meterpreter/channels/datagram.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@ def recvfrom_nonblock(length, flags = 0)
3939
[data, sockaddr]
4040
end
4141

42-
def send(buf, flags, saddr)
43-
channel.send(buf, flags, saddr)
42+
#
43+
# This should work just like a UDPSocket.send method
44+
#
45+
# send(mesg, flags, host, port) => numbytes_sent click to toggle source
46+
# send(mesg, flags, sockaddr_to) => numbytes_sent
47+
# send(mesg, flags) => numbytes_sent
48+
#
49+
def send(buf, flags, a = nil, b = nil)
50+
channel.send(buf, flags, a, b)
4451
end
4552
end
4653

lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/udp_channel.rb

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,41 @@ def initialize(client, cid, type, flags)
7777
end
7878

7979
#
80-
# This function is called by Rex::Socket::Udp.sendto and writes data to a specified
81-
# remote peer host/port via the remote end of the channel.
80+
# This function is called by Rex::Socket::Udp.sendto and writes data to a
81+
# specified remote peer host/port via the remote end of the channel.
8282
#
83-
def send(buf, flags, saddr)
84-
_af, peerhost, peerport = Rex::Socket.from_sockaddr(saddr)
83+
# This should work just like a UDPSocket.send method
84+
#
85+
# send(mesg, flags, host, port) => numbytes_sent click to toggle source
86+
# send(mesg, flags, sockaddr_to) => numbytes_sent
87+
# send(mesg, flags) => numbytes_sent
88+
#
89+
def send(buf, flags, a = nil, b = nil)
90+
host = nil
91+
port = nil
8592

86-
addends = [
87-
{
88-
'type' => TLV_TYPE_PEER_HOST,
89-
'value' => peerhost
90-
},
91-
{
92-
'type' => TLV_TYPE_PEER_PORT,
93-
'value' => peerport
94-
}
95-
]
93+
if a && b.nil?
94+
_, host, port = Rex::Socket.from_sockaddr(a)
95+
elsif a && b
96+
host = a
97+
port = b
98+
end
99+
100+
addends = nil
101+
if host && port
102+
addends = [
103+
{
104+
'type' => TLV_TYPE_PEER_HOST,
105+
'value' => host
106+
},
107+
{
108+
'type' => TLV_TYPE_PEER_PORT,
109+
'value' => port
110+
}
111+
]
112+
end
96113

97-
return _write(buf, buf.length, addends)
114+
_write(buf, buf.length, addends)
98115
end
99116

100117
end

0 commit comments

Comments
 (0)