-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathAWS-EC2-042.py
94 lines (71 loc) · 1.85 KB
/
AWS-EC2-042.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
"""
Remediate Prisma Policy:
AWS:EC2-042 EBS Snapshot set with Public Permissions
Description:
To avoid exposing personal and sensitive data, we recommend against sharing your EBS snapshots with
all AWS accounts.
Required Permissions:
- ec2:DescribeSnapshotAttribute
- ec2:ModifySnapshotAttribute
Sample IAM Policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EC2Permissions",
"Action": [
"ec2:DescribeSnapshotAttribute",
"ec2:ModifySnapshotAttribute"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
"""
import boto3
from botocore.exceptions import ClientError
def remediate(session, alert, lambda_context):
"""
Main Function invoked by index_prisma.py
"""
snapshot_id = alert['resource_id']
region = alert['region']
ec2 = session.client('ec2', region_name=region)
try:
snap_attrib = ec2.describe_snapshot_attribute(
Attribute = 'createVolumePermission',
SnapshotId = snapshot_id
)
except ClientError as e:
print(e.response['Error']['Message'])
return
vol_perms = snap_attrib['CreateVolumePermissions'] if ('CreateVolumePermissions' in snap_attrib) else ''
public = False
for perm in vol_perms:
try:
if perm['Group'] == 'all':
public = True
except KeyError:
continue
if public == True:
result = remove_pub_snapshot_attrib(ec2, snapshot_id)
return
def remove_pub_snapshot_attrib(ec2, snapshot_id):
"""
Remove Public Snaphot Attribute
"""
try:
result = ec2.modify_snapshot_attribute(
Attribute = 'createVolumePermission',
GroupNames = [
'all',
],
OperationType = 'remove',
SnapshotId = snapshot_id
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print('Removed "Public" attribute from EBS snapshot {}.'.format(snapshot_id))
return