-
Notifications
You must be signed in to change notification settings - Fork 201
/
main.go
93 lines (73 loc) · 2.89 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
91
92
93
package aws
import (
"context"
_ "embed"
"errors"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/datadog/stratus-red-team/v2/pkg/stratus"
"github.com/datadog/stratus-red-team/v2/pkg/stratus/mitreattack"
"log"
)
//go:embed main.tf
var tf []byte
func init() {
stratus.GetRegistry().RegisterAttackTechnique(&stratus.AttackTechnique{
ID: "aws.exfiltration.ec2-security-group-open-port-22-ingress",
FriendlyName: "Open Ingress Port 22 on a Security Group",
Platform: stratus.AWS,
IsIdempotent: false, // cannot call ec2:AuthorizeSecurityGroupIngress multiple times with the same parameters
MitreAttackTactics: []mitreattack.Tactic{mitreattack.Exfiltration},
Description: `
Opens ingress traffic on port 22 from the Internet (0.0.0.0/0).
Warm-up:
- Create a VPC and a security group inside it.
Detonation:
- Call ec2:AuthorizeSecurityGroupIngress to allow ingress traffic on port 22 from 0.0.0.0/0.
`,
Detection: `
You can use the CloudTrail event <code>AuthorizeSecurityGroupIngress</code> when:
- <code>requestParameters.cidrIp</code> is <code>0.0.0.0/0</code> (or an unknown external IP)
- and <code>requestParameters.fromPort</code>/<code>requestParameters.toPort</code> is not a commonly exposed port or corresponds to a known administrative protocol such as SSH or RDP
`,
PrerequisitesTerraformCode: tf,
Detonate: detonate,
Revert: revert,
})
}
func detonate(params map[string]string, providers stratus.CloudProviders) error {
ec2Client := ec2.NewFromConfig(providers.AWS().GetConnection())
// Find the snapshot to exfiltrate
securityGroupId := params["security_group_id"]
// Open port 22 to the world
log.Println("Opening port 22 from the Internet on " + securityGroupId)
_, err := ec2Client.AuthorizeSecurityGroupIngress(context.Background(), &ec2.AuthorizeSecurityGroupIngressInput{
GroupId: &securityGroupId,
CidrIp: aws.String("0.0.0.0/0"),
FromPort: aws.Int32(22),
ToPort: aws.Int32(22),
IpProtocol: aws.String("tcp"),
})
if err != nil {
return errors.New("unable to open port 22 on security group " + err.Error())
}
return nil
}
func revert(params map[string]string, providers stratus.CloudProviders) error {
ec2Client := ec2.NewFromConfig(providers.AWS().GetConnection())
// Find the snapshot to exfiltrate
securityGroupId := params["security_group_id"]
// Open port 22 to the world
log.Println("Closing port 22 from the Internet on " + securityGroupId)
_, err := ec2Client.RevokeSecurityGroupIngress(context.Background(), &ec2.RevokeSecurityGroupIngressInput{
GroupId: &securityGroupId,
CidrIp: aws.String("0.0.0.0/0"),
FromPort: aws.Int32(22),
ToPort: aws.Int32(22),
IpProtocol: aws.String("tcp"),
})
if err != nil {
return errors.New("unable to close port 22 on security group " + err.Error())
}
return nil
}