From e86ca56dd1a64b0d5b959b99ebe2bceefe8dd045 Mon Sep 17 00:00:00 2001 From: RageLtMan Date: Tue, 1 Mar 2016 02:06:55 -0500 Subject: [PATCH] add :closed? method to Meterpreter Channel Implement a check for self.cid.nil? in Meterpreter's Channel class in the :closed? method for compatibility with the Socket's :closed? Touch up the Rex DNS server's stop method using this method on pivot sockets. Add SOL_SOCKET and SO_REUSEADDR options to the Rex UDP sockets created by the DNS components - the server socket, as well as the client abstraction socket. --- lib/rex/post/meterpreter/channel.rb | 7 +++++++ lib/rex/proto/dns/server.rb | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/rex/post/meterpreter/channel.rb b/lib/rex/post/meterpreter/channel.rb index dbb89d34ff..10dc519467 100644 --- a/lib/rex/post/meterpreter/channel.rb +++ b/lib/rex/post/meterpreter/channel.rb @@ -250,6 +250,13 @@ class Channel written.nil? ? 0 : written.value end + # + # Wrapper around check for self.cid + # + def closed? + self.cid.nil? + end + # # Wrapper around the low-level close. # diff --git a/lib/rex/proto/dns/server.rb b/lib/rex/proto/dns/server.rb index 51ac95d536..0bcfb58422 100644 --- a/lib/rex/proto/dns/server.rb +++ b/lib/rex/proto/dns/server.rb @@ -190,6 +190,7 @@ class Server if self.serve_udp @udp_sock = Rex::Socket::Udp.create(self.sock_options) + udp_sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, 1) self.listener_thread = Rex::ThreadFactory.spawn("UDPDNSServerListener", false) { monitor_listener } @@ -221,7 +222,7 @@ class Server ensure while csock = ensure_close.shift csock.stop if csock.respond_to?(:stop) - csock.close unless csock.closed? + csock.close csock.respond_to?(:close) and !csock.closed? end end self.cache.stop(flush_cache) @@ -299,6 +300,7 @@ protected 'LocalHost' => self.udp_sock.localhost, 'LocalPort' => self.udp_sock.localport ) + cli.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, 1) dispatch_request(cli, buf) end end