What this change means
Atlas added serval to its detector catalog in this update.
| @@ -0,0 +1,213 @@ | ||
| 1 | added: package serval | |
| 2 | added: | |
| 3 | added: import ( | |
| 4 | added: "cmp" | |
| 5 | added: "context" | |
| 6 | added: "fmt" | |
| 7 | added: "hash/crc32" | |
| 8 | added: "io" | |
| 9 | added: "net/http" | |
| 10 | added: "net/url" | |
| 11 | added: "strings" | |
| 12 | added: | |
| 13 | added: regexp "github.com/wasilibs/go-re2" | |
| 14 | added: | |
| 15 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/common" | |
| 16 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | |
| 17 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" | |
| 18 | added: ) | |
| 19 | added: | |
| 20 | added: type Scanner struct { | |
| 21 | added: client *http.Client | |
| 22 | added: // endpoints overrides regionEndpoints in tests. | |
| 23 | added: endpoints []string | |
| 24 | added: } | |
| 25 | added: | |
| 26 | added: // Ensure the Scanner satisfies the interface at compile time. | |
| 27 | added: var _ detectors.Detector = (*Scanner)(nil) | |
| 28 | added: | |
| 29 | added: // regionEndpoints are the public API hosts of Serval's cloud regions. Regions | |
| 30 | added: // are isolated, so a key issued in one is unknown to the other and every | |
| 31 | added: // region must be tried before a key is called invalid. | |
| 32 | added: var regionEndpoints = []string{ | |
| 33 | added: "https://public.api.serval.com", | |
| 34 | added: "https://public.eu1.serval.com", | |
| 35 | added: } | |
| 36 | added: | |
| 37 | added: var ( | |
| 38 | added: defaultClient = common.SaneHttpClient() | |
| 39 | added: | |
| 40 | added: // Serval API key secrets are svl_secret_<tag>_<43 base62 random><6 base62 | |
| 41 | added: // CRC32 checksum>. The tag names the key type: pk public API, wk | |
| 42 | added: // self-hosted worker, mk Serval-managed worker. | |
| 43 | added: secretPat = regexp.MustCompile(`\b(svl_secret_(pk|wk|mk)_([0-9A-Za-z]{43})([0-9A-Za-z]{6}))\b`) | |
| 44 | added: | |
| 45 | added: // The client ID that pairs with a secret is the API key's UUID. | |
| 46 | added: idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"serval", "client"}) + | |
| 47 | added: `\b([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})\b`) | |
| 48 | added: ) | |
| 49 | added: | |
| 50 | added: var keyTypes = map[string]string{ | |
| 51 | added: "pk": "public_api", | |
| 52 | added: "wk": "worker", | |
| 53 | added: "mk": "managed_worker", | |
| 54 | added: } | |
| 55 | added: | |
| 56 | added: const base62Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
| 57 | added: | |
| 58 | added: // Keywords are used for efficiently pre-filtering chunks. | |
| 59 | added: // Use identifiers in the secret preferably, or the provider name. | |
| 60 | added: func (s Scanner) Keywords() []string { | |
| 61 | added: return []string{"svl_secret_"} | |
| 62 | added: } | |
| 63 | added: | |
| 64 | added: // FromData will find and optionally verify Serval API keys in a given set of bytes. | |
| 65 | added: func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | |
| 66 | added: dataStr := string(data) | |
| 67 | added: | |
| 68 | added: secrets := make(map[string]string) | |
| 69 | added: for _, match := range secretPat.FindAllStringSubmatch(dataStr, -1) { | |
| 70 | added: tag, random, checksum := match[2], match[3], match[4] | |
| 71 | added: // A failed checksum means the match is not a Serval-issued secret. | |
| 72 | added: if secretChecksum(tag, random) != checksum { | |
| 73 | added: continue | |
| 74 | added: } | |
| 75 | added: secrets[match[1]] = tag | |
| 76 | added: } | |
| 77 | added: if len(secrets) == 0 { | |
| 78 | added: return nil, nil | |
| 79 | added: } | |
| 80 | added: | |
| 81 | added: ids := make(map[string]struct{}) | |
| 82 | added: for _, match := range idPat.FindAllStringSubmatch(dataStr, -1) { | |
| 83 | added: ids[strings.ToLower(match[1])] = struct{}{} | |
| 84 | added: } | |
| 85 | added: | |
| 86 | added: for secret, tag := range secrets { | |
| 87 | added: // Verification needs the client ID, so a secret found without one | |
| 88 | added: // is still reported, unverified. | |
| 89 | added: if len(ids) == 0 { | |
| 90 | added: results = append(results, detectors.Result{ | |
| 91 | added: DetectorType: detector_typepb.DetectorType_Serval, | |
| 92 | added: Raw: []byte(secret), | |
| 93 | added: SecretParts: map[string]string{"client_secret": secret}, | |
| 94 | added: ExtraData: map[string]string{"key_type": keyTypes[tag]}, | |
| 95 | added: }) | |
| 96 | added: continue | |
| 97 | added: } | |
| 98 | added: | |
| 99 | added: for id := range ids { | |
| 100 | added: r := detectors.Result{ | |
| 101 | added: DetectorType: detector_typepb.DetectorType_Serval, | |
| 102 | added: Raw: []byte(secret), | |
| 103 | added: RawV2: []byte(id + ":" + secret), | |
| 104 | added: SecretParts: map[string]string{"client_id": id, "client_secret": secret}, | |
| 105 | added: ExtraData: map[string]string{"key_type": keyTypes[tag]}, | |
| 106 | added: } | |
| 107 | added: | |
| 108 | added: // Only public API keys can exchange credentials at the public | |
| 109 | added: // token endpoint. It rejects worker keys with the same 401 as a | |
| 110 | added: // wrong secret, so verifying them would report live keys as | |
| 111 | added: // invalid. | |
| 112 | added: if verify && tag == "pk" { | |
| 113 | added: client := cmp.Or(s.client, defaultClient) | |
| 114 | added: endpoints := s.endpoints | |
| 115 | added: if len(endpoints) == 0 { | |
| 116 | added: endpoints = regionEndpoints | |
| 117 | added: } | |
| 118 | added: isVerified, endpoint, vErr := verifyInRegions(ctx, client, endpoints, id, secret) | |
| 119 | added: r.Verified = isVerified | |
| 120 | added: if isVerified { | |
| 121 | added: r.ExtraData["endpoint"] = endpoint | |
| 122 | added: } | |
| 123 | added: r.SetVerificationError(vErr, secret) | |
| 124 | added: } | |
| 125 | added: | |
| 126 | added: results = append(results, r) | |
| 127 | added: | |
| 128 | added: // A secret belongs to exactly one client ID, so once a pair | |
| 129 | added: // verifies, trying the remaining IDs only spends rate limit. | |
| 130 | added: if r.Verified { | |
| 131 | added: break | |
| 132 | added: } | |
| 133 | added: } | |
| 134 | added: } | |
| 135 | added: | |
| 136 | added: return results, nil | |
| 137 | added: } | |
| 138 | added: | |
| 139 | added: // secretChecksum mirrors Serval's issuer: CRC32 (IEEE) of "<tag>_<random>", | |
| 140 | added: // written big-endian as six base62 digits. | |
| 141 | added: func secretChecksum(tag, random string) string { | |
| 142 | added: sum := crc32.ChecksumIEEE([]byte(tag + "_" + random)) | |
| 143 | added: out := make([]byte, 6) | |
| 144 | added: for i := len(out) - 1; i >= 0; i-- { | |
| 145 | added: out[i] = base62Alphabet[sum%62] | |
| 146 | added: sum /= 62 | |
| 147 | added: } | |
| 148 | added: return string(out) | |
| 149 | added: } | |
| 150 | added: | |
| 151 | added: // verifyInRegions tries each region until one verifies the key and returns | |
| 152 | added: // the endpoint that did. A 401 from one region does not override an | |
| 153 | added: // indeterminate error from another, since that region may own the key. | |
| 154 | added: func verifyInRegions(ctx context.Context, client *http.Client, endpoints []string, id, secret string) (bool, string, error) { | |
| 155 | added: var lastErr error | |
| 156 | added: for _, endpoint := range endpoints { | |
| 157 | added: isVerified, err := verifyKey(ctx, client, endpoint, id, secret) | |
| 158 | added: if isVerified { | |
| 159 | added: return true, endpoint, nil | |
| 160 | added: } | |
| 161 | added: if err != nil { | |
| 162 | added: lastErr = err | |
| 163 | added: } | |
| 164 | added: } | |
| 165 | added: return false, "", lastErr | |
| 166 | added: } | |
| 167 | added: | |
| 168 | added: // verifyKey exchanges the credentials for an access token with the OAuth2 | |
| 169 | added: // client credentials grant. The token in a successful response is discarded. | |
| 170 | added: // | |
| 171 | added: // The error is non-nil only when verification is indeterminate. Serval | |
| 172 | added: // answers 401 for any unknown, revoked, expired or mismatched key; every | |
| 173 | added: // other status (429 rate limit, 5xx) says nothing about the key. | |
| 174 | added: func verifyKey(ctx context.Context, client *http.Client, baseURL, id, secret string) (bool, error) { | |
| 175 | added: endpoint, err := url.JoinPath(baseURL, "/v2/auth/token") | |
| 176 | added: if err != nil { | |
| 177 | added: return false, err | |
| 178 | added: } | |
| 179 | added: | |
| 180 | added: form := url.Values{"grant_type": {"client_credentials"}} | |
| 181 | added: req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(form.Encode())) | |
| 182 | added: if err != nil { | |
| 183 | added: return false, err | |
| 184 | added: } | |
| 185 | added: req.SetBasicAuth(id, secret) | |
| 186 | added: req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
| 187 | added: | |
| 188 | added: res, err := client.Do(req) | |
| 189 | added: if err != nil { | |
| 190 | added: return false, err | |
| 191 | added: } | |
| 192 | added: defer func() { | |
| 193 | added: _, _ = io.Copy(io.Discard, res.Body) | |
| 194 | added: _ = res.Body.Close() | |
| 195 | added: }() | |
| 196 | added: | |
| 197 | added: switch res.StatusCode { | |
| 198 | added: case http.StatusOK: | |
| 199 | added: return true, nil | |
| 200 | added: case http.StatusUnauthorized: | |
| 201 | added: return false, nil | |
| 202 | added: default: | |
| 203 | added: return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | |
| 204 | added: } | |
| 205 | added: } | |
| 206 | added: | |
| 207 | added: func (s Scanner) Type() detector_typepb.DetectorType { | |
| 208 | added: return detector_typepb.DetectorType_Serval | |
| 209 | added: } | |
| 210 | added: | |
| 211 | added: func (s Scanner) Description() string { | |
| 212 | added: return "Serval is a workflow automation platform for IT and security teams. API keys pair a client ID with a secret and can call the Serval public API or run self-hosted workers." | |
| 213 | added: } | |