Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
added Magicbell scanner (#323)
Mildred Bernardo77522d58d0ca
diff --git a/pkg/detectors/magicbell/magicbell.go b/pkg/detectors/magicbell/magicbell.gonew file mode 100644index 000000000..0629e8689--- /dev/null+++ b/pkg/detectors/magicbell/magicbell.go@@ -0,0 +1,87 @@+package magicbell++import (+ "context"+ "net/http"+ "regexp"+ "strings"++ "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{}++// 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 boundry characters such as below to reduce false positives+ keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"magicbell"}) + `\b([a-zA-Z-0-9]{40})\b`)+ emailPat = regexp.MustCompile(`\b([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)\b`)+)++// Keywords are used for efficiently pre-filtering chunks.+// Use identifiers in the secret preferably, or the provider name.+func (s Scanner) Keywords() []string {+ return []string{"magicbell"}+}++// FromData will find and optionally verify MagicBell secrets in a given set of bytes.+func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {+ dataStr := string(data)++ apiKeyMatches := keyPat.FindAllStringSubmatch(dataStr, -1)+ emailMatches := emailPat.FindAllStringSubmatch(dataStr, -1)++ for _, keyMatch := range apiKeyMatches {+ if len(keyMatch) != 2 {+ continue+ }+ apiKeyRes := strings.TrimSpace(keyMatch[1])++ for _, emailMatch := range emailMatches {+ if len(emailMatch) != 2 {+ continue+ }+ emailRes := strings.TrimSpace(emailMatch[1])++ if detectors.IsKnownFalsePositive(apiKeyRes, detectors.DefaultFalsePositives, true) { // wait- (apiKeyRes, email) might be false positive does not mean (apiKeyRes, another_email) is ?+ continue+ }++ s1 := detectors.Result{+ DetectorType: detectorspb.DetectorType_MagicBell,+ Raw: []byte(apiKeyRes),+ }++ if verify {+ req, err := http.NewRequestWithContext(ctx, "GET", "https://api.magicbell.com/notification_preferences", nil)+ if err != nil {+ continue+ }+ req.Header.Add("X-MAGICBELL-API-KEY", apiKeyRes)+ req.Header.Add("X-MAGICBELL-USER-EMAIL", emailRes)+ 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(apiKeyRes, detectors.DefaultFalsePositives, true) {+ continue+ }+ }+ }+ }++ results = append(results, s1)+ }+ }++ return detectors.CleanResults(results), nil+}diff --git a/pkg/detectors/magicbell/magicbell_test.go b/pkg/detectors/magicbell/magicbell_test.gonew file mode 100644index 000000000..45270add4--- /dev/null+++ b/pkg/detectors/magicbell/magicbell_test.go@@ -0,0 +1,117 @@+package magicbell++import (+ "context"+ "fmt"+ "testing"+ "time"++ "github.com/kylelemons/godebug/pretty"+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"++ "github.com/trufflesecurity/trufflehog/v3/pkg/common"+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"+)++func TestMagicBell_FromChunk(t *testing.T) {+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)+ defer cancel()+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors3")+ if err != nil {+ t.Fatalf("could not get test secrets from GCP: %s", err)+ }+ secret := testSecrets.MustGetField("MAGICBELL")+ inactiveSecret := testSecrets.MustGetField("MAGICBELL_INACTIVE")+ userEmail := testSecrets.MustGetField("MAGICBELL_USER_EMAIL")++ type args struct {+ ctx context.Context+ data []byte+ verify bool+ }+ tests := []struct {+ name string+ s Scanner+ args args+ want []detectors.Result+ wantErr bool+ }{+ {+ name: "found, verified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a magicbell secret %s with email %s within", secret, userEmail)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_MagicBell,+ Verified: true,+ },+ },+ wantErr: false,+ },+ {+ name: "found, unverified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a magicbell secret %s with email %s within but not valid", inactiveSecret, userEmail)), // the secret would satisfy the regex but not pass validation+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_MagicBell,+ Verified: false,+ },+ },+ wantErr: false,+ },+ {+ name: "not found",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte("You cannot find the secret within"),+ verify: true,+ },+ want: nil,+ wantErr: false,+ },+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ s := Scanner{}+ got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)+ if (err != nil) != tt.wantErr {+ t.Errorf("MagicBell.FromData() error = %v, wantErr %v", err, tt.wantErr)+ return+ }+ for i := range got {+ if len(got[i].Raw) == 0 {+ t.Fatalf("no raw secret present: \n %+v", got[i])+ }+ got[i].Raw = nil+ }+ if diff := pretty.Compare(got, tt.want); diff != "" {+ t.Errorf("MagicBell.FromData() %s diff: (-got +want)\n%s", tt.name, diff)+ }+ })+ }+}++func BenchmarkFromData(benchmark *testing.B) {+ ctx := context.Background()+ s := Scanner{}+ for name, data := range detectors.MustGetBenchmarkData() {+ benchmark.Run(name, func(b *testing.B) {+ for n := 0; n < b.N; n++ {+ _, err := s.FromData(ctx, false, data)+ if err != nil {+ b.Fatal(err)+ }+ }+ })+ }+}diff --git a/pkg/detectors/twitter/twitter.go b/pkg/detectors/twitter/twitter.gonew file mode 100644index 000000000..bb89665d0--- /dev/null+++ b/pkg/detectors/twitter/twitter.go@@ -0,0 +1,69 @@+package twitter++import (+ "context"+ "fmt"+ "net/http"+ "regexp"+ "strings"++ "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{}++// 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 boundry characters such as below to reduce false positives+ keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"twitter"}) + `\b([A-Z]{22}%[a-zA-Z-0-9]{23}%[a-zA-Z-0-9]{6}%[a-zA-Z-0-9]{3}%[a-zA-Z-0-9]{9}%[a-zA-Z-0-9]{52})\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{"twitter"}+}++// FromData will find and optionally verify Twitter 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 {+ if len(match) != 2 {+ continue+ }+ resMatch := strings.TrimSpace(match[1])++ s1 := detectors.Result{+ DetectorType: detectorspb.DetectorType_Twitter,+ Raw: []byte(resMatch),+ }++ if verify {+ req, err := http.NewRequestWithContext(ctx, "GET", "https://api.twitter.com/2/tweets/20", 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+ }+ }+ }++ results = append(results, s1)+ }++ return detectors.CleanResults(results), nil+}diff --git a/pkg/detectors/twitter/twitter_test.go b/pkg/detectors/twitter/twitter_test.gonew file mode 100644index 000000000..49d18a5bc--- /dev/null+++ b/pkg/detectors/twitter/twitter_test.go@@ -0,0 +1,116 @@+package twitter++import (+ "context"+ "fmt"+ "testing"+ "time"++ "github.com/kylelemons/godebug/pretty"+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"++ "github.com/trufflesecurity/trufflehog/v3/pkg/common"+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"+)++func TestTwitter_FromChunk(t *testing.T) {+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)+ defer cancel()+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors3")+ if err != nil {+ t.Fatalf("could not get test secrets from GCP: %s", err)+ }+ secret := testSecrets.MustGetField("TWITTER")+ inactiveSecret := testSecrets.MustGetField("TWITTER_INACTIVE")++ type args struct {+ ctx context.Context+ data []byte+ verify bool+ }+ tests := []struct {+ name string+ s Scanner+ args args+ want []detectors.Result+ wantErr bool+ }{+ {+ name: "found, verified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a twitter secret %s within", secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Twitter,+ Verified: true,+ },+ },+ wantErr: false,+ },+ {+ name: "found, unverified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a twitter secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Twitter,+ Verified: false,+ },+ },+ wantErr: false,+ },+ {+ name: "not found",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte("You cannot find the secret within"),+ verify: true,+ },+ want: nil,+ wantErr: false,+ },+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ s := Scanner{}+ got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)+ if (err != nil) != tt.wantErr {+ t.Errorf("Twitter.FromData() error = %v, wantErr %v", err, tt.wantErr)+ return+ }+ for i := range got {+ if len(got[i].Raw) == 0 {+ t.Fatalf("no raw secret present: \n %+v", got[i])+ }+ got[i].Raw = nil+ }+ if diff := pretty.Compare(got, tt.want); diff != "" {+ t.Errorf("Twitter.FromData() %s diff: (-got +want)\n%s", tt.name, diff)+ }+ })+ }+}++func BenchmarkFromData(benchmark *testing.B) {+ ctx := context.Background()+ s := Scanner{}+ for name, data := range detectors.MustGetBenchmarkData() {+ benchmark.Run(name, func(b *testing.B) {+ for n := 0; n < b.N; n++ {+ _, err := s.FromData(ctx, false, data)+ if err != nil {+ b.Fatal(err)+ }+ }+ })+ }+}