-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathgetcloudpassword.py
executable file
·185 lines (154 loc) · 6.99 KB
/
getcloudpassword.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
#!/usr/bin/env python3
# Copyright 2021 Matthew Garrett <mjg59@srcf.ucam.org>
#
# Portions Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All
# Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the
# "License"). You may not use this file except in compliance with the
# License. A copy of the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
import sys, os, base64, datetime, hashlib, hmac
import random
import requests
import time
import urllib.parse
class awsRequest:
def __init__(self, region, access_key, secret_key, session_token, service):
self.region = region
self.access_key = access_key
self.secret_key = secret_key
self.session_token = session_token
self.service = service
def sign(self, key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(self, key, dateStamp, regionName, serviceName):
kDate = self.sign(('AWS4' + key).encode('utf-8'), dateStamp)
kRegion = self.sign(kDate, regionName)
kService = self.sign(kRegion, serviceName)
kSigning = self.sign(kService, 'aws4_request')
return kSigning
def get(self, host, uri, query=""):
method = "GET"
# Create a date for headers and the credential string
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
canonical_uri = uri
canonical_querystring = query
canonical_headers = 'host:' + host + '\n' + 'x-amz-date:' + amzdate + '\n' + 'x-amz-security-token:' + self.session_token + '\n'
signed_headers = 'host;x-amz-date;x-amz-security-token'
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + self.region + '/' + self.service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' + amzdate + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
signing_key = self.getSignatureKey(self.secret_key, datestamp, self.region, self.service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + self.access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
headers = {'x-amz-security-token': self.session_token, 'x-amz-date':amzdate, 'Authorization':authorization_header}
req = "https://%s%s" % (host, uri)
if query != "":
req += "?%s" % query
return requests.get(req, headers=headers)
class irobotAuth:
def __init__(self, username, password):
self.username = username
self.password = password
def login(self):
r = requests.get("https://disc-prod.iot.irobotapi.com/v1/discover/endpoints?country_code=US")
response = r.json()
deployment = response['deployments'][next(iter(response['deployments']))]
self.httpBase = deployment['httpBase']
iotBase = deployment['httpBaseAuth']
iotUrl = urllib.parse.urlparse(iotBase)
self.iotHost = iotUrl.netloc
region = deployment['awsRegion']
self.apikey = response['gigya']['api_key']
self.gigyaBase = response['gigya']['datacenter_domain']
data = {"apiKey": self.apikey,
"targetenv": "mobile",
"loginID": self.username,
"password": self.password,
"format": "json",
"targetEnv": "mobile",
}
r = requests.post("https://accounts.%s/accounts.login" % self.gigyaBase, data=data)
response = r.json()
'''
data = {"timestamp": int(time.time()),
"nonce": "%d_%d" % (int(time.time()), random.randint(0, 2147483647)),
"oauth_token": response.get('sessionInfo', {}).get('sessionToken', ''),
"targetEnv": "mobile"}
'''
uid = response['UID']
uidSig = response['UIDSignature']
sigTime = response['signatureTimestamp']
data = {
"app_id": "ANDROID-C7FB240E-DF34-42D7-AE4E-A8C17079A294",
"assume_robot_ownership": "0",
"gigya": {
"signature": uidSig,
"timestamp": sigTime,
"uid": uid,
}
}
r = requests.post("%s/v2/login" % self.httpBase, json=data)
response = r.json()
access_key = response['credentials']['AccessKeyId']
secret_key = response['credentials']['SecretKey']
session_token = response['credentials']['SessionToken']
self.data = response
self.amz = awsRequest(region, access_key, secret_key, session_token, "execute-api")
def get_robots(self):
return self.data['robots']
def get_maps(self, robot):
return self.amz.get(self.iotHost, '/dev/v1/%s/pmaps' % robot, query="activeDetails=2").json()
def get_newest_map(self, robot):
maps = self.get_maps(robot)
latest = ""
latest_time = 0
for map in maps:
if map['create_time'] > latest_time:
latest_time = map['create_time']
latest = map
return latest
def main():
import argparse, logging, json
loglevel = logging.DEBUG
LOG_FORMAT = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=LOG_FORMAT, datefmt=LOG_DATE_FORMAT, level=loglevel)
#-------- Command Line -----------------
parser = argparse.ArgumentParser(
description='Get password and map data from iRobot aws cloud service')
parser.add_argument(
'login',
nargs='*',
action='store',
type=str,
default=[],
help='iRobot Account Login and Password (default: None)')
parser.add_argument(
'-m', '--maps',
action='store_true',
default = False,
help='List maps (default: %(default)s)')
arg = parser.parse_args()
if len(arg.login) >= 2:
irobot = irobotAuth(arg.login[0], arg.login[1])
irobot.login()
robots = irobot.get_robots()
logging.info("Robot ID and data: {}".format(json.dumps(robots, indent=2)))
if arg.maps:
for robot in robots.keys():
logging.info("Robot ID {}, MAPS: {}".format(robot, json.dumps(irobot.get_maps(robot), indent=2)))
else:
logging.error("Please enter iRobot account login and password")
if __name__ == '__main__':
main()