Merge pull request #107 from camaya/feature/add-entropy-checks

Add option to check string entropy
This commit is contained in:
Zachary Rice authored and GitHub committed 2018-10-09 18:46:21 -05:00
commit 7c988c927d
3 files changed
+67 -4

No files matched your search

+3
View File
@@ -1,6 +1,9 @@
CHANGELOG CHANGELOG
========= =========
1.10.0
-----
- Add entropy option
1.9.0 1.9.0
----- -----
- exclude fork option - exclude fork option
+16
View File
@@ -560,6 +560,14 @@ func TestAuditRepo(t *testing.T) {
numLeaks: 0, numLeaks: 0,
configPath: path.Join(configsDir, "repo"), configPath: path.Join(configsDir, "repo"),
}, },
{
repo: leaksRepo,
description: "leaks present with entropy",
testOpts: Options{
Entropy: 4.7,
},
numLeaks: 7,
},
{ {
repo: leaksRepo, repo: leaksRepo,
description: "Audit until specific commit", description: "Audit until specific commit",
@@ -694,6 +702,14 @@ func TestOptionGuard(t *testing.T) {
description: "single search regex gaurd", description: "single search regex gaurd",
expectedErrMsg: "", expectedErrMsg: "",
}, },
{
testOpts: Options{
GithubOrg: "fakeOrg",
Entropy: 9,
},
description: "Invalid entropy level guard",
expectedErrMsg: "The maximum level of entropy is 8",
},
} }
g := goblin.Goblin(t) g := goblin.Goblin(t)
for _, test := range tests { for _, test := range tests {
+48 -4
View File
@@ -8,6 +8,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@@ -89,6 +90,7 @@ type Options struct {
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)"`
// 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
@@ -125,7 +127,7 @@ type gitDiff struct {
} }
const defaultGithubURL = "https://api.github.com/" const defaultGithubURL = "https://api.github.com/"
const version = "1.9.0" const version = "1.10.0"
const errExit = 2 const errExit = 2
const leakExit = 1 const leakExit = 1
const defaultConfig = ` const defaultConfig = `
@@ -578,10 +580,47 @@ func inspect(diff gitDiff) []Leak {
break break
} }
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)
}
}
}
}
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{ leak := Leak{
Line: line, Line: line,
Commit: diff.commit.Hash.String(), Commit: diff.commit.Hash.String(),
Offender: match, Offender: offender,
Type: leakType, Type: leakType,
Message: diff.commit.Message, Message: diff.commit.Message,
Author: diff.commit.Author.String(), Author: diff.commit.Author.String(),
@@ -589,16 +628,17 @@ func inspect(diff gitDiff) []Leak {
Branch: diff.branchName, Branch: diff.branchName,
Repo: diff.repoName, Repo: diff.repoName,
} }
if opts.Redact { if opts.Redact {
leak.Offender = "REDACTED" leak.Offender = "REDACTED"
leak.Line = "REDACTED" leak.Line = "REDACTED"
} }
if opts.Verbose { if opts.Verbose {
leak.log() leak.log()
} }
leaks = append(leaks, leak) leaks = append(leaks, leak)
}
}
return leaks return leaks
} }
@@ -868,6 +908,10 @@ func optsGuard() error {
} }
} }
if opts.Entropy > 8 {
return fmt.Errorf("The maximum level of entropy is 8")
}
return nil return nil
} }