GitPython Incomplete Denylist Allows Arbitrary Command Execution via Git Clone Hooks
A critical vulnerability in GitPython versions up to 3.1.53 allows attackers to achieve arbitrary command execution by influencing `git clone` options to include a malicious `--template` directory, which causes Git hooks to be copied and executed during cloning, even in default configurations.
A high-severity vulnerability exists in GitPython versions up to and including 3.1.53, stemming from an incomplete denylist within the unsafe_git_clone_options mechanism. Specifically, the --template option for git clone is not blocked, allowing an attacker to specify a path to an attacker-controlled directory containing malicious Git hooks. When GitPython executes the underlying git clone command, these hooks, such as post-checkout, are copied into the new repository and subsequently executed during the checkout phase. This oversight leads to arbitrary command execution on the system hosting the GitPython application. The exploitation requires the attacker to provide an attacker-readable directory containing an executable hook, which can be realistic via shared filesystems, upload directories, or attacker-writable network paths. This flaw impacts all platforms where GitPython is used.
Attack Chain
- Attacker crafts a malicious Git hook script (e.g.,
post-checkout) containing commands designed for arbitrary execution on the victim's system. - Attacker places this malicious script into a specially structured directory, such as
<attacker_controlled_path>/hooks/post-checkout, ensuring it has executable permissions. - Attacker identifies a vulnerable GitPython application that performs
Repo.clone_fromoperations and can be manipulated to accept an attacker-controlled value for thetemplateargument. - The vulnerable GitPython application is tricked into initiating a
Repo.clone_fromcall, passing the attacker-controlled path to thetemplateparameter. - GitPython's internal
check_unsafe_optionsfunction, intended to prevent dangerousgit cloneoptions, fails to block--templatebecause it is missing from itsunsafe_git_clone_optionsdenylist. - The underlying
gitcommand is executed on the host system, including the--template=<attacker_controlled_path>option. - The
gitcommand copies the maliciouspost-checkouthook from<attacker_controlled_path>/hooks/to the newly cloned repository's.git/hooks/directory. - During the repository checkout process, which is part of the
git cloneoperation, the newly copied maliciouspost-checkouthook is automatically executed, resulting in arbitrary command execution on the victim's system.
Impact
Successful exploitation of this vulnerability leads to arbitrary operating system command execution on the host running the GitPython application. An attacker could leverage this to install malware, exfiltrate sensitive data, establish persistence, or further compromise the affected system and network. While a second precondition of an attacker-readable directory with an executable hook is necessary, this is considered achievable in many real-world scenarios. This vulnerability affects any system using vulnerable versions of GitPython, regardless of the underlying operating system.
Recommendation
- Upgrade GitPython to a patched version once available.
- Implement host-based detection for unusual Git command-line parameters, specifically the
--templateoption, by deploying the Sigma rule provided in this brief. - Monitor process creation logs for
git.exeorgitprocesses executing with unusual arguments or child processes that deviate from expected behavior for Git operations. - Restrict write permissions on directories that could be used by attackers to stage malicious Git hooks, such as temporary directories or shared network drives.
Detection coverage 1
Detect GitPython Unsafe Template Arbitrary Command Execution
highDetects exploitation of the GitPython vulnerability where the `--template` option is used with the `git clone` command, potentially indicating arbitrary command execution via malicious Git hooks.
Detection queries are available on the platform. Get full rules →