-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathscan.py
124 lines (92 loc) · 4.01 KB
/
scan.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
import openai
import boto3
import argparse
import re
import os
import csv
import random
from core.policy import *
from datetime import datetime
parser = argparse.ArgumentParser(description='Retrieve all customer managed policies and check the default policy version for vulnerabilities')
parser.add_argument('--key', type=str, required=True, help='OpenAI API key')
parser.add_argument('--profile', type=str, default='default', help='AWS profile name to use (default: default)')
parser.add_argument('--redact', action='store_true', default=True, help='Redact sensitive information in the policy document (default: True)')
results = []
openai.api_key = ''
def redact_policy(policy):
new_policy = policy
new_policy.original_document = str(policy.policy)
match = re.search(r'\b\d{12}\b', new_policy.original_document)
if match:
original_account = match.group()
new_account = random.randint(100000000000, 999999999999)
new_policy.map_accounts(original_account, new_account)
new_policy.redacted_document = new_policy.original_document.replace(original_account, str(new_account))
else:
new_policy.redacted_document = new_policy.original_document
return new_policy
def check_policy(policy):
prompt = f'Does this AWS policy have any security vulnerabilities: \n{policy.redacted_document}'
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.5,
max_tokens=1000,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.0,
stream=False,
)
policy.ai_response = response.choices[0]['text'].strip()
is_vulnerable = policy.is_vulnerable()
log(f'Policy {policy.name} [{is_vulnerable}]')
return policy
def preserve(filename, results):
header = ['account', 'name', 'arn', 'version', 'vulnerable', 'policy', 'mappings']
mode = 'a' if os.path.exists(filename) else 'w'
log(f'Saving scan: {filename}')
with open(filename, mode) as f:
writer = csv.DictWriter(f, fieldnames=header)
if mode == 'w':
writer.writeheader()
for data in results:
mappings = '' if len(data.retrieve_mappings()) == 0 else data.retrieve_mappings()
row = {
'account': data.account, 'name': data.name, 'arn': data.arn,
'version': data.version, 'vulnerable': data.ai_response, 'policy':
data.original_document, 'mappings': mappings
}
writer.writerow(row)
def log(data):
print(f'[*] {data}')
def main(args):
openai.api_key = args.key
session = boto3.Session(profile_name=args.profile)
scan_utc = datetime.utcnow().strftime("%Y-%m-%d-%H%MZ")
client = session.client('iam')
account = session.client('sts').get_caller_identity().get('Account')
log(f'Retrieving and redacting policies for account: {account}')
paginator = client.get_paginator('list_policies')
response_iterator = paginator.paginate(Scope='Local', OnlyAttached=False)
for response in response_iterator:
for policy in response['Policies']:
policy_name = policy['PolicyName']
policy_arn = policy['Arn']
policy_version = client.get_policy_version(PolicyArn=policy['Arn'], VersionId=policy['DefaultVersionId'])
default_version = policy_version['PolicyVersion']['VersionId']
if not policy_arn.startswith("arn:aws:iam::aws"):
policy_document = ''
p = Policy()
p.account = account
p.arn = policy_arn
p.name = policy_name
p.policy = policy_version['PolicyVersion']['Document']
p.version = default_version
if args.redact:
p = redact_policy(p)
p = check_policy(p)
results.append(p)
preserve(f'cache/{account}_{scan_utc}.csv', results)
if __name__ == '__main__':
args = parser.parse_args()
main(args)