Skip to content

Add SOCKS5H Proxy Support #20147

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ group :test do
gem 'timecop'
end

gem 'rex-socket', git: 'https://github.com/zeroSteiner/rex-socket', branch: 'feat/proxy/socks5h'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to remove this before landing.

#gem 'rex-socket', path: '../rex-socket'

13 changes: 10 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
GIT
remote: https://github.com/zeroSteiner/rex-socket
revision: a19f90fd8e5298c998d677b709534f4f7e4adf37
branch: feat/proxy/socks5h
specs:
rex-socket (0.1.60)
dnsruby
rex-core

PATH
remote: .
specs:
Expand Down Expand Up @@ -476,9 +485,6 @@ GEM
metasm
rex-core
rex-text
rex-socket (0.1.60)
dnsruby
rex-core
rex-sslscan (0.1.11)
rex-core
rex-socket
Expand Down Expand Up @@ -620,6 +626,7 @@ DEPENDENCIES
pry-byebug
rake
redcarpet
rex-socket!
rspec-rails
rspec-rerun
rubocop (= 1.67.0)
Expand Down
4 changes: 2 additions & 2 deletions lib/msf/core/opt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def self.LPORT(default=nil, required=true, desc="The listen port")
end

# @return [OptString]
def self.Proxies(default=nil, required=false, desc="A proxy chain of format type:host:port[,type:host:port][...]")
Msf::OptString.new(__method__.to_s, [ required, desc, default ])
def self.Proxies(default=nil, required=false, desc="A proxy chain of format type:host:port[,type:host:port][...]. Supported proxies: #{Rex::Socket::Proxies.supported_types.join(', ')}")
Msf::OptProxies.new(__method__.to_s, [ required, desc, default ])
end

# @return [OptRhosts]
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def type
return 'address'
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)
return false unless value.kind_of?(String) or value.kind_of?(NilClass)

Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_address_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def normalize(value)
sorted_addrs.any? ? sorted_addrs.first : ''
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)
return false unless value.kind_of?(String) || value.kind_of?(NilClass)

Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_address_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def normalize(value)
return value
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)
return false unless value.kind_of?(String) or value.kind_of?(NilClass)

Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_address_routable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Msf
###
class OptAddressRoutable < OptAddress

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if Rex::Socket.is_ip_addr?(value) && Rex::Socket.addr_atoi(value) == 0
super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def validate_on_assignment?
#
# If it's required and the value is nil or empty, then it's not valid.
#
def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
if check_empty && required?
# required variable not set
return false if (value.nil? || value.to_s.empty?)
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_bool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def type
return 'bool'
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)
return true if value.nil? && !required?

Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(in_name, attrs = [],
super
end

def valid?(value = self.value, check_empty: true)
def valid?(value = self.value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)
return true if value.nil? && !required?
return false if value.nil?
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_float.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def normalize(value)
Float(value) if value.present? && valid?(value)
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)
Float(value) rescue return false if value.present?
super
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_int.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def normalize(value)
end
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)
return false if value.present? && !value.to_s.match(/^0x[0-9a-fA-F]+$|^-?\d+$/)
super
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_meterpreter_debug_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def validate_on_assignment?
true
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if !super(value, check_empty: check_empty)

begin
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def validate_on_assignment?
end

# Generally, 'value' should be a file that exists.
def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)
if value and !value.empty?
if value =~ /^memory:\s*([0-9]+)/i
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_port.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def type
return 'port'
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
port = normalize(value).to_i
super && port <= 65535 && port >= 0
end
Expand Down
37 changes: 37 additions & 0 deletions lib/msf/core/opt_proxies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: binary -*-

module Msf

###
#
# Proxies option
#
###
class OptProxies < OptBase

def type
'proxies'
end

def validate_on_assignment?
true
end

def normalize(value)
value
end

def valid?(value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)

begin
Rex::Socket::Proxies.parse(value)
rescue Rex::RuntimeError
return false
end

true
end
end

end
2 changes: 1 addition & 1 deletion lib/msf/core/opt_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def type
return 'regexp'
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
if check_empty && empty_required_value?(value)
return false
elsif value.nil?
Expand Down
7 changes: 4 additions & 3 deletions lib/msf/core/opt_rhosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def normalize(value)
value
end

def valid?(value, check_empty: true)
def valid?(value, check_empty: true, datastore: nil)
return false if check_empty && empty_required_value?(value)
return false unless value.is_a?(String) || value.is_a?(NilClass)

if !value.nil? && value.empty? == false
return Msf::RhostsWalker.new(value).valid?
if !value.nil? && !value.empty?
rhost_walker = datastore ? Msf::RhostsWalker.new(value, datastore) : Msf::RhostsWalker.new(value)
return rhost_walker.valid?
end

super
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/opt_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def normalize(value)
value
end

def valid?(value=self.value, check_empty: true)
def valid?(value=self.value, check_empty: true, datastore: nil)
value = normalize(value)
return false if check_empty && empty_required_value?(value)
return false if invalid_value_length?(value)
Expand Down
6 changes: 3 additions & 3 deletions lib/msf/core/option_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def add_evasion_options(opts, owner = nil)
def validate(datastore)
# First mutate the datastore and normalize all valid values before validating permutations of RHOST/etc.
each_pair do |name, option|
if option.valid?(datastore[name]) && (val = option.normalize(datastore[name])) != nil
if option.valid?(datastore[name], datastore: datastore) && (val = option.normalize(datastore[name])) != nil
# This *will* result in a module that previously used the
# global datastore to have its local datastore set, which
# means that changing the global datastore and re-running
Expand Down Expand Up @@ -233,7 +233,7 @@ def validate(datastore)

rhosts_walker.each do |datastore|
each_pair do |name, option|
unless option.valid?(datastore[name])
unless option.valid?(datastore[name], datastore: datastore)
error_options << name
if rhosts_count > 1
error_reasons[name] << "for rhosts value #{datastore['UNPARSED_RHOSTS']}"
Expand All @@ -249,7 +249,7 @@ def validate(datastore)
else
error_options = []
each_pair do |name, option|
unless option.valid?(datastore[name])
unless option.valid?(datastore[name], datastore: datastore)
error_options << name
end
end
Expand Down
61 changes: 44 additions & 17 deletions lib/msf/core/rhosts_walker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class RhostResolveError < StandardError
MESSAGE = 'Host resolution failed'
end

# @param [String] value
# @param [Msf::DataStore] datastore
def initialize(value = '', datastore = Msf::ModuleDataStore.new(nil))
@value = value
@datastore = datastore
Expand Down Expand Up @@ -143,30 +145,42 @@ def parse(value, datastore)
schema = Regexp.last_match(:schema)
raise InvalidSchemaError unless SUPPORTED_SCHEMAS.include?(schema)

found = false
parse_method = "parse_#{schema}_uri"
parsed_options = send(parse_method, value, datastore)
Rex::Socket::RangeWalker.new(parsed_options['RHOSTS']).each_ip do |ip|
results << datastore.merge(
parsed_options.merge('RHOSTS' => ip, 'UNPARSED_RHOSTS' => value)
)
found = true
end
unless found
raise RhostResolveError.new(value)
if perform_dns_resolution?(datastore)
found = false
Rex::Socket::RangeWalker.new(parsed_options['RHOSTS']).each_ip do |ip|
results << datastore.merge(
parsed_options.merge('RHOSTS' => ip, 'UNPARSED_RHOSTS' => value)
)
found = true
end
unless found
raise RhostResolveError.new(value)
end
else
results << datastore.merge(parsed_options.merge('UNPARSED_RHOSTS' => value))
end
else
found = false
Rex::Socket::RangeWalker.new(value).each_host do |rhost|
if perform_dns_resolution?(datastore)
found = false
Rex::Socket::RangeWalker.new(value).each_host do |rhost|
overrides = {}
overrides['UNPARSED_RHOSTS'] = value
overrides['RHOSTS'] = rhost[:address]
set_hostname(datastore, overrides, rhost[:hostname])
results << datastore.merge(overrides)
found = true
end
unless found
raise RhostResolveError.new(value)
end
else
overrides = {}
overrides['UNPARSED_RHOSTS'] = value
overrides['RHOSTS'] = rhost[:address]
set_hostname(datastore, overrides, rhost[:hostname])
overrides['RHOSTS'] = value
set_hostname(datastore, overrides, value)
results << datastore.merge(overrides)
found = true
end
unless found
raise RhostResolveError.new(value)
end
end
rescue ::Interrupt
Expand Down Expand Up @@ -385,6 +399,19 @@ def parse_tcp_uri(value, datastore)

protected

# @param [Msf::DataStore] datastore
# @return [Boolean] True if DNS resolution should be performed the RHOST values, false otherwise
def perform_dns_resolution?(datastore)
# If a socks proxy has been configured, don't perform DNS resolution - so that it instead happens via the proxy
return true unless datastore['PROXIES'].present?

last_proxy = Rex::Socket::Proxies.parse(datastore['PROXIES']).last
[
Rex::Socket::Proxies::ProxyType::HTTP,
Rex::Socket::Proxies::ProxyType::SOCKS5H
].exclude?(last_proxy.scheme)
end

def set_hostname(datastore, result, hostname)
hostname = Rex::Socket.is_ip_addr?(hostname) ? nil : hostname
result['RHOSTNAME'] = hostname if datastore['RHOSTNAME'].blank?
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/msf/core/opt_proxies_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding:binary -*-

require 'spec_helper'

RSpec.describe Msf::OptProxies do

valid_values = [
nil,
'',
' ',
'socks5:198.51.100.1:1080',
'socks4:198.51.100.1:1080',
'http:198.51.100.1:8080,socks4:198.51.100.1:1080',
'http:198.51.100.1:8080, socks4:198.51.100.1:1080',
'sapni:198.51.100.1:8080, socks4:198.51.100.1:1080',
].map { |value| { value: value, normalized: value } }

invalid_values = [
{ :value => 123 },
{ :value => 'foo(' },
{ :value => 'foo:198.51.100.1:8080' },
{ :value => 'foo:198.51.100.18080' },
{ :value => 'foo::' },
]

it_behaves_like "an option", valid_values, invalid_values, 'proxies'
end
4 changes: 2 additions & 2 deletions spec/lib/msf/core/opt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

context 'Proxies' do
subject { described_class::Proxies }
it { is_expected.to be_a(Msf::OptString) }
it { is_expected.to be_a(Msf::OptProxies) }
end

context 'RHOST' do
Expand Down Expand Up @@ -89,7 +89,7 @@

context 'Proxies()' do
subject { described_class::Proxies(default) }
it { is_expected.to be_a(Msf::OptString) }
it { is_expected.to be_a(Msf::OptProxies) }
specify 'sets default' do
expect(subject.default).to eq(default)
end
Expand Down
Loading
Loading