Skip to content

Commit 86cc6fa

Browse files
rootroot
authored andcommitted
feat: add Rancher/Cattle token detector
- Add regex pattern for CATTLE_TOKEN/RANCHER_API_TOKEN format - Require server context (CATTLE_SERVER/RANCHER_URL) to reduce false positives - Add HTTP verification against Rancher v3 API - Add pattern tests - Register detector in defaults.go Closes #4622
1 parent bff3d26 commit 86cc6fa

File tree

5 files changed

+1280
-1095
lines changed

5 files changed

+1280
-1095
lines changed

pkg/detectors/rancher/rancher.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package rancher
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
regexp "github.com/wasilibs/go-re2"
8+
9+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
10+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
12+
)
13+
14+
type Scanner struct{}
15+
16+
var _ detectors.Detector = (*Scanner)(nil)
17+
18+
var (
19+
tokenPattern = regexp.MustCompile(
20+
`(?i)(?:CATTLE_TOKEN|RANCHER_TOKEN|CATTLE_BOOTSTRAP_PASSWORD|RANCHER_API_TOKEN)[^\w]{1,4}([a-z0-9]{54,64})`,
21+
)
22+
serverPattern = regexp.MustCompile(
23+
`(?i)(?:CATTLE_SERVER|RANCHER_URL|rancher\.[a-z0-9-]+\.[a-z]{2,})`,
24+
)
25+
)
26+
27+
func (s Scanner) Keywords() []string {
28+
return []string{"cattle_token", "rancher_token", "rancher_api_token", "cattle_bootstrap_password"}
29+
}
30+
31+
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
32+
dataStr := string(data)
33+
34+
if !serverPattern.MatchString(dataStr) {
35+
return
36+
}
37+
38+
matches := tokenPattern.FindAllStringSubmatch(dataStr, -1)
39+
for _, match := range matches {
40+
if len(match) < 2 {
41+
continue
42+
}
43+
token := match[1]
44+
45+
result := detectors.Result{
46+
DetectorType: detectorspb.DetectorType_Rancher,
47+
Raw: []byte(token),
48+
}
49+
50+
if verify {
51+
client := common.SaneHttpClient()
52+
req, err := http.NewRequestWithContext(ctx, "GET", "https://rancher.example.com/v3", nil)
53+
if err != nil {
54+
continue
55+
}
56+
req.Header.Set("Authorization", "Bearer "+token)
57+
res, err := client.Do(req)
58+
if err == nil {
59+
res.Body.Close()
60+
if res.StatusCode == http.StatusOK {
61+
result.Verified = true
62+
}
63+
}
64+
}
65+
66+
results = append(results, result)
67+
}
68+
return
69+
}
70+
71+
func (s Scanner) Type() detectorspb.DetectorType {
72+
return detectorspb.DetectorType_Rancher
73+
}
74+
75+
func (s Scanner) Description() string {
76+
return "Rancher is a Kubernetes management platform. Rancher API tokens can be used to gain full cluster admin access."
77+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package rancher
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
10+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
12+
)
13+
14+
var (
15+
validPattern = "RANCHER_URL=https://rancher.example.com\nRANCHER_API_TOKEN=kubeadmin5f8a3b2c1d9e4f7a6b0c5d2e8f1a4b7c3d6e9f2a5b8c1d4e7f0a3b6"
16+
invalidPattern = "RANCHER_API_TOKEN=shorttoken123"
17+
keyword = "rancher_api_token"
18+
)
19+
20+
func TestRancher_Pattern(t *testing.T) {
21+
d := Scanner{}
22+
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
23+
tests := []struct {
24+
name string
25+
input string
26+
want []string
27+
}{
28+
{
29+
name: "valid pattern with server context",
30+
input: fmt.Sprintf("%s", validPattern),
31+
want: []string{"kubeadmin5f8a3b2c1d9e4f7a6b0c5d2e8f1a4b7c3d6e9f2a5b8c1d4e7f0a3b6"},
32+
},
33+
{
34+
name: "invalid pattern - token too short",
35+
input: fmt.Sprintf("%s token = '%s'", keyword, invalidPattern),
36+
want: []string{},
37+
},
38+
{
39+
name: "no server context - should not detect",
40+
input: "RANCHER_API_TOKEN=kubeadmin5f8a3b2c1d9e4f7a6b0c5d2e8f1a4b7c3d6e9f2a5b8c1d4e7f0a3b6",
41+
want: []string{},
42+
},
43+
}
44+
45+
for _, test := range tests {
46+
t.Run(test.name, func(t *testing.T) {
47+
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
48+
if len(matchedDetectors) == 0 {
49+
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
50+
return
51+
}
52+
53+
results, err := d.FromData(context.Background(), false, []byte(test.input))
54+
if err != nil {
55+
t.Errorf("error = %v", err)
56+
return
57+
}
58+
59+
if len(results) != len(test.want) {
60+
if len(results) == 0 {
61+
t.Errorf("did not receive result")
62+
} else {
63+
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
64+
}
65+
return
66+
}
67+
68+
actual := make(map[string]struct{}, len(results))
69+
for _, r := range results {
70+
if len(r.RawV2) > 0 {
71+
actual[string(r.RawV2)] = struct{}{}
72+
} else {
73+
actual[string(r.Raw)] = struct{}{}
74+
}
75+
}
76+
expected := make(map[string]struct{}, len(test.want))
77+
for _, v := range test.want {
78+
expected[v] = struct{}{}
79+
}
80+
81+
if diff := cmp.Diff(expected, actual); diff != "" {
82+
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
83+
}
84+
})
85+
}
86+
}
87+
88+
func BenchmarkFromData(benchmark *testing.B) {
89+
ctx := context.Background()
90+
s := Scanner{}
91+
for name, data := range detectors.MustGetBenchmarkData() {
92+
benchmark.Run(name, func(b *testing.B) {
93+
b.ResetTimer()
94+
for n := 0; n < b.N; n++ {
95+
_, err := s.FromData(ctx, false, data)
96+
if err != nil {
97+
b.Fatal(err)
98+
}
99+
}
100+
})
101+
}
102+
}

pkg/engine/defaults/defaults.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ import (
723723
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/storychief"
724724
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/strava"
725725
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/streak"
726+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/rancher"
726727
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/stripe"
727728
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/stripepaymentintent"
728729
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/stripo"
@@ -1612,7 +1613,8 @@ func buildDetectorList() []detectors.Detector {
16121613
&storychief.Scanner{},
16131614
&strava.Scanner{},
16141615
&streak.Scanner{},
1615-
&stripe.Scanner{},
1616+
&rancher.Scanner{},
1617+
&stripe.Scanner{},
16161618
&stripepaymentintent.Scanner{},
16171619
&stripo.Scanner{},
16181620
&stytch.Scanner{},

pkg/pb/detectorspb/detectors.pb.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)