· 6 years ago · Aug 21, 2019, 09:54 AM
1#!/usr/bin/env python3
2
3try:
4 import boto3
5 from datetime import date
6 import os
7 import sys
8 import re
9except Exception as e:
10 print("[+] Unable to import modules: %s" % e)
11# check users keys
12user = input("[+] Please provide your AWS username: ")
13try:
14 client = boto3.client('iam')
15 res = client.list_access_keys(UserName=user)
16 kDate=(res['AccessKeyMetadata'][0]['CreateDate'].date())
17 keyId = (res['AccessKeyMetadata'][0]['AccessKeyId'])
18 print("[+] The current AWS access key id is: %s" % keyId)
19 today = date.today()
20 days = today - kDate
21 if days.days >= 90:
22 print("[+] CRITICAL: You access keys have exceeded the 90 days threshold\r\n[+] It is important to update the access keys!")
23 answer = input("[+] Would you like to do this now? [yes/no] : ")
24 lAnswer = answer.lower()
25
26 if re.match("y.*",lAnswer) and len(lAnswer) <= 3 and lAnswer.isalpha():
27 print("updating keys")
28 # create new key
29 r2 = client.create_access_key(UserName=user)
30 newKeyId=(r2['AccessKey']['AccessKeyId'])
31 secretKey=(r2['AccessKey']['SecretAccessKey'])
32 print("[+] The new Access Key ID is: %s" % newKeyId)
33 print("[+] The new Secret Access Key is: %s" % secretKey)
34 print("[+] Configure your aws access by providing the new keys")
35 os.system("aws configure")
36 print("[+] Keys configured")
37 # delete old key from aws
38 r3 = client.delete_access_key(UserName=user,AccessKeyId=keyId)
39 print("[+] Old access key deleted from AWS")
40 elif re.match("n.*",lAnswer) and len(lAnswer) <= 2 and lAnswer.isalpha():
41 print("[+] Ok, please keep in mind you should update your keys soon")
42 sys.exit(1)
43 else:
44 print("[+] No proper answer defined\r\n[+] Aborting...")
45 sys.exit(1)
46 elif days.days > 75 and days.days < 90:
47 print("[+] Your access keys will soon expire")
48 daysLeft = 90 - days.days
49 print("[+] %s days left to update" % daysLeft)
50 else:
51 print("[+] No need to update your keys")
52except Exception as e:
53 print("[+] Unable to contact AWS services due to: %s" % e)
54 sys.exit(1)