-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
376 lines (229 loc) · 10.1 KB
/
main.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
Creates an AWS root account as automatically as possible. Saves the credentials
in the working directory.
"""
import secrets
import string
import traceback
import configparser
import pathlib
import urllib
import io
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from IPython.core.debugger import set_trace
def main():
try:
config = read_root_user_config()
config["account_credentials"]["root_user_password"] = (
generate_root_user_password()
)
save_credentials(config["account_credentials"])
driver = init_driver()
create_root_account(driver, config)
except Exception: # pylint: disable=broad-except
traceback.print_exc()
finally:
set_trace()
driver.close()
def read_root_user_config():
path = pathlib.Path.home().joinpath(".aws", "root_user_config.ini")
config = configparser.ConfigParser()
config.read_string(substitute(path.read_text()))
return config
def substitute(template_str):
return string.Template(template_str).substitute(
random=''.join(secrets.choice(string.ascii_lowercase) for _ in range(6))
)
def create_root_account(driver, config):
visit_signup_page(driver)
submit_account_credentials(driver, config["account_credentials"])
submit_contact_information(driver, config["contact_information"])
submit_billing_information(driver, config["billing_information"])
confirm_identity(driver, config["identity_verification"])
select_support_plan(driver)
def init_driver():
return webdriver.Firefox()
def visit_signup_page(driver):
driver.get("https://portal.aws.amazon.com/billing/signup")
def submit_account_credentials(driver, creds):
submit_account_identity(driver, creds)
submit_email_confirmation(driver)
submit_account_password(driver, creds)
def submit_account_identity(driver, creds):
wait_for_message(driver, "Sign up for AWS")
set_account_email_address(driver, creds["root_user_email_address"])
set_account_name(driver, creds["aws_account_name"])
hit_continue(driver, "Verify email address")
def submit_email_confirmation(driver):
wait_for_message(driver, "Confirm you are you")
set_verification_code(driver, input("Email verification code: "))
hit_continue(driver, "Verify")
def submit_account_password(driver, creds):
wait_for_message(driver, "Create your password")
set_account_password(driver, creds["root_user_password"])
hit_continue(driver)
def extract_img_captcha(driver):
img = driver.find_element(By.XPATH, "//img[@alt='captcha']")
url = img.get_property("src")
with urllib.request.urlopen(url) as response:
png_file = io.BytesIO(response.read())
return png_file
def submit_contact_information(driver, info):
wait_for_message(driver, "Contact Information")
set_purpose(driver, "Personal")
set_contact_name(driver, info["name"])
set_contact_phone_country_code(driver, info["phone_country_code"])
set_contact_phone_number(driver, info["phone_number"])
set_country(driver, info["country"])
set_address_line_1(driver, info["address_line_1"])
set_address_line_2(driver, info["address_line_2"])
set_city(driver, info["city"])
set_state(driver, info["state"])
set_postal_code(driver, info["postal_code"])
agree_to_terms(driver)
hit_continue(driver)
def submit_billing_information(driver, info):
wait_for_message(driver, "Billing Information")
set_card_number(driver, info["card_number"])
set_card_expiry_month(driver, info["card_expiry_month"])
set_card_expiry_year(driver, info["card_expiry_year"])
set_card_holder_name(driver, info["card_holder_name"])
use_contact_address_as_billing_address(driver)
hit_continue(driver)
def confirm_identity(driver, info):
# If the prompt appears, choose SMS because it's easier to use manually.
# If the prompt does not appear, AWS forces you to receive a call.
# Sometimes the prompt appears, sometimes it doesn't. I don't know why.
# TODO: Try to automate the call confirmation to avoid this choice.
wait_for_message(driver, "Confirm your identity")
body_text = driver.find_element(By.XPATH, "//body").text
choice_prompt = "How should we send you the verification code?"
if choice_prompt in body_text:
confirm_identity_by_sms(driver, info)
else:
confirm_identity_by_call(driver, info)
def confirm_identity_by_sms(driver, info):
wait_for_message(driver, "Confirm your identity")
set_verification_method(driver, "Text message (SMS)")
set_verification_phone_country_code(driver, info["phone_country_code"])
set_verification_phone_number(driver, info["phone_number"])
set_captcha_guess(driver, solve_captcha(extract_img_captcha(driver)))
hit_continue(driver, button_label="Send SMS")
wait_for_message(driver, "Verify code")
set_sms_pin(driver, input("Verify code: "))
hit_continue(driver)
def confirm_identity_by_call(driver, info):
raise Exception("Identity confirmation by call not implemented")
def select_support_plan(driver):
wait_for_message(driver, "Select a support plan")
set_support_plan(driver, "Basic")
hit_continue(driver, button_label="Complete sign up")
def generate_root_user_password():
# "Your password must include a minimum of three of the following mix of character types:
# uppercase, lowercase, numbers, and ! @ # $ % ^ & * () <> [] {} | _+-= symbols."
# The append ensures that all character classes are represented.
symbols = "!@#$%^&*()<>[]{}|_+-="
classes = [string.ascii_uppercase, string.ascii_lowercase, string.digits, symbols]
password = ''.join(secrets.choice(''.join(classes)) for i in range(8))
password += ''.join(map(secrets.choice, classes))
return password
def save_credentials(creds):
file_name = "credentials-{aws_account_name}.txt".format_map(creds)
with open(file_name, "w") as out:
template = "{aws_account_name},{root_user_email_address},{root_user_password}\n"
csv = template.format_map(creds)
out.write(csv)
def wait_for_message(driver, message, timeout=600):
WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.XPATH, f"//*[text() = '{message}']")))
def hit_continue(driver, button_label="Continue"):
# Use submit() function to avoid "not clickable" errors because of the
# button being obscured.
button = driver.find_element(By.XPATH, f"//button[contains(span/text(), '{button_label}')]")
button.submit()
def set_account_email_address(driver, address):
set_text(driver, "emailAddress", address)
def set_verification_code(driver, code):
set_text(driver, "otp", code)
def set_account_password(driver, password):
set_text(driver, "password", password)
set_text(driver, "rePassword", password)
def set_account_name(driver, name):
set_text(driver, "fullName", name)
def set_purpose(driver, purpose):
set_radio(driver, "accountType", purpose)
def set_contact_name(driver, name):
set_text(driver, "address.fullName", name)
def set_country(driver, country):
set_dropdown(driver, "address.country", country)
def set_contact_phone_country_code(driver, code):
set_dropdown(driver, "address.phoneCode", code)
def set_contact_phone_number(driver, number):
set_text(driver, "address.phoneNumber", number)
def set_address_line_1(driver, line):
set_text(driver, "address.addressLine1", line)
def set_address_line_2(driver, line):
set_text(driver, "address.addressLine2", line)
def set_city(driver, city):
set_text(driver, "address.city", city)
def set_state(driver, state):
set_text(driver, "address.state", state)
def set_postal_code(driver, code):
set_text(driver, "address.postalCode", code)
def agree_to_terms(driver):
set_checkbox(driver, "agreement")
def set_card_number(driver, number):
set_text(driver, "cardNumber", number)
def set_card_expiry_month(driver, month):
set_dropdown(driver, "expirationMonth", month)
def set_card_expiry_year(driver, year):
set_dropdown(driver, "expirationYear", year)
def set_card_holder_name(driver, name):
set_text(driver, "accountHolderName", name)
def use_contact_address_as_billing_address(driver):
set_radio(driver, "addressType", "Use my contact address")
def set_verification_method(driver, method):
set_radio(driver, "divaMethod", method)
def set_verification_phone_country_code(driver, country_code):
set_dropdown(driver, "country", country_code)
def set_verification_phone_number(driver, number):
set_text(driver, "phoneNumber", number)
def set_sms_pin(driver, pin):
set_text(driver, "smsPin", pin)
def set_support_plan(driver, plan):
set_radio(driver, "awsui-tiles-6", plan)
def set_captcha_guess(driver, guess):
set_text(driver, "captchaGuess", guess)
def solve_captcha(captcha_file):
show_image(captcha_file)
return input("Type the characters shown above: ")
def set_dropdown(driver, element_id, option):
dropdown = driver.find_element(By.XPATH, f"//awsui-select[@id='{element_id}']")
dropdown.click()
option = dropdown.find_element(By.XPATH,
f"//*[contains(@id, 'dropdown-option')]//span[text() = '{option}']")
option.click()
def set_text(driver, input_name, value):
field = driver.find_element(By.NAME, input_name)
field.clear()
field.send_keys(value)
def set_radio(driver, input_name, label):
field = driver.find_element(By.XPATH,
f"//div[@class = 'awsui-radio-button' and .//span[contains(text(), '{label}')]]"
f"//input[@name = '{input_name}']")
field.send_keys(" ")
def set_checkbox(driver, input_name, selected=True):
checkbox = driver.find_element(By.NAME, input_name)
current = checkbox.is_selected()
desired = selected
if current != desired:
checkbox.send_keys(" ")
def show_image(image_file):
with Image.open(image_file) as i:
i.show()
if __name__ == '__main__':
main()