How to detect cryptomining in your AWS account (before it costs you $50k)
Cryptomining is the most economically motivated form of AWS credential abuse. The playbook is simple: steal credentials, launch GPU instances in an unused region, mine cryptocurrency, walk away. For the attacker, the economics are excellent. For you, the bill can exceed $50,000 before anyone notices.
Here's what cryptomining looks like in your AWS account and how to detect it.
What cryptomining looks like in CloudTrail
The primary signal is ec2:RunInstances with GPU instance types. The most commonly abused instances are:
p3.2xlarge— 1 NVIDIA V100, $3.06/hrp3.8xlarge— 4 NVIDIA V100, $12.24/hrp3.16xlarge— 8 NVIDIA V100, $24.48/hrp4d.24xlarge— 8 NVIDIA A100, $32.77/hrg4dn.12xlarge— 4 NVIDIA T4, $3.912/hr
Attackers typically launch 10-50 instances simultaneously. A single CloudTrail RunInstances call can launch up to 20 instances — attackers will often call it multiple times in quick succession.
Secondary signals:
- Unusual region: the call happens in a region with zero prior EC2 usage in the account
- New IAM principal: the credentials used have never made a RunInstances call before
- High network egress: mining requires constant communication with mining pools; look for elevated
DataTransfer-Out-Bytesshortly after launch - UserData script: the RunInstances call will include base64-encoded UserData that downloads and runs mining software; decode it and you'll find wget/curl calls to suspicious domains or known mining pool addresses
- CloudTrail StopLogging: sophisticated attackers follow up with
cloudtrail:StopLoggingto remove evidence from the region they're operating in
How to query CloudTrail manually
If you suspect mining activity, here's how to investigate. First, check CloudTrail Event History in the AWS Console: set the time range to the last 24-48 hours, filter by Event Name = RunInstances. Scan for events where the instance type is a p3/p4/g4 family.
For a more powerful query, use CloudTrail Lake or Athena if you're archiving CloudTrail to S3. Here's a sample Athena query that identifies suspicious RunInstances calls across all regions:
SELECT
eventtime,
awsregion,
useridentity.arn AS principal,
json_extract_scalar(requestparameters, '$.instanceType') AS instance_type,
json_extract_scalar(requestparameters, '$.maxCount') AS max_count,
sourceipaddress
FROM cloudtrail_logs
WHERE eventname = 'RunInstances'
AND eventtime > to_iso8601(current_timestamp - interval '48' hour)
AND (
json_extract_scalar(requestparameters, '$.instanceType') LIKE 'p3%'
OR json_extract_scalar(requestparameters, '$.instanceType') LIKE 'p4%'
OR json_extract_scalar(requestparameters, '$.instanceType') LIKE 'g4%'
OR json_extract_scalar(requestparameters, '$.instanceType') LIKE 'g5%'
)
ORDER BY eventtime DESC;
Run this query daily as a baseline. Any results you don't recognize are worth investigating.
Mining vs legitimate ML workload: how to tell the difference
Not every GPU RunInstances call is an attack. Your data science team runs legitimate GPU workloads. Here's how to distinguish:
Indicators of cryptomining:
- Region the account has never used before (strongest signal)
- Source IP from outside your organization's IP range
- IAM credentials that have never called RunInstances before
- Multiple RunInstances calls in rapid succession (<5 minutes apart)
- UserData contains base64-encoded scripts (decode them — legit ML jobs typically use AMI-bundled software, not bootstrap scripts downloading miners)
- No corresponding Spot request — miners prefer On-Demand for reliability; your ML team almost certainly uses Spot
- Instances launched without any IAM instance profile attached (miners don't need AWS API access from inside the instance)
Indicators of legitimate ML workload:
- Same region as your existing infrastructure
- Principal is a known CI/CD role or data science team IAM user
- Spot instances requested (cost-conscious teams use Spot)
- Tagged with your project/team tagging convention
- Instance profile attached to the instances
- Launched from your known office or VPN IP range
The single most reliable discriminator is region. Legitimate workloads from your team run in your regions. Attackers deliberately run in unused regions. If you get a RunInstances alert for ap-northeast-3 and you've never operated in Japan, investigate immediately.
What to do if you find active mining
- Do not delete the instances immediately. First, note the instance IDs, public IPs, and the IAM principal that launched them. You'll need this for the incident report and for communicating with AWS support.
- Rotate the compromised credentials. Go to IAM → Users or Roles, find the principal from the CloudTrail event, and deactivate or delete the access key. If it's an IAM user's long-term key, delete it. If it's an assumed role, revoke active sessions by attaching an explicit Deny policy.
- Terminate the instances. EC2 → Instances → filter by region (switch to the attack region) → terminate all unauthorized instances.
- Check for persistence mechanisms. The attacker may have created additional IAM users, access keys, or modified policies during their dwell time. Check IAM → Users for any users created in the last 48 hours. Check IAM → Roles for new roles. Check EC2 for any security group changes that might have created backdoors.
- Contact AWS support. File a billing dispute. AWS has a documented process for providing partial credits for credential-abuse incidents, especially for first-time victims. You won't get 100% back, but credits of 50-80% of the unauthorized charges are common for accounts that respond quickly.
Automated detection: catching it in under 15 minutes
The manual Athena query above is useful after the fact. For real-time detection, you need to watch CloudTrail events as they arrive via EventBridge.
A well-configured EventBridge rule catches RunInstances events where the instance type matches the GPU family and the region has no prior activity. The event arrives within seconds of the API call. Detection and alert delivery — Slack, email, PagerDuty — adds another 2-5 minutes. Total: 15 minutes or less from credential use to alert.
Compare that to the manual process: you might look at CloudTrail tomorrow morning, or next week, or when your CTO asks why the AWS bill is $50,000 over budget. The automated approach has the attacker identified and stopped before they've mined enough to cover the compute cost.
Cryptomining attacks are automated. The bots that find and exploit leaked credentials act within minutes of the credentials being made public. Your detection cannot rely on manual review. Automate your CloudTrail monitoring or accept that you'll always be finding out about incidents after the damage is done.
The good news: the signals are clear and distinctive. GPU instances in unused regions, new IAM principals, rapid-fire RunInstances calls. These patterns are easy to detect once you're watching for them. The hard part isn't the detection logic — it's being set up to watch the API stream in real time, rather than the billing aggregate 24 hours later.
Related articles
The first thing attackers do after stealing AWS credentials: switch off your alarms
Intruders often disable GuardDuty, stop AWS Config or delete logs first. The CloudTrail events to alert on, plus a copy-paste EventBridge rule.
How to audit AWS for signs of past compromise (after the Qubole breach)
A vendor's IAM credentials assumed customer roles for two weeks unnoticed. A step-by-step audit with commands: trusted roles, AssumeRole hunting, persistence.
Bought the wrong Savings Plan? You have a short window to return it
AWS lets you return a mistaken Savings Plan for a limited time. The exact rules, the calendar-month trap, and which plans cannot be returned.
Protect your AWS account — 14-day free trial
Real-time threat and cost anomaly detection. Detects credential theft, cryptomining, and runaway costs in under 15 minutes. 5-minute setup via CloudFormation.
No credit card. Cancel anytime.