mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 13:34:23 +01:00
sprucing up the joint
This commit is contained in:
1 parent
6f5079c193
commit
1e250f1a14
6 files changed
+54
-55
No files matched your search
@@ -5,30 +5,27 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// check each line of a diff and see if there are any potential secrets
|
func doChecks(diff string) []string {
|
||||||
// [1] https://people.eecs.berkeley.edu/~rohanpadhye/files/key_leaks-msr15.pdf
|
|
||||||
func checkRegex(diff string) []string {
|
|
||||||
var match string
|
var match string
|
||||||
var results []string
|
var results []string
|
||||||
lines := strings.Split(diff, "\n")
|
lines := strings.Split(diff, "\n")
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
// doubtful a leak would be on a line > 120 characters
|
|
||||||
if len(line) == 0 || len(line) > 120 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for _, re := range regexes {
|
for _, re := range regexes {
|
||||||
match = re.FindString(line)
|
match = re.FindString(line)
|
||||||
if len(match) == 0 {
|
if len(match) == 0 ||
|
||||||
|
(opts.Strict && containsStopWords(line)) ||
|
||||||
|
(opts.Entropy && !checkShannonEntropy(line)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
results = append(results, line)
|
results = append(results, line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results
|
return results
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkShannonEntropy checks entropy of target
|
// checkShannonEntropy checks entropy of target
|
||||||
func checkShannonEntropy(target string, entropy64Cutoff int, entropyHexCutoff int) bool {
|
func checkShannonEntropy(target string) bool {
|
||||||
var (
|
var (
|
||||||
sum float64
|
sum float64
|
||||||
targetBase64Len int
|
targetBase64Len int
|
||||||
@@ -38,7 +35,6 @@ func checkShannonEntropy(target string, entropy64Cutoff int, entropyHexCutoff in
|
|||||||
bits int
|
bits int
|
||||||
)
|
)
|
||||||
|
|
||||||
// get assignment value
|
|
||||||
index := assignRegex.FindStringIndex(target)
|
index := assignRegex.FindStringIndex(target)
|
||||||
if len(index) == 0 {
|
if len(index) == 0 {
|
||||||
return false
|
return false
|
||||||
@@ -61,7 +57,7 @@ func checkShannonEntropy(target string, entropy64Cutoff int, entropyHexCutoff in
|
|||||||
}
|
}
|
||||||
|
|
||||||
bits = int(math.Ceil(sum*-1)) * targetBase64Len
|
bits = int(math.Ceil(sum*-1)) * targetBase64Len
|
||||||
if bits > entropy64Cutoff {
|
if bits > opts.B64EntropyCutoff {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +74,7 @@ func checkShannonEntropy(target string, entropy64Cutoff int, entropyHexCutoff in
|
|||||||
sum += f * math.Log2(f)
|
sum += f * math.Log2(f)
|
||||||
}
|
}
|
||||||
bits = int(math.Ceil(sum*-1)) * targetHexLen
|
bits = int(math.Ceil(sum*-1)) * targetHexLen
|
||||||
return bits > entropyHexCutoff
|
return bits > opts.HexEntropyCutoff
|
||||||
}
|
}
|
||||||
|
|
||||||
// containsStopWords checks if there are any stop words in target
|
// containsStopWords checks if there are any stop words in target
|
||||||
|
|||||||
+13
-12
@@ -4,23 +4,24 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init(){
|
||||||
|
opts = &Options{
|
||||||
|
Concurrency: 10,
|
||||||
|
B64EntropyCutoff: 70,
|
||||||
|
HexEntropyCutoff: 40,
|
||||||
|
Entropy: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCheckRegex(t *testing.T) {
|
func TestCheckRegex(t *testing.T) {
|
||||||
var results []string
|
var results []string
|
||||||
checks := map[string]int{
|
checks := map[string]int{
|
||||||
"github.com": 0,
|
"aws=\"AKIALALEMEL33243OLIAE": 1,
|
||||||
"github.com/user/": 0,
|
"aws\"afewafewafewafewaf\"": 0,
|
||||||
"github.com/user -- Sys": 0,
|
|
||||||
"github_api_client = \"sample key\"\naws=afewafewafewafewaf": 2,
|
|
||||||
"aws=\"afewafewafewafewaf\"": 1,
|
|
||||||
"aws\"afewafewafewafewaf\"": 0,
|
|
||||||
"heroku := \"afewafewafewafewaf\"": 1,
|
|
||||||
"heroku_client_secret := \"afewafewafewafewaf\"": 1,
|
|
||||||
"reddit_api_secreit = \"Fwe4fa431FgklreF\"": 1,
|
|
||||||
"+ * [Github Help: Managing Deploy Keys](https://help.github.com/articles/managing-deploy-keys)": 0,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range checks {
|
for k, v := range checks {
|
||||||
results = checkRegex(k)
|
results = doChecks(k)
|
||||||
if v != len(results) {
|
if v != len(results) {
|
||||||
t.Errorf("regexCheck failed on string %s", k)
|
t.Errorf("regexCheck failed on string %s", k)
|
||||||
}
|
}
|
||||||
@@ -36,7 +37,7 @@ func TestEntropy(t *testing.T) {
|
|||||||
"aws_secret= \"AKIAIMNOJVGFDXXFE4OA\"": true,
|
"aws_secret= \"AKIAIMNOJVGFDXXFE4OA\"": true,
|
||||||
}
|
}
|
||||||
for k, v := range checks {
|
for k, v := range checks {
|
||||||
enoughEntropy = checkShannonEntropy(k, 70, 40)
|
enoughEntropy = checkShannonEntropy(k)
|
||||||
if v != enoughEntropy {
|
if v != enoughEntropy {
|
||||||
t.Errorf("checkEntropy failed for %s. Expected %t, got %t", k, v, enoughEntropy)
|
t.Errorf("checkEntropy failed for %s. Expected %t, got %t", k, v, enoughEntropy)
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-7
@@ -1,11 +1,16 @@
|
|||||||
regexes:
|
regexes:
|
||||||
- '[g|G][i|I][t|T][h|H][u|U][b|B].*(=|:=|<-).*\w+.*'
|
- "-----BEGIN RSA PRIVATE KEY-----"
|
||||||
- '[a|A][w|W][s|S].*(=|:=|:|<-).*\w+.*'
|
- "-----BEGIN OPENSSH PRIVATE KEY-----"
|
||||||
- '[h|H][e|E][r|R][o|O][k|K][u|U].*(=|:=|<-).*\w+.*'
|
- "-----BEGIN PGP PRIVATE KEY BLOCK-----"
|
||||||
- '[f|F][a|A][c|C][e|E][b|B][o|O][o|O][k|K].*(=|:=|<-).*\w+.*'
|
- "[f|F][a|A][c|C][e|E][b|B][o|O][o|O][k|K].*['|\"][0-9a-f]{32}['|\"]"
|
||||||
- '[t|T][w|W][i|I][t|T][t|T][e|E][r|R].*(=|:=|<-).*\w+.*'
|
- "[t|T][w|W][i|I][t|T][t|T][e|E][r|R].*['|\"][0-9a-zA-Z]{35,44}['|\"]"
|
||||||
- '[r|R][e|E][d|D][d|D][i|I][t|T].*(=|:=|<-).*\w+.*'
|
- "[g|G][i|I][t|T][h|H][u|U][b|B].*[['|\"]0-9a-zA-Z]{35,40}['|\"]"
|
||||||
- '[t|T][w|W][i|I][l|L][i|I][o|O].*(=|:=|<-).*\w+.*'
|
- "AKIA[0-9A-Z]{16}"
|
||||||
|
- "[r|R][e|E][d|D][d|D][i|I][t|T].*['|\"][0-9a-zA-Z]{14}['|\"]"
|
||||||
|
- "[h|H][e|E][r|R][o|O][k|K][u|U].*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"
|
||||||
|
# add your own regex
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
stopwords:
|
stopwords:
|
||||||
- 'setting'
|
- 'setting'
|
||||||
@@ -20,4 +25,5 @@ stopwords:
|
|||||||
- 'environment'
|
- 'environment'
|
||||||
- 'Environment'
|
- 'Environment'
|
||||||
- 'ENVIRONMENT'
|
- 'ENVIRONMENT'
|
||||||
|
# add your own stopwords
|
||||||
|
|
||||||
@@ -107,10 +107,9 @@ func getLeaks(repoName string, opts *Options) []LeakElem {
|
|||||||
currCommit := string(currCommitB)
|
currCommit := string(currCommitB)
|
||||||
|
|
||||||
go func(currCommit string, repoName string, commitWG *sync.WaitGroup,
|
go func(currCommit string, repoName string, commitWG *sync.WaitGroup,
|
||||||
gitLeakReceiverWG *sync.WaitGroup, opts *Options) {
|
gitLeakReceiverWG *sync.WaitGroup) {
|
||||||
|
|
||||||
defer commitWG.Done()
|
defer commitWG.Done()
|
||||||
var leakPrs bool
|
|
||||||
|
|
||||||
if currCommit == "" {
|
if currCommit == "" {
|
||||||
return
|
return
|
||||||
@@ -131,22 +130,16 @@ func getLeaks(repoName string, opts *Options) []LeakElem {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := checkRegex(string(out))
|
lines := doChecks(string(out))
|
||||||
if len(lines) == 0 {
|
if len(lines) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
leakPrs = checkShannonEntropy(line, opts.B64EntropyCutoff, opts.HexEntropyCutoff)
|
gitLeakReceiverWG.Add(1)
|
||||||
if leakPrs {
|
gitLeaks <- LeakElem{line, currCommit}
|
||||||
if opts.Strict && containsStopWords(line) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
gitLeakReceiverWG.Add(1)
|
|
||||||
gitLeaks <- LeakElem{line, currCommit}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}(currCommit, repoName, &commitWG, &gitLeakReceiverWG, opts)
|
|
||||||
|
}(currCommit, repoName, &commitWG, &gitLeakReceiverWG)
|
||||||
}
|
}
|
||||||
|
|
||||||
commitWG.Wait()
|
commitWG.Wait()
|
||||||
|
|||||||
@@ -16,9 +16,10 @@ var (
|
|||||||
appRoot string
|
appRoot string
|
||||||
regexes []*regexp.Regexp
|
regexes []*regexp.Regexp
|
||||||
stopWords []string
|
stopWords []string
|
||||||
assignRegex *regexp.Regexp
|
|
||||||
base64Chars string
|
base64Chars string
|
||||||
hexChars string
|
hexChars string
|
||||||
|
opts *Options
|
||||||
|
assignRegex *regexp.Regexp
|
||||||
)
|
)
|
||||||
|
|
||||||
// config
|
// config
|
||||||
@@ -55,7 +56,6 @@ func init() {
|
|||||||
log.Fatalf("Unmarshal: %v", err)
|
log.Fatalf("Unmarshal: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// regex from config
|
|
||||||
stopWords = c.StopWords
|
stopWords = c.StopWords
|
||||||
for _, re := range c.Regexes {
|
for _, re := range c.Regexes {
|
||||||
regexes = append(regexes, regexp.MustCompile(re))
|
regexes = append(regexes, regexp.MustCompile(re))
|
||||||
@@ -65,7 +65,7 @@ func init() {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
opts := parseOptions(args)
|
opts = parseOptions(args)
|
||||||
if opts.RepoURL != "" {
|
if opts.RepoURL != "" {
|
||||||
start(opts)
|
start(opts)
|
||||||
} else if opts.UserURL != "" || opts.OrgURL != "" {
|
} else if opts.UserURL != "" || opts.OrgURL != "" {
|
||||||
|
|||||||
+11
-8
@@ -9,13 +9,14 @@ import (
|
|||||||
const usage = `usage: gitleaks [options] <url>
|
const usage = `usage: gitleaks [options] <url>
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-c Concurrency factor
|
-c Concurrency factor (default is 10)
|
||||||
-u --user Git user url
|
-u --user Git user url
|
||||||
-r --repo Git repo url
|
-r --repo Git repo url
|
||||||
-o --org Git organization url
|
-o --org Git organization url
|
||||||
-s --strict Strict mode uses stopwords in config.yml
|
-s --strict Strict mode uses stopwords in config.yml
|
||||||
-e --b64Entropy Base64 entropy cutoff, default is 70
|
-b --b64Entropy Base64 entropy cutoff (default is 70)
|
||||||
-x --hexEntropy Hex entropy cutoff, default is 40
|
-x --hexEntropy Hex entropy cutoff (default is 40)
|
||||||
|
-e --entropy Enable entropy
|
||||||
-h --help Display this message
|
-h --help Display this message
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ type Options struct {
|
|||||||
OrgURL string
|
OrgURL string
|
||||||
RepoURL string
|
RepoURL string
|
||||||
Strict bool
|
Strict bool
|
||||||
|
Entropy bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// help prints the usage string and exits
|
// help prints the usage string and exits
|
||||||
@@ -36,8 +38,7 @@ func help() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// optionsNextInt is a parseOptions helper that returns the value (int) of an option
|
// optionsNextInt is a parseOptions helper that returns the value (int) of an option if valid
|
||||||
// if valid.
|
|
||||||
func optionsNextInt(args []string, i *int) int {
|
func optionsNextInt(args []string, i *int) int {
|
||||||
if len(args) > *i+1 {
|
if len(args) > *i+1 {
|
||||||
*i++
|
*i++
|
||||||
@@ -52,8 +53,7 @@ func optionsNextInt(args []string, i *int) int {
|
|||||||
return argInt
|
return argInt
|
||||||
}
|
}
|
||||||
|
|
||||||
// optionsNextString is a parseOptions helper that returns the value (string) of an option
|
// optionsNextString is a parseOptions helper that returns the value (string) of an option if valid
|
||||||
// if valid.
|
|
||||||
func optionsNextString(args []string, i *int) string {
|
func optionsNextString(args []string, i *int) string {
|
||||||
if len(args) > *i+1 {
|
if len(args) > *i+1 {
|
||||||
*i++
|
*i++
|
||||||
@@ -70,6 +70,7 @@ func parseOptions(args []string) *Options {
|
|||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
B64EntropyCutoff: 70,
|
B64EntropyCutoff: 70,
|
||||||
HexEntropyCutoff: 40,
|
HexEntropyCutoff: 40,
|
||||||
|
Entropy: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(args); i++ {
|
for i := 0; i < len(args); i++ {
|
||||||
@@ -77,10 +78,12 @@ func parseOptions(args []string) *Options {
|
|||||||
switch arg {
|
switch arg {
|
||||||
case "-s", "--strict":
|
case "-s", "--strict":
|
||||||
opts.Strict = true
|
opts.Strict = true
|
||||||
case "-e", "--b64Entropy":
|
case "-b", "--b64Entropy":
|
||||||
opts.B64EntropyCutoff = optionsNextInt(args, &i)
|
opts.B64EntropyCutoff = optionsNextInt(args, &i)
|
||||||
case "-x", "--hexEntropy":
|
case "-x", "--hexEntropy":
|
||||||
opts.HexEntropyCutoff = optionsNextInt(args, &i)
|
opts.HexEntropyCutoff = optionsNextInt(args, &i)
|
||||||
|
case "-e", "--entropy":
|
||||||
|
opts.Entropy = true
|
||||||
case "-c":
|
case "-c":
|
||||||
opts.Concurrency = optionsNextInt(args, &i)
|
opts.Concurrency = optionsNextInt(args, &i)
|
||||||
case "-o", "--org":
|
case "-o", "--org":
|
||||||
|
|||||||
Reference in new issue
Block a user