@@ -3,8 +3,7 @@ package v2
3
3
import (
4
4
"context"
5
5
"errors"
6
- "fmt"
7
- "io"
6
+ logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context"
8
7
"net/http"
9
8
"regexp"
10
9
"strings"
32
31
defaultClient = common .SaneHttpClient ()
33
32
34
33
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)` )
35
- //clientSecretPat = regexp.MustCompile(`(?:[^a-zA-Z0-9_~.-]|\A)([a-zA-Z0-9_~.-]{3}\dQ~[a-zA-Z0-9_~.-]{31,34})(?:[^a-zA-Z0-9_~.-]|\z)|(?:secret|password| -p[ =]).{0,80}[^A-Za-z0-9!#$%&()*+,\-./:;<=>?@[\\\]^_{|}~]([A-Za-z0-9!#$%&()*+,\-./:;<=>?@[\\\]^_{|}~]{31,34})[^A-Za-z0-9!#$%&()*+,\-./:;<=>?@[\\\]^_{|}~]`)
36
34
)
37
35
38
36
func (s Scanner ) Version () int {
@@ -69,6 +67,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
69
67
}
70
68
71
69
func ProcessData (ctx context.Context , clientSecrets , clientIds , tenantIds map [string ]struct {}, verify bool , client * http.Client ) (results []detectors.Result ) {
70
+ logCtx := logContext .AddLogger (ctx )
72
71
invalidClientsForTenant := make (map [string ]map [string ]struct {})
73
72
74
73
SecretLoop:
@@ -96,10 +95,13 @@ SecretLoop:
96
95
}
97
96
98
97
if verify {
99
- if ! isValidTenant ( ctx , client , tenantId ) {
98
+ if ! azure_entra . TenantExists ( logCtx , client , tenantId ) {
100
99
// Tenant doesn't exist
101
100
delete (tenantIds , tenantId )
102
101
continue
102
+ } else {
103
+ // Ensure this isn't attempted as a clientId.
104
+ delete (clientIds , tenantId )
103
105
}
104
106
105
107
isVerified , extraData , verificationErr := serviceprincipal .VerifyCredentials (ctx , client , tenantId , clientId , clientSecret )
@@ -126,78 +128,6 @@ SecretLoop:
126
128
r = createResult (tenantId , clientId , clientSecret , isVerified , extraData , verificationErr )
127
129
break ClientLoop
128
130
}
129
-
130
- // The result may be valid for another client/tenant.
131
- //
132
- //
133
- //// https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow#request-an-access-token-with-a-client_secret
134
- //cred := auth.NewClientCredentialsConfig(clientId, clientSecret, tenantId)
135
- //token, err := cred.ServicePrincipalToken()
136
- //if err != nil {
137
- // // This can only fail if a value is empty, which shouldn't be possible.
138
- // continue
139
- //}
140
- //
141
- //err = token.Refresh()
142
- //if err != nil {
143
- // var refreshError adal.TokenRefreshError
144
- // if ok := errors.As(err, &refreshError); ok {
145
- // resp := refreshError.Response()
146
- // defer func() {
147
- // // Ensure we drain the response body so this connection can be reused.
148
- // _, _ = io.Copy(io.Discard, resp.Body)
149
- // _ = resp.Body.Close()
150
- // }()
151
- //
152
- // status := resp.StatusCode
153
- // errStr := refreshError.Error()
154
- // if status == 400 {
155
- // if strings.Contains(errStr, `"error_description":"AADSTS90002:`) {
156
- // // Tenant doesn't exist
157
- // delete(tenantIds, tenantId)
158
- // continue
159
- // } else if strings.Contains(errStr, `"error_description":"AADSTS700016:`) {
160
- // // Tenant is valid but the ClientID doesn't exist.
161
- // invalidTenantsForClientId[clientId] = append(invalidTenantsForClientId[clientId], tenantId)
162
- // continue
163
- // } else {
164
- // // Unexpected error.
165
- // r.SetVerificationError(refreshError, clientSecret)
166
- // break
167
- // }
168
- // } else if status == 401 {
169
- // // Tenant exists and the clientID is valid, but something is wrong.
170
- // if strings.Contains(errStr, `"error_description":"AADSTS7000215:`) {
171
- // // Secret is not valid.
172
- // setValidTenantIdForClientId(clientId, tenantId, tenantIds, invalidTenantsForClientId)
173
- // continue IdLoop
174
- // } else if strings.Contains(errStr, `"error_description":"AADSTS7000222:`) {
175
- // // The secret is expired.
176
- // setValidTenantIdForClientId(clientId, tenantId, tenantIds, invalidTenantsForClientId)
177
- // continue SecretLoop
178
- // } else {
179
- // // TODO: Investigate if it's possible to get a 401 with a valid id/secret.
180
- // r.SetVerificationError(refreshError, clientSecret)
181
- // break
182
- // }
183
- // } else {
184
- // // Unexpected status code.
185
- // r.SetVerificationError(refreshError, clientSecret)
186
- // break
187
- // }
188
- // } else {
189
- // // Unexpected error.
190
- // r.SetVerificationError(err, clientSecret)
191
- // break
192
- // }
193
- //} else {
194
- // r.Verified = true
195
- // r.ExtraData = map[string]string{
196
- // "token": token.OAuthToken(),
197
- // }
198
- // setValidTenantIdForClientId(clientId, tenantId, tenantIds, invalidTenantsForClientId)
199
- // break
200
- //}
201
131
}
202
132
}
203
133
}
@@ -243,32 +173,14 @@ func createResult(tenantId string, clientId string, clientSecret string, verifie
243
173
return r
244
174
}
245
175
246
- func isValidTenant (ctx context.Context , client * http.Client , tenant string ) bool {
247
- req , err := http .NewRequestWithContext (ctx , http .MethodGet , fmt .Sprintf ("https://login.microsoftonline.com/%s/.well-known/openid-configuration" , tenant ), nil )
248
- if err != nil {
249
- return false
250
- }
251
- res , err := client .Do (req )
252
- defer func () {
253
- _ , _ = io .Copy (io .Discard , res .Body )
254
- _ = res .Body .Close ()
255
- }()
256
-
257
- if res .StatusCode == 200 {
258
- return true
259
- } else if res .StatusCode == 400 {
260
- fmt .Printf ("Invalid tenant: %s\n " , tenant )
261
- return false
262
- } else {
263
- fmt .Printf ("[azure] Unexpected status code: %d for %s\n " , res .StatusCode , tenant )
264
- return false
265
- }
266
- }
267
-
268
176
func (s Scanner ) Type () detectorspb.DetectorType {
269
177
return detectorspb .DetectorType_Azure
270
178
}
271
179
180
+ func (s Scanner ) Description () string {
181
+ return serviceprincipal .Description
182
+ }
183
+
272
184
// region Helper methods.
273
185
func findSecretMatches (data string ) map [string ]struct {} {
274
186
uniqueMatches := make (map [string ]struct {})
0 commit comments