From 64c8d618a276c3ffe6a2bd1ca433d91ab1132058 Mon Sep 17 00:00:00 2001 From: Milo Minderbinder Date: Thu, 13 Dec 2018 13:14:54 -0500 Subject: [PATCH 01/13] fix file (#162) & line (#161) whitelist checks * only check entropyRegexes and whiteListRegexes once per line, and only if regex or entropy leak type was found, since regex checks are expensive * check whiteListFiles once for each FilePatch, instead of for every Chunk --- main.go | 56 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index 476fc24f..a843804e 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ import ( "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/transport/ssh" "gopkg.in/src-d/go-git.v4/storage/memory" @@ -30,9 +31,9 @@ import ( "github.com/BurntSushi/toml" "github.com/google/go-github/github" "github.com/hako/durafmt" - flags "github.com/jessevdk/go-flags" + "github.com/jessevdk/go-flags" 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. @@ -575,12 +576,17 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { } else if to != nil { filePath = to.Path() } + for _, re := range whiteListFiles { + if re.FindString(filePath) != "" { + skipFile = true + } + } if skipFile { continue } chunks := f.Chunks() for _, chunk := range chunks { - if chunk.Type() == 1 || chunk.Type() == 2 { + if chunk.Type() == diffType.Add || chunk.Type() == diffType.Delete { diff := gitDiff{ repoName: repoName, filePath: filePath, @@ -618,12 +624,6 @@ func inspect(diff gitDiff) []Leak { skipLine bool ) - for _, re := range whiteListFiles { - if re.FindString(diff.filePath) != "" { - return leaks - } - } - lines := strings.Split(diff.content, "\n") for _, line := range lines { @@ -633,36 +633,44 @@ func inspect(diff gitDiff) []Leak { if match == "" { continue } - - // if offender matches whitelist regex, ignore it - for _, wRe := range whiteListRegexes { - whitelistMatch := wRe.FindString(line) - if whitelistMatch != "" { - skipLine = true - break - } - } - if skipLine { + if skipLine = isLineWhitelisted(line); skipLine { break } - leaks = addLeak(leaks, line, match, leakType, diff) } - if opts.Entropy > 0 || len(entropyRanges) != 0 { + if !skipLine && (opts.Entropy > 0 || len(entropyRanges) != 0) { words := strings.Fields(line) for _, word := range words { entropy := getShannonEntropy(word) - - if entropyIsHighEnough(entropy) && highEntropyLineIsALeak(line) { - leaks = addLeak(leaks, line, word, fmt.Sprintf("Entropy: %.2f", entropy), diff) + // Only check entropyRegexes and whiteListRegexes once per line, and only if an entropy leak type + // was found above, since regex checks are expensive. + if !entropyIsHighEnough(entropy) { + continue } + // If either the line is whitelisted or the line fails the noiseReduction check (when enabled), + // then we can skip checking the rest of the line for high entropy words. + if skipLine = !highEntropyLineIsALeak(line) || isLineWhitelisted(line); skipLine { + break + } + leaks = addLeak(leaks, line, word, fmt.Sprintf("Entropy: %.2f", entropy), diff) } } } 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. func addLeak(leaks []Leak, line string, offender string, leakType string, diff gitDiff) []Leak { leak := Leak{ From bb9c459a1a68011be77d612f4141db4b3f4e6ecd Mon Sep 17 00:00:00 2001 From: Milo Minderbinder Date: Mon, 31 Dec 2018 12:49:49 -0500 Subject: [PATCH 02/13] check whiteListFiles for root commit & GitHub PRs add missing checks against `whiteListFiles` regexes for files in the root/initial commit and in GitHub PRs (in `auditGithubPR` function) --- github.go | 11 +++++++++++ main.go | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/github.go b/github.go index b3b7c5e3..091439a6 100644 --- a/github.go +++ b/github.go @@ -51,9 +51,20 @@ func auditGithubPR() ([]Leak, error) { } files := commit.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{ sha: commit.GetSHA(), diff --git a/main.go b/main.go index a843804e..cda203f4 100644 --- a/main.go +++ b/main.go @@ -509,6 +509,12 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { if bin || err != nil { return nil } + for _, re := range whiteListFiles { + if re.FindString(f.Name) != "" { + log.Infof("skipping whitelisted file (matched regex '%s'): %s", re.String(), f.Name) + return nil + } + } content, err := f.Contents() if err != nil { return nil @@ -578,7 +584,9 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { } for _, re := range whiteListFiles { if re.FindString(filePath) != "" { + log.Infof("skipping whitelisted file (matched regex '%s'): %s", re.String(), filePath) skipFile = true + break } } if skipFile { From 1e3bf42464ec7b0d85988d539f44c254d4ec680d Mon Sep 17 00:00:00 2001 From: Pepe Bawagan Date: Fri, 4 Jan 2019 15:23:15 +0800 Subject: [PATCH 03/13] Fix typo in recovery message --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index fbabad96..2b613804 100644 --- a/main.go +++ b/main.go @@ -566,7 +566,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { commitWg.Done() <-semaphore 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) From a24f9a2c92c41ddad5c8d633fce5fb4fc7590b8a Mon Sep 17 00:00:00 2001 From: zach rice Date: Fri, 4 Jan 2019 16:31:33 -0600 Subject: [PATCH 04/13] working on getting pinned config working --- gitleaks.toml | 5 +---- main.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/gitleaks.toml b/gitleaks.toml index 67d22979..496fe77b 100644 --- a/gitleaks.toml +++ b/gitleaks.toml @@ -21,9 +21,6 @@ regex = '''(?i)facebook.*['\"][0-9a-f]{32}['\"]''' description = "Twitter" regex = '''(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]''' [[regexes]] -description = "Telegram" -regex = '''(?i)telegram.*['\"][0-9]{1,12}+:[0-9a-zA-Z-]{32,44}['\"]''' -[[regexes]] description = "PGP" regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----''' [[regexes]] @@ -38,5 +35,5 @@ regexes = [ # "AKIA.*EXAMPLE", ] files = [ - "(.*?)(jpg|gif|doc|pdf|bin|go)$" + "(.*?)(jpg|gif|doc|pdf|bin)$" ] diff --git a/main.go b/main.go index 2b613804..f38969ce 100644 --- a/main.go +++ b/main.go @@ -86,6 +86,8 @@ type Options struct { 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)"` NoiseReduction bool `long:"noise-reduction" description:"Reduce the number of finds when entropy checks are enabled"` + Fast bool `long:"fast" description:"Run gitleaks on fast mode. This does not include information about commits."` + 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"` // Output options @@ -437,6 +439,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 commitMap = make(map[string]bool) @@ -457,6 +467,27 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) { return leaks, err } +func externalConfig(repo *RepoDescriptor) error { + var config Config + wt, err := repo.repository.Worktree() + if err != nil { + return err + } + c, err := wt.Filesystem.Open("gitleaks.toml") + if err != nil { + return err + } + if _, err := toml.DecodeReader(c, &config); err != nil { + return fmt.Errorf("problem loading config: %v", err) + } + c.Close() + if err != nil { + return err + } + updateConfig(config) + return nil +} + // auditGitReference beings the audit for a git reference. This function will // traverse the git reference and audit each line of each diff. func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { @@ -566,7 +597,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { commitWg.Done() <-semaphore if r := recover(); r != nil { - log.Warnf("recovering from panic on commit %s, likely large diff causing panic", c.Hash.String()) + log.Warnf("recoverying from panic on commit %s, likely large diff causing panic", c.Hash.String()) } }() patch, err := c.Patch(parent) @@ -894,6 +925,17 @@ func loadToml() error { } } + if len(config.Misc.Entropy) != 0 { + err := entropyLimits(config.Misc.Entropy) + if err != nil { + return err + } + } + return updateConfig(config) +} + +// updateConfig will update a the global config values +func updateConfig(config Config) error { if len(config.Misc.Entropy) != 0 { err := entropyLimits(config.Misc.Entropy) if err != nil { @@ -927,6 +969,7 @@ func loadToml() error { } return nil + } // entropyLimits hydrates entropyRanges which allows for fine tuning entropy checking From 0ba9dda0f02540a514c33e87277d7815ce47e9ef Mon Sep 17 00:00:00 2001 From: Milo Minderbinder Date: Fri, 4 Jan 2019 18:30:25 -0500 Subject: [PATCH 05/13] fix memory issue when writing json report (#168) Previously, gitleaks.writeReport used json.MarshallIndent on the entire set of leaks when writing json reports, which would cause failures due to excessive memory consumption when a large number of leaks were identified. This PR addresses the problem by doing incremental marshalling and writing of leaks to the report file, and reduces the memory footprint by several GB in tests with >30k leaks. --- main.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 2b613804..d31ff7fc 100644 --- a/main.go +++ b/main.go @@ -365,8 +365,41 @@ func writeReport(leaks []Leak) error { } w.Flush() } else { - reportJSON, _ := json.MarshalIndent(leaks, "", "\t") - err = ioutil.WriteFile(opts.Report, reportJSON, 0644) + var ( + 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 } From 9e39927ac68af482f676fa191be156c83a993aa1 Mon Sep 17 00:00:00 2001 From: zach rice Date: Sat, 5 Jan 2019 20:50:17 -0600 Subject: [PATCH 06/13] turning down logging, adding test --- gitleaks_test.go | 8 ++++++++ main.go | 12 +++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/gitleaks_test.go b/gitleaks_test.go index d9088dea..ac5b3303 100644 --- a/gitleaks_test.go +++ b/gitleaks_test.go @@ -489,6 +489,14 @@ func TestAuditRepo(t *testing.T) { whiteListRegexes []*regexp.Regexp configPath string }{ + { + repo: leaksRepo, + description: "pinned config", + numLeaks: 0, + testOpts: Options{ + RepoConfig: true, + }, + }, { repo: leaksRepo, description: "commit depth = 1, one leak", diff --git a/main.go b/main.go index f38969ce..cec224f0 100644 --- a/main.go +++ b/main.go @@ -467,20 +467,22 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) { 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 } - c, err := wt.Filesystem.Open("gitleaks.toml") + f, err := wt.Filesystem.Open(".gitleaks.toml") if err != nil { return err } - if _, err := toml.DecodeReader(c, &config); err != nil { + if _, err := toml.DecodeReader(f, &config); err != nil { return fmt.Errorf("problem loading config: %v", err) } - c.Close() + f.Close() if err != nil { return err } @@ -546,7 +548,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { } for _, re := range whiteListFiles { if re.FindString(f.Name) != "" { - log.Infof("skipping whitelisted file (matched regex '%s'): %s", re.String(), f.Name) + log.Debugf("skipping whitelisted file (matched regex '%s'): %s", re.String(), f.Name) return nil } } @@ -619,7 +621,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { } for _, re := range whiteListFiles { if re.FindString(filePath) != "" { - log.Infof("skipping whitelisted file (matched regex '%s'): %s", re.String(), filePath) + log.Debugf("skipping whitelisted file (matched regex '%s'): %s", re.String(), filePath) skipFile = true break } From 38a36add57162c41a860055cfbb1602db5435422 Mon Sep 17 00:00:00 2001 From: zach rice Date: Sat, 5 Jan 2019 20:51:59 -0600 Subject: [PATCH 07/13] typo --- main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.go b/main.go index cec224f0..b7ba6c57 100644 --- a/main.go +++ b/main.go @@ -86,7 +86,6 @@ type Options struct { 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)"` NoiseReduction bool `long:"noise-reduction" description:"Reduce the number of finds when entropy checks are enabled"` - Fast bool `long:"fast" description:"Run gitleaks on fast mode. This does not include information about commits."` 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"` @@ -599,7 +598,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak { commitWg.Done() <-semaphore 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) From 81ddfbed8880b8b3b3b0265f0bfc8f08ec605ce2 Mon Sep 17 00:00:00 2001 From: zach rice Date: Sun, 6 Jan 2019 08:52:31 -0600 Subject: [PATCH 08/13] dupe code --- main.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/main.go b/main.go index b7ba6c57..659242af 100644 --- a/main.go +++ b/main.go @@ -926,12 +926,6 @@ func loadToml() error { } } - if len(config.Misc.Entropy) != 0 { - err := entropyLimits(config.Misc.Entropy) - if err != nil { - return err - } - } return updateConfig(config) } From 8de38be418d8c08eef67a15c05d00dd77f74dedf Mon Sep 17 00:00:00 2001 From: zach rice Date: Sun, 6 Jan 2019 17:48:08 -0600 Subject: [PATCH 09/13] bump version --- README.md | 1 + main.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 05ab30c1..18c2b43e 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ Application Options: --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) --noise-reduction Reduce the number of finds when entropy checks are enabled + --repo-config Load config from target repo. Config file must be ".gitleaks.toml" -l, --log= log level -v, --verbose Show verbose output from gitleaks audit --report= path to write report file diff --git a/main.go b/main.go index 8606faeb..adc77586 100644 --- a/main.go +++ b/main.go @@ -136,7 +136,7 @@ type entropyRange struct { } const defaultGithubURL = "https://api.github.com/" -const version = "1.22.0" +const version = "1.23.0" const errExit = 2 const leakExit = 1 const defaultConfig = ` From 8f1bc74e26c8c04c3d5605448a024b7b14d59f2a Mon Sep 17 00:00:00 2001 From: zach rice Date: Sun, 6 Jan 2019 18:29:02 -0600 Subject: [PATCH 10/13] new logo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 18c2b43e..5d732c9c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- gitleaks + gitleaks

Travis

From 7fa9c802f543f1b35be8fdb2be30f7aba9b5cd97 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Sun, 6 Jan 2019 18:47:43 -0600 Subject: [PATCH 11/13] u r not prepared --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d732c9c..1b0337cf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- gitleaks + gitleaks

Travis

From d2baa9e675625366c3e79360e960ee048bf74346 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Sun, 6 Jan 2019 19:57:46 -0600 Subject: [PATCH 12/13] jk i am prepared. no more games --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1b0337cf..5d732c9c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- gitleaks + gitleaks

Travis

From 8485f881588d7577e35d5029e3d25a25eab60c4e Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Fri, 11 Jan 2019 15:35:29 -0600 Subject: [PATCH 13/13] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 5d732c9c..48d588ce 100644 --- a/README.md +++ b/README.md @@ -129,8 +129,5 @@ From C-U at Home: Donate: https://www.cuathome.us/give/ -#### Speaking of Community -Please read this https://corruptcu.com/ -