working tests out

This commit is contained in:
zach rice committed 2019-04-13 12:30:00 -05:00
1 parent 256ace38d7
commit 7bd55e33b5
10 files changed
+144 -141

No files matched your search

Binary file not shown.
+18 -14
View File
@@ -25,6 +25,7 @@ type Rule struct {
severity string severity string
tags []string tags []string
entropies []*entropyRange entropies []*entropyRange
entropyROI string
} }
// TomlConfig is used for loading gitleaks configs from a toml file // TomlConfig is used for loading gitleaks configs from a toml file
@@ -35,6 +36,7 @@ type TomlConfig struct {
Entropies []string Entropies []string
Tags []string Tags []string
Severity string Severity string
EntropyROI string
} }
Whitelist struct { Whitelist struct {
Files []string Files []string
@@ -117,24 +119,26 @@ func (config *Config) update(tomlConfig TomlConfig) error {
severity: rule.Severity, severity: rule.Severity,
tags: rule.Tags, tags: rule.Tags,
entropies: ranges, entropies: ranges,
entropyROI: rule.EntropyROI,
} }
config.Rules = append(config.Rules, r) config.Rules = append(config.Rules, r)
} }
// set stand alone rules from opts // // set stand alone rules from opts
if opts.Entropy != 0.0 { // if opts.Entropy != 0.0 {
ranges, err := getEntropyRanges([]string{fmt.Sprintf("0.0-%s", opts.Entropy)}) // ranges, err := getEntropyRanges([]string{fmt.Sprintf("0.0-%s", opts.Entropy)})
if err != nil { // if err != nil {
log.Fatalf("could not create entropy range for %s", opts.Entropy) // log.Fatalf("could not create entropy range for %s", opts.Entropy)
} // }
r := &Rule{ // r := &Rule{
description: "Entropy ", // description: "Entropy ",
severity: "5", // severity: "5",
tags: []string{"entropy"}, // tags: []string{"entropy"},
entropies: ranges, // entropies: ranges,
} // entropyROI:
config.Rules = append(config.Rules, r) // }
} // config.Rules = append(config.Rules, r)
// }
// set whitelists // set whitelists
config.WhiteList.commits = make(map[string]bool) config.WhiteList.commits = make(map[string]bool)
+48 -4
View File
@@ -15,13 +15,57 @@ const defaultConfig = `
title = "gitleaks config" title = "gitleaks config"
# add rules to the rule table # add rules to the rule table
# [[rules]]
# description = "Generic Key"
# regex = '''(?i)key(.{0,6})?(:|=|=>|:=)'''
# entropies = ["4.1-4.3"]
# entropyROI = "line"
# tags = ["key"]
[[rules]] [[rules]]
description = "AWS" description = "AWS Key"
regex = '''AKIA[0-9A-Z]{16}''' regex = '''AKIA[0-9A-Z]{16}'''
tags = ["key", "AWS"] tags = ["key", "AWS"]
entropies = [ "3.3-4.30" ]
filetypes = [ "*.go" ] [[rules]]
severity = "5" description = "PKCS8"
regex = '''-----BEGIN PRIVATE KEY-----'''
tags = ["key", "PKCS8"]
[[rules]]
description = "RSA"
regex = '''-----BEGIN RSA PRIVATE KEY-----'''
tags = ["key", "RSA"]
[[rules]]
description = "SSH"
regex = '''-----BEGIN OPENSSH PRIVATE KEY-----'''
tags = ["key", "SSH"]
[[rules]]
description = "PGP"
regex = '''-----BEGIN PGP PRIVATE KEY BLOCK-----'''
tags = ["key", "PGP"]
[[rules]]
description = "Facebook"
regex = '''(?i)facebook(.{0,4})?['\"][0-9a-f]{32}['\"]'''
tags = ["key", "Facebook"]
[[rules]]
description = "Twitter"
regex = '''(?i)twitter(.{0,4})?['\"][0-9a-zA-Z]{35,44}['\"]'''
tags = ["key", "Twitter"]
[[rules]]
description = "Github"
regex = '''(?i)github(.{0,4})?['\"][0-9a-zA-Z]{35,40}['\"]'''
tags = ["key", "Github"]
[[rules]]
description = "Slack"
regex = '''xox[baprs]-([0-9a-zA-Z]{10,48})?'''
tags = ["key", "Slack"]
[whitelist] [whitelist]
files = [ files = [
+8 -3
View File
@@ -27,9 +27,10 @@ func init() {
threads = defaultThreadNum threads = defaultThreadNum
} }
// Report is // Report can be exported as a json or csv. Used for logging informationn
// about the audit, (duration and # of commits)
type Report struct { type Report struct {
Leaks []*Leak Leaks []Leak
Duration string Duration string
Commits int64 Commits int64
} }
@@ -37,7 +38,7 @@ type Report struct {
// Run is the entry point for gitleaks // Run is the entry point for gitleaks
func Run(optsL *Options) (*Report, error) { func Run(optsL *Options) (*Report, error) {
var ( var (
leaks []*Leak leaks []Leak
err error err error
) )
@@ -47,6 +48,10 @@ func Run(optsL *Options) (*Report, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if opts.ValidateConfig {
log.Info("valid gitleaks config")
return nil, nil
}
if opts.Disk { if opts.Disk {
// temporary directory where all the gitleaks plain clones will reside // temporary directory where all the gitleaks plain clones will reside
+4 -4
View File
@@ -21,8 +21,8 @@ import (
var githubPages = 100 var githubPages = 100
// auditPR audits a single github PR // auditPR audits a single github PR
func auditGithubPR() ([]*Leak, error) { func auditGithubPR() ([]Leak, error) {
var leaks []*Leak var leaks []Leak
ctx := context.Background() ctx := context.Background()
githubClient := github.NewClient(githubToken()) githubClient := github.NewClient(githubToken())
splits := strings.Split(opts.GithubPR, "/") splits := strings.Split(opts.GithubPR, "/")
@@ -91,7 +91,7 @@ func auditGithubPR() ([]*Leak, error) {
// First, we gather all the github repositories from the github api (this doesnt actually clone the repo). // 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. // After all the repos have been pulled from github's api we proceed to audit the repos by calling auditGithubRepo.
// If an error occurs during an audit of a repo, that error is logged but won't break the execution cycle. // If an error occurs during an audit of a repo, that error is logged but won't break the execution cycle.
func auditGithubRepos() ([]*Leak, error) { func auditGithubRepos() ([]Leak, error) {
var ( var (
err error err error
githubRepos []*github.Repository githubRepos []*github.Repository
@@ -100,7 +100,7 @@ func auditGithubRepos() ([]*Leak, error) {
githubOrgOptions *github.RepositoryListByOrgOptions githubOrgOptions *github.RepositoryListByOrgOptions
githubOptions *github.RepositoryListOptions githubOptions *github.RepositoryListOptions
done bool done bool
leaks []*Leak leaks []Leak
ownerDir string ownerDir string
) )
ctx := context.Background() ctx := context.Background()
+2 -2
View File
@@ -17,11 +17,11 @@ const gitlabPages = 100
// auditGitlabRepos kicks off audits if --gitlab-user or --gitlab-org options are set. // auditGitlabRepos kicks off audits if --gitlab-user or --gitlab-org options are set.
// Getting all repositories from the GitLab API and run audit. If an error occurs during an audit of a repo, // Getting all repositories from the GitLab API and run audit. If an error occurs during an audit of a repo,
// that error is logged. // that error is logged.
func auditGitlabRepos() ([]*Leak, error) { func auditGitlabRepos() ([]Leak, error) {
var ( var (
ps []*gitlab.Project ps []*gitlab.Project
resp *gitlab.Response resp *gitlab.Response
leaks []*Leak leaks []Leak
tempDir string tempDir string
err error err error
) )
+16 -81
View File
@@ -17,7 +17,7 @@ import (
) )
const testWhitelistCommit = ` const testWhitelistCommit = `
[[regexes]] [[rules]]
description = "AWS" description = "AWS"
regex = '''AKIA[0-9A-Z]{16}''' regex = '''AKIA[0-9A-Z]{16}'''
@@ -27,7 +27,7 @@ commits = [
] ]
` `
const testWhitelistFile = ` const testWhitelistFile = `
[[regexes]] [[rules]]
description = "AWS" description = "AWS"
regex = '''AKIA[0-9A-Z]{16}''' regex = '''AKIA[0-9A-Z]{16}'''
@@ -38,7 +38,7 @@ files = [
` `
const testWhitelistRegex = ` const testWhitelistRegex = `
[[regexes]] [[rules]]
description = "AWS" description = "AWS"
regex = '''AKIA[0-9A-Z]{16}''' regex = '''AKIA[0-9A-Z]{16}'''
@@ -49,7 +49,7 @@ regexes= [
` `
const testWhitelistRepo = ` const testWhitelistRepo = `
[[regexes]] [[rules]]
description = "AWS" description = "AWS"
regex = '''AKIA[0-9A-Z]{16}''' regex = '''AKIA[0-9A-Z]{16}'''
@@ -60,34 +60,24 @@ repos = [
` `
const testEntropyRange = ` const testEntropyRange = `
[entropy] [[rules]]
ranges = [ description = "Entropy ranges"
entropies = [
"7.5-8.0", "7.5-8.0",
"3.2-3.4", "3.2-3.4",
] ]
lineregexes = [
"(?i)api",
"(?i)key",
"signature",
"secret",
"password",
"pass",
"pwd",
"token",
"curl",
"wget",
"https?",
]
` `
const testBadEntropyRange = ` const testBadEntropyRange = `
[entropy] [[rules]]
ranges = [ description = "Bad entropy ranges"
entropies = [
"8.0-3.0", "8.0-3.0",
] ]
` `
const testBadEntropyRange2 = ` const testBadEntropyRange2 = `
[entropy] [[rules]]
ranges = [ description = "Bad entropy ranges"
entropies = [
"8.0-8.9", "8.0-8.9",
] ]
` `
@@ -373,7 +363,7 @@ func TestWriteReport(t *testing.T) {
Line: "eat", Line: "eat",
Commit: "your", Commit: "your",
Offender: "veggies", Offender: "veggies",
Type: "and", Rule: "and",
Message: "get", Message: "get",
Author: "some", Author: "some",
File: "sleep", File: "sleep",
@@ -630,23 +620,6 @@ func TestAuditRepo(t *testing.T) {
testOpts: &Options{}, testOpts: &Options{},
configPath: path.Join(configsDir, "repo"), configPath: path.Join(configsDir, "repo"),
}, },
{
repo: leaksRepo,
description: "leaks present with entropy",
testOpts: &Options{
Entropy: 4.7,
},
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",
@@ -666,7 +639,7 @@ func TestAuditRepo(t *testing.T) {
{ {
repo: leaksRepo, repo: leaksRepo,
description: "toml entropy range from opts", description: "toml entropy range from opts",
numLeaks: 454, numLeaks: 426,
testOpts: &Options{ testOpts: &Options{
ConfigPath: path.Join(configsDir, "entropy"), ConfigPath: path.Join(configsDir, "entropy"),
}, },
@@ -674,19 +647,10 @@ func TestAuditRepo(t *testing.T) {
{ {
repo: leaksRepo, repo: leaksRepo,
description: "toml entropy range", description: "toml entropy range",
numLeaks: 454, numLeaks: 426,
testOpts: &Options{}, testOpts: &Options{},
configPath: path.Join(configsDir, "entropy"), configPath: path.Join(configsDir, "entropy"),
}, },
{
repo: leaksRepo,
testOpts: &Options{
NoiseReduction: true,
},
description: "toml entropy noise reduction range",
numLeaks: 64,
configPath: path.Join(configsDir, "entropy"),
},
{ {
repo: leaksRepo, repo: leaksRepo,
description: "toml bad entropy range", description: "toml bad entropy range",
@@ -775,30 +739,6 @@ func TestOptionGuard(t *testing.T) {
description: "local and remote target", description: "local and remote target",
expectedErrMsg: "github user set and local owner path", expectedErrMsg: "github user set and local owner path",
}, },
{
testOpts: &Options{
GithubUser: "fakeUser",
SingleSearch: "*/./....",
},
description: "single search invalid regex gaurd",
expectedErrMsgFuzzy: "unable to compile regex: */./...., ",
},
{
testOpts: &Options{
GithubUser: "fakeUser",
SingleSearch: "mystring",
},
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) g := goblin.Goblin(t)
for _, test := range tests { for _, test := range tests {
@@ -880,11 +820,6 @@ func TestLoadToml(t *testing.T) {
g.Describe("TestLoadToml", func() { g.Describe("TestLoadToml", func() {
g.It(test.description, func() { g.It(test.description, func() {
opts = test.testOpts opts = test.testOpts
if test.singleSearch {
singleSearchRegex = regexp.MustCompile("test")
} else {
singleSearchRegex = nil
}
if test.configPath != "" { if test.configPath != "" {
os.Setenv("GITLEAKS_CONFIG", test.configPath) os.Setenv("GITLEAKS_CONFIG", test.configPath)
} else { } else {
+4 -4
View File
@@ -40,7 +40,6 @@ 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)"`
RepoConfig bool `long:"repo-config" description:"Load config from target repo. Config file must be \".gitleaks.toml\""` RepoConfig bool `long:"repo-config" description:"Load config from target repo. Config file must be \".gitleaks.toml\""`
Branch string `long:"branch" description:"Branch to audit"` Branch string `long:"branch" description:"Branch to audit"`
// TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"` // TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"`
@@ -52,6 +51,7 @@ type Options struct {
Redact bool `long:"redact" description:"redact secrets from log messages and report"` Redact bool `long:"redact" description:"redact secrets from log messages and report"`
Version bool `long:"version" description:"version number"` Version bool `long:"version" description:"version number"`
SampleConfig bool `long:"sample-config" description:"prints a sample config file"` SampleConfig bool `long:"sample-config" description:"prints a sample config file"`
ValidateConfig bool `long:"validate-config" description:"validate gitleaks config"`
} }
// ParseOpts parses the options // ParseOpts parses the options
@@ -125,9 +125,9 @@ func (opts *Options) guard() error {
} }
} }
if opts.Entropy > 8 { // if opts.Entropy > 8 {
return fmt.Errorf("The maximum level of entropy is 8") // return fmt.Errorf("The maximum level of entropy is 8")
} // }
if opts.Report != "" { if opts.Report != "" {
if !strings.HasSuffix(opts.Report, ".json") && !strings.HasSuffix(opts.Report, ".csv") { if !strings.HasSuffix(opts.Report, ".json") && !strings.HasSuffix(opts.Report, ".csv") {
return fmt.Errorf("Report should be a .json or .csv file") return fmt.Errorf("Report should be a .json or .csv file")
+6 -5
View File
@@ -23,7 +23,8 @@ type Leak struct {
Line string `json:"line"` Line string `json:"line"`
Commit string `json:"commit"` Commit string `json:"commit"`
Offender string `json:"offender"` Offender string `json:"offender"`
Type string `json:"reason"` Rule string `json:"rule"`
Info string `json:"info"`
Message string `json:"commitMsg"` Message string `json:"commitMsg"`
Author string `json:"author"` Author string `json:"author"`
Email string `json:"email"` Email string `json:"email"`
@@ -106,10 +107,10 @@ func (repoInfo *RepoInfo) clone() error {
} }
// audit performs an audit // audit performs an audit
func (repoInfo *RepoInfo) audit() ([]*Leak, error) { func (repoInfo *RepoInfo) audit() ([]Leak, error) {
var ( var (
err error err error
leaks []*Leak leaks []Leak
commitCount int64 commitCount int64
commitWg sync.WaitGroup commitWg sync.WaitGroup
semaphore chan bool semaphore chan bool
@@ -281,8 +282,8 @@ func (repoInfo *RepoInfo) audit() ([]*Leak, error) {
return leaks, nil return leaks, nil
} }
func (repoInfo *RepoInfo) auditSingleCommit(c *object.Commit) []*Leak { func (repoInfo *RepoInfo) auditSingleCommit(c *object.Commit) []Leak {
var leaks []*Leak var leaks []Leak
fIter, err := c.Files() fIter, err := c.Files()
if err != nil { if err != nil {
return nil return nil
+38 -24
View File
@@ -28,7 +28,7 @@ type commitInfo struct {
// 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 { if len(leaks) == 0 {
return nil return nil
} }
@@ -43,7 +43,7 @@ func writeReport(leaks []*Leak) error {
w := csv.NewWriter(f) w := csv.NewWriter(f)
w.Write([]string{"repo", "line", "commit", "offender", "reason", "commitMsg", "author", "email", "file", "date"}) w.Write([]string{"repo", "line", "commit", "offender", "reason", "commitMsg", "author", "email", "file", "date"})
for _, leak := range leaks { for _, leak := range leaks {
w.Write([]string{leak.Repo, leak.Line, leak.Commit, leak.Offender, leak.Type, leak.Message, leak.Author, leak.Email, leak.File, leak.Date.Format(time.RFC3339)}) w.Write([]string{leak.Repo, leak.Line, leak.Commit, leak.Offender, leak.Rule, leak.Message, leak.Author, leak.Email, leak.File, leak.Date.Format(time.RFC3339)})
} }
w.Flush() w.Flush()
} else { } else {
@@ -91,28 +91,41 @@ func (rule *Rule) check(line string, commit *commitInfo) (*Leak, error) {
) )
if rule.entropies != nil { if rule.entropies != nil {
words := strings.Fields(line) if rule.entropyROI == "line" {
for _, word := range words { _entropy := getShannonEntropy(line)
_entropy := getShannonEntropy(word) for _, e := range rule.entropies {
for _, e := range rule.entropies { if _entropy > e.v1 && _entropy < e.v2 {
if _entropy > e.v1 && _entropy < e.v2 { entropy = _entropy
entropy = _entropy entropyWord = line
entropyWord = word goto postEntropy
} }
} }
} } else {
words := strings.Fields(line)
for _, word := range words {
_entropy := getShannonEntropy(word)
for _, e := range rule.entropies {
if _entropy > e.v1 && _entropy < e.v2 {
entropy = _entropy
entropyWord = word
goto postEntropy
}
}
}
}
} }
postEntropy:
if rule.regex != nil { if rule.regex != nil {
match = rule.regex.FindString(line) match = rule.regex.FindString(line)
} }
if match != "" && entropy != 0.0 { if match != "" && entropy != 0.0 {
return newLeak(line, entropyWord, rule, commit), nil return newLeak(line, fmt.Sprintf("%s regex match and entropy met at %.2f", rule.regex.String(), entropy), entropyWord, rule, commit), nil
} else if match != "" && rule.entropies == nil { } else if match != "" && rule.entropies == nil {
return newLeak(line, match, rule, commit), nil return newLeak(line, fmt.Sprintf("%s regex match", rule.regex.String()), match, rule, commit), nil
} else if entropy != 0.0 && rule.regex == nil { } else if entropy != 0.0 && rule.regex.String() == "" {
return newLeak(line, entropyWord, rule, commit), nil return newLeak(line, fmt.Sprintf("entropy met at %.2f", entropy), entropyWord, rule, commit), nil
} }
return nil, nil return nil, nil
} }
@@ -121,8 +134,8 @@ func (rule *Rule) check(line string, commit *commitInfo) (*Leak, error) {
// a set of regexes set by the config (see gitleaks.toml for example). This function // a set of regexes set by the config (see gitleaks.toml for example). This function
// 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(commit *commitInfo) []*Leak { func inspect(commit *commitInfo) []Leak {
var leaks []*Leak var leaks []Leak
lines := strings.Split(commit.content, "\n") lines := strings.Split(commit.content, "\n")
for _, line := range lines { for _, line := range lines {
@@ -131,10 +144,10 @@ func inspect(commit *commitInfo) []*Leak {
break break
} }
leak, err := rule.check(line, commit) leak, err := rule.check(line, commit)
if err != nil { if err != nil || leak == nil {
continue continue
} }
leaks = append(leaks, leak) leaks = append(leaks, *leak)
} }
} }
return leaks return leaks
@@ -151,12 +164,13 @@ func isLineWhitelisted(line string) bool {
return false return false
} }
func newLeak(line string, msg string, rule *Rule, commit *commitInfo) *Leak { func newLeak(line string, info string, offender string, rule *Rule, commit *commitInfo) *Leak {
leak := &Leak{ leak := &Leak{
Line: line, Line: line,
Commit: commit.sha, Commit: commit.sha,
Offender: msg, Offender: offender,
Type: rule.description, Rule: rule.description,
Info: info,
Author: commit.author, Author: commit.author,
Email: commit.email, Email: commit.email,
File: commit.filePath, File: commit.filePath,
@@ -168,7 +182,7 @@ func newLeak(line string, msg string, rule *Rule, commit *commitInfo) *Leak {
} }
if opts.Redact { if opts.Redact {
leak.Offender = "REDACTED" leak.Offender = "REDACTED"
leak.Line = strings.Replace(line, msg, "REDACTED", -1) leak.Line = strings.Replace(line, offender, "REDACTED", -1)
} }
if opts.Verbose { if opts.Verbose {