Skip to content

Find, verify, and analyze leaked credentials

Retry AWS verification 403s (#1757)

Cody Rose62ce9bac8bac
diff --git a/pkg/detectors/aws/aws.go b/pkg/detectors/aws/aws.go
index a0177de13..fa6702e07 100644
--- a/pkg/detectors/aws/aws.go
+++ b/pkg/detectors/aws/aws.go
@@ -118,120 +118,23 @@ func (s scanner) FromData(ctx context.Context, verify bool, data []byte) (result
}
if verify {
- // REQUEST VALUES.
- method := "GET"
- service := "sts"
- host := "sts.amazonaws.com"
- region := "us-east-1"
- endpoint := "https://sts.amazonaws.com"
- now := time.Now().UTC()
- datestamp := now.Format("20060102")
- amzDate := now.Format("20060102T150405Z0700")
-
- req, err := http.NewRequestWithContext(ctx, method, endpoint, nil)
- if err != nil {
+ verified, extraData, verificationErr := s.verifyMatch(ctx, resIDMatch, resSecretMatch, true)
+ s1.Verified = verified
+ s1.ExtraData = extraData
+ s1.VerificationError = verificationErr
+ }
+
+ if !s1.Verified {
+ // Unverified results that contain common test words are probably not secrets
+ if detectors.IsKnownFalsePositive(resSecretMatch, detectors.DefaultFalsePositives, true) {
continue
}
- req.Header.Set("Accept", "application/json")
-
- // TASK 1: CREATE A CANONICAL REQUEST.
- // http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
- canonicalURI := "/"
- canonicalHeaders := "host:" + host + "\n"
- signedHeaders := "host"
- algorithm := "AWS4-HMAC-SHA256"
- credentialScope := fmt.Sprintf("%s/%s/%s/aws4_request", datestamp, region, service)
-
- params := req.URL.Query()
- params.Add("Action", "GetCallerIdentity")
- params.Add("Version", "2011-06-15")
- params.Add("X-Amz-Algorithm", algorithm)
- params.Add("X-Amz-Credential", resIDMatch+"/"+credentialScope)
- params.Add("X-Amz-Date", amzDate)
- params.Add("X-Amz-Expires", "30")
- params.Add("X-Amz-SignedHeaders", signedHeaders)
-
- canonicalQuerystring := params.Encode()
- payloadHash := GetHash("") // empty payload
- canonicalRequest := method + "\n" + canonicalURI + "\n" + canonicalQuerystring + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + payloadHash
-
- // TASK 2: CREATE THE STRING TO SIGN.
- stringToSign := algorithm + "\n" + amzDate + "\n" + credentialScope + "\n" + GetHash(canonicalRequest)
-
- // TASK 3: CALCULATE THE SIGNATURE.
- // https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
- hash := GetHMAC([]byte(fmt.Sprintf("AWS4%s", resSecretMatch)), []byte(datestamp))
- hash = GetHMAC(hash, []byte(region))
- hash = GetHMAC(hash, []byte(service))
- hash = GetHMAC(hash, []byte("aws4_request"))
-
- signature2 := GetHMAC(hash, []byte(stringToSign)) // Get Signature HMAC SHA256
- signature := hex.EncodeToString(signature2)
-
- // TASK 4: ADD SIGNING INFORMATION TO THE REQUEST.
- params.Add("X-Amz-Signature", signature)
- req.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=utf-8")
- req.URL.RawQuery = params.Encode()
-
- client := s.verificationClient
- if client == nil {
- client = defaultVerificationClient
- }
- res, err := client.Do(req)
- if err == nil {
-
- if res.StatusCode >= 200 && res.StatusCode < 300 {
- identityInfo := identityRes{}
- err := json.NewDecoder(res.Body).Decode(&identityInfo)
- if err == nil {
- s1.Verified = true
- s1.ExtraData = map[string]string{
- "account": identityInfo.GetCallerIdentityResponse.GetCallerIdentityResult.Account,
- "user_id": identityInfo.GetCallerIdentityResponse.GetCallerIdentityResult.UserID,
- "arn": identityInfo.GetCallerIdentityResponse.GetCallerIdentityResult.Arn,
- }
- } else {
- s1.VerificationError = err
- }
- res.Body.Close()
- } else {
- // This function will check false positives for common test words, but also it will make sure the key appears "random" enough to be a real key.
- if detectors.IsKnownFalsePositive(resSecretMatch, detectors.DefaultFalsePositives, true) {
- continue
- }
-
- if res.StatusCode == 403 {
- var body awsErrorResponseBody
- err = json.NewDecoder(res.Body).Decode(&body)
- res.Body.Close()
- if err == nil {
- // All instances of the code I've seen in the wild are PascalCased but this check is
- // case-insensitive out of an abundance of caution
- if strings.EqualFold(body.Error.Code, "InvalidClientTokenId") {
- // determinate failure - nothing to do
- } else {
- // We see a surprising number of false-negative signature mismatch errors here
- // (The official SDK somehow elicits even more than just making the request
- // ourselves)
- s1.VerificationError = fmt.Errorf("request to %v returned status %d with an unexpected reason (%s: %s)", res.Request.URL, res.StatusCode, body.Error.Code, body.Error.Message)
- }
- } else {
- s1.VerificationError = fmt.Errorf("couldn't parse the sts response body (%v)", err)
- }
- } else {
- s1.VerificationError = fmt.Errorf("request to %v returned unexpected status %d", res.Request.URL, res.StatusCode)
- }
- }
- } else {
- s1.VerificationError = err
+ // Unverified results that look like hashes are probably not secrets
+ if falsePositiveSecretCheck.MatchString(resSecretMatch) {
+ continue
}
}
- // If the result is unverified and matches something like a git hash, don't include it in the results.
- if !s1.Verified && falsePositiveSecretCheck.MatchString(resSecretMatch) {
- continue
- }
-
results = append(results, s1)
// If we've found a verified match with this ID, we don't need to look for any more. So move on to the next ID.
if s1.Verified {
@@ -242,6 +145,125 @@ func (s scanner) FromData(ctx context.Context, verify bool, data []byte) (result
return awsCustomCleanResults(results), nil
}
+func (s scanner) verifyMatch(ctx context.Context, resIDMatch, resSecretMatch string, retryOn403 bool) (bool, map[string]string, error) {
+ // REQUEST VALUES.
+ method := "GET"
+ service := "sts"
+ host := "sts.amazonaws.com"
+ region := "us-east-1"
+ endpoint := "https://sts.amazonaws.com"
+ now := time.Now().UTC()
+ datestamp := now.Format("20060102")
+ amzDate := now.Format("20060102T150405Z0700")
+
+ req, err := http.NewRequestWithContext(ctx, method, endpoint, nil)
+ if err != nil {
+ return false, nil, err
+ }
+ req.Header.Set("Accept", "application/json")
+
+ // TASK 1: CREATE A CANONICAL REQUEST.
+ // http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
+ canonicalURI := "/"
+ canonicalHeaders := "host:" + host + "\n"
+ signedHeaders := "host"
+ algorithm := "AWS4-HMAC-SHA256"
+ credentialScope := fmt.Sprintf("%s/%s/%s/aws4_request", datestamp, region, service)
+
+ params := req.URL.Query()
+ params.Add("Action", "GetCallerIdentity")
+ params.Add("Version", "2011-06-15")
+ params.Add("X-Amz-Algorithm", algorithm)
+ params.Add("X-Amz-Credential", resIDMatch+"/"+credentialScope)
+ params.Add("X-Amz-Date", amzDate)
+ params.Add("X-Amz-Expires", "30")
+ params.Add("X-Amz-SignedHeaders", signedHeaders)
+
+ canonicalQuerystring := params.Encode()
+ payloadHash := GetHash("") // empty payload
+ canonicalRequest := method + "\n" + canonicalURI + "\n" + canonicalQuerystring + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + payloadHash
+
+ // TASK 2: CREATE THE STRING TO SIGN.
+ stringToSign := algorithm + "\n" + amzDate + "\n" + credentialScope + "\n" + GetHash(canonicalRequest)
+
+ // TASK 3: CALCULATE THE SIGNATURE.
+ // https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
+ hash := GetHMAC([]byte(fmt.Sprintf("AWS4%s", resSecretMatch)), []byte(datestamp))
+ hash = GetHMAC(hash, []byte(region))
+ hash = GetHMAC(hash, []byte(service))
+ hash = GetHMAC(hash, []byte("aws4_request"))
+
+ signature2 := GetHMAC(hash, []byte(stringToSign)) // Get Signature HMAC SHA256
+ signature := hex.EncodeToString(signature2)
+
+ // TASK 4: ADD SIGNING INFORMATION TO THE REQUEST.
+ params.Add("X-Amz-Signature", signature)
+ req.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=utf-8")
+ req.URL.RawQuery = params.Encode()
+
+ client := s.verificationClient
+ if client == nil {
+ client = defaultVerificationClient
+ }
+
+ res, err := client.Do(req)
+ if err == nil {
+
+ if res.StatusCode >= 200 && res.StatusCode < 300 {
+ identityInfo := identityRes{}
+ err := json.NewDecoder(res.Body).Decode(&identityInfo)
+ res.Body.Close()
+ if err == nil {
+ extraData := map[string]string{
+ "account": identityInfo.GetCallerIdentityResponse.GetCallerIdentityResult.Account,
+ "user_id": identityInfo.GetCallerIdentityResponse.GetCallerIdentityResult.UserID,
+ "arn": identityInfo.GetCallerIdentityResponse.GetCallerIdentityResult.Arn,
+ }
+ return true, extraData, nil
+ } else {
+ return false, nil, err
+ }
+ } else if res.StatusCode == 403 {
+ // Experimentation has indicated that if you make two GetCallerIdentity requests within five seconds that
+ // share a key ID but are signed with different secrets the second one will be rejected with a 403 that
+ // carries a SignatureDoesNotMatch code in its body. This happens even if the second ID-secret pair is
+ // valid. Since this is exactly our access pattern, we need to work around it.
+ //
+ // Fortunately, experimentation has also revealed a workaround: simply resubmit the second request. The
+ // response to the resubmission will be as expected. But there's a caveat: You can't have closed the body of
+ // the response to the original second request, or read to its end, or the resubmission will also yield a
+ // SignatureDoesNotMatch. For this reason, we have to re-request all 403s. We can't re-request only
+ // SignatureDoesNotMatch responses, because we can only tell whether a given 403 is a SignatureDoesNotMatch
+ // after decoding its response body, which requires reading the entire response body, which disables the
+ // workaround.
+ //
+ // We are clearly deep in the guts of AWS implementation details here, so this all might change with no
+ // notice. If you're here because something in this detector broke, you have my condolences.
+ if retryOn403 {
+ return s.verifyMatch(ctx, resIDMatch, resSecretMatch, false)
+ }
+ var body awsErrorResponseBody
+ err = json.NewDecoder(res.Body).Decode(&body)
+ res.Body.Close()
+ if err == nil {
+ // All instances of the code I've seen in the wild are PascalCased but this check is
+ // case-insensitive out of an abundance of caution
+ if strings.EqualFold(body.Error.Code, "InvalidClientTokenId") {
+ return false, nil, nil
+ } else {
+ return false, nil, fmt.Errorf("request to %v returned status %d with an unexpected reason (%s: %s)", res.Request.URL, res.StatusCode, body.Error.Code, body.Error.Message)
+ }
+ } else {
+ return false, nil, fmt.Errorf("couldn't parse the sts response body (%v)", err)
+ }
+ } else {
+ return false, nil, fmt.Errorf("request to %v returned unexpected status %d", res.Request.URL, res.StatusCode)
+ }
+ } else {
+ return false, nil, err
+ }
+}
+
func awsCustomCleanResults(results []detectors.Result) []detectors.Result {
if len(results) == 0 {
return results
diff --git a/pkg/detectors/aws/aws_test.go b/pkg/detectors/aws/aws_test.go
index d72e367ec..8c7799b78 100644
--- a/pkg/detectors/aws/aws_test.go
+++ b/pkg/detectors/aws/aws_test.go
@@ -248,6 +248,28 @@ func TestAWS_FromChunk(t *testing.T) {
wantErr: false,
wantVerificationError: true,
},
+ {
+ name: "verified secret checked directly after unverified secret with same key id",
+ s: scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("%s\n%s\n%s", inactiveSecret, id, secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_AWS,
+ Verified: true,
+ Redacted: "AKIASP2TPHJSQH3FJRUX",
+ ExtraData: map[string]string{
+ "account": "171436882533",
+ "arn": "arn:aws:iam::171436882533:user/canarytokens.com@@4dxkh0pdeop3bzu9zx5wob793",
+ "user_id": "AIDASP2TPHJSUFRSTTZX4",
+ },
+ },
+ },
+ wantErr: false,
+ },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {