Skip to content

Find, verify, and analyze leaked credentials

Add AWS account information (#782)

Dustin Decker67e8df96a49b
diff --git a/pkg/detectors/aws/aws.go b/pkg/detectors/aws/aws.go
index b74cc6869..1d236cd74 100644
--- a/pkg/detectors/aws/aws.go
+++ b/pkg/detectors/aws/aws.go
@@ -5,6 +5,7 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
+ "encoding/json"
"fmt"
"net/http"
"regexp"
@@ -98,6 +99,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
if err != nil {
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
@@ -140,10 +142,19 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
res, err := client.Do(req)
if err == nil {
- // Response body is unused so close it immediately.
- res.Body.Close()
+
if res.StatusCode >= 200 && res.StatusCode < 300 {
- s1.Verified = true
+ 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,
+ }
+ }
+ 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) {
@@ -194,3 +205,16 @@ func awsCustomCleanResults(results []detectors.Result) []detectors.Result {
}
return out
}
+
+type identityRes struct {
+ GetCallerIdentityResponse struct {
+ GetCallerIdentityResult struct {
+ Account string `json:"Account"`
+ Arn string `json:"Arn"`
+ UserID string `json:"UserId"`
+ } `json:"GetCallerIdentityResult"`
+ ResponseMetadata struct {
+ RequestID string `json:"RequestId"`
+ } `json:"ResponseMetadata"`
+ } `json:"GetCallerIdentityResponse"`
+}
diff --git a/pkg/detectors/aws/aws_test.go b/pkg/detectors/aws/aws_test.go
index 4a44d0808..354e3fa27 100644
--- a/pkg/detectors/aws/aws_test.go
+++ b/pkg/detectors/aws/aws_test.go
@@ -10,11 +10,11 @@ import (
"testing"
"time"
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/go-cmp/cmp/cmpopts"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
-
- "github.com/kylelemons/godebug/pretty"
)
func TestAWS_FromChunk(t *testing.T) {
@@ -57,6 +57,11 @@ func TestAWS_FromChunk(t *testing.T) {
DetectorType: detectorspb.DetectorType_AWS,
Verified: true,
Redacted: "AKIAWARWQKZNHMZBLY4I",
+ ExtraData: map[string]string{
+ "account": "413504919130",
+ "arn": "arn:aws:iam::413504919130:root",
+ "user_id": "413504919130",
+ },
},
},
wantErr: false,
@@ -74,6 +79,7 @@ func TestAWS_FromChunk(t *testing.T) {
DetectorType: detectorspb.DetectorType_AWS,
Verified: false,
Redacted: "AKIAWARWQKZNHMZBLY4I",
+ ExtraData: nil,
},
},
wantErr: false,
@@ -102,6 +108,11 @@ func TestAWS_FromChunk(t *testing.T) {
DetectorType: detectorspb.DetectorType_AWS,
Verified: true,
Redacted: id,
+ ExtraData: map[string]string{
+ "account": "413504919130",
+ "arn": "arn:aws:iam::413504919130:root",
+ "user_id": "413504919130",
+ },
},
{
DetectorType: detectorspb.DetectorType_AWS,
@@ -135,6 +146,11 @@ func TestAWS_FromChunk(t *testing.T) {
DetectorType: detectorspb.DetectorType_AWS,
Verified: true,
Redacted: id,
+ ExtraData: map[string]string{
+ "account": "413504919130",
+ "arn": "arn:aws:iam::413504919130:root",
+ "user_id": "413504919130",
+ },
},
{
DetectorType: detectorspb.DetectorType_AWS,
@@ -174,9 +190,9 @@ func TestAWS_FromChunk(t *testing.T) {
if len(got[i].Raw) == 0 {
t.Fatalf("no raw secret present: \n %+v", got[i])
}
- got[i].Raw = nil
}
- if diff := pretty.Compare(got, tt.want); diff != "" {
+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "RawV2", "Raw")
+ if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
t.Errorf("AWS.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
}
})