|
| 1 | +# Secret Detectors |
| 2 | + |
| 3 | +Secret Detectors have these two major functions: |
| 4 | + |
| 5 | +1. Given some bytes, extract possible secrets, typically using a regex. |
| 6 | +2. Validate the secrets against the target API, typically using a HTTP client. |
| 7 | + |
| 8 | +The purpose of Secret Detectors is to discover secrets with exceptionally high signal. High rates of false positives are not accepted. |
| 9 | + |
| 10 | +## Table of Contents |
| 11 | + |
| 12 | +- [Secret Detectors](#secret-detectors) |
| 13 | + * [Table of Contents](#table-of-contents) |
| 14 | + * [Getting Started](#getting-started) |
| 15 | + + [Sourcing Guidelines](#sourcing-guidelines) |
| 16 | + + [Development Guidelines](#development-guidelines) |
| 17 | + + [Development Dependencies](#development-dependencies) |
| 18 | + + [Creating a new Secret Scanner](#creating-a-new-secret-scanner) |
| 19 | + * [Addendum](#addendum) |
| 20 | + + [Using a test secret file](#using-a-test-secret-file) |
| 21 | + + [Adding Protos in Windows](#adding-protos-in-windows) |
| 22 | + |
| 23 | +## Getting Started |
| 24 | + |
| 25 | +### Sourcing Guidelines |
| 26 | + |
| 27 | +We are interested in detectors for services that meet at least one of these criteria |
| 28 | +- host data (they store any sort of data provided) |
| 29 | +- have paid services (having a free or trial tier is okay though) |
| 30 | + |
| 31 | +If you think that something should be included outside of these guidelines, please let us know. |
| 32 | + |
| 33 | +### Development Guidelines |
| 34 | + |
| 35 | +- When reasonable, favor using the `net/http` library to make requests instead of bringing in another library. |
| 36 | +- Use the [`common.SaneHttpClient`](pkg/common/http.go) for the `http.Client` whenever possible. |
| 37 | + |
| 38 | +### Development Dependencies |
| 39 | + |
| 40 | +- Go 1.17+ |
| 41 | +- Make |
| 42 | + |
| 43 | +### Creating a new Secret Scanner |
| 44 | + |
| 45 | +1. Identify the Secret Detector name from the [/proto/detectors.proto](/proto/detectors.proto) `DetectorType` enum. If necessary, run `make protos` when adding new ones. |
| 46 | + |
| 47 | +2. Generate the Secret Detector |
| 48 | + |
| 49 | + ```bash |
| 50 | + go run hack/generate/generate.go detector <DetectorType enum name> |
| 51 | + ``` |
| 52 | + |
| 53 | +3. Complete the secret detector. |
| 54 | + |
| 55 | + The previous step templated a boilerplate + some example code as a package in the `pkg/detectors` folder for you to work on. |
| 56 | + The secret detector can be completed with these general steps: |
| 57 | + |
| 58 | + 1. Create a [test secrets file, and export the variable](#using-a-test-secret-file) |
| 59 | + 2. Update the pattern regex and keywords. Try iterating with [regex101.com](http://regex101.com/). |
| 60 | + 3. Update the verifier code to use a non-destructive API call that can determine whether the secret is valid or not. |
| 61 | + 4. Update the tests with these test cases at minimum: |
| 62 | + 1. Found and verified (using a credential loaded from GCP Secrets) |
| 63 | + 2. Found and unverified |
| 64 | + 3. Not found |
| 65 | + 4. Any false positive cases that you come across |
| 66 | + 5. Create a pull request for review. |
| 67 | + |
| 68 | +## Addendum |
| 69 | + |
| 70 | +### Using a test secret file |
| 71 | + |
| 72 | +1. Create a file called `.env` with this env file format: |
| 73 | + |
| 74 | + ```bash |
| 75 | + SECRET_TYPE_ONE=value |
| 76 | + SECRET_TYPE_ONE_INACTIVE=v@lue |
| 77 | + ``` |
| 78 | + |
| 79 | +2. Export the `TEST_SECRET_FILE` variable, pointing to the env file: |
| 80 | + |
| 81 | + ```bash |
| 82 | + export TEST_SECRET_FILE=".env" |
| 83 | + ``` |
| 84 | + |
| 85 | +Now, the detector test should attempt to load the given env key from that file. |
| 86 | + |
| 87 | +### Adding Protos in Windows |
| 88 | + |
| 89 | +1. Install Ubuntu App in Microsoft Store https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6. |
| 90 | +2. Install Docker Desktop https://www.docker.com/products/docker-desktop. Enable WSL integration to Ubuntu. In Docker app, go to Settings->Resources->WSL INTEGRATION->enable Ubuntu. |
| 91 | +3. Open Ubuntu cli and install `dos2unix`. |
| 92 | + ```bash |
| 93 | + sudo apt install dos2unix |
| 94 | + ``` |
| 95 | +4. Identify the `trufflehog` local directory and convert `scripts/gen_proto.sh` file in Unix format. |
| 96 | + ```bash |
| 97 | + dos2unix ./scripts/gen_proto.sh |
| 98 | + ``` |
| 99 | +5. Open [/proto/detectors.proto](/proto/detectors.proto) file and add new detectors then save it. Make sure Docker is running and run this in Ubuntu command line. |
| 100 | + ```bash |
| 101 | + make protos |
| 102 | + ``` |
0 commit comments