Who has never used the AWS CLI? Almost everyone reaches for it to work with AWS outside the Management Console. It’s a convenient way to check workloads and manage services, but have you ever thought about why it matters to know which activity comes from it? When attackers compromise AWS keys, they commonly continue the attack from the CLI, making API calls to discover which services exist in the target. Once they find something useful, they move laterally or escalate privileges to gain higher access within the environment. So how can we tell whether such activity is legitimate? First, we need to understand how CloudTrail records AWS CLI activity.
Checking the Caller Identity
A basic first command is aws sts get-caller-identity, which shows which IAM user or role the credentials belong to.
{
"UserId": "AIDACFN343DLHU2Z7S6HJ",
"Account": "111122223333",
"Arn": "arn:aws:iam::111122223333:user/Administrator"
}
The Arn alone tells us whether the credentials belong to an IAM user or a temporary identity (an assumed role), and what it’s called. The name can hint at the attached permissions, so an attacker gets lucky when the target uses an obvious name like terraform-admin. In CloudTrail, the same call looks like this.
{
"eventVersion": "1.11",
"userIdentity": {
"type": "IAMUser",
"principalId": "AIDACFN343DLHU2Z7S6HJ",
"arn": "arn:aws:iam::111122223333:user/Administrator",
"accountId": "111122223333",
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"userName": "Administrator"
},
"eventTime": "2026-11-21T12:02:41Z",
"eventSource": "sts.amazonaws.com",
"eventName": "GetCallerIdentity",
"awsRegion": "us-east-1",
"sourceIPAddress": "203.0.113.10",
"userAgent": "aws-cli/2.42.1 md/awscrt#0.38.1 ua/2.1 os/macos#27.2.0 md/arch#arm64 lang/python#3.15.2 md/pyimpl#CPython m/n,Z,b,E cfg/retry-mode#standard md/installer#source sid/2dc6b2fc5fe1 md/prompt#off md/command#sts.get-caller-identity",
"requestParameters": null,
"responseElements": null,
"additionalEventData": {
"ExtendedRequestId": "dHcRIRa9ReBRcI+IMwXCRE2xtY8hfpDyZrrgCUgbgouUZ0rk",
"RequestDetails": {
"endpointType": "regional",
"awsServingRegion": "us-east-1"
}
},
"requestID": "085cfb5c-e7f5-4d1b-8c92-9dc0fa2d985c",
"eventID": "84ad972c-f4c3-4553-aab4-30b91ded39c1",
"readOnly": true,
"eventType": "AwsApiCall",
"managementEvent": true,
"recipientAccountId": "111122223333",
"eventCategory": "Management",
"tlsDetails": {
"tlsVersion": "TLSv1.3",
"cipherSuite": "TLS_AES_128_GCM_SHA256",
"clientProvidedHostHeader": "sts.us-east-1.amazonaws.com",
"keyExchange": "X25519MLKEM768"
}
}
Reading the User Agent
The userAgent field stands out, because it shows the request came from aws-cli. Does it stay there for other actions too? Let’s run another command.
aws ec2 describe-instances
This account has no instances, so the output is short.
{
"Reservations": []
}
Even with nothing to show, CloudTrail still records the call. Here is the resulting event, trimmed to the fields that matter.
{
"userIdentity": {
"type": "IAMUser",
"arn": "arn:aws:iam::111122223333:user/Administrator"
},
"eventTime": "2026-11-21T12:26:37Z",
"eventSource": "ec2.amazonaws.com",
"eventName": "DescribeInstances",
"sourceIPAddress": "203.0.113.10",
"userAgent": "aws-cli/2.42.1 md/awscrt#0.38.1 ua/2.1 os/macos#27.2.0 md/arch#arm64 lang/python#3.15.2 md/pyimpl#CPython m/E,C,Z,b,n cfg/retry-mode#standard md/installer#source sid/2dc6b2fc5fe1 md/prompt#off md/command#ec2.describe-instances"
}
What the String Contains
The userAgent field is still there. Before we go further, it helps to read what is in it. Everything after aws-cli/2.42.1 is a list of short facts, separated by spaces.
| Part | Meaning |
|---|---|
aws-cli/2.42.1 | the tool and its version |
ua/2.1 | the version of the user agent format itself |
os/macos#27.2.0, md/arch#arm64 | the operating system and the CPU |
lang/python#3.15.2, md/pyimpl#CPython | the runtime |
md/awscrt#0.38.1 | the AWS Common Runtime, used when the CLI is built with it |
m/E,C,Z,b,n | the features that were on for this call |
cfg/retry-mode#standard | the retry configuration |
md/installer#source | the CLI was installed from source |
sid/2dc6b2fc5fe1 | a session ID, shared by every call from the same CLI process |
md/prompt#off | the CLI’s telemetry prompt is turned off |
md/command#ec2.describe-instances | the exact command that ran |
The m/ part says the most. Each letter is a feature that AWS recorded for the call, taken from a table that ships inside the AWS CLI. n means the credentials came from a profile, E that the standard retry mode was used, C that a paginator was involved, and Z with b that the flexible checksums were enabled. Other letters cover other features, like waiters (B), gzip request compression (L) and SSO as a credential source (r for a profile, s for the cache).
Two parts are less obvious. sid/ ties calls from one CLI process together, so a series of calls spread over a few minutes still looks like one session. And md/command# shows what was typed rather than what AWS calls it, which is how a high-level command like aws s3 cp shows up as s3.cp on the PutObject it makes. That PutObject is a data event, so it only shows up in CloudTrail when S3 data events are turned on for the bucket.
One caveat before we move on. All of that is the AWS CLI v2 format. Version 1 sends a shorter string in the older style, close to aws-cli/1.29.0 Python/3.11.6 Darwin/23.0.0 botocore/1.31.0, with no m/, sid/ or md/ parts at all, so the table above doesn’t apply to it.
None of this is required by AWS. The client builds the whole string and sends it along, which is what the next section is about.
Spoofing the User Agent
How would you spoof it with the AWS CLI? Years ago, someone opened an issue asking for a configurable user agent, but the resulting option doesn’t replace the value. It only appends to it, as the source code shows.
...
if HAS_CRT:
base += f' awscrt/{self._get_crt_version()}'
if os.environ.get('AWS_EXECUTION_ENV') is not None:
base += ' exec-env/{}'.format(os.environ.get('AWS_EXECUTION_ENV'))
if self.user_agent_extra:
base += f' {self.user_agent_extra}'
...
In other words, replacing the user agent means overriding the code that builds it, or intercepting the request on its way out. Hacking the Cloud shows how with Burp Suite. Here, we’ll do the same thing with mitmproxy.
Rewriting It with mitmproxy
First, start mitmdump as a proxy that rewrites the User-Agent header.
mitmdump -p 8089 -H '|~q|User-Agent|Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36'
Then point the AWS CLI at the proxy and run the command as usual.
HTTPS_PROXY=http://127.0.0.1:8089 \
AWS_CA_BUNDLE=~/.mitmproxy/mitmproxy-ca-cert.pem \
aws sts get-caller-identity
It prints the same JSON as the first command. Only CloudTrail knows the difference.
Apart from the user agent, the event matches the earlier GetCallerIdentity one.
{
"userIdentity": {
"type": "IAMUser",
"arn": "arn:aws:iam::111122223333:user/Administrator"
},
"eventTime": "2026-11-21T12:46:04Z",
"eventSource": "sts.amazonaws.com",
"eventName": "GetCallerIdentity",
"sourceIPAddress": "203.0.113.10",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36"
}
We can no longer rely on userAgent alone, because it can be changed to avoid detection. A value starting with aws-cli/ does suggest someone used the AWS CLI, but since that value is easy to spoof, it isn’t enough on its own to detect CLI usage.
Looking at the TLS Details
If the user agent can lie, what about the connection itself? Each event above also comes with a tlsDetails block. CloudTrail fills it in from the TLS handshake between the client and AWS, and since every TLS library handles that handshake a little differently, maybe it can tell us which tool sent the request.
The field to watch here is keyExchange. I compared it across a few clients. The AWS CLI rows come from GetCallerIdentity, and the Chrome row comes from a call my browser made for the console’s Config dashboard.
| Client | keyExchange |
|---|---|
| AWS CLI | X25519MLKEM768 |
| AWS CLI through mitmproxy | X25519MLKEM768 |
| Chrome, from the Management Console | X25519MLKEM768 |
NOTE
curl on macOS goes another way. The same GetCallerIdentity call came back with secp256r1 instead, because the curl that ships with macOS is built against LibreSSL rather than OpenSSL, and that build doesn’t offer the newer post-quantum key exchange. The handshake settled on the older elliptic curve. The CloudTrail documentation files secp256r1 under TLS 1.2, but this connection was still TLSv1.3, which matters before you build a rule on it.
Every client used TLSv1.3 with TLS_AES_128_GCM_SHA256, so keyExchange was the only thing that changed. Now look at the mitmproxy row. Once the request goes through the proxy, it’s mitmproxy that talks to AWS, and it ends up with the same value as the AWS CLI. Even Chrome matches.
The signal stops there. Tools built on the same AWS library don’t always agree either. A Boto3 script on a Linux machine showed secp256r1, while the AWS CLI on my Mac showed X25519MLKEM768. Some services don’t record keyExchange at all, like CloudTrail itself and the sign-in service. On top of that, the value comes from whatever TLS library the client uses, so an attacker can change it just by switching to another one.
tlsDetails can tell us about the TLS library, but not the tool. It might catch a careless spoof, like a browser user agent sent from curl, but that is all it does.
Reading the Access Key
Another field worth looking at is userIdentity.accessKeyId. Its prefix tells us what kind of credential signed the request. Outside CloudShell, it starts with either AKIA for a long-term IAM user key or ASIA for temporary credentials. In CloudShell, it always starts with ASIA. So can we use it to detect AWS CLI usage? Not really, because the prefix describes the credential, not the tool behind it.
An AKIA key works the same way from the AWS CLI, an SDK, Terraform, or even curl, since curl can sign AWS requests by itself.
curl --aws-sigv4 "aws:amz:us-east-1:sts" \
--user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
-d "Action=GetCallerIdentity&Version=2011-06-15" \
https://sts.us-east-1.amazonaws.com/
It prints XML instead of JSON, but the same caller identity.
<GetCallerIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
<GetCallerIdentityResult>
<Arn>arn:aws:iam::111122223333:user/Administrator</Arn>
<UserId>AIDACFN343DLHU2Z7S6HJ</UserId>
<Account>111122223333</Account>
</GetCallerIdentityResult>
<ResponseMetadata>
<RequestId>EXAMPLE-REQUEST-ID</RequestId>
</ResponseMetadata>
</GetCallerIdentityResponse>
The event still carries the same accessKeyId as the AWS CLI call. Only userAgent and tlsDetails.keyExchange are different.
{
"userIdentity": {
"type": "IAMUser",
"arn": "arn:aws:iam::111122223333:user/Administrator",
"accessKeyId": "AKIAIOSFODNN7EXAMPLE"
},
"eventName": "GetCallerIdentity",
"userAgent": "curl/8.7.1",
"tlsDetails": {
"tlsVersion": "TLSv1.3",
"cipherSuite": "TLS_AES_128_GCM_SHA256",
"keyExchange": "secp256r1"
}
}
ASIA tells us even less. Temporary credentials sit behind the Management Console, CloudShell, the AWS CLI with SSO or an assumed role, CI pipelines, and any Lambda function or EC2 instance that calls AWS with its role.
Spotting Console Sessions
Still, the prefix gives us one useful hint. The Management Console never signs requests with an AKIA key, so when we see one, the call is programmatic. For ASIA, it’s not that simple, because the console also uses temporary credentials. So how do we know if an ASIA call came from the console?
CloudTrail adds sessionCredentialFromConsole with the value true to most requests made with console credentials. It’s a top-level field, not part of userIdentity, and it only shows up when the value is true. Here’s one of the calls my browser made for the console’s Config dashboard.
{
"userIdentity": {
"type": "Root",
"arn": "arn:aws:iam::111122223333:root"
},
"eventSource": "config.amazonaws.com",
"eventName": "DescribeConfigurationRecorders",
"sourceIPAddress": "203.0.113.10",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36",
"sessionCredentialFromConsole": "true",
"tlsDetails": {
"tlsVersion": "TLSv1.3",
"cipherSuite": "TLS_AES_128_GCM_SHA256",
"keyExchange": "X25519MLKEM768"
}
}
But not every console call has it. When I opened the IAM pages in the console, the calls came without sessionCredentialFromConsole, and without tlsDetails either. How can we tell they came from the console? We can look at the session. Every console session starts with a ConsoleLogin event.
{
"userIdentity": {
"type": "Root",
"arn": "arn:aws:iam::111122223333:root"
},
"eventTime": "2026-11-21T11:36:05Z",
"eventSource": "signin.amazonaws.com",
"eventName": "ConsoleLogin",
"eventType": "AwsConsoleSignIn",
"additionalEventData": {
"MFAUsed": "Yes"
}
}
Now look at one of the IAM calls made later in that session.
{
"userIdentity": {
"type": "Root",
"arn": "arn:aws:iam::111122223333:root",
"sessionContext": {
"attributes": {
"creationDate": "2026-11-21T11:36:05Z",
"mfaAuthenticated": "true"
}
}
},
"eventTime": "2026-11-21T12:15:32Z",
"eventSource": "iam.amazonaws.com",
"eventName": "ListAccessKeys"
}
Both say 11:36:05. That’s when the session was created, so we can link the IAM call back to that sign-in, even though the call itself doesn’t say it came from the console.
The field doesn’t always mean someone is clicking in a browser, though. A few calls had sessionCredentialFromConsole set to true, but their user agent was aws-sdk-go/1.55.8 (go1.27.3; linux; amd64) exec-env/AWS_Lambda_provided.al2023, and they came from AWS IP addresses. They turned out to come from CloudShell, and there is a sample of one of those events in the next section. So the field tells us the console session was used, not that the request came from a browser.
Both checks work better together. A call belongs to a console session when it has sessionCredentialFromConsole, or when its creationDate matches a ConsoleLogin. Just remember that the second check only works when the ConsoleLogin event is inside the time range you’re looking at. Anything else signed with an ASIA key came from somewhere outside the console.
Here is how that looks in CloudWatch Logs Insights, which needs CloudTrail to deliver events to a log group first. The first query lists the programmatic calls, with the console flag and the session start for each one.
NOTE
A few limits apply to everything below. Event history keeps management events for 90 days, which is enough to explore but not to backfill a year of history. Data events, like the PutObject behind aws s3 cp, only exist if a trail has them enabled and only where you enabled them. Logs Insights also bills by the amount of data it scans, which is why its own guidance is to pick the log group you need and the narrowest time range that answers the question.
fields @timestamp, eventSource, eventName, userIdentity.arn as principal,
sessionCredentialFromConsole as from_console,
userIdentity.sessionContext.attributes.creationDate as session_start
| filter userIdentity.accessKeyId like /^ASIA/
| filter not ispresent(userIdentity.invokedBy)
| sort @timestamp desc
| limit 100
It returns a table with the flag and the session start filled in.
| @timestamp | eventSource | eventName | principal | from_console | session_start |
|---|---|---|---|---|---|
| 2026-11-21 14:02:19.000 | sts.amazonaws.com | GetCallerIdentity | arn:aws:iam::111122223333:root | true | 2026-11-21T11:36:05Z |
| 2026-11-21 12:15:32.000 | iam.amazonaws.com | ListAccessKeys | arn:aws:iam::111122223333:root | 2026-11-21T11:36:05Z |
The second row is one of the IAM console calls, with nothing in the flag column. For rows like that, the second query lists the sign-ins to compare against.
filter eventName = "ConsoleLogin"
| fields @timestamp, userIdentity.arn as principal, eventTime as login_time
| sort @timestamp desc
That one comes back with the sign-in time for each session.
| @timestamp | principal | login_time |
|---|---|---|
| 2026-11-21 11:36:05.000 | arn:aws:iam::111122223333:root | 2026-11-21T11:36:05Z |
If a call has no flag, but its session_start matches a login_time for the same principal, it belongs to that console session. Everything else signed with an ASIA key came from outside the console. CloudWatch can’t join these two on the nested fields we need, so it stays two queries and a comparison. I tried the join and it doesn’t run.
I ran the same logic over 24 hours of my own events. invokedBy removed 278 calls made by AWS services, and every ASIA call that was left landed in a console group. 408 came from browser console sessions, 4 from CloudShell, and 25 from IAM console pages that only the creationDate check caught. When I tried the federation path later, its GetSigninToken and ConsoleLogin sat five seconds apart, so the comparison needs a little room rather than an exact match.
Excluding CloudShell
What about CloudShell? It runs the AWS CLI with the credentials of the console session that opened it, so its calls carry the same console markers even though someone is typing CLI commands. An attacker who already has console access could use it to blend in.
Opening CloudShell leaves its own trail, though. When I opened it, the console made a quick burst of calls to cloudshell.amazonaws.com, like StartEnvironment, CreateSession, and PutCredentials. All of them came from my browser, with sessionCredentialFromConsole and the same creationDate as my console sign-in. PutCredentials is the interesting one, because that’s how the console hands its credentials over to the CloudShell environment. And it didn’t happen just once. While CloudShell was open, it came back about every seven minutes.
{
"userIdentity": {
"type": "Root",
"arn": "arn:aws:iam::111122223333:root",
"sessionContext": {
"attributes": {
"creationDate": "2026-11-21T11:36:05Z"
}
}
},
"eventTime": "2026-11-21T11:38:28Z",
"eventSource": "cloudshell.amazonaws.com",
"eventName": "PutCredentials",
"sourceIPAddress": "203.0.113.10",
"sessionCredentialFromConsole": "true",
"requestParameters": {
"RefreshToken": "***"
}
}
Right after each PutCredentials, in the same second, there was a GetCallerIdentity call that didn’t come from my browser.
{
"userIdentity": {
"type": "Root",
"arn": "arn:aws:iam::111122223333:root",
"sessionContext": {
"attributes": {
"creationDate": "2026-11-21T11:36:05Z"
}
}
},
"eventTime": "2026-11-21T11:38:28Z",
"eventSource": "sts.amazonaws.com",
"eventName": "GetCallerIdentity",
"sourceIPAddress": "3.90.52.157",
"userAgent": "aws-sdk-go/1.55.8 (go1.27.3; linux; amd64) exec-env/AWS_Lambda_provided.al2023",
"sessionCredentialFromConsole": "true"
}
This one came from an AWS IP address with an aws-sdk-go user agent, but it still had sessionCredentialFromConsole and my console session’s creationDate. Since the timing matched every single time, it looks like CloudShell checking the credentials it just received. These are the calls I mentioned in the previous section.
What does a command typed inside CloudShell look like? I ran aws sts get-caller-identity there, and this is what CloudTrail recorded.
{
"userIdentity": {
"type": "Root",
"arn": "arn:aws:iam::111122223333:root",
"sessionContext": {
"attributes": {
"creationDate": "2026-11-21T11:36:05Z",
"mfaAuthenticated": "true"
}
}
},
"eventTime": "2026-11-21T14:02:19Z",
"eventSource": "sts.amazonaws.com",
"eventName": "GetCallerIdentity",
"sourceIPAddress": "18.207.129.133",
"userAgent": "aws-cli/2.41.0 md/awscrt#0.38.1 ua/2.1 os/linux#6.1.190-237.442.amzn2023.x86_64 md/arch#x86_64 lang/python#3.14.8 md/pyimpl#CPython exec-env/CloudShell m/z,Z,E,b cfg/retry-mode#standard md/installer#exe sid/f6563846d7f8 md/distrib#amzn.2023 md/prompt#off md/command#sts.get-caller-identity",
"tlsDetails": {
"tlsVersion": "TLSv1.3",
"cipherSuite": "TLS_AES_128_GCM_SHA256",
"keyExchange": "X25519MLKEM768"
},
"sessionCredentialFromConsole": "true"
}
It has sessionCredentialFromConsole set to true and the same creationDate as my console sign-in, more than two hours earlier. By those two checks alone, this AWS CLI call looks like any other console activity. The user agent does say exec-env/CloudShell, but we already know that part can be changed. The more useful clue is sourceIPAddress. The call came from an AWS IP address, the CloudShell environment, not from my own network like the rest of my console calls.
Someone opening CloudShell leaves cloudshell events behind, and commands typed inside it come from AWS IP addresses while still carrying console markers. Telling those apart from other console activity takes more work, though. That’s a different problem, so for the rest of this post, let’s assume the attacker works outside CloudShell, using stolen keys or session tokens from their own machine. With that in mind, an ASIA call comes from a programmatic client when it has no sessionCredentialFromConsole, its creationDate doesn’t match any ConsoleLogin, and it has no userIdentity.invokedBy, a field we’ll look at later.
Could an attacker make their own calls look like they came from CloudShell? The user agent part is easy. Setting AWS_EXECUTION_ENV=CloudShell is enough to put exec-env/CloudShell in the header, which I confirmed on my own laptop. The console markers are a different story. sessionCredentialFromConsole and the session’s creationDate come from having a console session in the first place, so a stolen access key can’t produce them.
Getting them takes more than a header, so I tried it. GetFederationToken with my access key returned a federated identity, the console sign-in endpoint accepted it, and CloudTrail recorded the whole thing in five seconds.
| Time | Event | Identity | Key |
|---|---|---|---|
14:24:00 | GetFederationToken | Administrator | AKIA |
14:24:04 | GetSigninToken | federated-user/validation-test | ASIA |
14:24:05 | ConsoleLogin, MFAUsed No | federated-user/validation-test | ASIA |
So look for a chain, not a single event. An AKIA key calling GetFederationToken, followed within seconds by GetSigninToken and a ConsoleLogin from the federated identity.
The creationDate check needs a small adjustment for these sessions. A federated session’s creationDate is the moment its credentials were created, which is a few seconds before the sign-in. They don’t match exactly the way they do for a password sign-in, so a strict equality check would miss a federated session. Matching within a minute, or alerting on GetFederationToken itself, covers it.
A real CloudShell call also has the session right behind it. When I ran the command above, PutCredentials happened six seconds earlier, and while the environment was open, the console refreshed those credentials every few minutes. So a call that claims to be CloudShell, from a session that never received any credentials, doesn’t add up.
That gives us two queries. The first lists CloudShell activity by session.
filter eventSource = "cloudshell.amazonaws.com" and eventName = "PutCredentials"
| fields @timestamp, userIdentity.arn as principal,
userIdentity.sessionContext.attributes.creationDate as session_start,
sourceIPAddress
| sort @timestamp desc
One row shows up for every credential refresh.
| @timestamp | principal | session_start | sourceIPAddress |
|---|---|---|---|
| 2026-11-21 11:38:28.000 | arn:aws:iam::111122223333:root | 2026-11-21T11:36:05Z | 203.0.113.10 |
The second looks for calls made from inside one of those sessions. Take the principal and the session start from the first query and put them in here.
filter sessionCredentialFromConsole = "true"
and userIdentity.arn = "arn:aws:iam::111122223333:root"
and userIdentity.sessionContext.attributes.creationDate = "2026-11-21T11:36:05Z"
| fields @timestamp, eventSource, eventName, sourceIPAddress, userAgent
| sort @timestamp desc
That returns both kinds of call, and the source IP is what separates them.
| @timestamp | eventSource | eventName | sourceIPAddress | userAgent |
|---|---|---|---|---|
| 2026-11-21 14:02:19.000 | sts.amazonaws.com | GetCallerIdentity | 18.207.129.133 | aws-cli/2.41.0 … exec-env/CloudShell … |
| 2026-11-21 12:15:32.000 | iam.amazonaws.com | ListAccessKeys | 203.0.113.10 | Mozilla/5.0 … Chrome/157.0.0.0 … |
On my events, the first row is the aws sts get-caller-identity call I ran inside CloudShell, along with the credential checks that followed it. The rows from my browser carry the same flag, since they use the same console session, and they come from my own network address instead of an AWS one.
Handling Missing Key IDs
And what if there’s no accessKeyId at all? That doesn’t make the call safe. Some programmatic calls are recorded without a key ID, like signin:CreateOAuth2Token, or sts:AssumeRoleWithWebIdentity, which isn’t signed and happens before any key exists.
You can see one of those events like this.
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=CreateOAuth2Token \
--max-results 1 --output json \
| jq -r '.Events[].CloudTrailEvent | fromjson
| {eventName, eventSource, userIdentity: {type: .userIdentity.type, arn: .userIdentity.arn,
accessKeyId: (.userIdentity.accessKeyId // "none")}}'
The output has no key to look at.
{
"eventName": "CreateOAuth2Token",
"eventSource": "signin.amazonaws.com",
"userIdentity": {
"type": "AssumedRole",
"arn": "arn:aws:sts::111122223333:assumed-role/automation-role/automation-role",
"accessKeyId": "none"
}
}
These events come from an AWS integration I set up a few days earlier. It assumes a role in my account, then exchanges those credentials for a token at signin.amazonaws.com, which is why an automation role shows up calling CreateOAuth2Token and why there is no accessKeyId to look at even though a role was involved. The role name and the account ID are changed in the samples.
An ASIA key brings its own problem, since it only lives for one session. Watching key IDs works well for AKIA keys, but for temporary credentials, it’s better to follow the role in userIdentity.sessionContext.sessionIssuer.arn. CloudTrail lets you search by the role’s session name.
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=Username,AttributeValue=automation-role \
--max-results 3 --output json \
| jq -r '.Events[] | .CloudTrailEvent | fromjson
| "\(.eventTime) \(.eventName) \(.userIdentity.sessionContext.sessionIssuer.arn)"'
Every event comes back with the same role, whatever the key was.
2026-11-21T12:27:58Z LookupEvents arn:aws:iam::111122223333:role/automation-role
2026-11-21T12:27:57Z LookupEvents arn:aws:iam::111122223333:role/automation-role
2026-11-21T12:27:57Z LookupEvents arn:aws:iam::111122223333:role/automation-role
accessKeyId can’t tell us whether the AWS CLI was used. What it does tell us is whose credential made the call, and whether that call was programmatic.
Filtering Out AWS Service Calls
Not every API call in CloudTrail comes from a person or a script. AWS services make calls in your account too. S3 asks KMS to decrypt an object you’re reading, CloudFormation creates the resources in your stack, and Lambda assumes a function’s execution role. Since these calls can use your permissions or roles, they end up mixed in with real user activity. So how do we tell them apart?
CloudTrail marks these calls with userIdentity.invokedBy. According to the documentation, the field only shows up when an AWS service made the request. That includes requests through forward access sessions (FAS), AWS service principals, service-linked roles, and service roles. Its value is the name of the service. Here’s Resource Explorer assuming its service-linked role.
{
"userIdentity": {
"type": "AWSService",
"invokedBy": "resource-explorer-2.amazonaws.com"
},
"eventSource": "sts.amazonaws.com",
"eventName": "AssumeRole",
"userAgent": "resource-explorer-2.amazonaws.com",
"requestParameters": {
"roleArn": "arn:aws:iam::111122223333:role/aws-service-role/resource-explorer-2.amazonaws.com/AWSServiceRoleForResourceExplorer",
"roleSessionName": "resource-explorer-2"
}
}
Unlike userAgent, invokedBy isn’t something a client can set. AWS adds it based on who made the request, so we can trust it to point out calls made by AWS itself. These events also come without tlsDetails, since the connection didn’t come from a client outside AWS.
Handling Your Own Workloads
But invokedBy doesn’t cover your own code running on AWS. A Lambda function or an EC2 instance calling AWS with its role shows up as an AssumedRole with an ASIA key and no invokedBy, just like any other programmatic client. That makes sense, because from AWS’s side, it’s your workload making the call, not the service.
invokedBy won’t detect key misuse on its own, but it helps the other way around. Any event that has it was made by AWS, so we can filter it out before we look for unauthorized key usage. What’s left was signed by a credential that someone, or something, outside AWS’s own services is holding.
Replacing Access Keys with SSO
If we can’t tell which tool made a call, can we at least make every call easier to trace back to a person? IAM Identity Center, or SSO, can help with that. When someone runs aws sso login, the AWS CLI gets temporary credentials for a role named like AWSReservedSSO_<PermissionSet>_<id>, and the role session is named after the user who signed in. So instead of an IAM user that a whole team might share, every call points to a real person.
The credentials also don’t live forever. An access key works until someone rotates or deletes it, which in many accounts means years. An SSO session ends after the duration set in the permission set, somewhere between 1 and 12 hours, so a leaked credential stops working by itself.
It also makes detection simpler. Once people sign in with SSO, the only long-term keys left should belong to automation, like a backup job. Any other AKIA key showing up in CloudTrail then stands out, without having to keep track of every key that people created over the years.
CI/CD pipelines don’t need long-term keys either. A lot of pipelines still run the AWS CLI with an access key saved as a secret, and that key is just as easy to leak as one on a laptop. Most CI platforms, like GitHub Actions and GitLab CI, can sign in to AWS with OIDC instead. The pipeline gets a signed token from the platform and trades it for temporary credentials with AssumeRoleWithWebIdentity. Those credentials expire after a short time, an hour by default, so there’s no key sitting in the pipeline’s secrets anymore. In CloudTrail, the calls then show up as the pipeline’s role with an ASIA key, so they’re easy to add to the list of roles we expect.
Also keep in mind that the console and the AWS CLI use the same SSO role, so we still need sessionCredentialFromConsole to tell the two apart. And just like before, SSO credentials work the same from an SDK or Terraform, so SSO tells us who made the call, not which tool they used.
Detecting Suspicious CLI Activity
After all of this, there is still no field that proves the AWS CLI made a call. Everything that names the tool can be replaced, and the checks that survive only tell us that a call was programmatic. So what do we actually detect?
The answer is to stop trying to catch the tool and make its use predictable. In most accounts, only a few things need the CLI at all. A handful of administrators, the CI pipeline, and some automation. Everyone else can work from the console. Once that’s the rule, an access key that isn’t on the list is worth looking at, whatever its user agent says.
That list has to exist before any rule can use it. An inventory of every IAM user, role and access key in the account, with a line about what each one is for. A person, a CI pipeline, a service that writes backups, a partner that reads one bucket. aws iam get-account-authorization-details gives you the users, roles and their attached policies in one call, and get-access-key-last-used tells you which keys have gone quiet. With that in hand, a key you can’t place is suspicious by default, and you also learn which identities should never show up in a CloudTrail query at all. Refresh it on a schedule, not once, or the entries you forget are the ones an attacker will use.
The second half is the network. A service control policy that only allows API calls from your office ranges and your VPC endpoints, with an exception for AWS services that call on your behalf through aws:ViaAWSService, takes most of the guessing away. A stolen key used from a cafe fails at the policy, and the attempt still lands in CloudTrail with AccessDenied.
With those two in place, the detection list is short.
TIP
- a programmatic call from a principal that isn’t on the allowlist
AccessDeniedfrom the network policy, which means a real credential was used somewhere it shouldn’t be- any
AKIAkey, if everyone is supposed to be on SSO GetFederationTokenfollowed by aConsoleLogin, the chain I tried earlier- CloudShell activity, if nobody is supposed to use it
None of those rules needs to know which tool sent the request. The CLI was never the thing to catch. Manual use of an access key outside the places you expect it is.
That list covers who is making calls and from where. It says nothing about what they do once a key works, so a second group of rules looks at behaviour instead. Reconnaissance is the one to start with, because it is usually the first thing that happens after stolen keys start working.
WARNING
- one principal calling many different
List,GetandDescribeoperations across services in a short window - IAM enumeration in particular, like
ListUsers,ListRoles,ListAttachedUserPoliciesandGetAccountAuthorizationDetails - a run of
AccessDeniedfrom one principal, which usually means someone is working out what a key is allowed to do - calls in a region the account has never used
- actions that keep access, like
CreateAccessKey,CreateUser,AttachUserPolicyorUpdateAssumeRolePolicy - actions that hide the trail, like
StopLogging,DeleteTrailor turning off GuardDuty
These are noisier than the first list. Inventory tools, Terraform and every aws sso login produce some of the same calls, and GetCallerIdentity sits at the start of almost every CLI session, so alerting on it alone just fills the queue. Run them after the allowlist and the network policy have removed the expected traffic, and tune them against your own baseline.
One last detail. sts:GetCallerIdentity can’t be denied, since AWS allows it even when a policy explicitly denies it. An attacker will always be able to check whether a key works, and that call will always be in CloudTrail. It just won’t be the call that gets them.