-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathAWS-EC2-039.py
165 lines (129 loc) · 3.93 KB
/
AWS-EC2-039.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
Remediate Prisma Policy:
AWS:EC2-039 Global Access on All Ports Detected
Description:
Global permission to access all ports should not be allowed in a security group. Best security practice
dictates restricting access to service ports solely to known static IP addresses within your control.
Required Permissions:
- ec2:DescribeSecurityGroups
- ec2:RevokeSecurityGroupIngress
Sample IAM Policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EC2Permissions",
"Effect": "Allow",
"Action": [
"ec2:DescribeSecurityGroups",
"ec2:RevokeSecurityGroupIngress"
],
"Resource": [
"*"
]
}
]
}
"""
import boto3
from botocore.exceptions import ClientError
# Options:
#
global_cidr_list = [ '0.0.0.0/0', '::/0' ]
def remediate(session, alert, lambda_context):
"""
Main Function invoked by index_prisma.py
"""
sg_id = alert['resource_id']
region = alert['region']
ec2 = session.client('ec2', region_name=region)
try:
group = ec2.describe_security_groups(GroupIds=[ sg_id, ])['SecurityGroups']
except ClientError as e:
print(e.response['Error']['Message'])
return
try:
ip_perms = group[0]['IpPermissions']
except (IndexError, KeyError):
print('IP permissions not found for security group {}.'.format(sg_id))
return
for ip_perm in ip_perms:
result = remove_offending_sg_rules(ec2, sg_id, ip_perm)
return
def remove_sg_rule(ec2, revoke_args):
"""
Revoke ingress security group rule
"""
try:
ec2.revoke_security_group_ingress(**revoke_args)
except ClientError as e:
print(e.response['Error']['Message'])
return
else:
print('Revoked security group rule: {}.'.format(revoke_args))
return
def remove_offending_sg_rules(ec2, sg_id, ip_perm):
"""
Remove offending security group rules (calls >> remove_sg_rule())
"""
# Look for IPv4 permissions
if ip_perm['IpRanges']:
for ip_range in ip_perm['IpRanges']:
if ip_range['CidrIp'] in global_cidr_list:
if (ip_perm['IpProtocol'] == '-1'):
revoke_args = {
'GroupId' : sg_id,
'IpPermissions' : [
{
'IpProtocol': ip_perm['IpProtocol'],
'IpRanges': [{ 'CidrIp': ip_range['CidrIp'] }]
}
]
}
elif (ip_perm['FromPort'] == 0 and ip_perm['ToPort'] == 65535):
revoke_args = {
'GroupId' : sg_id,
'IpPermissions' : [
{
'IpProtocol': ip_perm['IpProtocol'],
'FromPort': ip_perm['FromPort'],
'ToPort': ip_perm['ToPort'],
'IpRanges': [{ 'CidrIp': ip_range['CidrIp'] }]
}
]
}
else:
revoke_args = None
if revoke_args != None:
result = remove_sg_rule(ec2, revoke_args)
# Look for IPv6 permissions
if ip_perm['Ipv6Ranges']:
for ip_range in ip_perm['Ipv6Ranges']:
if ip_range['CidrIpv6'] in global_cidr_list:
if (ip_perm['IpProtocol'] == '-1'):
revoke_args = {
'GroupId' : sg_id,
'IpPermissions' : [
{
'IpProtocol': ip_perm['IpProtocol'],
'Ipv6Ranges': [{ 'CidrIpv6': ip_range['CidrIpv6'] }]
}
]
}
elif (ip_perm['FromPort'] == 0 and ip_perm['ToPort'] == 65535):
revoke_args = {
'GroupId' : sg_id,
'IpPermissions' : [
{
'IpProtocol': ip_perm['IpProtocol'],
'FromPort': ip_perm['FromPort'],
'ToPort': ip_perm['ToPort'],
'Ipv6Ranges': [{ 'CidrIpv6': ip_range['CidrIpv6'] }]
}
]
}
else:
revoke_args = None
if revoke_args != None:
result = remove_sg_rule(ec2, revoke_args)
return