-
Notifications
You must be signed in to change notification settings - Fork 0
/
SAML-Standalone-MFA-Caching-Key.py
194 lines (163 loc) · 6.06 KB
/
SAML-Standalone-MFA-Caching-Key.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
import requests
import json
import getpass
import re
import os
### SET THESE VARIABLES ###
identity_subdomain = "example" # e.g https://EXAMPLE.id.cyberark.cloud
pvwa_saml_app_key = os.getenv("pvwa_saml_app_key") # Retrieved from the SAML web app in Identity.
pvwa_url = "example.privilegecloud.cyberark.cloud" # Do not include https://
username = "paul@example.com"
key_path = "C:/Users/Paul/Desktop" # Directory to store the SSH key. No trailing /.
key_format = "PPK" # PEM, PPK or OPENSSH
require_ssl_verify = True # Set to False for self-signed certs (but use at your own risk)
# Ignore SSL cert warnings from requests module?
if require_ssl_verify is False:
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
### MFA MECHANISM OBJECT ###
class Mechanism():
def __init__(self, name, id):
self.name = name
self.id = id
### STEP 1: START AUTH WITH CYBERARK IDENTITY ###
password = getpass.getpass(f"Enter the password for {username}:\n")
url = f"https://{identity_subdomain}.id.cyberark.cloud/Security/StartAuthentication"
payload = json.dumps({
"User": username,
"Version": "1.0"
})
headers = {
'Content-Type': 'application/json',
'X-IDAP-NATIVE-CLIENT': 'true'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=require_ssl_verify)
json_object = json.loads(response.text)
# Parse response for important variables
session_id = json_object['Result']['SessionId']
tenant_id = json_object['Result']['TenantId']
pwd_mechanism_id = json_object['Result']['Challenges'][0]['Mechanisms'][0]['MechanismId']
mechanisms = json_object['Result']['Challenges'][1]['Mechanisms']
# Create list of MFA Mechanisms
options = []
for mechanism in mechanisms:
name = mechanism['Name']
id = mechanism['MechanismId']
obj = Mechanism(name,id)
options.append(obj)
# Print list of MFA mechanisms available to user (if there's more than one option)
if len(options) > 1:
i = 0
for option in options:
i = i + 1
print(f"{i}. {option.name}")
# Allow user to choose their MFA modality
choice = 0
while True:
if len(options) == 1:
break # No need to bother here if we only have one choice.
if len(options) < 1:
print(f"Doesn't look like any MFA options are configured for {username}. Exiting.")
exit()
choice = input("\nPlease choose an MFA mechanism: ")
error = f"ERROR: Your response must be an integer between 1 and {len(options)}."
try:
choice = int(choice)
except:
print(error)
continue
if 0 < choice <= len(options):
choice = choice - 1
break
print(error)
mfa_mechanism_name = options[choice].name
mfa_mechanism_id = options[choice].id
### STEP 2: ADVANCE AUTH WITH USERNAME AND PASSWORD ###
url = f"https://{identity_subdomain}.id.cyberark.cloud/Security/AdvanceAuthentication"
payload = json.dumps({
"SessionId": session_id,
"MechanismId": pwd_mechanism_id,
"Action": "Answer",
"Answer": password
})
headers = {
'X-IDAP-NATIVE-CLIENT': 'true',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=require_ssl_verify)
### STEP 3: ADVANCE AUTH WITH SELECTED MFA MODALITY ###
payload = json.dumps({
"SessionId": session_id,
"MechanismId": mfa_mechanism_id,
"Action": "StartOOB"
})
headers = {
'X-IDAP-NATIVE-CLIENT': 'true',
'Content-Type': 'application/json'
}
# Give user a chance to respond to their MFA challenge.
print(f"Requesting {mfa_mechanism_name} challenge...\n")
response = requests.request("POST", url, headers=headers, data=payload, verify=require_ssl_verify)
if response:
input(f"{mfa_mechanism_name} challenge sent. Press enter once you have completed it.\n")
### STEP 4: POLL FOR TOKEN ONCE MFA CHALLENGE HAS BEEN COMPLETED ###
payload = json.dumps({
"TenantId": tenant_id,
"SessionId": session_id,
"MechanismId": mfa_mechanism_id,
"Action": "Poll"
})
headers = {
'X-IDAP-NATIVE-CLIENT': 'true',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=require_ssl_verify)
json_object = json.loads(response.text)
token = json_object['Result']['Auth']
### STEP 5: GET SAMLRESPONSE FROM HANDLEAPPCLICK ENDPOINT ###
url = f'https://{identity_subdomain}.id.cyberark.cloud/uprest/HandleAppClick'
payload = json.dumps({
'appkey': pvwa_saml_app_key,
'antixss': 'AOk1nyB5OyLW0ovo_iiyZw__',
'markAppVisited': 'true'
})
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers, data=payload, verify=require_ssl_verify)
response = re.search(r'value=\"(.*)\"', response.text)
samlresponse = response.group(1)
### STEP 6: AUTH TO PVWA WITH SAMLRESPONSE TO GET SESSION TOKEN ###
url = f"https://{pvwa_url}/PasswordVault/API/auth/SAML/Logon"
payload = {
'concurrentSession': 'true',
'apiUse': 'true',
'SAMLResponse': samlresponse
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=require_ssl_verify)
token = response.text.replace('"',"") # token gets returned inside quotemarks
### STEP 7: GENERATE MFA CACHING KEY USING TOKEN ###
key_path = key_path.replace('//','/') # In case of a trailing slash in the path
url = f"https://{pvwa_url}/passwordvault/api/users/secret/sshkeys/cache"
payload = json.dumps({
"formats": [f"{key_format}"]
})
headers = {
'Authorization': f'{token}',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=require_ssl_verify)
json_object = json.loads(response.text)
try:
key = json_object['value'][0]['privateKey']
key = re.sub("\r","",key)
file = f"{key_path}/mfa_caching_key.{key_format.lower()}"
with open(file, "w") as f:
f.write(key)
print(f"Key successfully downloaded to {key_path}/mfa_caching_key.{key_format.lower()}.")
except:
print("Sorry, an error occurred. Please check your settings and try again.")