Compare commits

...
7 Commits
Author SHA1 Message Date
Zachary Rice 7c988c927d Merge pull request #107 from camaya/feature/add-entropy-checks
Add option to check string entropy
2018-10-09 18:46:21 -05:00
zach rice bde9af44ea merging, adding comments, updating flag description 2018-10-09 18:42:09 -05:00
Zachary Rice eee4d05642 Merge pull request #108 from camaya/fix-commit-option
Fix commit option
2018-10-09 16:18:54 -05:00
Cristhian Amaya 9144594d77 Fix commit option
The --commit option was being ignored becase the Close() function doesn't stop
the iterator.
According to the documentation:
https://godoc.org/gopkg.in/src-d/go-git.v4/plumbing/object#FileIter.ForEach
an ErrStop error should be send to stop it.
2018-10-09 16:49:16 +02:00
Cristhian Amaya 1cb15242fd Add option to check string entropy
Strings with high entropy have a good probability of being a secret.
2018-10-08 16:08:04 +02:00
Zachary Rice 08498600d1 Merge pull request #106 from zricethezav/exclude-fork
exclude fork
2018-10-07 18:42:42 -05:00
zach rice 88c0c7779b exclude fork 2018-10-07 18:39:17 -05:00
3 changed files with 116 additions and 26 deletions

No files matched your search

+7
View File
@@ -1,6 +1,13 @@
CHANGELOG
=========
1.10.0
-----
- Add entropy option
1.9.0
-----
- exclude fork option
1.8.0
-----
- whitelist repos
+34 -1
View File
@@ -279,11 +279,20 @@ func TestRun(t *testing.T) {
testOpts: Options{
GithubOrg: "gitleakstestorg",
},
description: "test github org",
description: "test github org, whitelist repo",
numLeaks: 0,
expectedErrMsg: "",
configPath: path.Join(configsDir, "repo"),
},
{
testOpts: Options{
GithubOrg: "gitleakstestorg",
ExcludeForks: true,
},
description: "test github org, exclude forks",
numLeaks: 0,
expectedErrMsg: "",
},
}
g := goblin.Goblin(t)
for _, test := range tests {
@@ -551,6 +560,22 @@ func TestAuditRepo(t *testing.T) {
numLeaks: 0,
configPath: path.Join(configsDir, "repo"),
},
{
repo: leaksRepo,
description: "leaks present with entropy",
testOpts: Options{
Entropy: 4.7,
},
numLeaks: 7,
},
{
repo: leaksRepo,
description: "Audit until specific commit",
numLeaks: 1,
testOpts: Options{
Commit: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c",
},
},
}
whiteListCommits = make(map[string]bool)
@@ -677,6 +702,14 @@ func TestOptionGuard(t *testing.T) {
description: "single search regex gaurd",
expectedErrMsg: "",
},
{
testOpts: Options{
GithubOrg: "fakeOrg",
Entropy: 9,
},
description: "Invalid entropy level guard",
expectedErrMsg: "The maximum level of entropy is 8",
},
}
g := goblin.Goblin(t)
for _, test := range tests {
+75 -25
View File
@@ -5,8 +5,10 @@ import (
"crypto/md5"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"net"
"net/http"
"net/url"
@@ -81,12 +83,14 @@ type Options struct {
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
// Process options
MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
ConfigPath string `long:"config" description:"path to gitleaks config"`
SSHKey string `long:"ssh-key" description:"path to ssh key"`
MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
ConfigPath string `long:"config" description:"path to gitleaks config"`
SSHKey string `long:"ssh-key" description:"path to ssh key"`
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)"`
// TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"`
// Output options
@@ -123,7 +127,7 @@ type gitDiff struct {
}
const defaultGithubURL = "https://api.github.com/"
const version = "1.8.0"
const version = "1.10.0"
const errExit = 2
const leakExit = 1
const defaultConfig = `
@@ -449,6 +453,7 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
err = cIter.ForEach(func(c *object.Commit) error {
if c.Hash.String() == opts.Commit {
cIter.Close()
return errors.New("ErrStop")
}
if whiteListCommits[c.Hash.String()] {
log.Infof("skipping commit: %s\n", c.Hash.String())
@@ -575,30 +580,68 @@ func inspect(diff gitDiff) []Leak {
break
}
leak := Leak{
Line: line,
Commit: diff.commit.Hash.String(),
Offender: match,
Type: leakType,
Message: diff.commit.Message,
Author: diff.commit.Author.String(),
File: diff.filePath,
Branch: diff.branchName,
Repo: diff.repoName,
leaks = addLeak(leaks, line, match, leakType, diff)
}
if opts.Entropy > 0 {
words := strings.Fields(line)
for _, word := range words {
if getShannonEntropy(word) >= opts.Entropy {
leaks = addLeak(leaks, line, word, "High Entropy", diff)
}
}
if opts.Redact {
leak.Offender = "REDACTED"
leak.Line = "REDACTED"
}
if opts.Verbose {
leak.log()
}
leaks = append(leaks, leak)
}
}
return leaks
}
// getShannonEntropy https://en.wiktionary.org/wiki/Shannon_entropy
func getShannonEntropy(data string) (entropy float64) {
if data == "" {
return 0
}
charCounts := make(map[rune]int)
for _, char := range data {
charCounts[char]++
}
invLength := 1.0 / float64(len(data))
for _, count := range charCounts {
freq := float64(count) * invLength
entropy -= freq * math.Log2(freq)
}
return entropy
}
// 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{
Line: line,
Commit: diff.commit.Hash.String(),
Offender: offender,
Type: leakType,
Message: diff.commit.Message,
Author: diff.commit.Author.String(),
File: diff.filePath,
Branch: diff.branchName,
Repo: diff.repoName,
}
if opts.Redact {
leak.Offender = "REDACTED"
leak.Line = "REDACTED"
}
if opts.Verbose {
leak.log()
}
leaks = append(leaks, leak)
return leaks
}
// auditGithubRepos kicks off audits if --github-user or --github-org options are set.
// First, we gather all the github repositories from the github api (this doesnt actually clone the repo).
// After all the repos have been pulled from github's api we proceed to audit the repos by calling auditGithubRepo.
@@ -715,6 +758,9 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) {
repo *git.Repository
err error
)
if opts.ExcludeForks && githubRepo.GetFork() {
return nil, fmt.Errorf("skipping %s, excluding forks", *githubRepo.Name)
}
for _, repoName := range whiteListRepos {
if repoName == *githubRepo.Name {
return nil, fmt.Errorf("skipping %s, whitelisted", repoName)
@@ -862,6 +908,10 @@ func optsGuard() error {
}
}
if opts.Entropy > 8 {
return fmt.Errorf("The maximum level of entropy is 8")
}
return nil
}