↓ Skip to main content

The Dangers of the Invisible Git Directory

Apache directory listing titled Index of /.git

I get very excited when I see this screen (hopefully with more recent dates though…)

Welcome to another installment of random things I stumble across in my spare time that turn into a security incident, but I’m unable to find an actual security contact to report it to. Whew. Quite the title, right? I’m working on a shorter version, I promise.

Let’s chat a little bit about Source Code Management (SCM) / Version Control systems. It’s quite common for software developers to be working on projects in a team or in an environment where being able to track changes is exceedingly helpful. SCM is essentially a requirement to develop software whether it be Mercurial, Subversion, or Git - teams are using SCM to handle all aspects of their code. SCM provides many huge benefits that I hinted at earlier. Individual files are version controlled and can be rolled back in case of a critical failure or bug. Projects can be broken in submodules which can be tracked individually allowing multiple teams to work on separate chunks of a larger project. In the end, SCM is just another part of the development life cycle.

Enter Git. Git is a form of SCM that many would argue is the most popular version control system for software. Git is a simple command line interface (CLI) for managing software projects. I won’t get too technical, but at the end of the day, Git manages its internal state via a hidden folder named .git. It begins with a period to make it automatically hidden on most *nix based systems. And for most people, they never ever worry about it beyond that. Git will manage this folder automatically to keep track of everything related to the project.

Awesome, so what’s the big deal? Well, many groups deploy their software by cloning their git repository right onto their web server. In theory, seems fine, right? Code from repo ends up on server. Server hosts said files.

The problem goes back to that .git directory I mentioned earlier. This directory contains ALL the needed information to recreate the entire git repository. When you clone a git repository to a web server (and your server is configured to allow access to dot files), anyone on the internet can make a copy of this .git directory.

Once you’ve made a copy of this directory, it’s game over. I now have a full copy of your source code, your intellectual property, your source of income. I can search offline for security vulnerabilities without triggering a WAF/IDS/Cloud WAF. I can harvest credentials and secrets stored in your repository. I can spin up a clone service just like yours with my branding. It’s pretty bad.

Exploiting It
#

Here’s the sad part: this is about as easy as it gets. No fancy tooling, no exploit chain, no 3AM caffeine-fueled debugging session. Just a couple of commands you probably already have installed.

Step 1: Is it even there?
#

First, check whether the .git directory is reachable at all. Every git repository has a HEAD file, so it makes for a perfect canary.

curl -s https://example.com/.git/HEAD

If you get back something like this, congratulations (or condolences, depending on which side of this you’re on):

ref: refs/heads/master

While you’re at it, take a peek at .git/config. It usually spills the remote URL of the repository, which tells you exactly where the code lives (and sometimes contains credentials baked right into the URL… yikes).

curl -s https://example.com/.git/config

Step 2: Grab everything
#

If the server has directory listing turned on (like the screenshot at the top of this post), you’ve hit the jackpot. wget will happily crawl the whole thing for you:

mkdir loot && cd loot
wget -r -np -nH -e robots=off -R "index.html*" https://example.com/.git/

A quick rundown of what all those flags are doing:

  • -r – Recursive. Follow every link in the directory listing.
  • -np – No parent. Don’t wander up out of the .git directory and crawl the rest of the site.
  • -nH – No host directory. Drops the files into ./.git instead of ./example.com/.git.
  • -e robots=off – Ignore robots.txt. Sorry, not sorry.
  • -R “index.html*” – Don’t save the auto-generated directory listing pages themselves.

Step 3: Turn it back into source code
#

At this point you have a .git directory but no actual files. Git thinks everything has been deleted, so just ask it nicely to put everything back:

git checkout -- .

And just like that, you have the full source code sitting on your machine. But don’t stop there! The working copy is only the latest version. The real fun is in the history:

git log --stat   # who changed what, and when
git log -p       # every change ever made, line by line

That second command is where the secrets live. Developers love to commit an API key, realize their mistake, and “fix” it by deleting it in the next commit. Guess what? It’s still sitting right there in the history. Forever.

What if directory listing is off?
#

Good news for the defenders: if directory listing is disabled, wget has nothing to crawl and the recursive download falls flat. Bad news for the defenders: the files are still there, you just have to know their names. Since git’s internal structure is well-known, tools like git-dumper will read HEAD, follow the refs, and walk the object graph to fetch every file one by one.

pip install git-dumper
git-dumper https://example.com/.git/ ./loot

Same end result, just a few more requests. Turning off directory listing is not a fix.

Wrapping Up
#

Exposed .git directories are one of those issues that feel way too simple to be real, and yet I keep stumbling across them. It’s not a fancy bug. It’s not going to get a cool logo and a website. But it hands over your source code, your commit history, and whatever secrets someone forgot to scrub, all with a single wget command. That’s a pretty bad trade.

So, what should you do about it?

  • Don’t deploy with git clone. Build your project and ship the artifacts to your web server. Your server doesn’t need your commit history, so don’t give it one.
  • Block dotfiles at the web server. If you absolutely must deploy from a checkout, deny access to .git (and honestly, every dotfile) outright. In nginx, something like location ~ /\.git { deny all; } does the trick. In Apache, RedirectMatch 404 /\.git works nicely.
  • Check your own stuff. Run that curl against /.git/HEAD on every site you own. It takes ten seconds. You might be unpleasantly surprised.
  • If you were exposed, rotate everything. Assume every secret that has ever been committed to that repository is compromised, not just the ones in the latest version. Remember, history is forever.

And one last plea, since it’s the reason this post took me forever to finish: please give people a way to report security issues to you. A security.txt file, a security@ email address, anything. I’d much rather send you a friendly heads-up than write a vague blog post about it.

Thanks for making it this far. Got a horror story of your own or think I missed something? Let me know in the comments below.

Sean Marpo
Author
Sean Marpo
I break web apps for a living and build software on the side.

Related