forked from PaloAltoNetworks/Prisma-Enhanced-Remediation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWS-EC2-010.py
136 lines (104 loc) · 3.34 KB
/
AWS-EC2-010.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
"""
Remediate Prisma Policy:
AWS:EC2-100 Global Admin Port Access Detected
Description:
Global permission to access the well-known services should not be allowed in a security group.
**Note: Remediation will be executed if the well-known service is found within a port range.
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 json
import re
import boto3
from botocore.exceptions import ClientError
# Options:
#
# Example
# admin_port_list = [ 'tcp-22', 'tcp-23', 'tcp-3389' ]
#
admin_port_list = [ 'tcp-20', 'tcp-21', 'tcp-25', 'tcp-53', 'tcp-3306', 'tcp-5432', 'tcp-1433', 'tcp-4333', 'tcp-5500', 'tcp-5900' ]
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:
try:
from_port = ip_perm['FromPort']
except KeyError:
continue
else:
to_port = ip_perm['ToPort']
ip_protocol = ip_perm['IpProtocol']
# Look for IPv4 permissions
if ip_perm['IpRanges']:
IpRanges = 'IpRanges'
IpCidr = 'CidrIp'
for ip_range in ip_perm['IpRanges']:
cidr_ip = ip_range['CidrIp']
remove_sg_rule(ec2, sg_id, from_port, to_port, ip_protocol, cidr_ip, IpRanges, IpCidr)
# Look for IPv6 permissions
if ip_perm['Ipv6Ranges']:
IpRanges = 'Ipv6Ranges'
IpCidr = 'CidrIpv6'
for ip_range in ip_perm['Ipv6Ranges']:
cidr_ip = ip_range['CidrIpv6']
remove_sg_rule(ec2, sg_id, from_port, to_port, ip_protocol, cidr_ip, IpRanges, IpCidr)
return
def remove_sg_rule(ec2, sg_id, from_port, to_port, ip_protocol, cidr_ip, IpRanges, IpCidr):
"""
Revoke Ingress Security Group Rule
"""
for admin_port in admin_port_list:
proto = re.split('-', admin_port)[0]
port = re.split('-', admin_port)[1]
find_port='true' if from_port <= int(port) <= to_port else 'false'
if cidr_ip in global_cidr_list and ip_protocol.lower() == proto and find_port == 'true':
revoke_args = {
'GroupId' : sg_id,
'IpPermissions' : [
{
'IpProtocol': ip_protocol,
'FromPort': from_port,
'ToPort': to_port,
IpRanges: [{ IpCidr: cidr_ip }]
}
]
}
try:
ec2.revoke_security_group_ingress(**revoke_args)
print('Revoked rule permitting {}/{:d}-{:d} with cidr {} from {}.'.format(ip_protocol, from_port, to_port, cidr_ip, sg_id))
except ClientError as e:
print(e.response['Error']['Message'])
return