From 3defcc3929ce42971526e56fc68ae93a324c3b23 Mon Sep 17 00:00:00 2001 From: Spencer McIntyre Date: Wed, 27 May 2026 09:35:25 -0400 Subject: [PATCH 1/2] Fix the address resolution for hostnames Rex::Socket.getaddress can return either IPv4 or IPv6 addresses so when it's a hostname, use the socket type hint (#v6) to resolve the correct family of address. --- lib/rex/socket/comm/local.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/rex/socket/comm/local.rb b/lib/rex/socket/comm/local.rb index dd8485d..b6ebd22 100644 --- a/lib/rex/socket/comm/local.rb +++ b/lib/rex/socket/comm/local.rb @@ -256,7 +256,13 @@ def self.create_by_type(param, type, proto = 0) ip = param.proxies.first.host port = param.proxies.first.port else - ip = Rex::Socket.getaddress(param.peerhost) + if Rex::Socket.is_ip_addr?(param.peerhost) + ip = param.peerhost + elsif param.v6 + ip = Rex::Socket.getaddresses(param.peerhost, true).select { |address| Rex::Socket.is_ipv6?(address) }.sample + else + ip = Rex::Socket.getaddresses(param.peerhost, false).select { |address| Rex::Socket.is_ipv4?(address) }.sample + end port = param.peerport end From a2e1d9dbd7ff9b9698d5bbc87ca76f950b23ab1b Mon Sep 17 00:00:00 2001 From: Spencer McIntyre Date: Wed, 27 May 2026 10:25:09 -0400 Subject: [PATCH 2/2] Fix the same issue but when binding --- lib/rex/socket/comm/local.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/rex/socket/comm/local.rb b/lib/rex/socket/comm/local.rb index b6ebd22..64886a3 100644 --- a/lib/rex/socket/comm/local.rb +++ b/lib/rex/socket/comm/local.rb @@ -177,7 +177,15 @@ def self.create_by_type(param, type, proto = 0) sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true) end - sock.bind(Rex::Socket.to_sockaddr(param.localhost, param.localport)) + if Rex::Socket.is_ip_addr?(param.localhost) + ip = param.localhost + elsif param.v6 + ip = Rex::Socket.getaddresses(param.localhost, true).select { |address| Rex::Socket.is_ipv6?(address) }.sample + else + ip = Rex::Socket.getaddresses(param.localhost, false).select { |address| Rex::Socket.is_ipv4?(address) }.sample + end + + sock.bind(Rex::Socket.to_sockaddr(ip, param.localport)) rescue ::Errno::EADDRNOTAVAIL,::Errno::EADDRINUSE sock.close