| @@ -20,12 +20,18 @@ type Scanner struct { |
| 20 | | 20 | |
| 21 | // Ensure the Scanner satisfies the interface at compile time. | 21 | // Ensure the Scanner satisfies the interface at compile time. |
| 22 | var _ detectors.Detector = (*Scanner)(nil) | 22 | var _ detectors.Detector = (*Scanner)(nil) |
| | 23 | +added: var _ detectors.CustomResultsCleaner = (*Scanner)(nil) |
| 23 | | 24 | |
| 24 | var ( | 25 | var ( |
| 25 | defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses | 26 | defaultClient = detectors.DetectorHttpClientWithNoLocalAddresses |
| 26 | domainPat = regexp.MustCompile(`\b[a-z0-9-]{1,40}\.okta(?:preview|-emea){0,1}\.com\b`) | 27 | domainPat = regexp.MustCompile(`\b[a-z0-9-]{1,40}\.okta(?:preview|-emea){0,1}\.com\b`) |
| 27 | tokenPat = regexp.MustCompile(`\b00[a-zA-Z0-9_-]{40}\b`) | 28 | tokenPat = regexp.MustCompile(`\b00[a-zA-Z0-9_-]{40}\b`) |
| 28 | −removed: // TODO: Oauth client secrets | 29 | +added: |
| | 30 | +added: // Okta app/client IDs: "0oa" + 17 base62 characters. |
| | 31 | +added: oauthClientIDPat = regexp.MustCompile(`\b0oa[a-zA-Z0-9]{17}\b`) |
| | 32 | +added: // Delimiters consume a char instead of using \b, since \b breaks on secrets |
| | 33 | +added: // starting/ending in '-' (a non-word char that's also part of the charset). |
| | 34 | +added: oauthClientSecretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"client_secret", "clientsecret", "client-secret"}) + `(?:^|[^a-zA-Z0-9_-])([a-zA-Z0-9_-]{40,64})(?:[^a-zA-Z0-9_-]|$)`) |
| 29 | ) | 35 | ) |
| 30 | | 36 | |
| 31 | // Keywords are used for efficiently pre-filtering chunks. | 37 | // Keywords are used for efficiently pre-filtering chunks. |
| @@ -38,7 +44,10 @@ func (s Scanner) Keywords() []string { |
| 38 | func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | 44 | func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { |
| 39 | dataStr := string(data) | 45 | dataStr := string(data) |
| 40 | | 46 | |
| 41 | −removed: var uniqueTokens, uniqueDomains = make(map[string]struct{}), make(map[string]struct{}) | 47 | +added: uniqueTokens := make(map[string]struct{}) |
| | 48 | +added: uniqueDomains := make(map[string]struct{}) |
| | 49 | +added: uniqueClientIDs := make(map[string]struct{}) |
| | 50 | +added: uniqueClientSecrets := make(map[string]struct{}) |
| 42 | | 51 | |
| 43 | for _, matches := range tokenPat.FindAllStringSubmatch(dataStr, -1) { | 52 | for _, matches := range tokenPat.FindAllStringSubmatch(dataStr, -1) { |
| 44 | uniqueTokens[matches[0]] = struct{}{} | 53 | uniqueTokens[matches[0]] = struct{}{} |
| @@ -48,11 +57,20 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result |
| 48 | uniqueDomains[matches[0]] = struct{}{} | 57 | uniqueDomains[matches[0]] = struct{}{} |
| 49 | } | 58 | } |
| 50 | | 59 | |
| | 60 | +added: for _, matches := range oauthClientIDPat.FindAllStringSubmatch(dataStr, -1) { |
| | 61 | +added: uniqueClientIDs[matches[0]] = struct{}{} |
| | 62 | +added: } |
| | 63 | +added: |
| | 64 | +added: for _, matches := range oauthClientSecretPat.FindAllStringSubmatch(dataStr, -1) { |
| | 65 | +added: uniqueClientSecrets[matches[1]] = struct{}{} |
| | 66 | +added: } |
| | 67 | +added: |
| 51 | for token := range uniqueTokens { | 68 | for token := range uniqueTokens { |
| 52 | for domain := range uniqueDomains { | 69 | for domain := range uniqueDomains { |
| 53 | s1 := detectors.Result{ | 70 | s1 := detectors.Result{ |
| 54 | DetectorType: detector_typepb.DetectorType_Okta, | 71 | DetectorType: detector_typepb.DetectorType_Okta, |
| 55 | Raw: []byte(token), | 72 | Raw: []byte(token), |
| | 73 | +added: Redacted: fmt.Sprintf("%s:%s...%s", domain, token[:4], token[len(token)-4:]), |
| 56 | SecretParts: map[string]string{ | 74 | SecretParts: map[string]string{ |
| 57 | "domain": domain, | 75 | "domain": domain, |
| 58 | "token": token, | 76 | "token": token, |
| @@ -75,6 +93,37 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result |
| 75 | } | 93 | } |
| 76 | } | 94 | } |
| 77 | | 95 | |
| | 96 | +added: for clientID := range uniqueClientIDs { |
| | 97 | +added: for clientSecret := range uniqueClientSecrets { |
| | 98 | +added: for domain := range uniqueDomains { |
| | 99 | +added: s2 := detectors.Result{ |
| | 100 | +added: DetectorType: detector_typepb.DetectorType_Okta, |
| | 101 | +added: Raw: []byte(clientSecret), |
| | 102 | +added: Redacted: fmt.Sprintf("%s:%s", domain, clientID), |
| | 103 | +added: SecretParts: map[string]string{ |
| | 104 | +added: "domain": domain, |
| | 105 | +added: "client_id": clientID, |
| | 106 | +added: "client_secret": clientSecret, |
| | 107 | +added: }, |
| | 108 | +added: RawV2: []byte(fmt.Sprintf("%s:%s:%s", domain, clientID, clientSecret)), |
| | 109 | +added: } |
| | 110 | +added: |
| | 111 | +added: if verify { |
| | 112 | +added: client := s.client |
| | 113 | +added: if client == nil { |
| | 114 | +added: client = defaultClient |
| | 115 | +added: } |
| | 116 | +added: |
| | 117 | +added: isVerified, verificationErr := verifyOktaOAuthClientCredentials(ctx, client, domain, clientID, clientSecret) |
| | 118 | +added: s2.Verified = isVerified |
| | 119 | +added: s2.SetVerificationError(verificationErr) |
| | 120 | +added: } |
| | 121 | +added: |
| | 122 | +added: results = append(results, s2) |
| | 123 | +added: } |
| | 124 | +added: } |
| | 125 | +added: } |
| | 126 | +added: |
| 78 | return | 127 | return |
| 79 | } | 128 | } |
| @@ -111,6 +160,89 @@ func verifyOktaToken(ctx context.Context, client *http.Client, domain, token str |
| 111 | } | 160 | } |
| 112 | } | 161 | } |
| 113 | | 162 | |
| | 163 | +added: func verifyOktaOAuthClientCredentials(ctx context.Context, client *http.Client, domain, clientID, clientSecret string) (bool, error) { |
| | 164 | +added: // curl -v -X POST \ |
| | 165 | +added: // -H "Accept: application/json" \ |
| | 166 | +added: // -H "Content-Type: application/x-www-form-urlencoded" \ |
| | 167 | +added: // -u "clientID:clientSecret" \ |
| | 168 | +added: // -d "grant_type=client_credentials" \ |
| | 169 | +added: // "https://subdomain.okta.com/oauth2/default/v1/token" |
| | 170 | +added: |
| | 171 | +added: const body = "grant_type=client_credentials" |
| | 172 | +added: |
| | 173 | +added: url := fmt.Sprintf("https://%s/oauth2/default/v1/token", domain) |
| | 174 | +added: req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(body)) |
| | 175 | +added: if err != nil { |
| | 176 | +added: return false, err |
| | 177 | +added: } |
| | 178 | +added: req.SetBasicAuth(clientID, clientSecret) |
| | 179 | +added: req.Header.Set("Accept", "application/json") |
| | 180 | +added: req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| | 181 | +added: |
| | 182 | +added: resp, err := client.Do(req) |
| | 183 | +added: if err != nil { |
| | 184 | +added: return false, err |
| | 185 | +added: } |
| | 186 | +added: defer func() { _ = resp.Body.Close() }() |
| | 187 | +added: |
| | 188 | +added: switch resp.StatusCode { |
| | 189 | +added: case http.StatusOK: |
| | 190 | +added: respBody, err := io.ReadAll(resp.Body) |
| | 191 | +added: if err != nil { |
| | 192 | +added: return false, err |
| | 193 | +added: } |
| | 194 | +added: return strings.Contains(string(respBody), "\"access_token\""), nil |
| | 195 | +added: case http.StatusBadRequest: |
| | 196 | +added: respBody, err := io.ReadAll(resp.Body) |
| | 197 | +added: if err != nil { |
| | 198 | +added: return false, err |
| | 199 | +added: } |
| | 200 | +added: // Okta authenticates the client before grant-type checks, so "unauthorized_client" still confirms the secret. |
| | 201 | +added: if strings.Contains(string(respBody), "unauthorized_client") { |
| | 202 | +added: return true, nil |
| | 203 | +added: } |
| | 204 | +added: if strings.Contains(string(respBody), "invalid_client") { |
| | 205 | +added: return false, nil |
| | 206 | +added: } |
| | 207 | +added: // Other 400s (e.g. invalid_scope) say nothing about the secret; surface as an error. |
| | 208 | +added: return false, fmt.Errorf("response body missing expected keyword") |
| | 209 | +added: case http.StatusUnauthorized: |
| | 210 | +added: return false, nil |
| | 211 | +added: default: |
| | 212 | +added: return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| | 213 | +added: } |
| | 214 | +added: } |
| | 215 | +added: |
| | 216 | +added: // CleanResults keeps the best (preferably verified) result per distinct secret |
| | 217 | +added: // identity (Redacted), rather than collapsing unrelated SSWS and OAuth findings |
| | 218 | +added: // down to a single arbitrary result the way detectors.CleanResults would. |
| | 219 | +added: func (s Scanner) CleanResults(results []detectors.Result, _ bool) []detectors.Result { |
| | 220 | +added: if len(results) == 0 { |
| | 221 | +added: return results |
| | 222 | +added: } |
| | 223 | +added: |
| | 224 | +added: byIdentity := make(map[string]detectors.Result, len(results)) |
| | 225 | +added: for _, r := range results { |
| | 226 | +added: if r.Verified { |
| | 227 | +added: byIdentity[r.Redacted] = r |
| | 228 | +added: continue |
| | 229 | +added: } |
| | 230 | +added: if _, exists := byIdentity[r.Redacted]; !exists { |
| | 231 | +added: byIdentity[r.Redacted] = r |
| | 232 | +added: } |
| | 233 | +added: } |
| | 234 | +added: |
| | 235 | +added: cleaned := make([]detectors.Result, 0, len(byIdentity)) |
| | 236 | +added: for _, r := range byIdentity { |
| | 237 | +added: cleaned = append(cleaned, r) |
| | 238 | +added: } |
| | 239 | +added: return cleaned |
| | 240 | +added: } |
| | 241 | +added: |
| | 242 | +added: func (s Scanner) ShouldCleanResultsIrrespectiveOfConfiguration() bool { |
| | 243 | +added: return false |
| | 244 | +added: } |
| | 245 | +added: |
| 114 | func (s Scanner) Type() detector_typepb.DetectorType { | 246 | func (s Scanner) Type() detector_typepb.DetectorType { |
| 115 | return detector_typepb.DetectorType_Okta | 247 | return detector_typepb.DetectorType_Okta |
| 116 | } | 248 | } |