-
Notifications
You must be signed in to change notification settings - Fork 33
Add Interface socket option for binding to a network interface
#80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
karandesai2005
wants to merge
6
commits into
rapid7:master
Choose a base branch
from
karandesai2005:feature/interface-param-21114
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2445c0
Add `Interface` socket option for binding to a network interface
karandesai2005 5769bf7
Add descriptive reason messages to BindFailed and macOS support
karandesai2005 932a664
Explicitly mark Windows as unsupported for Interface binding
karandesai2005 125e231
Add Windows support and harden Interface option
karandesai2005 d42e115
Address review feedback from smcintyre-r7
karandesai2005 25b0950
Update lib/rex/socket/comm/local.rb
karandesai2005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,6 +158,47 @@ def self.create_by_type(param, type, proto = 0) | |
| # Notify handlers of the before socket create event. | ||
| self.instance.notify_before_socket_create(self, param) | ||
|
|
||
| # Binding to a specific interface while routing through a proxy is not | ||
| # supported. The proxy comm handles its own socket creation and ignores | ||
| # the interface option entirely, so we fail fast here rather than | ||
| # silently binding to the wrong interface. | ||
| if param.interface && !param.interface.empty? && param.proxies? | ||
| raise Rex::BindFailed.new(param.localhost, param.localport, | ||
| reason: 'Interface option is incompatible with proxy use'), caller | ||
| end | ||
|
|
||
| if param.interface && !param.interface.empty? && Rex::Compat.is_windows | ||
| iface_ip = nil | ||
| iface_found = false | ||
| begin | ||
| ::Socket.getifaddrs.each do |ifaddr| | ||
| next unless ifaddr.name == param.interface | ||
| iface_found = true | ||
| if param.v6 | ||
| next unless ifaddr.addr&.ipv6? | ||
| else | ||
| next unless ifaddr.addr&.ipv4? | ||
| end | ||
| iface_ip = ifaddr.addr.ip_address | ||
| break | ||
| end | ||
| rescue ::SystemCallError, ::SocketError => e | ||
| raise Rex::BindFailed.new(param.localhost, param.localport, | ||
| reason: "Failed to enumerate interfaces: #{e.message}"), caller | ||
| end | ||
| if iface_ip.nil? | ||
| reason = if iface_found | ||
| "Interface #{param.interface} has no #{param.v6 ? 'IPv6' : 'IPv4'} address" | ||
| else | ||
| "Interface #{param.interface} not found" | ||
| end | ||
| raise Rex::BindFailed.new(param.localhost, param.localport, | ||
| reason: reason), caller | ||
| end | ||
| param = param.dup # avoid mutating the caller's instance | ||
| param.localhost = iface_ip | ||
smcintyre-r7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| # Create the socket | ||
| sock = nil | ||
| if param.v6 | ||
|
|
@@ -185,6 +226,46 @@ def self.create_by_type(param, type, proto = 0) | |
| end | ||
| end | ||
|
|
||
| if param.interface && !param.interface.empty? | ||
| if Rex::Compat.is_linux | ||
| begin | ||
| sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BINDTODEVICE, param.interface) | ||
| rescue ::Errno::ENODEV, ::Errno::ENXIO | ||
| sock.close | ||
| raise Rex::BindFailed.new(param.localhost, param.localport, | ||
| reason: "Interface #{param.interface} not found"), caller | ||
| rescue ::Errno::EPERM | ||
| sock.close | ||
| raise Rex::BindFailed.new(param.localhost, param.localport, | ||
| reason: "Binding to interface #{param.interface} requires elevated privileges"), caller | ||
| rescue ::SystemCallError | ||
| sock.close | ||
| raise | ||
| end | ||
| elsif Rex::Compat.is_macosx && defined?(::Socket::IP_BOUND_IF) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @karandesai2005 - thanks for the contribution. I was just testing on macOS Sequoia 15.7.5 (24G624) and I'm running into the following issue: It's due to this conditional statement evaluating to false due to |
||
| begin | ||
| idx = ::Socket.getifaddrs.find { |ifaddr| ifaddr.name == param.interface }&.ifindex | ||
| if idx.nil? | ||
| sock.close | ||
| raise Rex::BindFailed.new(param.localhost, param.localport, | ||
| reason: "Interface #{param.interface} not found"), caller | ||
| end | ||
| sock.setsockopt(::Socket::IPPROTO_IP, ::Socket::IP_BOUND_IF, [idx].pack('I')) | ||
| rescue ::SocketError, ::Errno::ENXIO | ||
| sock.close | ||
| raise Rex::BindFailed.new(param.localhost, param.localport, | ||
| reason: "Interface #{param.interface} not found"), caller | ||
| rescue ::SystemCallError | ||
| sock.close | ||
| raise | ||
| end | ||
| elsif !Rex::Compat.is_windows | ||
| sock.close | ||
| raise Rex::BindFailed.new(param.localhost, param.localport, | ||
| reason: 'Interface binding is not supported on this platform'), caller | ||
| end | ||
| end | ||
|
|
||
| # Configure broadcast support for all datagram sockets | ||
| if type == ::Socket::SOCK_DGRAM | ||
| sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, true) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.