From b630d5179faeec794210cc24ee8aff7bb47aa840 Mon Sep 17 00:00:00 2001 From: Ahrav Dutta Date: Fri, 25 Aug 2023 21:40:36 -0700 Subject: [PATCH] PR 16 of 40 --- pkg/detectors/heatmapapi/heatmapapi.go | 25 +++++----------- pkg/detectors/hellosign/hellosign.go | 23 +++++---------- pkg/detectors/helpcrunch/helpcrunch.go | 22 +++++--------- pkg/detectors/helpscout/helpscout.go | 18 +++++------- pkg/detectors/hereapi/hereapi.go | 21 +++++--------- pkg/detectors/heroku/heroku.go | 22 +++++--------- pkg/detectors/hive/hive.go | 29 +++++++------------ pkg/detectors/hiveage/hiveage.go | 25 ++++++---------- pkg/detectors/holidayapi/holidayapi.go | 24 ++++++--------- pkg/detectors/holistic/holistic.go | 25 +++++----------- pkg/detectors/honeycomb/honeycomb.go | 24 +++++---------- pkg/detectors/host/host.go | 23 +++++---------- pkg/detectors/html2pdf/html2pdf.go | 18 +++++------- pkg/detectors/hubspotapikey/hubspotapikey.go | 21 +++++--------- pkg/detectors/huggingface/huggingface.go | 14 ++++----- pkg/detectors/humanity/humanity.go | 23 +++++---------- pkg/detectors/hunter/hunter.go | 26 +++++------------ pkg/detectors/hybiscus/hybiscus.go | 22 +++++--------- pkg/detectors/hypertrack/hypertrack.go | 25 +++++++--------- .../ibmclouduserkey/ibmclouduserkey.go | 23 +++++---------- 20 files changed, 155 insertions(+), 298 deletions(-) diff --git a/pkg/detectors/heatmapapi/heatmapapi.go b/pkg/detectors/heatmapapi/heatmapapi.go index 284d06c1..406d2aec 100644 --- a/pkg/detectors/heatmapapi/heatmapapi.go +++ b/pkg/detectors/heatmapapi/heatmapapi.go @@ -1,12 +1,12 @@ package heatmapapi import ( + "bytes" "context" "fmt" "io" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -15,41 +15,34 @@ 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{"heatmapapi"}) + `\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{"heatmapapi"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("heatmapapi")} } -// FromData will find and optionally verify Heatmapapi 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_Heatmapapi, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://www.heatmapapi.com/javascript/HeatmapAPI2.aspx?k=%s", resMatch), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://www.heatmapapi.com/javascript/HeatmapAPI2.aspx?k=%s", string(resMatch)), nil) if err != nil { continue } @@ -60,9 +53,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - body := string(bodyBytes) - - if strings.Contains(body, "poststr") { + if bytes.Contains(bodyBytes, []byte("poststr")) { s1.Verified = true } else { if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { diff --git a/pkg/detectors/hellosign/hellosign.go b/pkg/detectors/hellosign/hellosign.go index 9b43595b..340fefdf 100644 --- a/pkg/detectors/hellosign/hellosign.go +++ b/pkg/detectors/hellosign/hellosign.go @@ -1,10 +1,10 @@ package hellosign import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,37 +13,29 @@ 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{"hellosign"}) + `\b([a-zA-Z-0-9/+]{64})\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{"hellosign"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("hellosign")} } -// FromData will find and optionally verify HelloSign 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_HelloSign, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -51,14 +43,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.SetBasicAuth(resMatch, "") + req.SetBasicAuth(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/helpcrunch/helpcrunch.go b/pkg/detectors/helpcrunch/helpcrunch.go index 55a82a37..a7959bd3 100644 --- a/pkg/detectors/helpcrunch/helpcrunch.go +++ b/pkg/detectors/helpcrunch/helpcrunch.go @@ -1,11 +1,11 @@ package helpcrunch 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{"helpcrunch"}) + `\b([a-zA-Z-0-9+/=]{328})`) ) -// 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{"helpcrunch"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("helpcrunch")} } -// FromData will find and optionally verify HelpCrunch 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_HelpCrunch, - 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/helpscout/helpscout.go b/pkg/detectors/helpscout/helpscout.go index becc8caa..c4dc1339 100644 --- a/pkg/detectors/helpscout/helpscout.go +++ b/pkg/detectors/helpscout/helpscout.go @@ -1,11 +1,11 @@ package helpscout import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -26,25 +26,24 @@ 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{"helpscout"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("helpscout")} } // FromData will find and optionally verify Helpscout 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_Helpscout, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,14 +51,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("Authorization", fmt.Sprintf("Basic %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Basic %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/hereapi/hereapi.go b/pkg/detectors/hereapi/hereapi.go index c6362a7b..50ab6d69 100644 --- a/pkg/detectors/hereapi/hereapi.go +++ b/pkg/detectors/hereapi/hereapi.go @@ -1,11 +1,11 @@ package hereapi import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,39 +14,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{"hereapi"}) + `\b([a-zA-Z0-9\S]{43})\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{"hereapi"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("hereapi")} } -// FromData will find and optionally verify HereAPI 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_HereAPI, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://weather.ls.hereapi.com/weather/1.0/report.json?product=alerts&name=Argentina&apiKey=%s", resMatch), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://weather.ls.hereapi.com/weather/1.0/report.json?product=alerts&name=Argentina&apiKey=%s", string(resMatch)), nil) if err != nil { continue } @@ -56,7 +50,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/heroku/heroku.go b/pkg/detectors/heroku/heroku.go index 2821fc7c..23502ce4 100644 --- a/pkg/detectors/heroku/heroku.go +++ b/pkg/detectors/heroku/heroku.go @@ -1,11 +1,11 @@ package heroku 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{"heroku"}) + `\b([0-9Aa-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{"heroku"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("heroku")} } -// FromData will find and optionally verify Heroku 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_Heroku, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -53,14 +46,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } req.Header.Add("Accept", "application/vnd.heroku+json; version=3") - 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/hive/hive.go b/pkg/detectors/hive/hive.go index eecb74c8..e8382784 100644 --- a/pkg/detectors/hive/hive.go +++ b/pkg/detectors/hive/hive.go @@ -1,10 +1,10 @@ package hive import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,60 +13,53 @@ 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. idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"hive"}) + `\b([0-9A-Za-z]{17})\b`) keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"hive"}) + `\b([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{"hive"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("hive")} } -// FromData will find and optionally verify Hive 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) - - idMatches := idPat.FindAllStringSubmatch(dataStr, -1) - keyMatches := keyPat.FindAllStringSubmatch(dataStr, -1) + idMatches := idPat.FindAllSubmatch(data, -1) + keyMatches := keyPat.FindAllSubmatch(data, -1) for _, match := range idMatches { if len(match) != 2 { continue } - idMatch := strings.TrimSpace(match[1]) + idMatch := bytes.TrimSpace(match[1]) for _, match := range keyMatches { if len(match) != 2 { continue } - keyMatch := strings.TrimSpace(match[1]) + + keyMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Hive, - Raw: []byte(idMatch), + Raw: idMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", "https://app.hive.com/api/v1/testcredentials?user_id="+idMatch+"&api_key="+keyMatch, nil) + req, err := http.NewRequestWithContext(ctx, "GET", "https://app.hive.com/api/v1/testcredentials?user_id="+string(idMatch)+"&api_key="+string(keyMatch), nil) if err != nil { continue } + 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(keyMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/hiveage/hiveage.go b/pkg/detectors/hiveage/hiveage.go index 1f84e97e..5f6688ae 100644 --- a/pkg/detectors/hiveage/hiveage.go +++ b/pkg/detectors/hiveage/hiveage.go @@ -1,12 +1,12 @@ package hiveage 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,42 +15,36 @@ 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{"hiveage"}) + `\b([0-9A-Za-z\_\-]{20})\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{"hiveage"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("hiveage")} } -// FromData will find and optionally verify Hiveage 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_Hiveage, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - data := fmt.Sprintf("%s:", resMatch) - encoded := b64.StdEncoding.EncodeToString([]byte(data)) + encData := []byte(fmt.Sprintf("%s:", string(resMatch))) + encoded := b64.StdEncoding.EncodeToString(encData) req, err := http.NewRequestWithContext(ctx, "GET", "https://mltb8350.hiveage.com/api/network", nil) if err != nil { continue @@ -63,7 +57,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/holidayapi/holidayapi.go b/pkg/detectors/holidayapi/holidayapi.go index f22ee728..436c50f3 100644 --- a/pkg/detectors/holidayapi/holidayapi.go +++ b/pkg/detectors/holidayapi/holidayapi.go @@ -1,11 +1,11 @@ package holidayapi import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,52 +14,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{"holidayapi"}) + `\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{"holidayapi"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("holidayapi")} } -// FromData will find and optionally verify HolidayAPI 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_HolidayAPI, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://holidayapi.com/v1/holidays?key=%s&country=PH&year=2020", resMatch), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://holidayapi.com/v1/holidays?key=%s&country=PH&year=2020", string(resMatch)), nil) if err != nil { continue } + 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(resMatch, detectors.DefaultFalsePositives, false) { continue } } diff --git a/pkg/detectors/holistic/holistic.go b/pkg/detectors/holistic/holistic.go index 71160576..44d57829 100644 --- a/pkg/detectors/holistic/holistic.go +++ b/pkg/detectors/holistic/holistic.go @@ -1,11 +1,11 @@ package holistic import ( + "bytes" "context" "io" "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{"holistic"}) + `\b([0-9a-f]{64})\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{"holistic"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("holistic")} } -// FromData will find and optionally verify Holistic 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_Holistic, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,7 +45,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("x-api-key", resMatch) + req.Header.Add("x-api-key", string(resMatch)) res, err := client.Do(req) if err == nil { bodyBytes, err := io.ReadAll(res.Body) @@ -60,14 +53,12 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } - bodyString := string(bodyBytes) - errorResponse := strings.Contains(bodyString, `"data":[]`) + errorResponse := bytes.Contains(bodyBytes, []byte(`"data":[]`)) defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 && !errorResponse { 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/honeycomb/honeycomb.go b/pkg/detectors/honeycomb/honeycomb.go index 08af012f..ab752971 100644 --- a/pkg/detectors/honeycomb/honeycomb.go +++ b/pkg/detectors/honeycomb/honeycomb.go @@ -1,10 +1,10 @@ package honeycomb import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,38 +13,29 @@ 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. - // Older + Newer API key format. See pull#687 for discussion keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"Honeycomb"}) + `\b([0-9a-f]{32}|[0-9a-zA-Z]{22})\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{"Honeycomb"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("Honeycomb")} } -// FromData will find and optionally verify Honeycomb 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_Honeycomb, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,14 +43,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("X-Honeycomb-Team", resMatch) + req.Header.Add("X-Honeycomb-Team", 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/host/host.go b/pkg/detectors/host/host.go index 0ed0db80..19bb8c94 100644 --- a/pkg/detectors/host/host.go +++ b/pkg/detectors/host/host.go @@ -1,11 +1,11 @@ package host import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,41 +14,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{"host"}) + `\b([a-z0-9]{14})\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{"host.io"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("host.io")} } -// FromData will find and optionally verify Host 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_Host, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://host.io/api/domains/ip/8.8.8.8?token=%s", resMatch), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://host.io/api/domains/ip/8.8.8.8?token=%s", string(resMatch)), nil) if err != nil { continue } @@ -58,7 +50,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/html2pdf/html2pdf.go b/pkg/detectors/html2pdf/html2pdf.go index 5b0594af..bd69a0d9 100644 --- a/pkg/detectors/html2pdf/html2pdf.go +++ b/pkg/detectors/html2pdf/html2pdf.go @@ -6,7 +6,6 @@ import ( "encoding/json" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" @@ -23,37 +22,35 @@ type html2pdfRequest struct { var _ detectors.Detector = (*Scanner)(nil) var ( - // 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{"html2pdf"}) + `\b([a-zA-Z0-9]{64})\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{"html2pdf"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("html2pdf")} } // FromData will find and optionally verify Html2Pdf 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_Html2Pdf, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { req := html2pdfRequest{ HTML: "Helloworld", - ApiKey: resMatch, + ApiKey: string(resMatch), } reqJson, _ := json.Marshal(&req) reqBuf := bytes.NewReader(reqJson) @@ -64,7 +61,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/hubspotapikey/hubspotapikey.go b/pkg/detectors/hubspotapikey/hubspotapikey.go index 4adb62c7..6f77d5b8 100644 --- a/pkg/detectors/hubspotapikey/hubspotapikey.go +++ b/pkg/detectors/hubspotapikey/hubspotapikey.go @@ -1,11 +1,10 @@ package hubspotapikey import ( + "bytes" "context" - // "log" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,7 +13,6 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( @@ -23,30 +21,27 @@ var ( keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"hubspot"}) + `\b([A-Za-z0-9]{8}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{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{"hubspot"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("hubspot")} } -// FromData will find and optionally verify HubSpotApiKey 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.FindAllSubmatch(data, -1) - matches := keyPat.FindAllStringSubmatch(dataStr, -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_HubSpotApiKey, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", "https://api.hubapi.com/contacts/v1/lists?hapikey="+resMatch, nil) + req, err := http.NewRequestWithContext(ctx, "GET", "https://api.hubapi.com/contacts/v1/lists?hapikey="+string(resMatch), nil) if err != nil { continue } diff --git a/pkg/detectors/huggingface/huggingface.go b/pkg/detectors/huggingface/huggingface.go index 694da79f..81a4b1dc 100644 --- a/pkg/detectors/huggingface/huggingface.go +++ b/pkg/detectors/huggingface/huggingface.go @@ -1,11 +1,11 @@ package huggingface import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -27,21 +27,19 @@ 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{"huggingface", "hugging_face", "hf"} // Huggingface docs occasionally use "hf" instead of "huggingface" +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("huggingface"), []byte("hugging_face"), []byte("hf")} } // FromData will find and optionally verify Huggingface 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) != 1 { continue } - resMatch := strings.TrimSpace(match[0]) + resMatch := bytes.TrimSpace(match[0]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_HuggingFace, @@ -58,7 +56,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } - req.Header.Set("Authorization", "Bearer "+resMatch) + req.Header.Set("Authorization", "Bearer "+string(resMatch)) res, err := client.Do(req) if err == nil { diff --git a/pkg/detectors/humanity/humanity.go b/pkg/detectors/humanity/humanity.go index 87994860..c9fe51c9 100644 --- a/pkg/detectors/humanity/humanity.go +++ b/pkg/detectors/humanity/humanity.go @@ -1,11 +1,11 @@ package humanity import ( + "bytes" "context" "io" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -24,31 +24,26 @@ var ( keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"humanity"}) + `\b([0-9a-z]{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{"humanity"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("humanity")} } -// FromData will find and optionally verify Humanity 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_Humanity, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", "https://www.humanity.com/api/v2/me?access_token="+resMatch, nil) + req, err := http.NewRequestWithContext(ctx, "GET", "https://www.humanity.com/api/v2/me?access_token="+string(resMatch), nil) if err != nil { continue } @@ -59,9 +54,8 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - body := string(bodyBytes) - validResponse := strings.Contains(body, "name") + validResponse := bytes.Contains(bodyBytes, []byte("name")) if validResponse { s1.Verified = true @@ -76,7 +70,6 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result results = append(results, s1) } - return results, nil } diff --git a/pkg/detectors/hunter/hunter.go b/pkg/detectors/hunter/hunter.go index c53fda8a..c9e3781f 100644 --- a/pkg/detectors/hunter/hunter.go +++ b/pkg/detectors/hunter/hunter.go @@ -1,12 +1,10 @@ package hunter import ( + "bytes" "context" - // "fmt" - // "log" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -15,43 +13,33 @@ 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{"hunter"}) + `\b([a-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{"hunter"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("hunter")} } -// FromData will find and optionally verify Hunter 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_Hunter, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", "https://api.hunter.io/v2/leads_lists?api_key="+resMatch, nil) - if err != nil { - continue - } + req, _ := http.NewRequestWithContext(ctx, "GET", "https://api.hunter.io/v2/leads_lists?api_key="+string(resMatch), nil) res, err := client.Do(req) if err == nil { defer res.Body.Close() diff --git a/pkg/detectors/hybiscus/hybiscus.go b/pkg/detectors/hybiscus/hybiscus.go index 61c173bf..f2edcf95 100644 --- a/pkg/detectors/hybiscus/hybiscus.go +++ b/pkg/detectors/hybiscus/hybiscus.go @@ -1,10 +1,10 @@ package hybiscus 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{"hybiscus"}) + `\b([0-9a-zA-Z]{43})\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{"hybiscus"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("hybiscus")} } -// FromData will find and optionally verify Hybiscus 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_Hybiscus, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,14 +45,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } req.Header.Add("Accept", "application/vnd.hybiscus+json; version=3") - 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 } diff --git a/pkg/detectors/hypertrack/hypertrack.go b/pkg/detectors/hypertrack/hypertrack.go index 318bed54..952ca5dc 100644 --- a/pkg/detectors/hypertrack/hypertrack.go +++ b/pkg/detectors/hypertrack/hypertrack.go @@ -1,10 +1,10 @@ package hypertrack import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -19,40 +19,37 @@ 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 accPat = regexp.MustCompile(detectors.PrefixRegex([]string{"hypertrack"}) + `\b([0-9a-zA-Z\_\-]{27})\b`) keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"hypertrack"}) + `\b([0-9a-zA-Z\_\-]{54})\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{"hypertrack"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("hypertrack")} } // FromData will find and optionally verify Hypertrack 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) - accMatches := accPat.FindAllStringSubmatch(dataStr, -1) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + accMatches := accPat.FindAllSubmatch(data, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, accMatch := range accMatches { - if len(accMatch) != 2 { continue } - resAccMatch := strings.TrimSpace(accMatch[1]) + resAccMatch := bytes.TrimSpace(accMatch[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_Hypertrack, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -60,7 +57,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.SetBasicAuth(resAccMatch, resMatch) + req.SetBasicAuth(string(resAccMatch), string(resMatch)) req.Header.Add("Content-Type", "application/json") res, err := client.Do(req) if err == nil { @@ -68,11 +65,9 @@ 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 } - if detectors.IsKnownFalsePositive(resAccMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/ibmclouduserkey/ibmclouduserkey.go b/pkg/detectors/ibmclouduserkey/ibmclouduserkey.go index d94f8942..87b55cd0 100644 --- a/pkg/detectors/ibmclouduserkey/ibmclouduserkey.go +++ b/pkg/detectors/ibmclouduserkey/ibmclouduserkey.go @@ -1,10 +1,10 @@ package ibmclouduserkey import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,41 +13,34 @@ 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{"ibm"}) + `\b([A-Za-z0-9_-]{44})\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{"ibm"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("ibm")} } -// FromData will find and optionally verify IbmCloudUserKey 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_IbmCloudUserKey, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - payload := strings.NewReader(`apikey=` + resMatch + `&grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey`) + payload := bytes.NewBuffer([]byte("apikey=" + string(resMatch) + "&grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey")) req, err := http.NewRequestWithContext(ctx, "POST", "https://iam.cloud.ibm.com/identity/token", payload) if err != nil { continue @@ -60,8 +53,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 }