forked from PaloAltoNetworks/Prisma-Enhanced-Remediation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWS-CONFIG-001.py
315 lines (250 loc) · 7.56 KB
/
AWS-CONFIG-001.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""
Remediate Prisma Policy:
AWS:CONFIG-001 AWS Config is not enabled
Description:
The AWS Config service provides visibility to user configuration history and configuration change
notifications. Along with use of CloudTrail, we consider enabling AWS Config to be a best practice for users.
**Note: No Config Rules are created.
Required Permissions:
- iam:AttachRolePolicy
- iam:CreateRole
- iam:GetRole
- s3:CreateBucket
- s3:PutBucketPolicy
- config:PutConfigurationRecorder
- config:PutDeliveryChannel
- config:StartConfigurationRecorder
Sample IAM Policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "IAMPermissions",
"Action": [
"iam:AttachRolePolicy",
"iam:CreateRole",
"iam:GetRole"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "S3Permissions",
"Action": [
"s3:CreateBucket",
"s3:PutBucketPolicy"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "ConfigPermissions",
"Action": [
"config:PutConfigurationRecorder",
"config:PutDeliveryChannel",
"config:StartConfigurationRecorder"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
"""
import json
import boto3
from botocore.exceptions import ClientError
from time import sleep
def remediate(session, alert, lambda_context):
"""
Main Function invoked by index_prisma.py
"""
region = alert['region']
iam = session.client('iam', region_name=region)
s3 = session.client('s3', region_name=region)
config = session.client('config', region_name=region)
# Create IAM Role
role_arn, account_id = new_iam_role(iam, region)
# Create Config Recorder
recorder_name = new_config_recorder(config, role_arn) if (role_arn != 'fail') else 'fail'
# Create S3 Bucket
bucket_name = new_s3_bucket(s3, account_id, region) if (role_arn != 'fail') else 'fail'
# Create Config Delivery Channel
channel_name = new_config_channel(config, bucket_name) if (bucket_name != 'fail') else 'fail'
# Start Config Recorder
start_result = start_recorder(config, recorder_name) if (channel_name != 'fail' and recorder_name != 'fail') else 'fail'
# Results
if start_result != 'fail':
print('AWS Config enabled in region {}.'.format(region))
else:
print('Failed to enable AWS Config in region {}.'.format(region))
return
def new_iam_role(iam, region):
"""
Create new IAM Role and attach AWS Config Managed Policy
"""
role_name = 'config-role-' + region
try:
role = iam.create_role(
Path = '/',
RoleName = role_name,
AssumeRolePolicyDocument = json.dumps(RoleTemplate.RolePolicy)
)
role_arn = role['Role']['Arn']
print('New IAM Role created: {}'.format(role_arn))
sleep(10) # Wait for IAM resource to be available. >> Gotta be a better way.. { wait? get? }.
except ClientError as e:
if e.response['Error']['Code'] == 'EntityAlreadyExists':
role = iam.get_role(
RoleName = role_name
)
role_arn = role['Role']['Arn']
print('Using existing IAM Role: {}'.format(role_arn))
else:
print(e.response['Error']['Message'])
return 'fail', None
try:
result = iam.attach_role_policy(
RoleName = role_name,
PolicyArn = 'arn:aws:iam::aws:policy/service-role/AWSConfigRole'
)
except ClientError as e:
print(e.response['Error']['Message'])
return 'fail', None
account_id = role_arn.split(':')[4]
return role_arn, account_id
def new_s3_bucket(s3, account_id, region):
"""
Create new S3 Bucket
"""
bucket_name = 'config-bucket-' + account_id
try:
if region == 'us-east-1':
result = s3.create_bucket(
ACL = 'private',
Bucket = bucket_name
)
else:
result = s3.create_bucket(
ACL = 'private',
Bucket = bucket_name,
CreateBucketConfiguration = {'LocationConstraint': region}
)
print('New S3 bucket created: {}'.format(bucket_name))
except ClientError as e:
if e.response['Error']['Code'] == 'BucketAlreadyExists':
print('Using existing S3 bucket: {}'.format(bucket_name))
elif e.response['Error']['Code'] == 'BucketAlreadyOwnedByYou':
print('Using already owned and existing S3 bucket: {}'.format(bucket_name))
else:
print(e.response['Error']['Message'])
return 'fail'
try:
result = s3.put_bucket_policy(
Bucket = bucket_name,
Policy = json.dumps(BucketTemplate.BucketPolicy(bucket_name, account_id))
)
except ClientError as e:
print(e.response['Error']['Message'])
return 'fail'
return bucket_name
def new_config_recorder(config, role_arn):
"""
Create new Config Recorder
"""
recorder_name = 'default'
try:
result = config.put_configuration_recorder(
ConfigurationRecorder = {
'name': recorder_name,
'roleARN': role_arn,
'recordingGroup': {
'allSupported': True
}
}
)
print('New Config recorder created: {}'.format(recorder_name))
except ClientError as e:
print(e.response['Error']['Message'])
return 'fail'
return recorder_name
def new_config_channel(config, bucket_name):
"""
Create new Config Delivery Channel
"""
channel_name = 'default'
try:
result = config.put_delivery_channel(
DeliveryChannel = {
'name': channel_name,
's3BucketName': bucket_name,
'configSnapshotDeliveryProperties': {
'deliveryFrequency': 'One_Hour'
}
}
)
print('New Config delivery channel created: {}'.format(channel_name))
except ClientError as e:
print(e.response['Error']['Message'])
return 'fail'
return channel_name
def start_recorder(config, recorder_name):
"""
Start Config Recorder
"""
try:
result = config.start_configuration_recorder(
ConfigurationRecorderName = recorder_name
)
except ClientError as e:
print(e.response['Error']['Message'])
return 'fail'
return
class RoleTemplate:
RolePolicy = {
'Version': '2012-10-17',
'Statement': [
{
'Sid': '',
'Effect': 'Allow',
'Principal': {
'Service': 'config.amazonaws.com'
},
'Action': 'sts:AssumeRole'
}
]
}
class BucketTemplate():
def BucketPolicy(bucket_name, account_id):
Policy = {
'Version': '2012-10-17',
'Statement': [
{
'Sid': 'AWSConfigBucketPermissionsCheck',
'Effect': 'Allow',
'Principal': {
'Service': [
'config.amazonaws.com'
]
},
'Action': 's3:GetBucketAcl',
'Resource': 'arn:aws:s3:::' + bucket_name
},
{
'Sid': ' AWSConfigBucketDelivery',
'Effect': 'Allow',
'Principal': {
'Service': [
'config.amazonaws.com'
]
},
'Action': 's3:PutObject',
'Resource': 'arn:aws:s3:::' + bucket_name + '/AWSLogs/' + account_id + '/Config/*',
'Condition': {
'StringEquals': {
's3:x-amz-acl': 'bucket-owner-full-control'
}
}
}
]
}
return Policy