The dangers of the .git . directory

by doris
7 minutes read

Have you tried browsing to ? If you get a 403 error, that’s normal. Because directory browsing has blocked this access, this is a basic security feature. However, many files in the directory .git/ still accessible. So it is extremely dangerous, in this article, I will demo how to exploit the .git directory for you to see.

The dangers of the .git . directory

The dangers of the .git . directory

Anyone can use automated scanning tools like githacker to download your source code and see the full git history. Git is also a file system that follows some rules, so it’s easy to guess directories and files.

Most projects use masteror mainas the main branch, so you can easily guess the hidden path in the directory /.git/. This tool can even brute force branches and tags.

If the scan is successful, you will get the folderresulton your machine (you can customize the folder name with the option --output-dir).

Do not deloy or block access to the .git . directory

Folder .git/can contain a lot of information, including source code as well as name, email and in the worst case information accuracy encrypted (e.g. database, token, key).

You should completely block public access to that folder. Modern CI/CD and deployment solutions are relatively easy to configure and can remove directories that have nothing to do with production environments.

Some web hosting providers also block access to this directory for security reasons, but they do not always do so and it is not the default configuration. . So you need to double check your system configuration before deloying anything.

You can perform the following solutions to make your system more secure:

  1. Turn off directory public access .git/ by default
  2. Add a rule that forbids access to directories in the source code, e.g. Apache configuration files .htaccess
  3. Do not deploy such folders in public folders

Should return 404 for directory.git/in server configuration or file .htaccess:

RedirectMatch 404 /\.git

You should add both rules if possible, as two layers of security. In case someone modifies.htaccessand delete the rule, there is still the backup rule in the directory .git/. But as far as I know, the use of file .htaccess will slow down the Apache http server.

Demo how to exploit the .git . directory

I created 2 virtual machines, Ubuntu (Server) and Kali (Attacker). In the server, I use nginx to do the webserver, I create the file index.php and commit the first time to git.

The dangers of the .git 11 directory

The dangers of the .git 12 . directory

Then create a file conn.php containing the password as sensitive data and add this file to git and then stash this change.

The dangers of the .git 13 directory

When accessing the .git path, we will be blocked due to nginx’s security mechanism.

The dangers of the .git 14 . directory

That’s the basic setup on the server. Now we will proceed to exploit this bug.

On kali, you run the following command to install GitHacker:

python3 -m pip install -i GitHacker

As you can see in the image below, gitHacker will be installed in the path /home/user/.local/bin.

The dangers of the .git 15 . directory

You access this path and run the command. Replace IP-SERVER with the ip of your server.

python3 githacker --url http://IP-SERVER/.git/ --output-folder result

Go to the result folder, you will see the server’s commit.

The dangers of the .git 16 . directory

Check logs and stash.

The dangers of the .git 17 . directory

Restore the previously saved stash and we will have a conn.php file.

The dangers of the .git 18 . directory

So I have successfully exploited it, don’t for the sake of convenience to pull the repo on production but always deloy the .git directory.

Rate this post

Related Posts

Leave a Comment