diff --git a/pkg/detectors/circleci/circleci.go b/pkg/detectors/circleci/circleci.go index 8c8d77da..6048007b 100644 --- a/pkg/detectors/circleci/circleci.go +++ b/pkg/detectors/circleci/circleci.go @@ -5,10 +5,9 @@ import ( "net/http" "regexp" + "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" - - "github.com/trufflesecurity/trufflehog/v3/pkg/common" ) type Scanner struct{} @@ -21,54 +20,52 @@ var ( ) // Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"circle"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("circle")} } // FromData will find and optionally verify Circle secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { - + if len(match) < 2 { + continue + } token := match[1] - s := detectors.Result{ + s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Circle, - Raw: []byte(token), + Raw: token, } if verify { client := common.SaneHttpClient() - // https://circleci.com/docs/api/#authentication req, err := http.NewRequestWithContext(ctx, "GET", "https://circleci.com/api/v2/me", nil) if err != nil { continue } req.Header.Add("Accept", "application/json;") - req.Header.Add("Circle-Token", token) + req.Header.Add("Circle-Token", string(token)) res, err := client.Do(req) - if err == nil { + if err == nil && res != nil { defer res.Body.Close() - } - if res != nil && res.StatusCode >= 200 && res.StatusCode < 300 { - s.Verified = true + if res.StatusCode >= 200 && res.StatusCode < 300 { + s1.Verified = true + } } } - if !s.Verified { - if detectors.IsKnownFalsePositive(string(s.Raw), detectors.DefaultFalsePositives, true) { + if !s1.Verified { + if detectors.IsKnownFalsePositive(token, detectors.DefaultFalsePositives, true) { continue } } - results = append(results, s) + results = append(results, s1) } - return + return results, nil } func (s Scanner) Type() detectorspb.DetectorType { diff --git a/pkg/detectors/clarifai/clarifai.go b/pkg/detectors/clarifai/clarifai.go index 14ba3545..e7556131 100644 --- a/pkg/detectors/clarifai/clarifai.go +++ b/pkg/detectors/clarifai/clarifai.go @@ -1,11 +1,11 @@ package clarifai import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -20,39 +20,34 @@ var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clarifai"}) + `\b([a-zA-Z0-9]{32})\b`) // could be an api key tied to an app or a personal access token (pat) + keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clarifai"}) + `\b([a-zA-Z0-9]{32})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"clarifai"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("clarifai")} } -// FromData will find and optionally verify Clarifai secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Clarifai, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - // test for api key req, err := http.NewRequestWithContext(ctx, "GET", "https://api.clarifai.com/v2/inputs", nil) if err != nil { continue } - req.Header.Add("Authorization", fmt.Sprintf("Key %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Key %s", string(resMatch))) res, err := client.Do(req) if err == nil { defer res.Body.Close() @@ -60,14 +55,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result s1.Verified = true } } - if !s1.Verified { // test for pat req, err := http.NewRequestWithContext(ctx, "GET", "https://api.clarifai.com/v2/users/me", nil) if err != nil { continue } - req.Header.Add("Authorization", fmt.Sprintf("Key %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Key %s", string(resMatch))) res, err := client.Do(req) if err == nil { defer res.Body.Close() diff --git a/pkg/detectors/clearbit/clearbit.go b/pkg/detectors/clearbit/clearbit.go index 6fcb7e8d..cf5ea9d3 100644 --- a/pkg/detectors/clearbit/clearbit.go +++ b/pkg/detectors/clearbit/clearbit.go @@ -1,11 +1,11 @@ package clearbit import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,37 +14,30 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clearbit"}) + `\b([0-9a-z_]{35})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"clearbit"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("clearbit")} } -// FromData will find and optionally verify Clearbit secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Clearbit, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,14 +45,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", string(resMatch))) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/clickhelp/clickhelp.go b/pkg/detectors/clickhelp/clickhelp.go index 97f35617..e8cd0bde 100644 --- a/pkg/detectors/clickhelp/clickhelp.go +++ b/pkg/detectors/clickhelp/clickhelp.go @@ -1,12 +1,12 @@ package clickhelp import ( + "bytes" "context" b64 "encoding/base64" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -15,59 +15,52 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. serverPat = regexp.MustCompile(`\b([0-9A-Za-z]{3,20}.try.clickhelp.co)\b`) emailPat = regexp.MustCompile(`\b([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-z]+)\b`) keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clickhelp"}) + `\b([0-9A-Za-z]{24})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"clickhelp"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("clickhelp")} } -// FromData will find and optionally verify Clickhelp secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - serverMatches := serverPat.FindAllStringSubmatch(dataStr, -1) - emailMatches := emailPat.FindAllStringSubmatch(dataStr, -1) - keyMatches := keyPat.FindAllStringSubmatch(dataStr, -1) + serverMatches := serverPat.FindAllSubmatch(data, -1) + emailMatches := emailPat.FindAllSubmatch(data, -1) + keyMatches := keyPat.FindAllSubmatch(data, -1) for _, match := range serverMatches { if len(match) != 2 { continue } - resServer := strings.TrimSpace(match[1]) + resServer := bytes.TrimSpace(match[1]) for _, emailMatch := range emailMatches { if len(emailMatch) != 2 { continue } - resEmail := strings.TrimSpace(emailMatch[1]) + resEmail := bytes.TrimSpace(emailMatch[1]) for _, keyMatch := range keyMatches { if len(keyMatch) != 2 { continue } - resKey := strings.TrimSpace(keyMatch[1]) + resKey := bytes.TrimSpace(keyMatch[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_ClickHelp, - Raw: []byte(resServer), - RawV2: []byte(resServer + resEmail), + Raw: resServer, + RawV2: append(resServer, resEmail...), } if verify { - data := fmt.Sprintf("%s:%s", resEmail, resKey) - sEnc := b64.StdEncoding.EncodeToString([]byte(data)) - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://%s/api/v1/projects", resServer), nil) + data := bytes.Join([][]byte{resEmail, resKey}, []byte(":")) + sEnc := b64.StdEncoding.EncodeToString(data) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://%s/api/v1/projects", string(resServer)), nil) if err != nil { continue } @@ -79,7 +72,6 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resServer, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/clicksendsms/clicksendsms.go b/pkg/detectors/clicksendsms/clicksendsms.go index 45c050f4..770dfd12 100644 --- a/pkg/detectors/clicksendsms/clicksendsms.go +++ b/pkg/detectors/clicksendsms/clicksendsms.go @@ -1,12 +1,12 @@ package clicksendsms import ( + "bytes" "context" b64 "encoding/base64" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -21,43 +21,43 @@ var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. + // Keeping the regexp patterns unchanged keyPat = regexp.MustCompile(common.UUIDPatternUpperCase) idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"sms"}) + common.EmailPattern) ) // Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"clicksendsms"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("clicksendsms")} } // FromData will find and optionally verify ClickSendsms secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) - idMatches := idPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) + idMatches := idPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) for _, idMatch := range idMatches { - resIdMatch := strings.TrimSpace(idMatch[0][strings.LastIndex(idMatch[0], " ")+1:]) + if len(idMatch) == 0 { + continue + } + resIdMatch := bytes.TrimSpace(idMatch[0][bytes.LastIndex(idMatch[0], []byte(" "))+1:]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_ClickSendsms, - Raw: []byte(resMatch), - RawV2: []byte(resMatch + resIdMatch), + Raw: resMatch, + RawV2: append(resMatch, resIdMatch...), } if verify { - - data := fmt.Sprintf("%s:%s", resIdMatch, resMatch) - sEnc := b64.StdEncoding.EncodeToString([]byte(data)) - + data := append(resIdMatch, ':') + data = append(data, resMatch...) + sEnc := b64.StdEncoding.EncodeToString(data) req, err := http.NewRequestWithContext(ctx, "GET", "https://rest.clicksend.com/v3/account", nil) if err != nil { continue @@ -69,7 +69,6 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } @@ -78,9 +77,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result } results = append(results, s1) - } - } return results, nil diff --git a/pkg/detectors/clickuppersonaltoken/clickuppersonaltoken.go b/pkg/detectors/clickuppersonaltoken/clickuppersonaltoken.go index f3ce2df5..2bbab42f 100644 --- a/pkg/detectors/clickuppersonaltoken/clickuppersonaltoken.go +++ b/pkg/detectors/clickuppersonaltoken/clickuppersonaltoken.go @@ -1,10 +1,10 @@ package clickuppersonaltoken import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -19,31 +19,28 @@ var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. + // Keeping the regexp patterns unchanged keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clickup"}) + `\b(pk_[0-9]{8}_[0-9A-Z]{32})\b`) ) // Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"clickup"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("clickup")} } // FromData will find and optionally verify ClickupPersonalToken secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_ClickupPersonalToken, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -51,14 +48,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("Authorization", resMatch) + req.Header.Add("Authorization", string(resMatch)) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/cliengo/cliengo.go b/pkg/detectors/cliengo/cliengo.go index 86ef11e8..8397b505 100644 --- a/pkg/detectors/cliengo/cliengo.go +++ b/pkg/detectors/cliengo/cliengo.go @@ -1,10 +1,10 @@ package cliengo import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,41 +13,33 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cliengo"}) + `\b([0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cliengo"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cliengo")} } -// FromData will find and optionally verify Cliengo secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) - s1 := detectors.Result{ + result := detectors.Result{ DetectorType: detectorspb.DetectorType_Cliengo, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", "https://api.cliengo.com/1.0/account?api_key="+resMatch, nil) + req, err := http.NewRequestWithContext(ctx, "GET", "https://api.cliengo.com/1.0/account?api_key="+string(resMatch), nil) if err != nil { continue } @@ -55,9 +47,8 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { - s1.Verified = true + result.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } @@ -65,7 +56,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result } } - results = append(results, s1) + results = append(results, result) } return results, nil diff --git a/pkg/detectors/clinchpad/clinchpad.go b/pkg/detectors/clinchpad/clinchpad.go index 27c4c755..9a966e30 100644 --- a/pkg/detectors/clinchpad/clinchpad.go +++ b/pkg/detectors/clinchpad/clinchpad.go @@ -1,12 +1,12 @@ package clinchpad import ( + "bytes" "context" - b64 "encoding/base64" + "encoding/base64" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -21,36 +21,30 @@ var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. - keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clinchpad"}) + `\b([a-z0-9]{32})\b`) + keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clinchpad"}) + `\b([a-f0-9]{32})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"clinchpad"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("clinchpad")} } -// FromData will find and optionally verify Clinchpad secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Clinchpad, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { data := fmt.Sprintf("api-key:%s", resMatch) - sEnc := b64.StdEncoding.EncodeToString([]byte(data)) + sEnc := base64.StdEncoding.EncodeToString([]byte(data)) req, err := http.NewRequestWithContext(ctx, "GET", "https://www.clinchpad.com/api/v1/pipelines", nil) if err != nil { continue @@ -62,17 +56,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } } } } - results = append(results, s1) } - return results, nil } diff --git a/pkg/detectors/clockify/clockify.go b/pkg/detectors/clockify/clockify.go index bb7d2158..0f0647f9 100644 --- a/pkg/detectors/clockify/clockify.go +++ b/pkg/detectors/clockify/clockify.go @@ -1,10 +1,10 @@ package clockify import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,37 +13,30 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clockify"}) + `\b([a-zA-Z0-9]{48})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"clockify"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("clockify")} } -// FromData will find and optionally verify Clockify secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Clockify, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,7 +45,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } req.Header.Add("content-type", "application/json") - req.Header.Add("X-Api-Key", resMatch) + req.Header.Add("X-Api-Key", string(resMatch)) res, err := client.Do(req) if err == nil { defer res.Body.Close() diff --git a/pkg/detectors/clockworksms/clockworksms.go b/pkg/detectors/clockworksms/clockworksms.go index 643ab851..44324973 100644 --- a/pkg/detectors/clockworksms/clockworksms.go +++ b/pkg/detectors/clockworksms/clockworksms.go @@ -1,10 +1,10 @@ package clockworksms import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,46 +13,39 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. userKeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clockwork", "textanywhere"}) + `\b([0-9]{5})\b`) tokenPat = regexp.MustCompile(detectors.PrefixRegex([]string{"clockwork", "textanywhere"}) + `\b([0-9a-zA-Z]{24})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"clockworksms", "textanywhere"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("clockworksms"), []byte("textanywhere")} } -// FromData will find and optionally verify Clockworksms secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - userKeyMatches := userKeyPat.FindAllStringSubmatch(dataStr, -1) - tokenMatches := tokenPat.FindAllStringSubmatch(dataStr, -1) + userKeyMatches := userKeyPat.FindAllSubmatch(data, -1) + tokenMatches := tokenPat.FindAllSubmatch(data, -1) for _, match := range userKeyMatches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) for _, tokenMatch := range tokenMatches { if len(tokenMatch) != 2 { continue } - tokenRes := strings.TrimSpace(tokenMatch[1]) + tokenRes := bytes.TrimSpace(tokenMatch[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_ClockworkSMS, - Raw: []byte(resMatch), - RawV2: []byte(resMatch + tokenRes), + Raw: resMatch, + RawV2: append(resMatch, tokenRes...), } if verify { @@ -60,15 +53,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("user_key", resMatch) - req.Header.Add("access_token", tokenRes) + req.Header.Add("user_key", string(resMatch)) + req.Header.Add("access_token", string(tokenRes)) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/closecrm/close.go b/pkg/detectors/closecrm/close.go index e55c56d5..9dd077b9 100644 --- a/pkg/detectors/closecrm/close.go +++ b/pkg/detectors/closecrm/close.go @@ -1,12 +1,11 @@ package closecrm import ( + "bytes" "context" b64 "encoding/base64" - "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -15,47 +14,39 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. keyPat = regexp.MustCompile(`\b(api_[a-z0-9A-Z.]{45})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"close"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("close")} } -// FromData will find and optionally verify Close secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Close, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - data := fmt.Sprintf("%s:", resMatch) - sEnc := b64.StdEncoding.EncodeToString([]byte(data)) + data := append(resMatch, byte(':')) + sEnc := b64.StdEncoding.EncodeToString(data) req, err := http.NewRequestWithContext(ctx, "GET", "https://api.close.com/api/v1/me/", nil) if err != nil { continue } - req.Header.Add("Authorization", fmt.Sprintf("Basic %s", sEnc)) + req.Header.Add("Authorization", "Basic "+sEnc) req.Header.Add("Content-Type", "application/json") res, err := client.Do(req) if err == nil { @@ -63,7 +54,6 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/cloudconvert/cloudconvert.go b/pkg/detectors/cloudconvert/cloudconvert.go index f454543c..13e2e220 100644 --- a/pkg/detectors/cloudconvert/cloudconvert.go +++ b/pkg/detectors/cloudconvert/cloudconvert.go @@ -1,11 +1,11 @@ package cloudconvert import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,36 +14,35 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time +// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives + // Keeping the regexp patterns unchanged keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cloudconvert"}) + common.BuildRegexJWT("30,34", "200,500", "600,700")) ) // Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cloudconvert"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cloudconvert")} } // FromData will find and optionally verify CloudConvert secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) + for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_CloudConvert, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -59,7 +58,6 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/cloudelements/cloudelements.go b/pkg/detectors/cloudelements/cloudelements.go index 4413783f..e136866f 100644 --- a/pkg/detectors/cloudelements/cloudelements.go +++ b/pkg/detectors/cloudelements/cloudelements.go @@ -1,11 +1,11 @@ package cloudelements import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,47 +14,41 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cloudelements"}) + `\b([a-zA-Z0-9]{43})\b`) orgPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cloudelements"}) + `\b([a-z0-9]{32})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cloudelements"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cloudelements")} } -// FromData will find and optionally verify CloudElements secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) - orgMatches := orgPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) + orgMatches := orgPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) for _, orgMatch := range orgMatches { if len(orgMatch) != 2 { continue } - resOrgMatch := strings.TrimSpace(orgMatch[1]) + resOrgMatch := bytes.TrimSpace(orgMatch[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_CloudElements, - Raw: []byte(resMatch), - RawV2: []byte(resMatch + resOrgMatch), + Raw: resMatch, + RawV2: append(resMatch, resOrgMatch...), } if verify { @@ -62,14 +56,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("Authorization", fmt.Sprintf("User %s, Organization %s", resMatch, resOrgMatch)) + req.Header.Add("Authorization", fmt.Sprintf("User %s, Organization %s", string(resMatch), string(resOrgMatch))) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/cloudflareapitoken/cloudflareapitoken.go b/pkg/detectors/cloudflareapitoken/cloudflareapitoken.go index 3557ba86..38302218 100644 --- a/pkg/detectors/cloudflareapitoken/cloudflareapitoken.go +++ b/pkg/detectors/cloudflareapitoken/cloudflareapitoken.go @@ -1,11 +1,11 @@ package cloudflareapitoken import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,36 +14,29 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - - keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cloudflare"}) + `\b([A-Za-z0-9_-]{40})\b`) + keyPat = regexp.MustCompile(string(detectors.PrefixRegex([]string{"cloudflare"})) + `\b([A-Za-z0-9_-]{40})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cloudflare"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cloudflare")} } -// FromData will find and optionally verify CloudflareApiToken secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_CloudflareApiToken, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,7 +45,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } req.Header.Add("Content-Type", "application/json") - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", string(resMatch))) res, err := client.Do(req) if err == nil { defer res.Body.Close() diff --git a/pkg/detectors/cloudflarecakey/cloudflarecakey.go b/pkg/detectors/cloudflarecakey/cloudflarecakey.go index f2d7783f..63fe53e7 100644 --- a/pkg/detectors/cloudflarecakey/cloudflarecakey.go +++ b/pkg/detectors/cloudflarecakey/cloudflarecakey.go @@ -1,12 +1,10 @@ package cloudflarecakey import ( + "bytes" "context" - // "fmt" - // "log" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -15,32 +13,25 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cloudflare"}) + `\b(v[A-Za-z0-9._-]{173,})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cloudflare"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cloudflare")} } -// FromData will find and optionally verify CloudflareCaKey secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue @@ -48,7 +39,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_CloudflareCaKey, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -57,9 +48,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } req.Header.Add("Content-Type", "application/json") - req.Header.Add("user-agent", "curl/7.68.0") // pretend to be from curl so we do not wait 100+ seconds -> nice try did not work + req.Header.Add("user-agent", "curl/7.68.0") - req.Header.Add("X-Auth-User-Service-Key", resMatch) + req.Header.Add("X-Auth-User-Service-Key", string(resMatch)) res, err := client.Do(req) if err == nil { defer res.Body.Close() diff --git a/pkg/detectors/cloudflareglobalapikey/cloudflareglobalapikey.go b/pkg/detectors/cloudflareglobalapikey/cloudflareglobalapikey.go index c1798fc9..3170e547 100644 --- a/pkg/detectors/cloudflareglobalapikey/cloudflareglobalapikey.go +++ b/pkg/detectors/cloudflareglobalapikey/cloudflareglobalapikey.go @@ -1,10 +1,10 @@ package cloudflareglobalapikey import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,7 +13,6 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( @@ -21,46 +20,38 @@ var ( apiKeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cloudflare"}) + `([A-Za-z0-9_-]{37})`) - // email pattern thanks https://golangcode.com/validate-an-email-address/ - // emailPat = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") - emailPat = regexp.MustCompile(`\b([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cloudflare"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cloudflare")} } -// FromData will find and optionally verify CloudflareGlobalApiKey secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - apiKeyMatches := apiKeyPat.FindAllStringSubmatch(dataStr, -1) - emailMatches := emailPat.FindAllStringSubmatch(dataStr, -1) + apiKeyMatches := apiKeyPat.FindAllSubmatch(data, -1) + emailMatches := emailPat.FindAllSubmatch(data, -1) for _, apiKeyMatch := range apiKeyMatches { if len(apiKeyMatch) != 2 { continue } - apiKeyRes := strings.TrimSpace(apiKeyMatch[1]) + apiKeyRes := bytes.TrimSpace(apiKeyMatch[1]) for _, emailMatch := range emailMatches { if len(emailMatch) != 2 { continue } - emailRes := strings.TrimSpace(emailMatch[1]) + emailRes := bytes.TrimSpace(emailMatch[1]) - if detectors.IsKnownFalsePositive(apiKeyRes, detectors.DefaultFalsePositives, true) { // wait- (apiKeyRes, email) might be false positive does not mean (apiKeyRes, another_email) is ? + if detectors.IsKnownFalsePositive(apiKeyRes, detectors.DefaultFalsePositives, true) { continue } s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_CloudflareGlobalApiKey, - Redacted: emailRes, - Raw: []byte(apiKeyRes), - RawV2: []byte(apiKeyRes + emailRes), + Redacted: string(emailRes), + Raw: apiKeyRes, + RawV2: append(apiKeyRes, emailRes...), } if verify { @@ -68,8 +59,8 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("X-Auth-Email", emailRes) - req.Header.Add("X-Auth-Key", apiKeyRes) + req.Header.Add("X-Auth-Email", string(emailRes)) + req.Header.Add("X-Auth-Key", string(apiKeyRes)) req.Header.Add("Content-Type", "application/json") res, err := client.Do(req) diff --git a/pkg/detectors/cloudimage/cloudimage.go b/pkg/detectors/cloudimage/cloudimage.go index c097f0b5..9aacbff0 100644 --- a/pkg/detectors/cloudimage/cloudimage.go +++ b/pkg/detectors/cloudimage/cloudimage.go @@ -1,11 +1,10 @@ package cloudimage import ( + "bytes" "context" "net/http" "regexp" - "strings" - "time" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,58 +13,48 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cloudimage"}) + `\b([a-z0-9_]{30})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cloudimage"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cloudimage")} } -// FromData will find and optionally verify CloudImage secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_CloudImage, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - payload := strings.NewReader(`{"scope":"urls","urls":["/sample.li/paris.jpg?width=400","/sample.li/flat.jpg?width=400"]} - `) - timeout := 10 * time.Second - client.Timeout = timeout + payload := bytes.NewReader([]byte(`{"scope":"urls","urls":["/sample.li/paris.jpg?width=400","/sample.li/flat.jpg?width=400"]}`)) + req, err := http.NewRequest("POST", "https://api.cloudimage.com/invalidate", payload) if err != nil { continue } req.Header.Add("Content-Type", "application/json") - req.Header.Add("X-Client-Key", resMatch) + req.Header.Add("X-Client-Key", string(resMatch)) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. - if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { + if detectors.IsKnownFalsePositive([]byte(resMatch), detectors.DefaultFalsePositives, true) { continue } } diff --git a/pkg/detectors/cloudmersive/cloudmersive.go b/pkg/detectors/cloudmersive/cloudmersive.go index 768b3c39..7e712d88 100644 --- a/pkg/detectors/cloudmersive/cloudmersive.go +++ b/pkg/detectors/cloudmersive/cloudmersive.go @@ -1,10 +1,10 @@ package cloudmersive import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,54 +13,46 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cloudmersive"}) + `\b([a-z0-9-]{36})\b`) ) -// Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cloudmersive"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cloudmersive")} } -// FromData will find and optionally verify Cloudmersive secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Cloudmersive, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - payload := strings.NewReader(`{"AddressString":"string","CapitalizationMode":"string"}`) + payload := bytes.NewReader([]byte(`{"AddressString":"string","CapitalizationMode":"string"}`)) req, err := http.NewRequestWithContext(ctx, "POST", "https://api.cloudmersive.com/validate/address/parse", payload) if err != nil { continue } req.Header.Add("Content-Type", "application/json") - req.Header.Add("Apikey", resMatch) + req.Header.Add("Apikey", string(resMatch)) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/cloudplan/cloudplan.go b/pkg/detectors/cloudplan/cloudplan.go index df0db176..773dbb49 100644 --- a/pkg/detectors/cloudplan/cloudplan.go +++ b/pkg/detectors/cloudplan/cloudplan.go @@ -1,10 +1,10 @@ package cloudplan import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -19,31 +19,29 @@ var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. + // Keeping the regexp patterns unchanged keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"cloudplan"}) + `\b([A-Z0-9-]{32})\b`) ) // Keywords are used for efficiently pre-filtering chunks. -// Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cloudplan"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cloudplan")} } // FromData will find and optionally verify Cloudplan secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Cloudplan, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -51,14 +49,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("session_id", resMatch) + req.Header.Add("session_id", string(resMatch)) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/cloudsmith/cloudsmith.go b/pkg/detectors/cloudsmith/cloudsmith.go index 9ddc3b53..b1b3c07e 100644 --- a/pkg/detectors/cloudsmith/cloudsmith.go +++ b/pkg/detectors/cloudsmith/cloudsmith.go @@ -1,10 +1,10 @@ package cloudsmith import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -25,25 +25,23 @@ var ( // Keywords are used for efficiently pre-filtering chunks. // Use identifiers in the secret preferably, or the provider name. -func (s Scanner) Keywords() []string { - return []string{"cloudsmith"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("cloudsmith")} } // FromData will find and optionally verify Cloudsmith secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Cloudsmith, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,14 +50,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } req.Header.Add("Accept", "application/json") - req.Header.Add("X-Api-Key", resMatch) + req.Header.Add("X-Api-Key", string(resMatch)) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } else { - // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue }