init stopword work

This commit is contained in:
zricethezav committed 2018-02-19 16:26:18 -06:00
1 parent 840e9e873f
commit d465c3d2f1
3 files changed
+66 -30

No files matched your search

+38 -18
View File
@@ -1,9 +1,9 @@
package main package main
import ( import (
//"fmt"
"math"
"strings" "strings"
"github.com/nbutton23/zxcvbn-go"
) )
// check each line of a diff and see if there are any potential secrets // check each line of a diff and see if there are any potential secrets
@@ -28,25 +28,45 @@ func checkRegex(diff string) []string {
return results return results
} }
// checkEntropy determines whether target contains enough func checkShannonEntropy(target string, entropyCutoff int) bool {
// entropy for a hash var sum float64
// TODO remove stop words: frq := make(map[rune]float64)
// setting(s), config(s), property(s), etc
func checkEntropy(target string) bool { for _, i := range target {
index := assignRegex.FindStringIndex(target) frq[i]++
if len(index) == 0 {
return false
} }
// TODO check for stop words here for _, v := range frq {
target = strings.Trim(target[index[1]:], " ") f := v / float64(len(target))
sum += f * math.Log2(f)
if len(target) > 70 {
return false
} }
entropy := zxcvbn.PasswordStrength(target, nil).Entropy bits := int(math.Ceil(sum*-1)) * len(target)
return bits > entropyCutoff
}
// tune this/make option func checkStopWords(target string) bool {
return entropy > 70 stopWords := []string{
"setting",
"Setting",
"SETTING",
"info",
"Info",
"INFO",
"env",
"Env",
"ENV",
"environment",
"Environment",
"ENVIRONMENT",
}
for _, stopWord := range stopWords {
if strings.Contains(target, stopWord) {
// fmt.Println("FOUND STOP", stopWord)
return true
}
}
return false
} }
+18 -8
View File
@@ -22,15 +22,16 @@ type LeakElem struct {
// start clones and determines if there are any leaks // start clones and determines if there are any leaks
func start(opts *Options) { func start(opts *Options) {
fmt.Printf("\nEvaluating \x1b[37;1m%s\x1b[0m...\n", opts.RepoURL)
c := make(chan os.Signal, 2) c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM) signal.Notify(c, os.Interrupt, syscall.SIGTERM)
fmt.Printf("Cloning \x1b[37;1m%s\x1b[0m...\n", opts.RepoURL)
err := exec.Command("git", "clone", opts.RepoURL).Run() err := exec.Command("git", "clone", opts.RepoURL).Run()
if err != nil { if err != nil {
log.Printf("failed to clone repo %v", err) log.Printf("failed to clone repo %v", err)
return return
} }
fmt.Printf("Evaluating \x1b[37;1m%s\x1b[0m...\n", opts.RepoURL)
repoName := getLocalRepoName(opts.RepoURL) repoName := getLocalRepoName(opts.RepoURL)
if err = os.Chdir(repoName); err != nil { if err = os.Chdir(repoName); err != nil {
log.Fatal(err) log.Fatal(err)
@@ -41,7 +42,7 @@ func start(opts *Options) {
os.Exit(1) os.Exit(1)
}() }()
report := getLeaks(repoName, opts.Concurrency) report := getLeaks(repoName, opts)
if len(report) == 0 { if len(report) == 0 {
fmt.Printf("No Leaks detected for \x1b[35;2m%s\x1b[0m...\n\n", opts.RepoURL) fmt.Printf("No Leaks detected for \x1b[35;2m%s\x1b[0m...\n\n", opts.RepoURL)
} }
@@ -76,7 +77,7 @@ func cleanup(repoName string) {
} }
// getLeaks will attempt to find gitleaks // getLeaks will attempt to find gitleaks
func getLeaks(repoName string, concurrency int) []LeakElem { func getLeaks(repoName string, opts *Options) []LeakElem {
var ( var (
out []byte out []byte
err error err error
@@ -84,15 +85,18 @@ func getLeaks(repoName string, concurrency int) []LeakElem {
gitLeakReceiverWG sync.WaitGroup gitLeakReceiverWG sync.WaitGroup
gitLeaks = make(chan LeakElem) gitLeaks = make(chan LeakElem)
report []LeakElem report []LeakElem
concurrency = 10
) )
if concurrency == 0 { if opts.Concurrency != 0 {
concurrency = 10 concurrency = opts.Concurrency
} }
semaphoreChan := make(chan struct{}, concurrency) semaphoreChan := make(chan struct{}, concurrency)
go func(commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) { go func(commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) {
for gitLeak := range gitLeaks { for gitLeak := range gitLeaks {
//defer gitLeakReceiverWG.Done()
fmt.Println(gitLeak) fmt.Println(gitLeak)
report = append(report, gitLeak) report = append(report, gitLeak)
gitLeakReceiverWG.Done() gitLeakReceiverWG.Done()
@@ -108,7 +112,10 @@ func getLeaks(repoName string, concurrency int) []LeakElem {
commitWG.Add(len(commits)) commitWG.Add(len(commits))
for _, currCommitB := range commits { for _, currCommitB := range commits {
currCommit := string(currCommitB) currCommit := string(currCommitB)
go func(currCommit string, repoName string, commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) {
go func(currCommit string, repoName string, commitWG *sync.WaitGroup,
gitLeakReceiverWG *sync.WaitGroup, opts *Options) {
defer commitWG.Done() defer commitWG.Done()
var leakPrs bool var leakPrs bool
@@ -137,13 +144,16 @@ func getLeaks(repoName string, concurrency int) []LeakElem {
} }
for _, line := range lines { for _, line := range lines {
leakPrs = checkEntropy(line) leakPrs = checkShannonEntropy(line, opts.EntropyCutoff)
if leakPrs { if leakPrs {
if opts.Strict && checkStopWords(line) {
continue
}
gitLeakReceiverWG.Add(1) gitLeakReceiverWG.Add(1)
gitLeaks <- LeakElem{line, currCommit} gitLeaks <- LeakElem{line, currCommit}
} }
} }
}(currCommit, repoName, &commitWG, &gitLeakReceiverWG) }(currCommit, repoName, &commitWG, &gitLeakReceiverWG, opts)
} }
commitWG.Wait() commitWG.Wait()
+10 -4
View File
@@ -18,10 +18,12 @@ Options:
// Options for gitleaks // Options for gitleaks
type Options struct { type Options struct {
Concurrency int Concurrency int
UserURL string EntropyCutoff int
OrgURL string UserURL string
RepoURL string OrgURL string
RepoURL string
Strict bool
} }
// help prints the usage string and exits // help prints the usage string and exits
@@ -71,6 +73,10 @@ func parseOptions(args []string) *Options {
for i := 0; i < len(args); i++ { for i := 0; i < len(args); i++ {
arg := args[i] arg := args[i]
switch arg { switch arg {
case "-s":
opts.Strict = true
case "-e":
opts.EntropyCutoff = optionsNextInt(args, &i)
case "-c": case "-c":
opts.Concurrency = optionsNextInt(args, &i) opts.Concurrency = optionsNextInt(args, &i)
case "-o": case "-o":