-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathspv2.go
More file actions
199 lines (171 loc) · 6.05 KB
/
spv2.go
File metadata and controls
199 lines (171 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package v2
import (
"context"
"errors"
"maps"
"net/http"
"regexp"
"slices"
"strings"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/azure_entra"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/azure_entra/serviceprincipal"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
type Scanner struct {
client *http.Client
detectors.DefaultMultiPartCredentialProvider
}
// Ensure the Scanner satisfies the interface at compile time.
var _ interface {
detectors.Detector
detectors.Versioner
} = (*Scanner)(nil)
var (
defaultClient = common.SaneHttpClient()
SecretPat = regexp.MustCompile(`(?:[^a-zA-Z0-9_~.-]|\A)([a-zA-Z0-9_~.-]{3}\dQ~[a-zA-Z0-9_~.-]{31,34})(?:[^a-zA-Z0-9_~.-]|\z)`)
)
func (s Scanner) Version() int {
return 2
}
// 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{"q~"}
}
func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_Azure
}
func (s Scanner) Description() string {
return serviceprincipal.Description
}
// FromData will find and optionally verify Azure 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)
clientSecrets := findSecretMatches(dataStr)
if len(clientSecrets) == 0 {
return results, nil
}
clientIds := azure_entra.FindClientIdMatches(dataStr)
tenantIds := azure_entra.FindTenantIdMatches(dataStr)
client := s.client
if client == nil {
client = defaultClient
}
results = append(results, ProcessData(ctx, clientSecrets, clientIds, tenantIds, verify, client)...)
return results, nil
}
func ProcessData(ctx context.Context, clientSecrets, clientIds, tenantIds map[string]struct{}, verify bool, client *http.Client) (results []detectors.Result) {
logCtx := logContext.AddLogger(ctx)
invalidClientsForTenant := make(map[string]map[string]struct{})
// Clone maps so verification-driven deletions don't mutate the caller's
// data or produce non-deterministic results across scanner runs.
activeClients := maps.Clone(clientIds)
activeTenants := maps.Clone(tenantIds)
for _, clientSecret := range slices.Sorted(maps.Keys(clientSecrets)) {
var (
r *detectors.Result
clientId string
tenantId string
)
ClientLoop:
for _, cId := range slices.Sorted(maps.Keys(activeClients)) {
if _, ok := activeClients[cId]; !ok {
continue
}
clientId = cId
for _, tId := range slices.Sorted(maps.Keys(activeTenants)) {
if _, ok := activeTenants[tId]; !ok {
continue
}
tenantId = tId
// Skip known-invalid client/tenant combinations.
invalidClients := invalidClientsForTenant[tenantId]
if invalidClients == nil {
invalidClients = map[string]struct{}{}
invalidClientsForTenant[tenantId] = invalidClients
}
if _, ok := invalidClients[clientId]; ok {
continue
}
if verify {
if !azure_entra.TenantExists(logCtx, client, tenantId) {
// Tenant doesn't exist.
delete(activeTenants, tenantId)
continue
}
// Tenant exists, ensure this isn't attempted as a clientId.
delete(activeClients, tenantId)
isVerified, extraData, verificationErr := serviceprincipal.VerifyCredentials(ctx, client, tenantId, clientId, clientSecret)
if verificationErr != nil {
switch {
case errors.Is(verificationErr, serviceprincipal.ErrConditionalAccessPolicy):
// Do nothing.
case errors.Is(verificationErr, serviceprincipal.ErrSecretInvalid):
continue ClientLoop
case errors.Is(verificationErr, serviceprincipal.ErrSecretExpired):
r = createResult(tenantId, clientId, clientSecret, false, nil, nil)
break ClientLoop
case errors.Is(verificationErr, serviceprincipal.ErrTenantNotFound):
// Tenant doesn't exist; shouldn't happen given the check above.
delete(activeTenants, tenantId)
continue
case errors.Is(verificationErr, serviceprincipal.ErrClientNotFoundInTenant):
// Tenant is valid but the client ID doesn't exist in it.
invalidClients[clientId] = struct{}{}
continue
}
}
// The result is verified or there's only one associated client and tenant.
if isVerified || (len(activeClients) == 1 && len(activeTenants) == 1) {
r = createResult(tenantId, clientId, clientSecret, isVerified, extraData, verificationErr)
break ClientLoop
}
}
}
}
if r == nil {
// Only include the clientId and tenantId if we're confident which one it is.
if len(activeClients) != 1 {
clientId = ""
}
if len(activeTenants) != 1 {
tenantId = ""
}
r = createResult(tenantId, clientId, clientSecret, false, nil, nil)
}
results = append(results, *r)
}
return results
}
func createResult(tenantId string, clientId string, clientSecret string, verified bool, extraData map[string]string, err error) *detectors.Result {
r := &detectors.Result{
DetectorType: detectorspb.DetectorType_Azure,
Raw: []byte(clientSecret),
ExtraData: extraData,
Verified: verified,
Redacted: clientSecret[:5] + "...",
}
r.SetVerificationError(err, clientSecret)
// Tenant ID is required for verification, but it may not always be present.
// e.g., ACR or Azure SQL use client id+secret without tenant.
if clientId != "" && tenantId != "" {
var sb strings.Builder
sb.WriteString(`{`)
sb.WriteString(`"clientSecret":"` + clientSecret + `"`)
sb.WriteString(`,"clientId":"` + clientId + `"`)
sb.WriteString(`,"tenantId":"` + tenantId + `"`)
sb.WriteString(`}`)
r.RawV2 = []byte(sb.String())
}
return r
}
func findSecretMatches(data string) map[string]struct{} {
uniqueMatches := make(map[string]struct{})
for _, match := range SecretPat.FindAllStringSubmatch(data, -1) {
uniqueMatches[match[1]] = struct{}{}
}
return uniqueMatches
}