AWS Builder Center

Authenticating an AWS Security Agent Pentest with a Token in Secrets Manager

When your app's login is protected by hardware MFA, push notifications, or OAuth consent, AWS Security Agent can't sign in on its own. This runbook shows a token-injection pattern that gets around that: authenticate once yourself, hand the agent the already-authenticated token through Secrets Manager, and let it test everything behind the login.

The problem: login the agent can't complete

AWS Security Agent's penetration testing capability can authenticate to your app at runtime, and it automates the standard machine-completable login flows: username and password, plus a TOTP second factor. What it intentionally does not do is complete authentication steps that require a human in the loop, challenges that only a person interacting with a device or an out-of-band channel can finish. That covers hardware security keys (FIDO2, WebAuthn, YubiKey), SMS, email, or push-notification MFA, and interactive OAuth consent flows.
If your app sits behind any of these, the agent can't complete sign-in on its own. Without credentials at all, it's limited to testing public, unauthenticated surface area, which is exactly the gap the pattern below closes.

The solution: inject a token instead of logging in

Don't make the agent log in. Authenticate once, out of band, by completing the hardware-MFA login yourself. Capture the post-login token (a bearer token, JWT, or session cookie), store it in Secrets Manager, and tell the agent to inject that token on every request instead of navigating a login form.
This works because almost every app enforces strong MFA at login only. After that, a bearer token or session cookie carries the authenticated session, and the agent just needs to present it.
You (human) AWS Security Agent
─────────── ──────────────────
1. Login w/ hardware MFA
2. Copy post-login token
3. Put token in Secrets Mgr ─────► 4. Reads secret via service role
5. Injects "Authorization: Bearer <token>"
on every request to the target
6. Tests authenticated surface area
I validated this end to end against an OWASP Juice Shop target. The same pattern applies to a production app whose login is protected by hardware MFA.

Prerequisites

  • An AWS account where the agent space, secret, and IAM role all live. Secrets Manager secrets and Lambda functions used for credentials must be in the same AWS account as your AWS Security Agent setup; cross-account credential sources are not supported.
  • AWS Security Agent available in your Region. This guide uses  us-east-1 .
  • A reachable target over HTTPS with a verified domain (Step 3 covers verification).
  • Permissions to create Secrets Manager secrets and IAM roles.
  • Confirm first that your app accepts the token or cookie on subsequent requests without re-prompting for the hardware key. This is the assumption the whole approach rests on.

Step by step

Every step below has a console equivalent (AWS Security Agent, Secrets Manager, IAM, and Route 53 consoles) alongside the CLI command. The console is often easier for one-off setup, including one-click domain verification when your zone is in the same account. The CLI is the scriptable, repeatable version. Use whichever fits, or mix the two.
All  aws  commands run against the account that owns the agent space. Adjust  --region , ARNs, and IDs to your environment.

Step 0: Capture the post-login token, manually, once

  1. Log into your application normally, completing the hardware-MFA challenge.
  1. Open DevTools, go to Application → Storage, and copy the token your app uses for API auth. For a JWT this is usually a  token  cookie and/or  localStorage.getItem('token') .
  • Tip: running  copy(localStorage.getItem('token'))  in the console copies the full value directly to your clipboard.
  1. Copy the entire value. Long tokens are easy to truncate or corrupt. A JWT should be three  . -separated base64url segments with no non-ASCII characters. Validate what you stored in the next step.

Step 1: Store the token in Secrets Manager

Store the raw token as plaintext, or as JSON if you want structure. The agent reads whatever you store and applies it according to the instructions you give it later, so the format is yours to choose.
aws secretsmanager create-secret \
--name myapp/pentest-token \
--description "Bearer token for AWS Security Agent pentest" \
--secret-string '<PASTE_FULL_TOKEN>' \
--region us-east-1
Note the returned ARN, for example  arn:aws:secretsmanager:us-east-1:<acct>:secret:myapp/pentest-token-AbCdEf .
Validate what you stored, which catches corruption from copy-paste:
aws secretsmanager get-secret-value \
--secret-id myapp/pentest-token \
--region us-east-1 --query SecretString --output text

Step 2: Create the IAM service role

This is the Penetration Test Service Role the agent assumes to read the secret and write run logs to CloudWatch. Grant both permission sets. A missing logs permission tends to surface early in the run, since the run writes logs continuously.
Create the role with a trust policy for the service principal. AWS's own guidance for this role recommends adding an  sts:ExternalId  condition for defense in depth:
aws iam create-role \
--role-name SecurityAgentPentestRole \
--description "Assumed by AWS Security Agent for pentests" \
--assume-role-policy-document '{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Principal":{"Service":"securityagent.amazonaws.com"},
"Action":"sts:AssumeRole",
"Condition":{"StringEquals":{"sts:ExternalId":"<your-external-id>"}}
}]
}'
Attach secret-read access, scoped to just your secret:
aws iam put-role-policy \
--role-name SecurityAgentPentestRole \
--policy-name PentestSecretAccess \
--policy-document '{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Action":["secretsmanager:GetSecretValue","secretsmanager:DescribeSecret"],
"Resource":"arn:aws:secretsmanager:us-east-1:<acct>:secret:myapp/pentest-token-*"
}]
}'
Attach CloudWatch Logs access, since the run writes here:
aws iam put-role-policy \
--role-name SecurityAgentPentestRole \
--policy-name PentestLogsAccess \
--policy-document '{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Action":["logs:CreateLogGroup","logs:CreateLogStream","logs:PutLogEvents",
"logs:DescribeLogGroups","logs:DescribeLogStreams"],
"Resource":["arn:aws:logs:us-east-1:<acct>:log-group:/aws/securityagent/*",
"arn:aws:logs:us-east-1:<acct>:log-group:/aws/securityagent/*:*"]
}]
}'

Step 3: Verify the target domain

The agent only tests domains you've proven ownership of. DNS TXT is the most reliable method for most setups.
aws securityagent create-target-domain \
--target-domain-name app.example.com \
--verification-method DNS_TXT \
--region us-east-1
The response includes the exact TXT record name and token value to create. Use the record name returned by the call rather than assuming a fixed prefix. Add it to your DNS zone, wait for it to resolve, then confirm using that same record name:
dig +short TXT <txt-record-name-from-response>
Once it resolves, trigger verification:
aws securityagent verify-target-domain \
--target-domain-id td-xxxxxxxx \
--region us-east-1
Look for  "status": "VERIFIED" . Verifying a base domain automatically covers its subdomains.
If the zone is in the same account as the agent and registered in Route 53, the console offers one-click verification that creates the TXT record for you. Zones in other accounts or with other DNS providers need the manual copy-paste-verify flow above.

Step 4: Register the role, secret, and domain with the agent space

The Penetration Test Service Role must be on the agent space's list of available roles before you can select it when creating a pentest, and the secret and domain need to be registered too.  --name  is optional if you just want to keep the space's existing name.
aws securityagent update-agent-space \
--agent-space-id as-xxxxxxxx \
--name MySpace \
--aws-resources '{
"iamRoles":["arn:aws:iam::<acct>:role/SecurityAgentPentestRole"],
"secretArns":["arn:aws:secretsmanager:us-east-1:<acct>:secret:myapp/pentest-token-AbCdEf"]
}' \
--target-domain-ids td-xxxxxxxx \
--region us-east-1

Step 5: Create the pentest with token auth

The credential lives on an actor. Set  authentication.providerType  to  SECRETS_MANAGER  and  value  to the secret ARN. The actor's  description  field is where you tell the agent how to apply the token: it functions the same way as the console's "Agent Space login prompt," giving the agent instructions for using the credential, though the two aren't explicitly documented as the same underlying field.
aws securityagent create-pentest \
--agent-space-id as-xxxxxxxx \
--title myapp-bearer-test \
--service-role arn:aws:iam::<acct>:role/SecurityAgentPentestRole \
--assets '{
"endpoints":[{"uri":"https://app.example.com"}],
"actors":[{
"identifier":"authenticated-user",
"description":"The Secrets Manager secret value is a raw JWT bearer token. Do NOT attempt an interactive login form. Apply the credential by setting the HTTP header Authorization to \"Bearer <token>\" on all requests to the target.",
"authentication":{
"providerType":"SECRETS_MANAGER",
"value":"arn:aws:secretsmanager:us-east-1:<acct>:secret:myapp/pentest-token-AbCdEf"
},
"uris":["https://app.example.com"]
}]
}' \
--region us-east-1
Note the returned  pentestId .
If your token goes somewhere else, adjust the  description  accordingly. For example: "Set the X-API-Key header to the provided token," or "Set a cookie named 'session' to the provided token."

Step 6: Start the job

aws securityagent start-pentest-job \
--agent-space-id as-xxxxxxxx \
--pentest-id pt-xxxxxxxx \
--region us-east-1
Returns a  pentestJobId  and a status of  IN_PROGRESS .

Step 7: Monitor and collect findings

# status (steps: PREFLIGHT -> STATIC_ANALYSIS -> PENTEST -> VALIDATION -> FINALIZING)
aws securityagent batch-get-pentest-jobs \
--agent-space-id as-xxxxxxxx \
--pentest-job-ids pj-xxxxxxxx \
--region us-east-1

# findings (available during the run and after COMPLETED)
aws securityagent list-findings \
--agent-space-id as-xxxxxxxx \
--pentest-job-id pj-xxxxxxxx \
--region us-east-1

Confirming the auth actually worked

Two signals tell you token injection succeeded:
  1. Preflight completes. This step validates target and auth reachability before the run moves on to static analysis and the actual pentest.
  1. Authenticated-only findings show up, things like an IDOR on a user's basket or privilege escalation through registration mass-assignment. These are unreachable without a valid session, so seeing them proves the token made it through.
You can also tail the run logs to watch Preflight confirm auth directly. The log group and stream are whatever you set in the pentest's  logConfig . If you don't set one, AWS Security Agent creates a default log group under the  /aws/securityagent  prefix (AWS's own docs aren't fully consistent on whether the full path includes the agent space and pentest ID, so don't hardcode the exact name), and any log group your service role can write to works.

References

  • 
This content publication is assisted by Claude Sonnet 5.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article