Skip to content

Find, verify, and analyze leaked credentials

add thog enterprise detector for web keys (#1448)

Zubair Khanbe549a72874c
diff --git a/pkg/detectors/trufflehogenterprise/trufflehogenterprise.go b/pkg/detectors/trufflehogenterprise/trufflehogenterprise.go
new file mode 100644
index 000000000..d9880bde1
--- /dev/null
+++ b/pkg/detectors/trufflehogenterprise/trufflehogenterprise.go
@@ -0,0 +1,108 @@
+package trufflehogenterprise
+
+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 boundary characters such as below to reduce false positives.
+ keyPat = regexp.MustCompile(`\bthog-key-[0-9a-f]{16}\b`)
+ secretPat = regexp.MustCompile(`\bthog-secret-[0-9a-f]{32}\b`)
+ hostnamePat = regexp.MustCompile(`\b[a-z]+-[a-z]+-[a-z]+\.[a-z][0-9]\.[a-z]+\.trufflehog\.org\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{"thog"}
+}
+
+// FromData will find and optionally verify Trufflehog Enterprise 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)
+
+ keyMatches := keyPat.FindAllStringSubmatch(dataStr, -1)
+ secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1)
+ hostnameMatches := hostnamePat.FindAllStringSubmatch(dataStr, -1)
+
+ for _, keyMatch := range keyMatches {
+ if len(keyMatch) != 1 {
+ continue
+ }
+ resKeyMatch := strings.TrimSpace(keyMatch[0])
+ for _, secretMatch := range secretMatches {
+
+ if len(secretMatch) != 1 {
+ continue
+ }
+ resSecretMatch := strings.TrimSpace(secretMatch[0])
+
+ for _, hostnameMatch := range hostnameMatches {
+ if len(hostnameMatch) != 1 {
+ continue
+ }
+
+ resHostnameMatch := strings.TrimSpace(hostnameMatch[0])
+
+ s1 := detectors.Result{
+ DetectorType: detectorspb.DetectorType_TrufflehogEnterprise,
+ Raw: []byte(resKeyMatch),
+ }
+
+ if verify {
+ endpoint := fmt.Sprintf("https://%s/api/v1/sources", resHostnameMatch)
+ req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
+ if err != nil {
+ continue
+ }
+
+ req.Header.Add("Accept", "application/vnd.trufflehogenterprise+json; version=3")
+ req.Header.Add("X-Thog-Secret", resSecretMatch)
+ req.Header.Add("X-Thog-Key", resKeyMatch)
+
+ res, err := client.Do(req)
+ if err == nil {
+ verifiedBodyResponse, err := common.ResponseContainsSubstring(res.Body, "data")
+ if err != nil {
+ return nil, err
+ }
+
+ defer res.Body.Close()
+
+ if res.StatusCode >= 200 && res.StatusCode < 300 && verifiedBodyResponse {
+ 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(resSecretMatch, detectors.DefaultFalsePositives, true) {
+ continue
+ }
+ }
+ }
+ }
+
+ results = append(results, s1)
+ }
+ }
+ }
+
+ return results, nil
+}
+
+func (s Scanner) Type() detectorspb.DetectorType {
+ return detectorspb.DetectorType_TrufflehogEnterprise
+}
diff --git a/pkg/detectors/trufflehogenterprise/trufflehogenterprise_test.go b/pkg/detectors/trufflehogenterprise/trufflehogenterprise_test.go
new file mode 100644
index 000000000..85f5daf9c
--- /dev/null
+++ b/pkg/detectors/trufflehogenterprise/trufflehogenterprise_test.go
@@ -0,0 +1,121 @@
+//go:build detectors
+// +build detectors
+
+package trufflehogenterprise
+
+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 TestTrufflehogenterprise_FromChunk(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
+ defer cancel()
+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
+ if err != nil {
+ t.Fatalf("could not get test secrets from GCP: %s", err)
+ }
+ host := testSecrets.MustGetField("THOG_HOSTNAME")
+ key := testSecrets.MustGetField("THOG_WEB_KEY")
+ secret := testSecrets.MustGetField("THOG_WEB_SECRET")
+ inactiveSecret := testSecrets.MustGetField("THOG_WEB_INACTIVE_SECRET")
+
+ 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 thog secret %s for %s with key %s within", secret, host, key)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_TrufflehogEnterprise,
+ Verified: true,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "found, unverified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a trufflehogenterprise secret %s for %s with key %s within but not valid", inactiveSecret, host, key)), // the secret would satisfy the regex but not pass validation
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_TrufflehogEnterprise,
+ 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("Trufflehogenterprise.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("Trufflehogenterprise.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/pb/detectorspb/detectors.pb.go b/pkg/pb/detectorspb/detectors.pb.go
index 2a39badd9..bdf7db71e 100644
--- a/pkg/pb/detectorspb/detectors.pb.go
+++ b/pkg/pb/detectorspb/detectors.pb.go
@@ -993,6 +993,7 @@ const (
DetectorType_Docusign DetectorType = 919
DetectorType_Couchbase DetectorType = 920
DetectorType_EnvoyApiKey DetectorType = 921
+ DetectorType_TrufflehogEnterprise DetectorType = 922
)
// Enum value maps for DetectorType.
@@ -1916,6 +1917,7 @@ var (
919: "Docusign",
920: "Couchbase",
921: "EnvoyApiKey",
+ 922: "TrufflehogEnterprise",
}
DetectorType_value = map[string]int32{
"Alibaba": 0,
@@ -2836,6 +2838,7 @@ var (
"Docusign": 919,
"Couchbase": 920,
"EnvoyApiKey": 921,
+ "TrufflehogEnterprise": 922,
}
)
@@ -3214,7 +3217,7 @@ var file_detectors_proto_rawDesc = []byte{
0x44, 0x65, 0x63, 0x6f, 0x64, 0x65, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55,
0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x50, 0x4c, 0x41, 0x49,
0x4e, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x42, 0x41, 0x53, 0x45, 0x36, 0x34, 0x10, 0x02, 0x12,
- 0x09, 0x0a, 0x05, 0x55, 0x54, 0x46, 0x31, 0x36, 0x10, 0x03, 0x2a, 0x9b, 0x73, 0x0a, 0x0c, 0x44,
+ 0x09, 0x0a, 0x05, 0x55, 0x54, 0x46, 0x31, 0x36, 0x10, 0x03, 0x2a, 0xb6, 0x73, 0x0a, 0x0c, 0x44,
0x65, 0x74, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x41,
0x6c, 0x69, 0x62, 0x61, 0x62, 0x61, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x41, 0x4d, 0x51, 0x50,
0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x41, 0x57, 0x53, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x41,
@@ -4136,11 +4139,13 @@ var file_detectors_proto_rawDesc = []byte{
0x66, 0x65, 0x63, 0x74, 0x10, 0x96, 0x07, 0x12, 0x0d, 0x0a, 0x08, 0x44, 0x6f, 0x63, 0x75, 0x73,
0x69, 0x67, 0x6e, 0x10, 0x97, 0x07, 0x12, 0x0e, 0x0a, 0x09, 0x43, 0x6f, 0x75, 0x63, 0x68, 0x62,
0x61, 0x73, 0x65, 0x10, 0x98, 0x07, 0x12, 0x10, 0x0a, 0x0b, 0x45, 0x6e, 0x76, 0x6f, 0x79, 0x41,
- 0x70, 0x69, 0x4b, 0x65, 0x79, 0x10, 0x99, 0x07, 0x42, 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68,
- 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x73, 0x65,
- 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x68, 0x6f,
- 0x67, 0x2f, 0x76, 0x33, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x62, 0x2f, 0x64, 0x65, 0x74, 0x65,
- 0x63, 0x74, 0x6f, 0x72, 0x73, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+ 0x70, 0x69, 0x4b, 0x65, 0x79, 0x10, 0x99, 0x07, 0x12, 0x19, 0x0a, 0x14, 0x54, 0x72, 0x75, 0x66,
+ 0x66, 0x6c, 0x65, 0x68, 0x6f, 0x67, 0x45, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x72, 0x69, 0x73, 0x65,
+ 0x10, 0x9a, 0x07, 0x42, 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f,
+ 0x6d, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x73, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74,
+ 0x79, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x68, 0x6f, 0x67, 0x2f, 0x76, 0x33, 0x2f,
+ 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x62, 0x2f, 0x64, 0x65, 0x74, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x73,
+ 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
diff --git a/proto/detectors.proto b/proto/detectors.proto
index e60e561d4..1a04b30c0 100644
--- a/proto/detectors.proto
+++ b/proto/detectors.proto
@@ -930,6 +930,7 @@ enum DetectorType {
Docusign = 919;
Couchbase = 920;
EnvoyApiKey = 921;
+ TrufflehogEnterprise = 922;
}
message Result {