Skip to content

Find, verify, and analyze leaked credentials

added auth0oauth detector

Jonee Ryan Tyad146a4b5959
diff --git a/pkg/detectors/auth0oauth/auth0oauth.go b/pkg/detectors/auth0oauth/auth0oauth.go
new file mode 100644
index 000000000..d287f14e5
--- /dev/null
+++ b/pkg/detectors/auth0oauth/auth0oauth.go
@@ -0,0 +1,114 @@
+package auth0oauth
+
+import (
+ "context"
+ // "fmt"
+ // "log"
+ "regexp"
+ "strings"
+
+ "io/ioutil"
+ "net/http"
+ "net/url"
+
+ "github.com/trufflesecurity/trufflehog/pkg/common"
+ "github.com/trufflesecurity/trufflehog/pkg/detectors"
+ "github.com/trufflesecurity/trufflehog/pkg/pb/detectorspb"
+)
+
+type Scanner struct{}
+
+// Ensure the Scanner satisfies the interface at compile time
+var _ detectors.Detector = (*Scanner)(nil)
+
+var (
+ client = common.SaneHttpClient()
+
+ clientIdPat = regexp.MustCompile(detectors.PrefixRegex([]string{"auth0"}) + `\b([a-zA-Z0-9_-]{32,60})\b`)
+ clientSecretPat = regexp.MustCompile(`\b([a-zA-Z0-9_-]{64,})\b`)
+ domainPat = regexp.MustCompile(`\b([a-zA-Z0-9][a-zA-Z0-9._-]*auth0.com)\b`) // could be part of url
+)
+
+// 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{"auth0"}
+}
+
+// FromData will find and optionally verify Auth0oauth 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)
+
+ clientIdMatches := clientIdPat.FindAllStringSubmatch(dataStr, -1)
+ clientSecretMatches := clientSecretPat.FindAllStringSubmatch(dataStr, -1)
+ domainMatches := domainPat.FindAllStringSubmatch(dataStr, -1)
+
+ for _, clientIdMatch := range clientIdMatches {
+ if len(clientIdMatch) != 2 {
+ continue
+ }
+ clientIdRes := strings.TrimSpace(clientIdMatch[1])
+
+ for _, clientSecretMatch := range clientSecretMatches {
+ if len(clientSecretMatch) != 2 {
+ continue
+ }
+ clientSecretRes := strings.TrimSpace(clientSecretMatch[1])
+
+ for _, domainMatch := range domainMatches {
+ if len(domainMatch) != 2 {
+ continue
+ }
+ domainRes := strings.TrimSpace(domainMatch[1])
+
+ s1 := detectors.Result{
+ DetectorType: detectorspb.DetectorType_Auth0oauth,
+ Redacted: clientIdRes,
+ Raw: []byte(clientSecretRes),
+ }
+
+ if verify {
+ /*
+ curl --request POST \
+ --url 'https://YOUR_DOMAIN/oauth/token' \
+ --header 'content-type: application/x-www-form-urlencoded' \
+ --data 'grant_type=authorization_code&client_id=W44JmL3qD6LxHeEJyKe9lMuhcwvPOaOq&client_secret=YOUR_CLIENT_SECRET&code=AUTHORIZATION_CODE&redirect_uri=undefined'
+ */
+
+ data := url.Values{}
+ data.Set("grant_type", "authorization_code")
+ data.Set("client_id", clientIdRes)
+ data.Set("client_secret", clientSecretRes)
+ data.Set("code", "AUTHORIZATION_CODE")
+ data.Set("redirect_uri", "undefined")
+
+ req, _ := http.NewRequest(http.MethodPost, "https://"+domainRes+"/oauth/token", strings.NewReader(data.Encode())) // URL-encoded payload
+ req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
+ res, err := client.Do(req)
+ if err == nil {
+ defer res.Body.Close()
+ bodyBytes, _ := ioutil.ReadAll(res.Body)
+ body := string(bodyBytes)
+
+ // if client_id and client_secret is valid -> 403 {"error":"invalid_grant","error_description":"Invalid authorization code"}
+ // if invalid -> 401 {"error":"access_denied","error_description":"Unauthorized"}
+ // ingenious!
+
+ if !strings.Contains(body, "access_denied") {
+ s1.Verified = true
+ } else {
+ if detectors.IsKnownFalsePositive(clientIdRes, detectors.DefaultFalsePositives, true) {
+ continue
+ }
+ }
+
+ }
+ }
+
+ results = append(results, s1)
+ }
+ }
+ }
+
+ return detectors.CleanResults(results), nil
+}
diff --git a/pkg/detectors/auth0oauth/auth0oauth_test.go b/pkg/detectors/auth0oauth/auth0oauth_test.go
new file mode 100644
index 000000000..87dfd6d4d
--- /dev/null
+++ b/pkg/detectors/auth0oauth/auth0oauth_test.go
@@ -0,0 +1,118 @@
+package auth0oauth
+
+import (
+ "context"
+ "fmt"
+ "testing"
+ "time"
+
+ "github.com/kylelemons/godebug/pretty"
+ "github.com/trufflesecurity/trufflehog/pkg/detectors"
+
+ "github.com/trufflesecurity/trufflehog/pkg/common"
+ "github.com/trufflesecurity/trufflehog/pkg/pb/detectorspb"
+)
+
+func TestAuth0oauth_FromChunk(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
+ defer cancel()
+ testSecrets, err := common.GetScannersTestSecretsVersion(ctx, 3)
+ if err != nil {
+ t.Fatalf("could not get test secrets from GCP: %s", err)
+ }
+
+ domain := testSecrets.MustGetField("AUTH0_DOMAIN")
+ clientId := testSecrets.MustGetField("AUTH0_CLIENT_ID") // there is AUTH0_CLIENT_ID2 and AUTH0_CLIENT_SECRET2 pair as well
+ clientSecret := testSecrets.MustGetField("AUTH0_CLIENT_SECRET")
+ inactiveClientSecret := testSecrets.MustGetField("AUTH0_CLIENT_SECRET_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 auth0 client id %s client secret %s domain %s", clientId, clientSecret, domain)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Auth0oauth,
+ Redacted: clientId,
+ Verified: true,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "found, unverified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a auth0 client id %s client secret %s domain https://%s/oauth/token within but not valid", clientId, inactiveClientSecret, domain)), // the secret would satisfy the regex but not pass validation
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Auth0oauth,
+ Redacted: clientId,
+ 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("Auth0oauth.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("Auth0oauth.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++ {
+ s.FromData(ctx, false, data)
+ }
+ })
+ }
+}