# Course #204: Penetration Testing with gitleaks$

## Section 1: Introduction to Gitleaks

### What is Gitleaks?

Gitleaks is an open-source tool designed for finding hardcoded secrets in Git repositories. It provides automated scanning capabilities that help penetration testers and developers identify sensitive data, such as API keys, passwords, and other secrets, that may be inadvertently committed to the source code. This tool is essential for maintaining the security and integrity of applications, especially in continuous integration and deployment (CI/CD) environments.

### Why Use Gitleaks?

In the world of cybersecurity, the protection of sensitive information is paramount. Tools like Gitleaks help organizations to:
– Quickly identify secrets before they are exploited by malicious actors.
– Automate the detection of vulnerabilities during code reviews.
– Ensure compliance with security best practices.

## Installation and Configuration on Kali Linux

### Prerequisites

Before installing Gitleaks, ensure that you have Kali Linux installed and updated. You can update your system using the following commands:

"`bash
sudo apt update
sudo apt upgrade
"`

### Installing Gitleaks

Gitleaks can be installed via several methods. The preferred way in Kali Linux is through the package manager.

#### Method 1: Using APT

1. **Install Gitleaks** using the following command:

2. **Verify the Installation**:

After installation, you can verify that Gitleaks has been installed correctly by checking its version:

#### Method 2: Building from Source

If you prefer to build Gitleaks from source, follow these steps:

1. **Install Go** (Golang):

2. **Set up Go Environment Variables**:


mkdir -p ~/go/src ~/go/bin
echo "export GOPATH=~/go" >> ~/.bashrc
echo "export PATH=$PATH:$GOPATH/bin" >> ~/.bashrc
source ~/.bashrc

3. **Clone the Gitleaks Repository**:


git clone https://github.com/zricethezav/gitleaks.git
cd gitleaks

4. **Build the Tool**:

5. **Move the Binary to a Directory in your PATH**:

6. **Verify the Installation**:

### Configuration

Gitleaks can be configured using a configuration file. You can create a `.gitleaks.toml` file to customize its behavior.

#### Default Configuration File Example

"`toml
[[rules]]
description = "AWS Access Key"
regex = "'(?i)AKIA[0-9A-Z]{16}"'

[[rules]]
description = "AWS Secret Key"
regex = "'(?i)([A-Za-z0-9/+=]{40})"'
"`

Save this under the location you prefer (e.g., in your project root).

## Step-by-Step Usage and Real-World Use Cases

### Basic Command Syntax

The basic syntax of the Gitleaks command is as follows:

"`bash
gitleaks detect –source–config
"`

### Example Use Case: Scanning a Local Repository

1. **Navigate to Your Repository**:

2. **Run Gitleaks**:


gitleaks detect –source . –config ~/.gitleaks.toml

3. **Review the Output**:

The output will display any detected secrets along with their file locations and line numbers:

[/dm_code_snippet]plaintext
[0] [AWS Access Key] Found in file: "config.json" at line 10
[1] [AWS Secret Key] Found in file: "secrets.yml" at line 15
[/dm_code_snippet]

### Advanced Usage

Gitleaks supports various options such as filtering detection by commit history, specific branches, and more.

#### Scanning a Remote Repository

To scan a remote repository, first, clone the repository:

"`bash
git clone https://github.com/example/repository.git
cd repository
"`

Then, run Gitleaks:

"`bash
gitleaks detect –source . –config ~/.gitleaks.toml
"`

#### Continuous Integration

Gitleaks can be integrated into CI/CD pipelines to enforce security checks:

"`yaml
# Example GitHub Actions Workflow
name: Gitleaks Scan

on: [push]

jobs:
gitleaks:
runs-on: ubuntu-latest
steps:
– name: Checkout Code
uses: actions/checkout@v2

– name: Run Gitleaks
run: |
wget https://github.com/zricethezav/gitleaks/releases/latest/download/gitleaks-linux-amd64 -O gitleaks
chmod +x gitleaks
./gitleaks detect –source . –config .gitleaks.toml
"`

### Real-World Example: Responding to a Breach

When a breach is suspected, using Gitleaks can help identify whether secrets were inadvertently exposed in the codebase.

1. **Scan the Repository**:


gitleaks detect –source /path/to/repo –config ~/.gitleaks.toml

2. **Fix Issues**:

If any secrets are found, rotate them immediately and ensure they are removed from the repository history with Git commands such as `git filter-branch` or tools like `BFG Repo-Cleaner`.

## Detailed Technical Explanations and External Reference Links

### Working Principles of Gitleaks

Gitleaks operates on the principle of regex matching against the contents of the files in the Git repo. It scans files for patterns that resemble secrets and can be configured to look for additional patterns as necessary.

– **Regex Patterns**: Gitleaks uses regular expressions to identify secrets. You can extend the tool to recognize patterns specific to your organization.

### External References

– [Gitleaks GitHub Repository](https://github.com/zricethezav/gitleaks)
– [Regex Tutorial](https://www.regular-expressions.info/)
– [Kali Linux Documentation](https://www.kali.org/docs/)

### Conclusion

Gitleaks is a powerful tool for securing application source code against the risk of leaked secrets. By incorporating it into your development and deployment workflows, you can significantly reduce the potential attack surface. This concludes our introduction to Gitleaks; in the following sections, we will delve deeper into advanced configurations and practical use cases in penetration testing scenarios.

Made by pablo rotem / פבלו רותם

Pablo Guides