mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 21:44:23 +01:00
Merge pull request #159 from camaya/reduce-entropy-noise
Reduce noise when running entropy checks
This commit is contained in:
3 files changed
+75
-13
No files matched your search
@@ -81,6 +81,7 @@ Application Options:
|
|||||||
--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)
|
||||||
|
--noise-reduction Reduce the number of finds when entropy checks are enabled
|
||||||
-l, --log= log level
|
-l, --log= log level
|
||||||
-v, --verbose Show verbose output from gitleaks audit
|
-v, --verbose Show verbose output from gitleaks audit
|
||||||
--report= path to write report file
|
--report= path to write report file
|
||||||
|
|||||||
@@ -592,6 +592,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",
|
||||||
@@ -614,6 +623,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",
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ type Options struct {
|
|||||||
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"`
|
||||||
// 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 +102,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
|
||||||
@@ -169,6 +173,21 @@ 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)$"
|
||||||
@@ -196,6 +215,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
|
||||||
@@ -630,23 +650,12 @@ func inspect(diff gitDiff) []Leak {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if opts.Entropy > 0 || len(entropyRanges) != 0 {
|
if 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 {
|
|
||||||
entropyLeak = true
|
if entropyIsHighEnough(entropy) && highEntropyLineIsALeak(line) {
|
||||||
}
|
|
||||||
if len(entropyRanges) != 0 {
|
|
||||||
for _, eR := range entropyRanges {
|
|
||||||
if entropy > eR.v1 && entropy < eR.v2 {
|
|
||||||
entropyLeak = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if entropyLeak {
|
|
||||||
leaks = addLeak(leaks, line, word, fmt.Sprintf("Entropy: %.2f", entropy), diff)
|
leaks = addLeak(leaks, line, word, fmt.Sprintf("Entropy: %.2f", entropy), diff)
|
||||||
entropyLeak = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -700,6 +709,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) {
|
||||||
@@ -842,6 +881,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 {
|
||||||
|
|||||||
Reference in new issue
Block a user