Add the new MaxMind license key format (#2181)

* Add the new MaxMind license key format

* feedback

* reorg rules

---------

Co-authored-by: Dustin Decker <dustin@trufflesec.com>
This commit is contained in:
faktas2
2024-01-26 14:49:47 -05:00
committed by GitHub
parent 3a6cfd9d97
commit 76fcdae3a0
6 changed files with 225 additions and 1 deletions
+1 -1
View File
@@ -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/
@@ -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)
@@ -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
}
@@ -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)
}
}
})
}
}
+2
View File
@@ -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{},