Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
add new key pat for mailgun detector (#1375)
Zubair Khan0c3410c5cd5d
diff --git a/pkg/detectors/mailgun/mailgun.go b/pkg/detectors/mailgun/mailgun.goindex dbaf63308..dd9556617 100644--- a/pkg/detectors/mailgun/mailgun.go+++ b/pkg/detectors/mailgun/mailgun.go@@ -20,7 +20,11 @@ var _ detectors.Detector = (*Scanner)(nil)var (client = common.SaneHttpClient()- keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"mailgun"}) + `\b([a-zA-Z-0-9]{72})\b`)+ tokenPats = map[string]*regexp.Regexp{+ "Original MailGun Token": regexp.MustCompile(detectors.PrefixRegex([]string{"mailgun"}) + `\b([a-zA-Z-0-9]{72})\b`),+ "Key-MailGun Token": regexp.MustCompile(`\b(key-[a-z0-9]{32})\b`),+ "Hex MailGun Token": regexp.MustCompile(`\b([a-f0-9]{32}-[a-f0-9]{8}-[a-f0-9]{8})\b`),+ })// Keywords are used for efficiently pre-filtering chunks.@@ -33,39 +37,48 @@ 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)- for _, match := range matches {- if len(match) != 2 {- continue- }- resMatch := strings.TrimSpace(match[1])-- s1 := detectors.Result{- DetectorType: detectorspb.DetectorType_Mailgun,- Raw: []byte(resMatch),- }-- if verify {- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.mailgun.net/v3/domains", nil)- if err != nil {+ for _, tokenPat := range tokenPats {+ matches := tokenPat.FindAllStringSubmatch(dataStr, -1)+ for _, match := range matches {+ if len(match) != 2 {continue}- req.Header.Add("Authorization", fmt.Sprintf("Basic %s", resMatch))- res, err := client.Do(req)- if err == nil {- defer res.Body.Close()- if res.StatusCode >= 200 && res.StatusCode < 300 {- s1.Verified = true+ resMatch := strings.TrimSpace(match[1])++ s1 := detectors.Result{+ DetectorType: detectorspb.DetectorType_Mailgun,+ Raw: []byte(resMatch),+ }++ if verify {+ req, err := http.NewRequestWithContext(ctx, "GET", "https://api.mailgun.net/v3/domains", nil)+ if err != nil {+ continue+ }++ // If resMatch has "key" prefix, use it as the username for basic auth.+ if strings.HasPrefix(resMatch, "key-") {+ req.SetBasicAuth("api", resMatch)} else {- if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) {- continue+ req.Header.Add("Authorization", fmt.Sprintf("Basic %s", resMatch))+ }++ res, err := client.Do(req)+ if err == nil {+ defer res.Body.Close()+ if res.StatusCode >= 200 && res.StatusCode < 300 {+ s1.Verified = true+ } else {+ if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) {+ continue+ }}}+}+ results = append(results, s1)}-- results = append(results, s1)}return results, nildiff --git a/pkg/detectors/mailgun/mailgun_test.go b/pkg/detectors/mailgun/mailgun_test.goindex ba2edf2a2..971869bb6 100644--- a/pkg/detectors/mailgun/mailgun_test.go+++ b/pkg/detectors/mailgun/mailgun_test.go@@ -9,8 +9,6 @@ import ("testing""time"- // "strings"-"github.com/kylelemons/godebug/pretty""github.com/trufflesecurity/trufflehog/v3/pkg/detectors"@@ -21,12 +19,15 @@ import (func TestMailgun_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("MAILGUN_TOKEN")inactiveSecret := testSecrets.MustGetField("MAILGUN_INACTIVE")+ keyDashSecret := testSecrets.MustGetField("NEW_MAILGUN_TOKEN_ACTIVE")+ inactiveHexEncodedSecret := testSecrets.MustGetField("NEW_MAILGUN_TOKEN_INACTIVE")+type args struct {ctx context.Contextdata []byte@@ -55,6 +56,38 @@ func TestMailgun_FromChunk(t *testing.T) {},wantErr: false,},+ {+ name: "found, verified key-dash mailgun pattern token",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a mailgun secret %s within https://api.mailgun.net/v3/domains", keyDashSecret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Mailgun,+ Verified: true,+ },+ },+ wantErr: false,+ },+ {+ name: "found, unverified key-dash mailgun pattern token",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a mailgun secret %s within https://api.mailgun.net/v3/domains", inactiveHexEncodedSecret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Mailgun,+ Verified: false,+ },+ },+ wantErr: false,+ },{name: "found, unverified",s: Scanner{},