Compare commits

...
7 Commits
Author SHA1 Message Date
Nejc Habjan afdccad46f Add missing GitLab token patterns (#1077) 2023-01-21 20:03:05 -06:00
very-doge-wowandFabian F Groß e002920355 Fix rule for private keys (#1072)
* refactor: fix rule for private keys

Wouldn't match old keys which have been created with the BLOCK statement at the beginning and end of the key.

* fix: rule for private keys

* fix: missing comma

Co-authored-by: Fabian F Groß <fabian.f.gross@deutschebahn.com>
2023-01-12 08:38:48 -06:00
Zachary Rice d805fb9144 remove color formatting when #1042 is encountered (#1050) 2022-12-01 19:11:44 -06:00
Zachary Rice 391d4d75f6 Update README.md 2022-11-28 09:16:32 -06:00
icecream-monsterandEsther f77493274a adding jwt tokens with padding format "=" (#1031)
* adding jwt tokens with padding format =

* forgot to run go run main.go

* manually putting database.refractorlog back

Co-authored-by: Esther <esther.ay@snaptravel.com>
2022-11-22 15:55:03 -06:00
Zachary Rice 7f229faf12 include default newline pairs when calculating location (#1038)
* include default newline pairs when calculating location
2022-11-20 12:34:38 -06:00
Becojo d0733f96f3 Add rule for fine-grained GitHub PAT (#1026) 2022-11-09 17:02:35 -06:00
10 changed files with 131 additions and 25 deletions

No files matched your search

+1 -1
View File
@@ -370,7 +370,7 @@ stopwords = [
] ]
``` ```
Refer to the default [gitleaks config](https://github.com/zricethezav/gitleaks/blob/master/config/gitleaks.toml) for examples or follow the [contributing guidelines](https://github.com/zricethezav/gitleaks/blob/master/README.md). Refer to the default [gitleaks config](https://github.com/zricethezav/gitleaks/blob/master/config/gitleaks.toml) for examples or follow the [contributing guidelines](https://github.com/zricethezav/gitleaks/blob/master/README.md) if you would like to contribute to the default configuration. Additionally, you can check out [this gitleaks blog post](https://blog.gitleaks.io/stop-leaking-secrets-configuration-2-3-aeed293b1fbf) which covers advanced configuration setups.
### Additional Configuration ### Additional Configuration
+4 -1
View File
@@ -73,10 +73,13 @@ func main() {
// configRules = append(configRules, rules.GCPServiceAccount()) // configRules = append(configRules, rules.GCPServiceAccount())
configRules = append(configRules, rules.GCPAPIKey()) configRules = append(configRules, rules.GCPAPIKey())
configRules = append(configRules, rules.GitHubPat()) configRules = append(configRules, rules.GitHubPat())
configRules = append(configRules, rules.GitHubFineGrainedPat())
configRules = append(configRules, rules.GitHubOauth()) configRules = append(configRules, rules.GitHubOauth())
configRules = append(configRules, rules.GitHubApp()) configRules = append(configRules, rules.GitHubApp())
configRules = append(configRules, rules.GitHubRefresh()) configRules = append(configRules, rules.GitHubRefresh())
configRules = append(configRules, rules.Gitlab()) configRules = append(configRules, rules.GitlabPat())
configRules = append(configRules, rules.GitlabPipelineTriggerToken())
configRules = append(configRules, rules.GitlabRunnerRegistrationToken())
configRules = append(configRules, rules.GitterAccessToken()) configRules = append(configRules, rules.GitterAccessToken())
configRules = append(configRules, rules.GrafanaApiKey()) configRules = append(configRules, rules.GrafanaApiKey())
configRules = append(configRules, rules.GrafanaCloudApiToken()) configRules = append(configRules, rules.GrafanaCloudApiToken())
+16
View File
@@ -23,6 +23,22 @@ func GitHubPat() *config.Rule {
return validate(r, tps, nil) return validate(r, tps, nil)
} }
func GitHubFineGrainedPat() *config.Rule {
// define rule
r := config.Rule{
Description: "GitHub Fine-Grained Personal Access Token",
RuleID: "github-fine-grained-pat",
Regex: regexp.MustCompile(`github_pat_[0-9a-zA-Z_]{82}`),
Keywords: []string{"github_pat_"},
}
// validate
tps := []string{
generateSampleSecret("github", "github_pat_"+secrets.NewSecret(alphaNumeric("82"))),
}
return validate(r, tps, nil)
}
func GitHubOauth() *config.Rule { func GitHubOauth() *config.Rule {
// define rule // define rule
r := config.Rule{ r := config.Rule{
+33 -1
View File
@@ -7,7 +7,7 @@ import (
"github.com/zricethezav/gitleaks/v8/config" "github.com/zricethezav/gitleaks/v8/config"
) )
func Gitlab() *config.Rule { func GitlabPat() *config.Rule {
// define rule // define rule
r := config.Rule{ r := config.Rule{
Description: "GitLab Personal Access Token", Description: "GitLab Personal Access Token",
@@ -22,3 +22,35 @@ func Gitlab() *config.Rule {
} }
return validate(r, tps, nil) return validate(r, tps, nil)
} }
func GitlabPipelineTriggerToken() *config.Rule {
// define rule
r := config.Rule{
Description: "GitLab Pipeline Trigger Token",
RuleID: "gitlab-ptt",
Regex: regexp.MustCompile(`glptt-[0-9a-f]{40}`),
Keywords: []string{"glptt-"},
}
// validate
tps := []string{
generateSampleSecret("gitlab", "glptt-"+secrets.NewSecret(hex("40"))),
}
return validate(r, tps, nil)
}
func GitlabRunnerRegistrationToken() *config.Rule {
// define rule
r := config.Rule{
Description: "GitLab Runner Registration Token",
RuleID: "gitlab-rrt",
Regex: regexp.MustCompile(`GR1348941[0-9a-zA-Z\-\_]{20}`),
Keywords: []string{"GR1348941"},
}
// validate
tps := []string{
generateSampleSecret("gitlab", "GR1348941"+secrets.NewSecret(alphaNumeric("20"))),
}
return validate(r, tps, nil)
}
+2 -2
View File
@@ -9,12 +9,12 @@ func JWT() *config.Rule {
r := config.Rule{ r := config.Rule{
Description: "JSON Web Token", Description: "JSON Web Token",
RuleID: "jwt", RuleID: "jwt",
Regex: generateUniqueTokenRegex(`ey[0-9a-z]{30,34}\.ey[0-9a-z-\/_]{30,500}\.[0-9a-zA-Z-\/_]{10,200}`), Regex: generateUniqueTokenRegex(`ey[0-9a-z]{30,34}\.ey[0-9a-z-\/_]{30,500}\.[0-9a-zA-Z-\/_]{10,200}={0,2}`),
Keywords: []string{"ey"}, Keywords: []string{"ey"},
} }
// validate // validate
tps := []string{`eyJhbGciOieeeiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwic3ViZSI6IjEyMzQ1Njc4OTAiLCJuYW1lZWEiOiJKb2huIERvZSIsInN1ZmV3YWZiIjoiMTIzNDU2Nzg5MCIsIm5hbWVmZWF3ZnciOiJKb2huIERvZSIsIm5hbWVhZmV3ZmEiOiJKb2huIERvZSIsInN1ZndhZndlYWIiOiIxMjM0NTY3ODkwIiwibmFtZWZ3YWYiOiJKb2huIERvZSIsInN1YmZ3YWYiOiI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VCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwic3ViZSI6IjEyMzQ1Njc4OTAiLCJuYW1lZWEiOiJKb2huIERvZSIsInN1ZmV3YWZiIjoiMTIzNDU2Nzg5MCIsIm5hbWVmZWF3ZnciOiJKb2huIERvZSIsIm5hbWVhZmV3ZmEiOiJKb2huIERvZSIsInN1ZndhZndlYWIiOiIxMjM0NTY3ODkwIiwibmFtZWZ3YWYiOiJKb2huIERvZSIsInN1YmZ3YWYiOiIxMjM0NTY3ODkwIiwibmFtZndhZSI6IkpvaG4gRG9lIiwiaWZ3YWZhYXQiOjE1MTYyMzkwMjJ9.a_5icKBDo-8EjUlrfvz2k2k-FYaindQ0DEYNrlsnRG0`, // gitleaks:allow
} }
return validate(r, tps, nil) return validate(r, tps, nil)
+4 -1
View File
@@ -11,7 +11,7 @@ func PrivateKey() *config.Rule {
r := config.Rule{ r := config.Rule{
Description: "Private Key", Description: "Private Key",
RuleID: "private-key", RuleID: "private-key",
Regex: regexp.MustCompile(`(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY----`), Regex: regexp.MustCompile(`(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY( BLOCK)?----`),
Keywords: []string{"-----BEGIN"}, Keywords: []string{"-----BEGIN"},
} }
@@ -23,6 +23,9 @@ anything
abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz
-----END RSA PRIVATE KEY----- -----END RSA PRIVATE KEY-----
`, `,
`-----BEGIN PRIVATE KEY BLOCK-----
anything
-----END PRIVATE KEY BLOCK-----`,
} // gitleaks:allow } // gitleaks:allow
return validate(r, tps, nil) return validate(r, tps, nil)
} }
+26 -2
View File
@@ -1976,6 +1976,14 @@ keywords = [
"ghu_","ghs_", "ghu_","ghs_",
] ]
[[rules]]
description = "GitHub Fine-Grained Personal Access Token"
id = "github-fine-grained-pat"
regex = '''github_pat_[0-9a-zA-Z_]{82}'''
keywords = [
"github_pat_",
]
[[rules]] [[rules]]
description = "GitHub OAuth Access Token" description = "GitHub OAuth Access Token"
id = "github-oauth" id = "github-oauth"
@@ -2008,6 +2016,22 @@ keywords = [
"glpat-", "glpat-",
] ]
[[rules]]
description = "GitLab Pipeline Trigger Token"
id = "gitlab-ptt"
regex = '''glptt-[0-9a-f]{40}'''
keywords = [
"glptt-",
]
[[rules]]
description = "GitLab Runner Registration Token"
id = "gitlab-rrt"
regex = '''GR1348941[0-9a-zA-Z\-\_]{20}'''
keywords = [
"gr1348941",
]
[[rules]] [[rules]]
description = "Gitter Access Token" description = "Gitter Access Token"
id = "gitter-access-token" id = "gitter-access-token"
@@ -2091,7 +2115,7 @@ keywords = [
[[rules]] [[rules]]
description = "JSON Web Token" description = "JSON Web Token"
id = "jwt" id = "jwt"
regex = '''(?i)\b(ey[0-9a-z]{30,34}\.ey[0-9a-z-\/_]{30,500}\.[0-9a-zA-Z-\/_]{10,200})(?:['|\"|\n|\r|\s|\x60|;]|$)''' regex = '''(?i)\b(ey[0-9a-z]{30,34}\.ey[0-9a-z-\/_]{30,500}\.[0-9a-zA-Z-\/_]{10,200}={0,2})(?:['|\"|\n|\r|\s|\x60|;]|$)'''
keywords = [ keywords = [
"ey", "ey",
] ]
@@ -2403,7 +2427,7 @@ keywords = [
[[rules]] [[rules]]
description = "Private Key" description = "Private Key"
id = "private-key" id = "private-key"
regex = '''(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY----''' regex = '''(?i)-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY( BLOCK)?-----[\s\S-]*KEY( BLOCK)?----'''
keywords = [ keywords = [
"-----begin", "-----begin",
] ]
+21 -16
View File
@@ -18,8 +18,13 @@ const repoBasePath = "../testdata/repos/"
func TestDetect(t *testing.T) { func TestDetect(t *testing.T) {
tests := []struct { tests := []struct {
cfgName string cfgName string
fragment Fragment fragment Fragment
// NOTE: for expected findings, all line numbers will be 0
// because line deltas are added _after_ the finding is created.
// I.e, if the finding is from a --no-git file, the line number will be
// increase by 1 in DetectFromFiles(). If the finding is from git,
// the line number will be increased by the patch delta.
expectedFindings []report.Finding expectedFindings []report.Finding
wantError error wantError error
}{ }{
@@ -85,8 +90,8 @@ func TestDetect(t *testing.T) {
File: "tmp.go", File: "tmp.go",
RuleID: "pypi-upload-token", RuleID: "pypi-upload-token",
Tags: []string{"key", "pypi"}, Tags: []string{"key", "pypi"},
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 1, StartColumn: 1,
EndColumn: 86, EndColumn: 86,
Entropy: 1.9606875, Entropy: 1.9606875,
@@ -108,8 +113,8 @@ func TestDetect(t *testing.T) {
File: "tmp.go", File: "tmp.go",
RuleID: "aws-access-key", RuleID: "aws-access-key",
Tags: []string{"key", "AWS"}, Tags: []string{"key", "AWS"},
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 15, StartColumn: 15,
EndColumn: 34, EndColumn: 34,
Entropy: 3.0841837, Entropy: 3.0841837,
@@ -132,8 +137,8 @@ func TestDetect(t *testing.T) {
RuleID: "sidekiq-secret", RuleID: "sidekiq-secret",
Tags: []string{}, Tags: []string{},
Entropy: 2.6098502, Entropy: 2.6098502,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 8, StartColumn: 8,
EndColumn: 60, EndColumn: 60,
}, },
@@ -155,8 +160,8 @@ func TestDetect(t *testing.T) {
RuleID: "sidekiq-secret", RuleID: "sidekiq-secret",
Tags: []string{}, Tags: []string{},
Entropy: 2.6098502, Entropy: 2.6098502,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 21, StartColumn: 21,
EndColumn: 74, EndColumn: 74,
}, },
@@ -178,8 +183,8 @@ func TestDetect(t *testing.T) {
RuleID: "sidekiq-sensitive-url", RuleID: "sidekiq-sensitive-url",
Tags: []string{}, Tags: []string{},
Entropy: 2.984234, Entropy: 2.984234,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 8, StartColumn: 8,
EndColumn: 58, EndColumn: 58,
}, },
@@ -226,8 +231,8 @@ func TestDetect(t *testing.T) {
RuleID: "discord-api-key", RuleID: "discord-api-key",
Tags: []string{}, Tags: []string{},
Entropy: 3.7906237, Entropy: 3.7906237,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 7, StartColumn: 7,
EndColumn: 93, EndColumn: 93,
}, },
@@ -257,8 +262,8 @@ func TestDetect(t *testing.T) {
RuleID: "generic-api-key", RuleID: "generic-api-key",
Tags: []string{}, Tags: []string{},
Entropy: 3.7906237, Entropy: 3.7906237,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 22, StartColumn: 22,
EndColumn: 93, EndColumn: 93,
}, },
+10
View File
@@ -24,6 +24,16 @@ func location(fragment Fragment, matchIndex []int) Location {
// default startLineIndex to 0 // default startLineIndex to 0
location.startLineIndex = 0 location.startLineIndex = 0
// Fixes: https://github.com/zricethezav/gitleaks/issues/1037
// When a fragment does NOT have any newlines, a default "newline"
// will be counted to make the subsequent location calculation logic work
// for fragments will no newlines.
if len(fragment.newlineIndices) == 0 {
fragment.newlineIndices = [][]int{
{len(fragment.Raw), len(fragment.Raw) + 1},
}
}
for lineNum, pair := range fragment.newlineIndices { for lineNum, pair := range fragment.newlineIndices {
_lineNum = lineNum _lineNum = lineNum
newLineByteIndex := pair[0] newLineByteIndex := pair[0]
+14 -1
View File
@@ -102,6 +102,13 @@ func printFinding(f report.Finding) {
matchInLineIDX := strings.Index(f.Line, f.Match) matchInLineIDX := strings.Index(f.Line, f.Match)
secretInMatchIdx := strings.Index(f.Match, f.Secret) secretInMatchIdx := strings.Index(f.Match, f.Secret)
skipColor := false
if matchInLineIDX == -1 {
skipColor = true
matchInLineIDX = 0
}
start := f.Line[0:matchInLineIDX] start := f.Line[0:matchInLineIDX]
startMatchIdx := 0 startMatchIdx := 0
if matchInLineIDX > 20 { if matchInLineIDX > 20 {
@@ -127,7 +134,13 @@ func printFinding(f report.Finding) {
} }
finding := fmt.Sprintf("%s%s%s%s%s\n", strings.TrimPrefix(strings.TrimLeft(start, " "), "\n"), matchBeginning, secret, matchEnd, lineEnd) finding := fmt.Sprintf("%s%s%s%s%s\n", strings.TrimPrefix(strings.TrimLeft(start, " "), "\n"), matchBeginning, secret, matchEnd, lineEnd)
fmt.Printf("%-12s %s", "Finding:", finding)
if skipColor {
fmt.Printf("%-12s %s\n", "Finding:", f.Match)
} else {
fmt.Printf("%-12s %s", "Finding:", finding)
}
fmt.Printf("%-12s %s\n", "Secret:", secret) fmt.Printf("%-12s %s\n", "Secret:", secret)
fmt.Printf("%-12s %s\n", "RuleID:", f.RuleID) fmt.Printf("%-12s %s\n", "RuleID:", f.RuleID)
fmt.Printf("%-12s %f\n", "Entropy:", f.Entropy) fmt.Printf("%-12s %f\n", "Entropy:", f.Entropy)