mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 12:44:22 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d2baa9e675 | ||
|
|
7fa9c802f5 | ||
|
|
8f1bc74e26 | ||
|
|
8de38be418 | ||
|
|
ec1ebd4686 | ||
|
|
81ddfbed88 | ||
|
|
63814f1eaf | ||
|
|
38a36add57 | ||
|
|
9e39927ac6 | ||
|
|
0ba9dda0f0 | ||
|
|
a24f9a2c92 | ||
|
|
f57158a893 | ||
|
|
1e3bf42464 | ||
|
|
9d6a75fbab | ||
|
|
6fa52ae7cc | ||
|
|
bb9c459a1a | ||
|
|
777601a0ea | ||
|
|
35d170018e | ||
|
|
af2e99ef45 | ||
|
|
734befb555 | ||
|
|
64c8d618a2 | ||
|
|
92bb4ea6fc | ||
|
|
98b025da72 | ||
|
|
af7717412f | ||
|
|
68c2a18f6f | ||
|
|
ea49078ffd | ||
|
|
b902d7310a | ||
|
|
3591560b34 | ||
|
|
949029ca89 | ||
|
|
cde0894711 | ||
|
|
342e9f80db | ||
|
|
96b0f4aab7 | ||
|
|
3af6a546ac | ||
|
|
6e086c4297 | ||
|
|
db08fc0564 | ||
|
|
dbb6d4cc5f | ||
|
|
d0f44da4c2 | ||
|
|
cb6b01cc5d | ||
|
|
392d79b9e2 | ||
|
|
f8e609d4bf | ||
|
|
573bf46fd1 | ||
|
|
d65592bbcc | ||
|
|
169a137e7f |
No files matched your search
@@ -1,6 +1,16 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
1.22.0
|
||||||
|
----
|
||||||
|
- context inclusion for redactions
|
||||||
|
- noise reduction for entropy signals
|
||||||
|
|
||||||
|
1.21.0
|
||||||
|
----
|
||||||
|
- added support for cloning repositories using github api
|
||||||
|
- auditing PRs now allows for whitelisting files
|
||||||
|
|
||||||
1.20.0
|
1.20.0
|
||||||
----
|
----
|
||||||
- adding gitlab user and group support
|
- adding gitlab user and group support
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<p align="center">
|
<p align="center">
|
||||||
<img alt="gitleaks" src="https://raw.githubusercontent.com/zricethezav/gifs/master/gitleaks5.png" height="140" />
|
<img alt="gitleaks" src="https://raw.githubusercontent.com/zricethezav/gifs/master/gitleaksnew2.gif"/>
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://travis-ci.org/zricethezav/gitleaks"><img alt="Travis" src="https://img.shields.io/travis/zricethezav/gitleaks/master.svg?style=flat-square"></a>
|
<a href="https://travis-ci.org/zricethezav/gitleaks"><img alt="Travis" src="https://img.shields.io/travis/zricethezav/gitleaks/master.svg?style=flat-square"></a>
|
||||||
</p>
|
</p>
|
||||||
@@ -33,7 +33,7 @@ It has been sucessfully used in a number of different scenarios, including;
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
#### Installation
|
#### Installation
|
||||||
Written in Go, gitleaks is available in binary form for many popular platforms and OS types from the [releases page](https://github.com/zricethezav/gitleaks). Alternatively, executed via Docker or it can be installed using Go directly, as per the below;
|
Written in Go, gitleaks is available in binary form for many popular platforms and OS types from the [releases page](https://github.com/zricethezav/gitleaks/releases). Alternatively, executed via Docker or it can be installed using Go directly, as per the below;
|
||||||
|
|
||||||
##### Docker
|
##### Docker
|
||||||
|
|
||||||
@@ -63,30 +63,32 @@ Usage:
|
|||||||
gitleaks [OPTIONS]
|
gitleaks [OPTIONS]
|
||||||
|
|
||||||
Application Options:
|
Application Options:
|
||||||
-r, --repo= Repo url to audit
|
-r, --repo= Repo url to audit
|
||||||
--github-user= Github user to audit
|
--github-user= Github user to audit
|
||||||
--github-org= Github organization to audit
|
--github-org= Github organization to audit
|
||||||
--github-url= GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/ (default: https://api.github.com/)
|
--github-url= GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/ (default: https://api.github.com/)
|
||||||
--github-pr= Github PR url to audit. This does not clone the repo. GITHUB_TOKEN must be set
|
--github-pr= Github PR url to audit. This does not clone the repo. GITHUB_TOKEN must be set
|
||||||
--gitlab-user= GitLab user ID to audit
|
--gitlab-user= GitLab user ID to audit
|
||||||
--gitlab-org= GitLab group ID to audit
|
--gitlab-org= GitLab group ID to audit
|
||||||
-c, --commit= sha of commit to stop at
|
-c, --commit= sha of commit to stop at
|
||||||
--depth= maximum commit depth
|
--depth= maximum commit depth
|
||||||
--repo-path= Path to repo
|
--repo-path= Path to repo
|
||||||
--owner-path= Path to owner directory (repos discovered)
|
--owner-path= Path to owner directory (repos discovered)
|
||||||
--threads= Maximum number of threads gitleaks spawns
|
--threads= Maximum number of threads gitleaks spawns
|
||||||
--disk Clones repo(s) to disk
|
--disk Clones repo(s) to disk
|
||||||
--single-search= single regular expression to search for
|
--single-search= single regular expression to search for
|
||||||
--config= path to gitleaks config
|
--config= path to gitleaks config
|
||||||
--ssh-key= path to ssh key
|
--ssh-key= path to ssh key
|
||||||
--exclude-forks exclude forks for organization/user audits
|
--exclude-forks exclude forks for organization/user audits
|
||||||
-e, --entropy= Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)
|
-e, --entropy= Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)
|
||||||
-l, --log= log level
|
--noise-reduction Reduce the number of finds when entropy checks are enabled
|
||||||
-v, --verbose Show verbose output from gitleaks audit
|
--repo-config Load config from target repo. Config file must be ".gitleaks.toml"
|
||||||
--report= path to write report file
|
-l, --log= log level
|
||||||
--redact redact secrets from log messages and report
|
-v, --verbose Show verbose output from gitleaks audit
|
||||||
--version version number
|
--report= path to write report file
|
||||||
--sample-config prints a sample config file
|
--redact redact secrets from log messages and report
|
||||||
|
--version version number
|
||||||
|
--sample-config prints a sample config file
|
||||||
|
|
||||||
Help Options:
|
Help Options:
|
||||||
-h, --help Show this help message
|
-h, --help Show this help message
|
||||||
@@ -105,11 +107,7 @@ The code return codes are:
|
|||||||
2: error encountered
|
2: error encountered
|
||||||
```
|
```
|
||||||
|
|
||||||
### GitLab
|
### Additional information
|
||||||
To audit public repositories for user on GitLab use `--gitlab-user=` and for group `--gitlab-org=`. For private repositories add `GITLAB_TOKEN` environment variable with your personal access token (is used to list all repositories via API) and set `--ssh-key=` to SSH key path (is used to clone repository).
|
|
||||||
If self hosted GitLab server add `GITLAB_URL` environment variable with your URL.
|
|
||||||
|
|
||||||
#### Additional information
|
|
||||||
* Additional documentation about how gitleaks functions can be found on the [wiki page](https://github.com/zricethezav/gitleaks/wiki)
|
* Additional documentation about how gitleaks functions can be found on the [wiki page](https://github.com/zricethezav/gitleaks/wiki)
|
||||||
* The below links detail the various approaches to remediating unwanted data in git repos
|
* The below links detail the various approaches to remediating unwanted data in git repos
|
||||||
* [Removing sensitive data from a repository (github.com)](https://help.github.com/articles/removing-sensitive-data-from-a-repository/)
|
* [Removing sensitive data from a repository (github.com)](https://help.github.com/articles/removing-sensitive-data-from-a-repository/)
|
||||||
@@ -122,7 +120,17 @@ If self hosted GitLab server add `GITLAB_URL` environment variable with your URL
|
|||||||
* How does gitleaks differ to Github token scanning?
|
* How does gitleaks differ to Github token scanning?
|
||||||
* [Github recently announced](https://blog.github.com/2018-10-16-future-of-software/#github-token-scanning-for-public-repositories-public-beta) a new capability to their cloud platform that detects exposed credentials for a number of common services and platforms and automatically notifies the provider for revocation or similar action. Gitleaks provides a similar detection capability for non-Github cloud users, in which repositories can be easily audited and results provided in a number of formats.
|
* [Github recently announced](https://blog.github.com/2018-10-16-future-of-software/#github-token-scanning-for-public-repositories-public-beta) a new capability to their cloud platform that detects exposed credentials for a number of common services and platforms and automatically notifies the provider for revocation or similar action. Gitleaks provides a similar detection capability for non-Github cloud users, in which repositories can be easily audited and results provided in a number of formats.
|
||||||
|
|
||||||
|
### Give Thanks
|
||||||
|
If using gitleaks has made you job easier consider donating to an organization, C-U at Home, that does vital work for those who most need it in the community of Champaign-Urbana, IL (my home).
|
||||||
|
|
||||||
|
From C-U at Home:
|
||||||
|
|
||||||
|
```C-U at Home is a grassroots, 501 c (3), faith-based homelessness ministry with facilities located in the Champaign-Urbana area. Due to our not-for-profit status, all gifts made to C-U at Home (both monetary and in-kind) are tax deductible. Our ministry is funded 100% by the community and we have made it a priority not to accept any state or federal funds. We feel that through community dollars, we can stay true to our mission. While our mission statement reads, “C-U at Home engages and mobilizes our community to house and support the most vulnerable homeless on their journey of healing and restoration,” our connection with those we serve goes much deeper.```
|
||||||
|
|
||||||
|
<b>Donate</b>: https://www.cuathome.us/give/
|
||||||
|
|
||||||
|
#### Speaking of Community
|
||||||
|
Please read this https://corruptcu.com/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
git "gopkg.in/src-d/go-git.v4"
|
git "gopkg.in/src-d/go-git.v4"
|
||||||
|
gitHttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
|
||||||
"gopkg.in/src-d/go-git.v4/storage/memory"
|
"gopkg.in/src-d/go-git.v4/storage/memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,6 +51,21 @@ func auditGithubPR() ([]Leak, error) {
|
|||||||
}
|
}
|
||||||
files := commit.Files
|
files := commit.Files
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
|
skipFile := false
|
||||||
|
if f.Patch == nil || f.Filename == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, re := range whiteListFiles {
|
||||||
|
if re.FindString(f.GetFilename()) != "" {
|
||||||
|
log.Infof("skipping whitelisted file (matched regex '%s'): %s", re.String(), f.GetFilename())
|
||||||
|
skipFile = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if skipFile {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
diff := gitDiff{
|
diff := gitDiff{
|
||||||
sha: commit.GetSHA(),
|
sha: commit.GetSHA(),
|
||||||
content: *f.Patch,
|
content: *f.Patch,
|
||||||
@@ -58,6 +74,7 @@ func auditGithubPR() ([]Leak, error) {
|
|||||||
githubCommit: commit,
|
githubCommit: commit,
|
||||||
author: commit.GetCommitter().GetLogin(),
|
author: commit.GetCommitter().GetLogin(),
|
||||||
message: *commit.Commit.Message,
|
message: *commit.Commit.Message,
|
||||||
|
date: *commit.Commit.Committer.Date,
|
||||||
}
|
}
|
||||||
leaks = append(leaks, inspect(diff)...)
|
leaks = append(leaks, inspect(diff)...)
|
||||||
}
|
}
|
||||||
@@ -177,6 +194,7 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) {
|
|||||||
repo *git.Repository
|
repo *git.Repository
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
githubToken := os.Getenv("GITHUB_TOKEN")
|
||||||
if opts.ExcludeForks && githubRepo.GetFork() {
|
if opts.ExcludeForks && githubRepo.GetFork() {
|
||||||
return nil, fmt.Errorf("skipping %s, excluding forks", *githubRepo.Name)
|
return nil, fmt.Errorf("skipping %s, excluding forks", *githubRepo.Name)
|
||||||
}
|
}
|
||||||
@@ -191,22 +209,38 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to generater owner temp dir: %v", err)
|
return nil, fmt.Errorf("unable to generater owner temp dir: %v", err)
|
||||||
}
|
}
|
||||||
if sshAuth != nil {
|
if sshAuth != nil && githubToken == "" {
|
||||||
repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{
|
repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{
|
||||||
URL: *githubRepo.SSHURL,
|
URL: *githubRepo.SSHURL,
|
||||||
Auth: sshAuth,
|
Auth: sshAuth,
|
||||||
})
|
})
|
||||||
|
} else if githubToken != "" {
|
||||||
|
repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{
|
||||||
|
URL: *githubRepo.CloneURL,
|
||||||
|
Auth: &gitHttp.BasicAuth{
|
||||||
|
Username: "fakeUsername", // yes, this can be anything except an empty string
|
||||||
|
Password: githubToken,
|
||||||
|
},
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{
|
repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{
|
||||||
URL: *githubRepo.CloneURL,
|
URL: *githubRepo.CloneURL,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if sshAuth != nil {
|
if sshAuth != nil && githubToken == "" {
|
||||||
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||||
URL: *githubRepo.SSHURL,
|
URL: *githubRepo.SSHURL,
|
||||||
Auth: sshAuth,
|
Auth: sshAuth,
|
||||||
})
|
})
|
||||||
|
} else if githubToken != "" {
|
||||||
|
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||||
|
URL: *githubRepo.CloneURL,
|
||||||
|
Auth: &gitHttp.BasicAuth{
|
||||||
|
Username: "fakeUsername", // yes, this can be anything except an empty string
|
||||||
|
Password: githubToken,
|
||||||
|
},
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||||
URL: *githubRepo.CloneURL,
|
URL: *githubRepo.CloneURL,
|
||||||
|
|||||||
+4
-4
@@ -21,19 +21,19 @@ regex = '''(?i)facebook.*['\"][0-9a-f]{32}['\"]'''
|
|||||||
description = "Twitter"
|
description = "Twitter"
|
||||||
regex = '''(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]'''
|
regex = '''(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]'''
|
||||||
[[regexes]]
|
[[regexes]]
|
||||||
description = "Telegram"
|
|
||||||
regex = '''(?i)telegram.*['\"][0-9]{1,12}+:[0-9a-zA-Z-]{32,44}['\"]'''
|
|
||||||
[[regexes]]
|
|
||||||
description = "PGP"
|
description = "PGP"
|
||||||
regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----'''
|
regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----'''
|
||||||
[[regexes]]
|
[[regexes]]
|
||||||
description = "Slack token"
|
description = "Slack token"
|
||||||
regex = '''xox[baprs]-.*'''
|
regex = '''xox[baprs]-.*'''
|
||||||
|
[[regexes]]
|
||||||
|
description = "Strip API Key"
|
||||||
|
regex = '''(?i)(sk|pk)_(test|live)_[0-9a-zA-Z]{10,32}'''
|
||||||
|
|
||||||
[whitelist]
|
[whitelist]
|
||||||
regexes = [
|
regexes = [
|
||||||
# "AKIA.*EXAMPLE",
|
# "AKIA.*EXAMPLE",
|
||||||
]
|
]
|
||||||
files = [
|
files = [
|
||||||
"(.*?)(jpg|gif|doc|pdf|bin|go)$"
|
"(.*?)(jpg|gif|doc|pdf|bin)$"
|
||||||
]
|
]
|
||||||
@@ -176,6 +176,7 @@ func TestRun(t *testing.T) {
|
|||||||
description string
|
description string
|
||||||
expectedErrMsg string
|
expectedErrMsg string
|
||||||
whiteListRepos []string
|
whiteListRepos []string
|
||||||
|
whiteListFiles []*regexp.Regexp
|
||||||
numLeaks int
|
numLeaks int
|
||||||
configPath string
|
configPath string
|
||||||
commitPerPage int
|
commitPerPage int
|
||||||
@@ -298,6 +299,27 @@ func TestRun(t *testing.T) {
|
|||||||
expectedErrMsg: "",
|
expectedErrMsg: "",
|
||||||
commitPerPage: 1,
|
commitPerPage: 1,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
testOpts: Options{
|
||||||
|
GithubPR: "https://github.com/gitleakstest/gronit/pull/1",
|
||||||
|
},
|
||||||
|
description: "test github pr with whitelisted files",
|
||||||
|
numLeaks: 0,
|
||||||
|
expectedErrMsg: "",
|
||||||
|
commitPerPage: 1,
|
||||||
|
whiteListFiles: []*regexp.Regexp{
|
||||||
|
regexp.MustCompile("main.go"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testOpts: Options{
|
||||||
|
GithubPR: "https://github.com/gitleakstest/gronit/pull/2",
|
||||||
|
},
|
||||||
|
description: "test github pr with commits without patch info",
|
||||||
|
numLeaks: 0,
|
||||||
|
expectedErrMsg: "",
|
||||||
|
commitPerPage: 1,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
g := goblin.Goblin(t)
|
g := goblin.Goblin(t)
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@@ -309,6 +331,11 @@ func TestRun(t *testing.T) {
|
|||||||
if test.commitPerPage != 0 {
|
if test.commitPerPage != 0 {
|
||||||
githubPages = test.commitPerPage
|
githubPages = test.commitPerPage
|
||||||
}
|
}
|
||||||
|
if test.whiteListFiles != nil {
|
||||||
|
whiteListFiles = test.whiteListFiles
|
||||||
|
} else {
|
||||||
|
whiteListFiles = nil
|
||||||
|
}
|
||||||
opts = test.testOpts
|
opts = test.testOpts
|
||||||
leaks, err := run()
|
leaks, err := run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -462,6 +489,14 @@ func TestAuditRepo(t *testing.T) {
|
|||||||
whiteListRegexes []*regexp.Regexp
|
whiteListRegexes []*regexp.Regexp
|
||||||
configPath string
|
configPath string
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
repo: leaksRepo,
|
||||||
|
description: "pinned config",
|
||||||
|
numLeaks: 0,
|
||||||
|
testOpts: Options{
|
||||||
|
RepoConfig: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
repo: leaksRepo,
|
repo: leaksRepo,
|
||||||
description: "commit depth = 1, one leak",
|
description: "commit depth = 1, one leak",
|
||||||
@@ -565,6 +600,15 @@ func TestAuditRepo(t *testing.T) {
|
|||||||
},
|
},
|
||||||
numLeaks: 6,
|
numLeaks: 6,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
repo: leaksRepo,
|
||||||
|
description: "leaks present with entropy",
|
||||||
|
testOpts: Options{
|
||||||
|
Entropy: 4.7,
|
||||||
|
NoiseReduction: true,
|
||||||
|
},
|
||||||
|
numLeaks: 2,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
repo: leaksRepo,
|
repo: leaksRepo,
|
||||||
description: "Audit until specific commit",
|
description: "Audit until specific commit",
|
||||||
@@ -587,6 +631,15 @@ func TestAuditRepo(t *testing.T) {
|
|||||||
numLeaks: 298,
|
numLeaks: 298,
|
||||||
configPath: path.Join(configsDir, "entropy"),
|
configPath: path.Join(configsDir, "entropy"),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
repo: leaksRepo,
|
||||||
|
testOpts: Options{
|
||||||
|
NoiseReduction: true,
|
||||||
|
},
|
||||||
|
description: "toml entropy range",
|
||||||
|
numLeaks: 58,
|
||||||
|
configPath: path.Join(configsDir, "entropy"),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
repo: leaksRepo,
|
repo: leaksRepo,
|
||||||
description: "toml bad entropy range",
|
description: "toml bad entropy range",
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
|
|
||||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||||
|
|
||||||
|
diffType "gopkg.in/src-d/go-git.v4/plumbing/format/diff"
|
||||||
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
||||||
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
|
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
|
||||||
"gopkg.in/src-d/go-git.v4/storage/memory"
|
"gopkg.in/src-d/go-git.v4/storage/memory"
|
||||||
@@ -30,9 +31,9 @@ import (
|
|||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/google/go-github/github"
|
"github.com/google/go-github/github"
|
||||||
"github.com/hako/durafmt"
|
"github.com/hako/durafmt"
|
||||||
flags "github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
git "gopkg.in/src-d/go-git.v4"
|
"gopkg.in/src-d/go-git.v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Leak represents a leaked secret or regex match.
|
// Leak represents a leaked secret or regex match.
|
||||||
@@ -77,13 +78,15 @@ type Options struct {
|
|||||||
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
|
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
|
||||||
|
|
||||||
// Process options
|
// Process options
|
||||||
Threads int `long:"threads" description:"Maximum number of threads gitleaks spawns"`
|
Threads int `long:"threads" description:"Maximum number of threads gitleaks spawns"`
|
||||||
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
||||||
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
||||||
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
||||||
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
||||||
ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"`
|
ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"`
|
||||||
Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"`
|
Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"`
|
||||||
|
NoiseReduction bool `long:"noise-reduction" description:"Reduce the number of finds when entropy checks are enabled"`
|
||||||
|
RepoConfig bool `long:"repo-config" description:"Load config from target repo. Config file must be \".gitleaks.toml\""`
|
||||||
// TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"`
|
// TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"`
|
||||||
|
|
||||||
// Output options
|
// Output options
|
||||||
@@ -101,6 +104,9 @@ type Config struct {
|
|||||||
Description string
|
Description string
|
||||||
Regex string
|
Regex string
|
||||||
}
|
}
|
||||||
|
Entropy struct {
|
||||||
|
LineRegexes []string
|
||||||
|
}
|
||||||
Whitelist struct {
|
Whitelist struct {
|
||||||
Files []string
|
Files []string
|
||||||
Regexes []string
|
Regexes []string
|
||||||
@@ -130,7 +136,7 @@ type entropyRange struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const defaultGithubURL = "https://api.github.com/"
|
const defaultGithubURL = "https://api.github.com/"
|
||||||
const version = "1.20.0"
|
const version = "1.23.0"
|
||||||
const errExit = 2
|
const errExit = 2
|
||||||
const leakExit = 1
|
const leakExit = 1
|
||||||
const defaultConfig = `
|
const defaultConfig = `
|
||||||
@@ -169,9 +175,24 @@ regex = '''(?i)github(.{0,4})?['\"][0-9a-zA-Z]{35,40}['\"]'''
|
|||||||
description = "Slack"
|
description = "Slack"
|
||||||
regex = '''xox[baprs]-([0-9a-zA-Z]{10,48})?'''
|
regex = '''xox[baprs]-([0-9a-zA-Z]{10,48})?'''
|
||||||
|
|
||||||
|
[entropy]
|
||||||
|
lineregexes = [
|
||||||
|
"api",
|
||||||
|
"key",
|
||||||
|
"signature",
|
||||||
|
"secret",
|
||||||
|
"password",
|
||||||
|
"pass",
|
||||||
|
"pwd",
|
||||||
|
"token",
|
||||||
|
"curl",
|
||||||
|
"wget",
|
||||||
|
"https?",
|
||||||
|
]
|
||||||
|
|
||||||
[whitelist]
|
[whitelist]
|
||||||
files = [
|
files = [
|
||||||
"(.*?)(jpg|gif|doc|pdf|bin)$"
|
"(.*?)(jpg|gif|doc|pdf|bin)$"
|
||||||
]
|
]
|
||||||
#commits = [
|
#commits = [
|
||||||
# "BADHA5H1",
|
# "BADHA5H1",
|
||||||
@@ -196,6 +217,7 @@ var (
|
|||||||
whiteListCommits map[string]bool
|
whiteListCommits map[string]bool
|
||||||
whiteListRepos []*regexp.Regexp
|
whiteListRepos []*regexp.Regexp
|
||||||
entropyRanges []entropyRange
|
entropyRanges []entropyRange
|
||||||
|
entropyRegexes []*regexp.Regexp
|
||||||
fileDiffRegex *regexp.Regexp
|
fileDiffRegex *regexp.Regexp
|
||||||
sshAuth *ssh.PublicKeys
|
sshAuth *ssh.PublicKeys
|
||||||
dir string
|
dir string
|
||||||
@@ -325,6 +347,10 @@ func run() ([]Leak, error) {
|
|||||||
// writeReport writes a report to a file specified in the --report= option.
|
// writeReport writes a report to a file specified in the --report= option.
|
||||||
// Default format for report is JSON. You can use the --csv option to write the report as a csv
|
// Default format for report is JSON. You can use the --csv option to write the report as a csv
|
||||||
func writeReport(leaks []Leak) error {
|
func writeReport(leaks []Leak) error {
|
||||||
|
if len(leaks) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
log.Infof("writing report to %s", opts.Report)
|
log.Infof("writing report to %s", opts.Report)
|
||||||
if strings.HasSuffix(opts.Report, ".csv") {
|
if strings.HasSuffix(opts.Report, ".csv") {
|
||||||
@@ -340,8 +366,41 @@ func writeReport(leaks []Leak) error {
|
|||||||
}
|
}
|
||||||
w.Flush()
|
w.Flush()
|
||||||
} else {
|
} else {
|
||||||
reportJSON, _ := json.MarshalIndent(leaks, "", "\t")
|
var (
|
||||||
err = ioutil.WriteFile(opts.Report, reportJSON, 0644)
|
f *os.File
|
||||||
|
encoder *json.Encoder
|
||||||
|
)
|
||||||
|
f, err := os.Create(opts.Report)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
encoder = json.NewEncoder(f)
|
||||||
|
encoder.SetIndent("", "\t")
|
||||||
|
if _, err := f.WriteString("[\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for i := 0; i < len(leaks); i++ {
|
||||||
|
if err := encoder.Encode(leaks[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// for all but the last leak, seek back and overwrite the newline appended by Encode() with comma & newline
|
||||||
|
if i+1 < len(leaks) {
|
||||||
|
if _, err := f.Seek(-1, 1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := f.WriteString(",\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := f.WriteString("]"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := f.Sync(); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -412,6 +471,14 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if target contains an external gitleaks toml
|
||||||
|
if opts.RepoConfig {
|
||||||
|
err := externalConfig(repo)
|
||||||
|
if err != nil {
|
||||||
|
return leaks, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// clear commit cache
|
// clear commit cache
|
||||||
commitMap = make(map[string]bool)
|
commitMap = make(map[string]bool)
|
||||||
|
|
||||||
@@ -432,6 +499,29 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) {
|
|||||||
return leaks, err
|
return leaks, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// externalConfig will attempt to load a pinned ".gitleaks.toml" configuration file
|
||||||
|
// from a remote or local repo. Use the --repo-config option to trigger this.
|
||||||
|
func externalConfig(repo *RepoDescriptor) error {
|
||||||
|
var config Config
|
||||||
|
wt, err := repo.repository.Worktree()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f, err := wt.Filesystem.Open(".gitleaks.toml")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := toml.DecodeReader(f, &config); err != nil {
|
||||||
|
return fmt.Errorf("problem loading config: %v", err)
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
updateConfig(config)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// auditGitReference beings the audit for a git reference. This function will
|
// auditGitReference beings the audit for a git reference. This function will
|
||||||
// traverse the git reference and audit each line of each diff.
|
// traverse the git reference and audit each line of each diff.
|
||||||
func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
||||||
@@ -490,6 +580,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
}
|
}
|
||||||
for _, re := range whiteListFiles {
|
for _, re := range whiteListFiles {
|
||||||
if re.FindString(f.Name) != "" {
|
if re.FindString(f.Name) != "" {
|
||||||
|
log.Debugf("skipping whitelisted file (matched regex '%s'): %s", re.String(), f.Name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -540,7 +631,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
commitWg.Done()
|
commitWg.Done()
|
||||||
<-semaphore
|
<-semaphore
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String())
|
log.Warnf("recovering from panic on commit %s, likely large diff causing panic", c.Hash.String())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
patch, err := c.Patch(parent)
|
patch, err := c.Patch(parent)
|
||||||
@@ -562,6 +653,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
}
|
}
|
||||||
for _, re := range whiteListFiles {
|
for _, re := range whiteListFiles {
|
||||||
if re.FindString(filePath) != "" {
|
if re.FindString(filePath) != "" {
|
||||||
|
log.Debugf("skipping whitelisted file (matched regex '%s'): %s", re.String(), filePath)
|
||||||
skipFile = true
|
skipFile = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -571,7 +663,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
}
|
}
|
||||||
chunks := f.Chunks()
|
chunks := f.Chunks()
|
||||||
for _, chunk := range chunks {
|
for _, chunk := range chunks {
|
||||||
if chunk.Type() == 1 || chunk.Type() == 2 {
|
if chunk.Type() == diffType.Add || chunk.Type() == diffType.Delete {
|
||||||
diff := gitDiff{
|
diff := gitDiff{
|
||||||
repoName: repoName,
|
repoName: repoName,
|
||||||
filePath: filePath,
|
filePath: filePath,
|
||||||
@@ -604,12 +696,13 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
|||||||
// will skip lines that include a whitelisted regex. A list of leaks is returned.
|
// will skip lines that include a whitelisted regex. A list of leaks is returned.
|
||||||
// If verbose mode (-v/--verbose) is set, then checkDiff will log leaks as they are discovered.
|
// If verbose mode (-v/--verbose) is set, then checkDiff will log leaks as they are discovered.
|
||||||
func inspect(diff gitDiff) []Leak {
|
func inspect(diff gitDiff) []Leak {
|
||||||
lines := strings.Split(diff.content, "\n")
|
|
||||||
var (
|
var (
|
||||||
leaks []Leak
|
leaks []Leak
|
||||||
skipLine bool
|
skipLine bool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
lines := strings.Split(diff.content, "\n")
|
||||||
|
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
skipLine = false
|
skipLine = false
|
||||||
for leakType, re := range regexes {
|
for leakType, re := range regexes {
|
||||||
@@ -617,47 +710,44 @@ func inspect(diff gitDiff) []Leak {
|
|||||||
if match == "" {
|
if match == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if skipLine = isLineWhitelisted(line); skipLine {
|
||||||
// if offender matches whitelist regex, ignore it
|
|
||||||
for _, wRe := range whiteListRegexes {
|
|
||||||
whitelistMatch := wRe.FindString(line)
|
|
||||||
if whitelistMatch != "" {
|
|
||||||
skipLine = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if skipLine {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
leaks = addLeak(leaks, line, match, leakType, diff)
|
leaks = addLeak(leaks, line, match, leakType, diff)
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Entropy > 0 || len(entropyRanges) != 0 {
|
if !skipLine && (opts.Entropy > 0 || len(entropyRanges) != 0) {
|
||||||
entropyLeak := false
|
|
||||||
words := strings.Fields(line)
|
words := strings.Fields(line)
|
||||||
for _, word := range words {
|
for _, word := range words {
|
||||||
entropy := getShannonEntropy(word)
|
entropy := getShannonEntropy(word)
|
||||||
if entropy >= opts.Entropy && len(entropyRanges) == 0 {
|
// Only check entropyRegexes and whiteListRegexes once per line, and only if an entropy leak type
|
||||||
entropyLeak = true
|
// was found above, since regex checks are expensive.
|
||||||
|
if !entropyIsHighEnough(entropy) {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if len(entropyRanges) != 0 {
|
// If either the line is whitelisted or the line fails the noiseReduction check (when enabled),
|
||||||
for _, eR := range entropyRanges {
|
// then we can skip checking the rest of the line for high entropy words.
|
||||||
if entropy > eR.v1 && entropy < eR.v2 {
|
if skipLine = !highEntropyLineIsALeak(line) || isLineWhitelisted(line); skipLine {
|
||||||
entropyLeak = true
|
break
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if entropyLeak {
|
|
||||||
leaks = addLeak(leaks, line, word, fmt.Sprintf("Entropy: %.2f", entropy), diff)
|
|
||||||
entropyLeak = false
|
|
||||||
}
|
}
|
||||||
|
leaks = addLeak(leaks, line, word, fmt.Sprintf("Entropy: %.2f", entropy), diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return leaks
|
return leaks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isLineWhitelisted returns true iff the line is matched by at least one of the whiteListRegexes.
|
||||||
|
func isLineWhitelisted(line string) bool {
|
||||||
|
for _, wRe := range whiteListRegexes {
|
||||||
|
whitelistMatch := wRe.FindString(line)
|
||||||
|
if whitelistMatch != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// addLeak is helper for func inspect() to append leaks if found during a diff check.
|
// addLeak is helper for func inspect() to append leaks if found during a diff check.
|
||||||
func addLeak(leaks []Leak, line string, offender string, leakType string, diff gitDiff) []Leak {
|
func addLeak(leaks []Leak, line string, offender string, leakType string, diff gitDiff) []Leak {
|
||||||
leak := Leak{
|
leak := Leak{
|
||||||
@@ -673,7 +763,7 @@ func addLeak(leaks []Leak, line string, offender string, leakType string, diff g
|
|||||||
}
|
}
|
||||||
if opts.Redact {
|
if opts.Redact {
|
||||||
leak.Offender = "REDACTED"
|
leak.Offender = "REDACTED"
|
||||||
leak.Line = "REDACTED"
|
leak.Line = strings.Replace(line, offender, "REDACTED", -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Verbose {
|
if opts.Verbose {
|
||||||
@@ -704,6 +794,36 @@ func getShannonEntropy(data string) (entropy float64) {
|
|||||||
return entropy
|
return entropy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func entropyIsHighEnough(entropy float64) bool {
|
||||||
|
if entropy >= opts.Entropy && len(entropyRanges) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(entropyRanges) != 0 {
|
||||||
|
for _, eR := range entropyRanges {
|
||||||
|
if entropy > eR.v1 && entropy < eR.v2 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func highEntropyLineIsALeak(line string) bool {
|
||||||
|
if !opts.NoiseReduction {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, re := range entropyRegexes {
|
||||||
|
if re.FindString(line) != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// discoverRepos walks all the children of `path`. If a child directory
|
// discoverRepos walks all the children of `path`. If a child directory
|
||||||
// contain a .git file then that repo will be added to the list of repos returned
|
// contain a .git file then that repo will be added to the list of repos returned
|
||||||
func discoverRepos(ownerPath string) ([]*RepoDescriptor, error) {
|
func discoverRepos(ownerPath string) ([]*RepoDescriptor, error) {
|
||||||
@@ -839,6 +959,11 @@ func loadToml() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return updateConfig(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateConfig will update a the global config values
|
||||||
|
func updateConfig(config Config) error {
|
||||||
if len(config.Misc.Entropy) != 0 {
|
if len(config.Misc.Entropy) != 0 {
|
||||||
err := entropyLimits(config.Misc.Entropy)
|
err := entropyLimits(config.Misc.Entropy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -846,6 +971,10 @@ func loadToml() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, regex := range config.Entropy.LineRegexes {
|
||||||
|
entropyRegexes = append(entropyRegexes, regexp.MustCompile(regex))
|
||||||
|
}
|
||||||
|
|
||||||
if singleSearchRegex != nil {
|
if singleSearchRegex != nil {
|
||||||
regexes["singleSearch"] = singleSearchRegex
|
regexes["singleSearch"] = singleSearchRegex
|
||||||
} else {
|
} else {
|
||||||
@@ -868,6 +997,7 @@ func loadToml() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// entropyLimits hydrates entropyRanges which allows for fine tuning entropy checking
|
// entropyLimits hydrates entropyRanges which allows for fine tuning entropy checking
|
||||||
|
|||||||
Reference in new issue
Block a user