Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Detect API keys without app keys (#1605)
Bill Richc2b49b060bb0
diff --git a/pkg/detectors/datadogtoken/datadogtoken.go b/pkg/detectors/datadogtoken/datadogtoken.goindex fe75fc705..eeda41ae3 100644--- a/pkg/detectors/datadogtoken/datadogtoken.go+++ b/pkg/detectors/datadogtoken/datadogtoken.go@@ -20,7 +20,7 @@ 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{"datadog", "dd"}) + `\b([a-zA-Z-0-9]{40})\b`)+ appPat = regexp.MustCompile(detectors.PrefixRegex([]string{"datadog", "dd"}) + `\b([a-zA-Z-0-9]{40})\b`)apiPat = regexp.MustCompile(detectors.PrefixRegex([]string{"datadog", "dd"}) + `\b([a-zA-Z-0-9]{32})\b`))@@ -34,24 +34,28 @@ 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)+ appMatches := appPat.FindAllStringSubmatch(dataStr, -1)apiMatches := apiPat.FindAllStringSubmatch(dataStr, -1)- for _, match := range matches {- if len(match) != 2 {+ for _, apiMatch := range apiMatches {+ if len(apiMatch) != 2 {continue}- resMatch := strings.TrimSpace(match[1])- for _, apimatch := range apiMatches {- if len(apimatch) != 2 {+ resApiMatch := strings.TrimSpace(apiMatch[1])+ appIncluded := false+ for _, appMatch := range appMatches {+ if len(appMatch) != 2 {continue}- resApiMatch := strings.TrimSpace(apimatch[1])+ resAppMatch := strings.TrimSpace(appMatch[1])s1 := detectors.Result{DetectorType: detectorspb.DetectorType_DatadogToken,- Raw: []byte(resMatch),- RawV2: []byte(resMatch + resApiMatch),+ Raw: []byte(resAppMatch),+ RawV2: []byte(resAppMatch + resApiMatch),+ ExtraData: map[string]string{+ "Type": "Application+APIKey",+ },}if verify {@@ -62,7 +66,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}req.Header.Add("Content-Type", "application/json")req.Header.Add("DD-API-KEY", resApiMatch)- req.Header.Add("DD-APPLICATION-KEY", resMatch)+ req.Header.Add("DD-APPLICATION-KEY", resAppMatch)res, err := client.Do(req)if err == nil {defer res.Body.Close()@@ -70,16 +74,49 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results1.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(resApiMatch, detectors.DefaultFalsePositives, true) {continue}}}}-+ appIncluded = trueresults = append(results, s1)}+ if !appIncluded {+ s1 := detectors.Result{+ DetectorType: detectorspb.DetectorType_DatadogToken,+ Raw: []byte(resApiMatch),+ RawV2: []byte(resApiMatch),+ ExtraData: map[string]string{+ "Type": "APIKeyOnly",+ },+ }++ if verify {++ req, err := http.NewRequestWithContext(ctx, "GET", "https://api.datadoghq.com/api/v1/validate", nil)+ if err != nil {+ continue+ }+ req.Header.Add("Content-Type", "application/json")+ req.Header.Add("DD-API-KEY", resApiMatch)+ 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(resApiMatch, detectors.DefaultFalsePositives, true) {+ continue+ }+ }+ }+ }+ results = append(results, s1)+ }}return results, nildiff --git a/pkg/detectors/datadogtoken/datadogtoken_test.go b/pkg/detectors/datadogtoken/datadogtoken_test.goindex bc540e26a..52c752920 100644--- a/pkg/detectors/datadogtoken/datadogtoken_test.go+++ b/pkg/detectors/datadogtoken/datadogtoken_test.go@@ -19,11 +19,11 @@ import (func TestDatadogToken_FromChunk(t *testing.T) {ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)defer cancel()- testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors3")+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")if err != nil {t.Fatalf("could not get test secrets from GCP: %s", err)}- secret := testSecrets.MustGetField("DATADOGTOKEN_TOKEN")+ apiKey := testSecrets.MustGetField("DATADOGTOKEN_TOKEN")appKey := testSecrets.MustGetField("DATADOGTOKEN_APPKEY")inactiveAppKey := testSecrets.MustGetField("DATADOGTOKEN_INACTIVE")@@ -44,13 +44,16 @@ func TestDatadogToken_FromChunk(t *testing.T) {s: Scanner{},args: args{ctx: context.Background(),- data: []byte(fmt.Sprintf("You can find a datadogtoken secret %s within datadog %s", appKey, secret)),+ data: []byte(fmt.Sprintf("You can find a datadogtoken secret %s within datadog %s", appKey, apiKey)),verify: true,},want: []detectors.Result{{DetectorType: detectorspb.DetectorType_DatadogToken,Verified: true,+ ExtraData: map[string]string{+ "Type": "Application+APIKey",+ },},},wantErr: false,@@ -60,13 +63,35 @@ func TestDatadogToken_FromChunk(t *testing.T) {s: Scanner{},args: args{ctx: context.Background(),- data: []byte(fmt.Sprintf("You can find a datadogtoken secret %s within but datadog %s not valid", inactiveAppKey, secret)), // the secret would satisfy the regex but not pass validation+ data: []byte(fmt.Sprintf("You can find a datadogtoken secret %s within but datadog %s not valid", inactiveAppKey, apiKey)), // the secret would satisfy the regex but not pass validationverify: true,},want: []detectors.Result{{DetectorType: detectorspb.DetectorType_DatadogToken,Verified: false,+ ExtraData: map[string]string{+ "Type": "Application+APIKey",+ },+ },+ },+ wantErr: false,+ },+ {+ name: "api key found, verified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a datadogtoken secret %s", apiKey)), // the secret would satisfy the regex but not pass validation+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_DatadogToken,+ Verified: true,+ ExtraData: map[string]string{+ "Type": "APIKeyOnly",+ },},},wantErr: false,@@ -96,6 +121,7 @@ func TestDatadogToken_FromChunk(t *testing.T) {t.Fatalf("no raw secret present: \n %+v", got[i])}got[i].Raw = nil+ got[i].RawV2 = nil}if diff := pretty.Compare(got, tt.want); diff != "" {t.Errorf("DatadogToken.FromData() %s diff: (-got +want)\n%s", tt.name, diff)