Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
updated 24 a letter detectors with indeterminate verification error returns (#4061)
Nabeel Alam11ba4ee8eee8
diff --git a/pkg/detectors/adobeio/adobeio.go b/pkg/detectors/adobeio/adobeio.goindex b6461c54e..81eedc3ea 100644--- a/pkg/detectors/adobeio/adobeio.go+++ b/pkg/detectors/adobeio/adobeio.go@@ -2,8 +2,9 @@ package adobeioimport ("context"+ "fmt"+ "io""net/http"- "strings"regexp "github.com/wasilibs/go-re2"@@ -12,7 +13,8 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{+type Scanner struct {+ client *http.Clientdetectors.DefaultMultiPartCredentialProvider}@@ -20,7 +22,7 @@ type Scanner struct{var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"adobe"}) + `\b([a-z0-9]{32})\b`)@@ -37,34 +39,33 @@ func (s Scanner) Keywords() []string {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)+ var uniqueKeys, uniqueIds = make(map[string]struct{}), make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])- for _, idMatch := range idMatches {- resIdMatch := strings.TrimSpace(idMatch[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }++ for _, matches := range idPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueIds[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {+ for id := range uniqueIds {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_AdobeIO,- Raw: []byte(resMatch),- RawV2: []byte(resMatch + resIdMatch),+ Raw: []byte(key),+ RawV2: []byte(key + id),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://stock.adobe.io/Rest/Media/1/Search/Files?locale=en_US%2526search_parameters%255Bwords%255D=kittens", nil)- if err != nil {- continue- }- req.Header.Add("x-api-key", resMatch)- req.Header.Add("x-product", resIdMatch)- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAdobeIOSecret(ctx, client, key, id)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -75,6 +76,35 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAdobeIOSecret(ctx context.Context, client *http.Client, key string, id string) (bool, error) {+ url := "https://stock.adobe.io/Rest/Media/1/Search/Files?locale=en_US%2526search_parameters%255Bwords%255D=kittens"+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("x-api-key", key)+ req.Header.Add("x-product", id)++ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_AdobeIO}diff --git a/pkg/detectors/airbrakeprojectkey/airbrakeprojectkey.go b/pkg/detectors/airbrakeprojectkey/airbrakeprojectkey.goindex db1c32b27..29ec88769 100644--- a/pkg/detectors/airbrakeprojectkey/airbrakeprojectkey.go+++ b/pkg/detectors/airbrakeprojectkey/airbrakeprojectkey.go@@ -2,16 +2,20 @@ package airbrakeprojectkeyimport ("context"- regexp "github.com/wasilibs/go-re2"+ "fmt"+ "io""net/http""strings"+ regexp "github.com/wasilibs/go-re2"+"github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{+type Scanner struct {+ client *http.Clientdetectors.DefaultMultiPartCredentialProvider}@@ -19,7 +23,7 @@ type Scanner struct{var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"airbrake"}) + `\b([a-zA-Z-0-9]{32})\b`)@@ -36,40 +40,36 @@ func (s Scanner) Keywords() []string {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)-- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ var uniqueKeys, uniqueIds = make(map[string]struct{}), make(map[string]struct{})- for _, idMatch := range idMatches {+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }- resIdMatch := strings.TrimSpace(idMatch[1])+ for _, matches := range idPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueIds[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {+ for id := range uniqueIds {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_AirbrakeProjectKey,- Raw: []byte(resMatch),- RawV2: []byte(resMatch + resIdMatch),+ Raw: []byte(key),+ RawV2: []byte(key + id),}s1.ExtraData = map[string]string{"rotation_guide": "https://howtorotate.com/docs/tutorials/airbrake/",}if verify {- payload := strings.NewReader(`{"environment":"production","username":"john","email":"[email protected]","repository":"https://github.com/airbrake/airbrake","revision":"38748467ea579e7ae64f7815452307c9d05e05c5","version":"v2.0"}`)-- req, err := http.NewRequestWithContext(ctx, "POST", "https://api.airbrake.io/api/v4/projects/"+resIdMatch+"/deploys?key="+resMatch, payload)- if err != nil {- continue- }- req.Header.Add("Content-Type", "application/json")- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAirbrakeProjectKey(ctx, client, key, id)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -81,6 +81,35 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAirbrakeProjectKey(ctx context.Context, client *http.Client, key string, id string) (bool, error) {+ url := "https://api.airbrake.io/api/v4/projects/" + id + "/deploys?key=" + key+ payload := strings.NewReader(`{"environment":"production","username":"john","email":"[email protected]","repository":"https://github.com/airbrake/airbrake","revision":"38748467ea579e7ae64f7815452307c9d05e05c5","version":"v2.0"}`)+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, payload)+ if err != nil {+ return false, err+ }++ resp, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, resp.Body)+ _ = resp.Body.Close()+ }()++ // handle according to detector API responses.+ switch resp.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_AirbrakeProjectKey}diff --git a/pkg/detectors/airbrakeuserkey/airbrakeuserkey.go b/pkg/detectors/airbrakeuserkey/airbrakeuserkey.goindex 467b87eaa..97515e406 100644--- a/pkg/detectors/airbrakeuserkey/airbrakeuserkey.go+++ b/pkg/detectors/airbrakeuserkey/airbrakeuserkey.go@@ -2,22 +2,26 @@ package airbrakeuserkeyimport ("context"- regexp "github.com/wasilibs/go-re2"+ "fmt"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"airbrake"}) + `\b([a-zA-Z-0-9]{40})\b`)@@ -33,31 +37,30 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_AirbrakeUserKey,- Raw: []byte(resMatch),+ Raw: []byte(key),ExtraData: map[string]string{"rotation_guide": "https://howtorotate.com/docs/tutorials/airbrake/",},}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.airbrake.io/api/v4/projects?key="+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- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAirbrakeUserKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -66,6 +69,31 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAirbrakeUserKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.airbrake.io/api/v4/projects?key="+key, nil)+ if err != nil {+ return false, err+ }+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_AirbrakeUserKey}diff --git a/pkg/detectors/airship/airship.go b/pkg/detectors/airship/airship.goindex cd64fb22d..d2d83565f 100644--- a/pkg/detectors/airship/airship.go+++ b/pkg/detectors/airship/airship.go@@ -3,22 +3,25 @@ package airshipimport ("context""fmt"- regexp "github.com/wasilibs/go-re2"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"airship"}) + `\b([0-9a-zA-Z]{91})\b`)@@ -34,30 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Airship,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://go.urbanairship.com/api/schedules", nil)- if err != nil {- continue- }- req.Header.Add("Accept", "application/vnd.urbanairship+json; version=3")- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAirshipKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -66,6 +66,34 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAirshipKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://go.urbanairship.com/api/schedules", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Accept", "application/vnd.urbanairship+json; version=3")+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", key))++ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Airship}diff --git a/pkg/detectors/airvisual/airvisual.go b/pkg/detectors/airvisual/airvisual.goindex b0a83359d..573e312fe 100644--- a/pkg/detectors/airvisual/airvisual.go+++ b/pkg/detectors/airvisual/airvisual.go@@ -3,22 +3,25 @@ package airvisualimport ("context""fmt"- regexp "github.com/wasilibs/go-re2"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"airvisual"}) + `\b([a-z0-9-]{36})\b`)@@ -34,30 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_AirVisual,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.airvisual.com/v2/countries?key=%s", resMatch), nil)- if err != nil {- continue- }- req.Header.Add("Accept", "application/vnd.airvisual+json; version=3")- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAirVisualKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -66,6 +66,34 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAirVisualKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://api.airvisual.com/v2/countries?key=%s", key), nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Accept", "application/vnd.airvisual+json; version=3")+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", key))++ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_AirVisual}diff --git a/pkg/detectors/aiven/aiven.go b/pkg/detectors/aiven/aiven.goindex 41b9aa2c5..b72a1aeef 100644--- a/pkg/detectors/aiven/aiven.go+++ b/pkg/detectors/aiven/aiven.go@@ -3,22 +3,25 @@ package aivenimport ("context""fmt"- regexp "github.com/wasilibs/go-re2"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"aiven"}) + `([a-zA-Z0-9/+=]{372})`)@@ -34,29 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Aiven,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.aiven.io/v1/project", nil)- if err != nil {- continue- }- req.Header.Add("Authorization", fmt.Sprintf("aivenv1 %s", resMatch))- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAivenKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -65,6 +66,32 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAivenKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.aiven.io/v1/project", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Authorization", fmt.Sprintf("aivenv1 %s", key))+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Aiven}diff --git a/pkg/detectors/alconost/alconost.go b/pkg/detectors/alconost/alconost.goindex 47cad8f8f..b183b59f2 100644--- a/pkg/detectors/alconost/alconost.go+++ b/pkg/detectors/alconost/alconost.go@@ -4,22 +4,25 @@ import ("context"b64 "encoding/base64""fmt"- regexp "github.com/wasilibs/go-re2"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"alconost"}) + `\b([0-9Aa-z]{32})\b`)@@ -35,32 +38,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Alconost,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- data := fmt.Sprintf("%s:", resMatch)- sEnc := b64.StdEncoding.EncodeToString([]byte(data))- req, err := http.NewRequestWithContext(ctx, "GET", "https://nitro.alconost.com/api/v1/account", nil)- if err != nil {- continue- }- req.Header.Add("Content-Type", "application/json")- req.Header.Add("Authorization", fmt.Sprintf("Basic %s", sEnc))- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAlconostKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -69,6 +67,35 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAlconostKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ data := fmt.Sprintf("%s:", key)+ sEnc := b64.StdEncoding.EncodeToString([]byte(data))+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://nitro.alconost.com/api/v1/account", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Content-Type", "application/json")+ req.Header.Add("Authorization", fmt.Sprintf("Basic %s", sEnc))+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Alconost}diff --git a/pkg/detectors/aletheiaapi/aletheiaapi.go b/pkg/detectors/aletheiaapi/aletheiaapi.goindex 996022155..550bf9b08 100644--- a/pkg/detectors/aletheiaapi/aletheiaapi.go+++ b/pkg/detectors/aletheiaapi/aletheiaapi.go@@ -2,8 +2,9 @@ package aletheiaapiimport ("context"+ "fmt"+ "io""net/http"- "strings""time"regexp "github.com/wasilibs/go-re2"@@ -13,13 +14,15 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"aletheiaapi"}) + `\b([A-Z0-9]{32})\b`)@@ -35,31 +38,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_AletheiaApi,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- timeout := 10 * time.Second- client.Timeout = timeout- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.aletheiaapi.com/StockData?symbol=msft&summary=true", nil)- if err != nil {- continue- }- req.Header.Add("Key", resMatch)- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAletheiaAPIKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -68,6 +67,34 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAletheiaAPIKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ timeout := 10 * time.Second+ client.Timeout = timeout+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.aletheiaapi.com/StockData?symbol=msft&summary=true", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Key", key)+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_AletheiaApi}diff --git a/pkg/detectors/alienvault/alienvault.go b/pkg/detectors/alienvault/alienvault.goindex 5b3b3b43f..aa4452730 100644--- a/pkg/detectors/alienvault/alienvault.go+++ b/pkg/detectors/alienvault/alienvault.go@@ -2,22 +2,26 @@ package alienvaultimport ("context"- regexp "github.com/wasilibs/go-re2"+ "fmt"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"alienvault"}) + `\b([a-z0-9]{64})\b`)@@ -33,29 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_AlienVault,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://otx.alienvault.com/api/v1/users/me", nil)- if err != nil {- continue- }- req.Header.Add("X-OTX-API-KEY", resMatch)- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAlienVaultKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -68,6 +70,32 @@ func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_AlienVault}+func verifyAlienVaultKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://otx.alienvault.com/api/v1/users/me", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("X-OTX-API-KEY", key)+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Description() string {return "AlienVault is a threat intelligence platform providing real-time data on emerging threats. AlienVault API keys can be used to access threat data and other services."}diff --git a/pkg/detectors/allsports/allsports.go b/pkg/detectors/allsports/allsports.goindex 09b33f05b..348f92be6 100644--- a/pkg/detectors/allsports/allsports.go+++ b/pkg/detectors/allsports/allsports.go@@ -2,6 +2,7 @@ package allsportsimport ("context"+ "fmt""io""net/http""strings"@@ -13,13 +14,15 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"allsports"}) + `\b([0-9a-z]{64})\b`)@@ -35,34 +38,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Allsports,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://apiv2.allsportsapi.com/football/?met=Countries&APIkey="+resMatch, nil)- if err != nil {- continue- }- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- bodyBytes, err := io.ReadAll(res.Body)- if err != nil {- continue- }- body := string(bodyBytes)-- if strings.Contains(body, "success") {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAllSportsKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -71,6 +67,39 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAllSportsKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://apiv2.allsportsapi.com/football/?met=Countries&APIkey="+key, nil)+ if err != nil {+ return false, err+ }+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ bodyBytes, err := io.ReadAll(res.Body)+ if err != nil {+ return false, err+ }+ body := string(bodyBytes)+ if strings.Contains(body, "Wrong login credentials") {+ return false, nil+ }+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Allsports}diff --git a/pkg/detectors/amadeus/amadeus.go b/pkg/detectors/amadeus/amadeus.goindex 5383cedac..ad68b77e0 100644--- a/pkg/detectors/amadeus/amadeus.go+++ b/pkg/detectors/amadeus/amadeus.go@@ -2,6 +2,7 @@ package amadeusimport ("context"+ "fmt""io""net/http""strings"@@ -13,7 +14,8 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{+type Scanner struct {+ client *http.Clientdetectors.DefaultMultiPartCredentialProvider}@@ -21,7 +23,7 @@ type Scanner struct{var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"amadeus"}) + `\b([0-9A-Za-z]{32})\b`)@@ -38,40 +40,33 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)- secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys, uniqueSecrets = make(map[string]struct{}), make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])- for _, secretMatch := range secretMatches {- resSecret := strings.TrimSpace(secretMatch[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }++ for _, matches := range secretPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueSecrets[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {+ for secret := range uniqueSecrets {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Amadeus,- Raw: []byte(resMatch),- RawV2: []byte(resMatch + resSecret),+ Raw: []byte(key),+ RawV2: []byte(key + secret),}if verify {- payload := strings.NewReader("grant_type=client_credentials&client_id=" + resMatch + "&client_secret=" + resSecret)-- req, err := http.NewRequestWithContext(ctx, "POST", "https://test.api.amadeus.com/v1/security/oauth2/token", payload)- if err != nil {- continue- }- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- bodyBytes, err := io.ReadAll(res.Body)- if err != nil {- continue- }- body := string(bodyBytes)- if (res.StatusCode >= 200 && res.StatusCode < 300) && strings.Contains(body, "access_token") {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAdobeIOSecret(ctx, client, key, secret)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -81,6 +76,42 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAdobeIOSecret(ctx context.Context, client *http.Client, key string, secret string) (bool, error) {+ payload := strings.NewReader("grant_type=client_credentials&client_id=" + key + "&client_secret=" + secret)++ req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://test.api.amadeus.com/v1/security/oauth2/token", payload)+ if err != nil {+ return false, err+ }+ req.Header.Add("Content-Type", "application/x-www-form-urlencoded")+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ bodyBytes, err := io.ReadAll(res.Body)+ if err != nil {+ return false, err+ }+ body := string(bodyBytes)+ if !strings.Contains(body, "access_token") {+ return false, nil+ }+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Amadeus}diff --git a/pkg/detectors/ambee/ambee.go b/pkg/detectors/ambee/ambee.goindex 0354c4d20..82d8637c8 100644--- a/pkg/detectors/ambee/ambee.go+++ b/pkg/detectors/ambee/ambee.go@@ -2,22 +2,26 @@ package ambeeimport ("context"- regexp "github.com/wasilibs/go-re2"+ "fmt"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"ambee"}) + `\b([0-9a-f]{64})\b`)@@ -33,30 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Ambee,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.ambeedata.com/latest/by-lat-lng?lat=12&lng=77", nil)- if err != nil {- continue- }- req.Header.Add("Content-Type", "application/json")- req.Header.Add("x-api-key", resMatch)- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAmbeeKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -65,6 +66,33 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAmbeeKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.ambeedata.com/latest/by-lat-lng?lat=12&lng=77", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Content-Type", "application/json")+ req.Header.Add("x-api-key", key)+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Ambee}diff --git a/pkg/detectors/amplitudeapikey/amplitudeapikey.go b/pkg/detectors/amplitudeapikey/amplitudeapikey.goindex e3d1f16da..d1b13a10e 100644--- a/pkg/detectors/amplitudeapikey/amplitudeapikey.go+++ b/pkg/detectors/amplitudeapikey/amplitudeapikey.go@@ -2,8 +2,9 @@ package amplitudeapikeyimport ("context"+ "fmt"+ "io""net/http"- "strings"regexp "github.com/wasilibs/go-re2"@@ -13,6 +14,7 @@ import ()type Scanner struct {+ client *http.Clientdetectors.DefaultMultiPartCredentialProvider}@@ -20,7 +22,7 @@ type Scanner struct {var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"amplitude"}) + `\b([0-9a-f]{32})\b`)@@ -37,39 +39,38 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)- secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys, uniqueSecrets = make(map[string]struct{}), make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }- for _, secretMatch := range secretMatches {- resSecretMatch := strings.TrimSpace(secretMatch[1])+ for _, matches := range secretPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueSecrets[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {+ for secret := range uniqueSecrets {// regex for both key and secret are same so the set of strings could possibly be same as well- if resMatch == resSecretMatch {+ if key == secret {continue}s1 := detectors.Result{DetectorType: detectorspb.DetectorType_AmplitudeApiKey,- Raw: []byte(resMatch),- RawV2: []byte(resMatch + resSecretMatch),+ Raw: []byte(key),+ RawV2: []byte(key + secret),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://amplitude.com/api/2/taxonomy/category", nil)- if err != nil {- continue- }- req.SetBasicAuth(resMatch, resSecretMatch)- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAdobeIOSecret(ctx, client, key, secret)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -79,6 +80,32 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAdobeIOSecret(ctx context.Context, client *http.Client, key string, secret string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://amplitude.com/api/2/taxonomy/category", nil)+ if err != nil {+ return false, err+ }+ req.SetBasicAuth(key, secret)+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_AmplitudeApiKey}diff --git a/pkg/detectors/anypoint/anypoint.go b/pkg/detectors/anypoint/anypoint.goindex 7d779cea8..ecd228aea 100644--- a/pkg/detectors/anypoint/anypoint.go+++ b/pkg/detectors/anypoint/anypoint.go@@ -3,8 +3,8 @@ package anypointimport ("context""fmt"+ "io""net/http"- "strings"regexp "github.com/wasilibs/go-re2"@@ -14,6 +14,7 @@ import ()type Scanner struct {+ client *http.Clientdetectors.DefaultMultiPartCredentialProvider}@@ -21,7 +22,7 @@ type Scanner struct {var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.keyPat = regexp.MustCompile(`\b([0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12})\b`)@@ -38,39 +39,38 @@ func (s Scanner) Keywords() []string {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)+ var uniqueKeys, uniquePats = make(map[string]struct{}), make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])- for _, orgMatch := range orgMatches {- orgRes := strings.TrimSpace(orgMatch[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }++ for _, matches := range orgPat.FindAllStringSubmatch(dataStr, -1) {+ uniquePats[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {+ for org := range uniquePats {// regex for both key and org are same, so to avoid same string processing- if resMatch == orgRes {+ if key == org {continue}s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Anypoint,- Raw: []byte(resMatch),- RawV2: []byte(resMatch + orgRes),+ Raw: []byte(key),+ RawV2: []byte(key + org),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://anypoint.mulesoft.com/apiplatform/repository/v2/organizations/%s/apis/by-name?apiName=%s", orgRes, ""), nil)- if err != nil {- continue- }- req.Header.Add("Content-Type", "application/json")- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAnypointSecret(ctx, client, key, org)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -80,6 +80,33 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAnypointSecret(ctx context.Context, client *http.Client, key string, org string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://anypoint.mulesoft.com/apiplatform/repository/v2/organizations/%s/apis/by-name?apiName=%s", org, ""), nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Content-Type", "application/json")+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", key))+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Anypoint}diff --git a/pkg/detectors/apacta/apacta.go b/pkg/detectors/apacta/apacta.goindex 185d19e65..77d9a6c6c 100644--- a/pkg/detectors/apacta/apacta.go+++ b/pkg/detectors/apacta/apacta.go@@ -3,22 +3,25 @@ package apactaimport ("context""fmt"- regexp "github.com/wasilibs/go-re2"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"apacta"}) + `\b([a-z0-9-]{36})\b`)@@ -34,28 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Apacta,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://app.apacta.com/api/v1/time_entries?api_key=%s", 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- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyApactaKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -64,6 +66,31 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyApactaKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://app.apacta.com/api/v1/time_entries?api_key=%s", key), nil)+ if err != nil {+ return false, err+ }+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Apacta}diff --git a/pkg/detectors/api2cart/api2cart.go b/pkg/detectors/api2cart/api2cart.goindex 20f371cac..6fa8c2bda 100644--- a/pkg/detectors/api2cart/api2cart.go+++ b/pkg/detectors/api2cart/api2cart.go@@ -6,7 +6,6 @@ import ("fmt""io""net/http"- "strings"regexp "github.com/wasilibs/go-re2"@@ -15,13 +14,15 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"api2cart"}) + `\b([0-9a-f]{32})\b`)@@ -36,38 +37,28 @@ func (s Scanner) Keywords() []string {// FromData will find and optionally verify Api2Cart 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)- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ var uniqueKeys = make(map[string]struct{})++ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Api2Cart,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.api2cart.com/v1.1/account.cart.list.json?api_key=%s", resMatch), nil)- if err != nil {- continue- }- req.Header.Add("Accept", "application/json")- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- body, errBody := io.ReadAll(res.Body)-- var result Response- if errBody == nil {- if err := json.Unmarshal(body, &result); err != nil {- continue- }-- if res.StatusCode >= 200 && res.StatusCode < 300 && result.ReturnCode == 0 {- s1.Verified = true- }- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyApi2CartKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -76,6 +67,44 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyApi2CartKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://api.api2cart.com/v1.1/account.cart.list.json?api_key=%s", key), nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Accept", "application/json")+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ body, err := io.ReadAll(res.Body)+ if err != nil {+ return false, err+ }++ var result Response+ if err := json.Unmarshal(body, &result); err != nil {+ return false, err+ }+ if result.ReturnCode == 0 {+ return true, nil+ }+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+ return false, nil+}+type Response struct {ReturnCode int `json:"return_code"`}diff --git a/pkg/detectors/apideck/apideck.go b/pkg/detectors/apideck/apideck.goindex 40c8d7012..5b109614b 100644--- a/pkg/detectors/apideck/apideck.go+++ b/pkg/detectors/apideck/apideck.go@@ -3,8 +3,8 @@ package apideckimport ("context""fmt"+ "io""net/http"- "strings"regexp "github.com/wasilibs/go-re2"@@ -13,7 +13,8 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{+type Scanner struct {+ client *http.Clientdetectors.DefaultMultiPartCredentialProvider}@@ -21,7 +22,7 @@ type Scanner struct{var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.keyPat = regexp.MustCompile(`\b(sk_live_[a-z0-9A-Z-]{93})\b`)@@ -38,35 +39,33 @@ func (s Scanner) Keywords() []string {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)+ var uniqueKeys, uniqueIds = make(map[string]struct{}), make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }- for _, idmatch := range idMatches {- resIdMatch := strings.TrimSpace(idmatch[1])+ for _, matches := range idPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueIds[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {+ for id := range uniqueIds {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_ApiDeck,- Raw: []byte(resMatch),- RawV2: []byte(resMatch + resIdMatch),+ Raw: []byte(key),+ RawV2: []byte(key + id),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://unify.apideck.com/vault/consumers", nil)- if err != nil {- continue- }- req.Header.Add("x-apideck-app-id", resIdMatch)- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAdobeIOSecret(ctx, client, key, id)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -77,6 +76,33 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAdobeIOSecret(ctx context.Context, client *http.Client, key string, id string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://unify.apideck.com/vault/consumers", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("x-apideck-app-id", id)+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", key))+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_ApiDeck}diff --git a/pkg/detectors/apifonica/apifonica.go b/pkg/detectors/apifonica/apifonica.goindex 420922326..3c6b17b91 100644--- a/pkg/detectors/apifonica/apifonica.go+++ b/pkg/detectors/apifonica/apifonica.go@@ -4,8 +4,8 @@ import ("context"b64 "encoding/base64""fmt"+ "io""net/http"- "strings"regexp "github.com/wasilibs/go-re2"@@ -14,13 +14,15 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"apifonica"}) + `\b([0-9a-z]{11}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12})\b`)@@ -36,35 +38,32 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)- tokenMatches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys, uniqueTokens = make(map[string]struct{}), make(map[string]struct{})++ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])- for _, tokenMatch := range tokenMatches {- resToken := strings.TrimSpace(tokenMatch[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueTokens[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {+ for token := range uniqueTokens {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_ApiFonica,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- data := fmt.Sprintf("%s:%s", resMatch, resToken)- sEnc := b64.StdEncoding.EncodeToString([]byte(data))- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.apifonica.com/v2/accounts", nil)- if err != nil {- continue- }- req.Header.Add("Content-Type", "application/json")- req.Header.Add("Authorization", fmt.Sprintf("Basic %s", sEnc))- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyApifonicaSecret(ctx, client, key, token)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -74,6 +73,35 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyApifonicaSecret(ctx context.Context, client *http.Client, key string, token string) (bool, error) {+ data := fmt.Sprintf("%s:%s", key, token)+ sEnc := b64.StdEncoding.EncodeToString([]byte(data))+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.apifonica.com/v2/accounts", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Content-Type", "application/json")+ req.Header.Add("Authorization", fmt.Sprintf("Basic %s", sEnc))+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_ApiFonica}diff --git a/pkg/detectors/apify/apify.go b/pkg/detectors/apify/apify.goindex 641deead9..682d0f9d7 100644--- a/pkg/detectors/apify/apify.go+++ b/pkg/detectors/apify/apify.go@@ -2,22 +2,26 @@ package apifyimport ("context"- regexp "github.com/wasilibs/go-re2"+ "fmt"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.keyPat = regexp.MustCompile(`\b(apify\_api\_[a-zA-Z-0-9]{36})\b`)@@ -33,28 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Apify,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.apify.com/v2/acts?token="+resMatch+"&my=true&offset=10&limit=99&desc=true", 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- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyApifyKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -63,6 +66,31 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyApifyKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.apify.com/v2/acts?token="+key+"&my=true&offset=10&limit=99&desc=true", nil)+ if err != nil {+ return false, err+ }+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Apify}diff --git a/pkg/detectors/apilayer/apilayer.go b/pkg/detectors/apilayer/apilayer.goindex 502e726c1..f3b50b620 100644--- a/pkg/detectors/apilayer/apilayer.go+++ b/pkg/detectors/apilayer/apilayer.go@@ -2,22 +2,26 @@ package apilayerimport ("context"- regexp "github.com/wasilibs/go-re2"+ "fmt"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}-// 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()+ defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positiveskeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"apilayer"}) + `\b([a-zA-Z0-9]{32})\b`)@@ -33,29 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Apilayer,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.apilayer.com/number_verification/countries", nil)- if err != nil {- continue- }- req.Header.Add("apikey", resMatch)- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAPILayerKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -64,6 +66,33 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAPILayerKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.apilayer.com/number_verification/countries", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("apikey", key)++ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Apilayer}diff --git a/pkg/detectors/apimatic/apimatic.go b/pkg/detectors/apimatic/apimatic.goindex 0edd4d43a..754c109c3 100644--- a/pkg/detectors/apimatic/apimatic.go+++ b/pkg/detectors/apimatic/apimatic.go@@ -2,6 +2,8 @@ package apimaticimport ("context"+ "fmt"+ "io""net/http""time"@@ -13,6 +15,7 @@ import ()type Scanner struct {+ client *http.Clientdetectors.DefaultMultiPartCredentialProvider}@@ -20,7 +23,7 @@ type Scanner struct {var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.apiKeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"apimatic", "apikey"}) + `\b([a-zA-Z0-9_-]{64})\b`)@@ -48,27 +51,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}if verify {- timeout := 10 * time.Second- client.Timeout = timeout-- // api docs: https://docs.apimatic.io/platform-api/#/http/api-endpoints/code-generation-external-apis/list-all-code-generations- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.apimatic.io/code-generations", http.NoBody)- if err != nil {- continue+ client := s.client+ if client == nil {+ client = defaultClient}- // authentication documentation: https://docs.apimatic.io/platform-api/#/http/guides/authentication- req.Header.Set("Authorization", "X-Auth-Key "+apiKey)- resp, err := client.Do(req)- if err == nil {- defer resp.Body.Close()- if resp.StatusCode == 200 {- s1.Verified = true- }- } else {- // if any error happens during api request capture that as verification error- s1.SetVerificationError(err, apiKey)- }+ isVerified, verificationErr := verifyAPImaticKey(ctx, client, apiKey)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -77,6 +67,38 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAPImaticKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ timeout := 10 * time.Second+ client.Timeout = timeout++ // api docs: https://docs.apimatic.io/platform-api/#/http/api-endpoints/code-generation-external-apis/list-all-code-generations+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.apimatic.io/code-generations", http.NoBody)+ if err != nil {+ return false, err+ }++ // authentication documentation: https://docs.apimatic.io/platform-api/#/http/guides/authentication+ req.Header.Set("Authorization", "X-Auth-Key "+key)+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_APIMatic}diff --git a/pkg/detectors/apimetrics/apimetrics.go b/pkg/detectors/apimetrics/apimetrics.goindex 25840f35c..cf477535e 100644--- a/pkg/detectors/apimetrics/apimetrics.go+++ b/pkg/detectors/apimetrics/apimetrics.go@@ -3,8 +3,8 @@ package apimetricsimport ("context""fmt"+ "io""net/http"- "strings"regexp "github.com/wasilibs/go-re2"@@ -13,13 +13,15 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"apimetrics"}) + `\b([a-zA-Z0-9]{32})\b`)@@ -35,29 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_ApiMetrics,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://client.apimetrics.io/api/2/calls/", nil)- if err != nil {- continue- }- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAPIMetricsKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -66,6 +66,32 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAPIMetricsKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://client.apimetrics.io/api/2/calls/", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", key))+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_ApiMetrics}diff --git a/pkg/detectors/apitemplate/apitemplate.go b/pkg/detectors/apitemplate/apitemplate.goindex e634a21a4..757564ef0 100644--- a/pkg/detectors/apitemplate/apitemplate.go+++ b/pkg/detectors/apitemplate/apitemplate.go@@ -2,22 +2,26 @@ package apitemplateimport ("context"- regexp "github.com/wasilibs/go-re2"+ "fmt"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"apitemplate"}) + `\b([0-9a-zA-Z]{39})\b`)@@ -33,30 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_APITemplate,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.apitemplate.io/v1/list-templates", nil)- if err != nil {- continue- }- req.Header.Add("Content-Type", "application/json")- req.Header.Add("X-API-KEY", resMatch)- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyAPITemplateKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -65,6 +66,34 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyAPITemplateKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.apitemplate.io/v1/list-templates", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Content-Type", "application/json")+ req.Header.Add("X-API-KEY", key)++ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_APITemplate}diff --git a/pkg/detectors/apollo/apollo.go b/pkg/detectors/apollo/apollo.goindex 86e0cd0f6..17f91e6ed 100644--- a/pkg/detectors/apollo/apollo.go+++ b/pkg/detectors/apollo/apollo.go@@ -3,22 +3,25 @@ package apolloimport ("context""fmt"- regexp "github.com/wasilibs/go-re2"+ "io""net/http"- "strings"++ regexp "github.com/wasilibs/go-re2""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = 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{"apollo"}) + `\b([a-zA-Z0-9]{22})\b`)@@ -34,28 +37,27 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ var uniqueKeys = make(map[string]struct{})- for _, match := range matches {- resMatch := strings.TrimSpace(match[1])+ for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueKeys[matches[1]] = struct{}{}+ }+ for key := range uniqueKeys {s1 := detectors.Result{DetectorType: detectorspb.DetectorType_Apollo,- Raw: []byte(resMatch),+ Raw: []byte(key),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.apollo.io/v1/account_stages?api_key=%s", 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- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyApolloKey(ctx, client, key)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr)}results = append(results, s1)@@ -64,6 +66,32 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyApolloKey(ctx context.Context, client *http.Client, key string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.apollo.io/api/v1/mixed_people/search", nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("x-api-key", key)+ res, err := client.Do(req)+ if err != nil {+ return false, err+ }++ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ return true, nil+ case http.StatusUnauthorized, http.StatusForbidden:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_Apollo}