From 76fcdae3a0ed1b71feb3fe599dd4d3e82de5757d Mon Sep 17 00:00:00 2001 From: faktas2 <104642023+faktas2@users.noreply.github.com> Date: Fri, 26 Jan 2024 14:49:47 -0500 Subject: [PATCH] Add the new MaxMind license key format (#2181) * Add the new MaxMind license key format * feedback * reorg rules --------- Co-authored-by: Dustin Decker --- .github/workflows/lint.yml | 2 +- .../detectors.yaml} | 0 .../maxmindlicense/maxmindlicense.go | 5 + .../maxmindlicense_v2/maxmindlicense_v2.go | 88 ++++++++++++ .../maxmindlicense_v2_test.go | 129 ++++++++++++++++++ pkg/engine/defaults.go | 2 + 6 files changed, 225 insertions(+), 1 deletion(-) rename hack/{semgrep-rules.yaml => semgrep-rules/detectors.yaml} (100%) create mode 100644 pkg/detectors/maxmindlicense_v2/maxmindlicense_v2.go create mode 100644 pkg/detectors/maxmindlicense_v2/maxmindlicense_v2_test.go diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1e1309768..bfbac4d90 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -43,4 +43,4 @@ jobs: if: (github.actor != 'dependabot[bot]') steps: - uses: actions/checkout@v4 - - run: semgrep --config=hack/semgrep-rules.yaml pkg/detectors/ + - run: semgrep --config=hack/semgrep-rules/detectors.yaml pkg/detectors/ diff --git a/hack/semgrep-rules.yaml b/hack/semgrep-rules/detectors.yaml similarity index 100% rename from hack/semgrep-rules.yaml rename to hack/semgrep-rules/detectors.yaml diff --git a/pkg/detectors/maxmindlicense/maxmindlicense.go b/pkg/detectors/maxmindlicense/maxmindlicense.go index 98b396bdd..fda7d5579 100644 --- a/pkg/detectors/maxmindlicense/maxmindlicense.go +++ b/pkg/detectors/maxmindlicense/maxmindlicense.go @@ -29,6 +29,8 @@ func (s Scanner) Keywords() []string { return []string{"maxmind", "geoip"} } +func (Scanner) Version() int { return 1 } + // FromData will find and optionally verify MaxMindLicense 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) @@ -50,6 +52,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result Redacted: idRes, Raw: []byte(keyRes), } + s1.ExtraData = map[string]string{ + "rotation_guide": "https://howtorotate.com/docs/tutorials/maxmind/", + } if verify { req, err := http.NewRequestWithContext(ctx, "GET", "https://geoip.maxmind.com/geoip/v2.1/country/8.8.8.8", nil) diff --git a/pkg/detectors/maxmindlicense_v2/maxmindlicense_v2.go b/pkg/detectors/maxmindlicense_v2/maxmindlicense_v2.go new file mode 100644 index 000000000..c9b1cac2e --- /dev/null +++ b/pkg/detectors/maxmindlicense_v2/maxmindlicense_v2.go @@ -0,0 +1,88 @@ +package maxmindlicense_v2 + +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() + + idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"maxmind", "geoip"}) + `\b([0-9]{2,7})\b`) + keyPat = regexp.MustCompile(`\b([0-9A-Za-z]{6}_[0-9A-Za-z]{29}_mmk)\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{"maxmind", "geoip"} +} + +func (Scanner) Version() int { return 2 } + +// FromData will find and optionally verify MaxMindLicense 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) + idMatches := idPat.FindAllStringSubmatch(dataStr, -1) + + for _, keyMatch := range keyMatches { + keyRes := strings.TrimSpace(keyMatch[1]) + + for _, idMatch := range idMatches { + if len(idMatch) != 2 { + continue + } + idRes := strings.TrimSpace(idMatch[1]) + + s := detectors.Result{ + DetectorType: detectorspb.DetectorType_MaxMindLicense, + Redacted: idRes, + Raw: []byte(keyRes), + RawV2: []byte(keyRes + idRes), + } + s.ExtraData = map[string]string{ + "rotation_guide": "https://howtorotate.com/docs/tutorials/maxmind/", + } + + if verify { + req, err := http.NewRequestWithContext(ctx, "GET", "https://geoip.maxmind.com/geoip/v2.1/country/8.8.8.8", nil) + if err != nil { + continue + } + req.SetBasicAuth(idRes, keyRes) + res, err := client.Do(req) + if err == nil { + defer res.Body.Close() + if res.StatusCode >= 200 && res.StatusCode < 300 { + s.Verified = true + } else { + if detectors.IsKnownFalsePositive(keyRes, detectors.DefaultFalsePositives, true) { + continue + } + } + } + } + + results = append(results, s) + } + } + + return results, nil +} + +func (s Scanner) Type() detectorspb.DetectorType { + return detectorspb.DetectorType_MaxMindLicense +} diff --git a/pkg/detectors/maxmindlicense_v2/maxmindlicense_v2_test.go b/pkg/detectors/maxmindlicense_v2/maxmindlicense_v2_test.go new file mode 100644 index 000000000..84b2598fa --- /dev/null +++ b/pkg/detectors/maxmindlicense_v2/maxmindlicense_v2_test.go @@ -0,0 +1,129 @@ +//go:build detectors +// +build detectors + +package maxmindlicense_v2 + +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 TestMaxMindLicense_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("MAXMIND_LICENSE") + user := testSecrets.MustGetField("MAXMIND_USER") + inactiveSecret := testSecrets.MustGetField("MAXMIND_LICENSE_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 geoip secret %s within with maxmind user %s", secret, user)), + verify: true, + }, + want: []detectors.Result{ + { + DetectorType: detectorspb.DetectorType_MaxMindLicense, + Redacted: "662034", + Verified: true, + ExtraData: map[string]string{ + "rotation_guide": "https://howtorotate.com/docs/tutorials/maxmind/", + }, + }, + }, + wantErr: false, + }, + { + name: "found, unverified", + s: Scanner{}, + args: args{ + ctx: context.Background(), + data: []byte(fmt.Sprintf("You can find a maxmind secret %s within with maxmind user %s", inactiveSecret, user)), + verify: true, + }, + want: []detectors.Result{ + { + DetectorType: detectorspb.DetectorType_MaxMindLicense, + Redacted: "662034", + Verified: false, + ExtraData: map[string]string{ + "rotation_guide": "https://howtorotate.com/docs/tutorials/maxmind/", + }, + }, + }, + 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("MaxMindLicense.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("MaxMindLicense.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) { + b.ResetTimer() + for n := 0; n < b.N; n++ { + _, err := s.FromData(ctx, false, data) + if err != nil { + b.Fatal(err) + } + } + }) + } +} diff --git a/pkg/engine/defaults.go b/pkg/engine/defaults.go index 53000ba4e..285d8ed34 100644 --- a/pkg/engine/defaults.go +++ b/pkg/engine/defaults.go @@ -418,6 +418,7 @@ import ( "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/mattermostpersonaltoken" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/mavenlink" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/maxmindlicense" + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/maxmindlicense_v2" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/meaningcloud" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/mediastack" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/meistertask" @@ -823,6 +824,7 @@ func DefaultDetectors() []detectors.Detector { &jdbc.Scanner{}, &privatekey.Scanner{}, &maxmindlicense.Scanner{}, + &maxmindlicense_v2.Scanner{}, &airtableapikey.Scanner{}, &bitfinex.Scanner{}, &telegrambottoken.Scanner{},