Security forensics
Tracing a leaked key the standard tools said was barely used
An audit of scheduled jobs found a long-lived access key baked into container images. Before rotating it, we needed to know everything that used it. The standard tools showed it had barely been used.
- Stack
- AWS CloudTrail, CloudWatch Logs Insights, IAM, AWS Batch, Python
- My role
- Investigation, tooling and the rotation plan
- Outcome
- A full report per key in about 33 seconds. Every caller identified before rotation.
Where it started
While auditing scheduled jobs, I found the access key of a long-lived IAM user inside two container images that AWS Batch ran every night. Anyone who could pull those images had the key. Rotating a key takes seconds. The real work is rotating it without breaking something that quietly depends on it. So the first question was who was using this key, from where, and for what.
The obvious tools undercount
aws cloudtrail lookup-events returned one old event across 90 days. The same key, queried against the organization’s CloudTrail log group in CloudWatch Logs, showed 1,434 events in two days. Three things explained the gap:
lookup-eventsonly returns management events. Most of this key’s traffic was data events: S3 object writes, Firehose records and SES sends.- The CloudTrail log group was in a different region from the one everyone assumed, so the first queries searched an empty place and found nothing.
- Our read-only role wasn’t allowed to call the IAM “access key last used” API, so we couldn’t start from there either.
Logs Insights has its own traps
Two limits make large investigations silently wrong:
- Results stop at 10,000 rows. A busy key goes past that within an hour.
- On wide time ranges, a query can report billions of skipped records in
estimatedRecordsSkippedand still return success. “No events found” means nothing unless that number is zero.
So I wrote a Python tool that keeps splitting the time window until every slice is under the row limit and skips nothing. It then merges the slices into one result.
What the report shows
For a key and a time window, the report lists:
- every distinct source IP, with its call count, successes and errors, error codes, first and last seen time, and user agents
- the services and API operations the key called
- a table of which IP called which service and operation
- a short list of raw events, with the full JSON available on request
A companion lookup matches each IP to our own NAT gateways, load balancers and compute. If an IP isn’t ours, it names the cloud provider or ISP.
Making it fast
I timed it on a second, much busier key: a two-hour window with 16,916 events. The first version took 5 minutes 15 seconds. Most of that time wasn’t spent querying. It went on repeated connectivity checks, identity calls made one account at a time, an IAM call that was always going to be denied, and grouping user agents after the fact. With those removed, the same report took about 33 seconds.
What it found
On that busier key, the report showed normal traffic from containers and Firehose. Next to it were eight IPs on mobile networks in two countries, all with Android user agents, calling S3 PutObject directly. One of those calls succeeded. That pattern means the key has shipped inside a mobile app. The key went straight to rotation, and the triage logic now treats that pattern as its own “rotate immediately” rule.
What changed afterwards
- The rotation plan listed every caller, and what credential each one would move to, before anything changed.
- Deploy pipelines were moved off a deploy image that had a key built in, to deploy steps that use roles. This covered every region’s pipeline for each service.
- The tool is now the first step for any “who used this key” question.
What I’d do differently
- Push more of the work into the query. Per-IP counts and first and last seen times can come straight from
stats … by sourceIPAddress, eventName, which returns far fewer rows. For windows longer than a few days, Athena over the trail’s S3 logs is the better tool. - Treat the key inside the mobile app as its own incident. Rotating a key that ships inside an app breaks every installed copy, so the rotation needs a replacement: short-lived credentials from Cognito identity pools, or presigned S3 URLs issued by the backend. The one successful write also needs scoping: which object, where, and whether anything read it.
What I took from it
- Check where the data actually lives before trusting an empty result.
- A query can report success and still have skipped most of the data.
- Time the slow parts before optimizing. Here, the overhead around the query cost more than the query itself.