This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
forked from PiotrMachowski/Xiaomi-cloud-tokens-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_extractor.py
217 lines (197 loc) · 7.95 KB
/
token_extractor.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
import base64
import hashlib
import hmac
import json
import os
import time
import requests
import random
from getpass import getpass
from Crypto.Hash import MD5, SHA256
class XiaomiCloudConnector:
def __init__(self, username, password):
self._username = username
self._password = password
self._agent = self.generate_agent()
self._device_id = self.generate_device_id()
self._session = requests.session()
self._sign = None
self._ssecurity = None
self._userId = None
self._cUserId = None
self._passToken = None
self._location = None
self._code = None
self._serviceToken = None
def login_step_1(self):
url = "https://account.xiaomi.com/pass/serviceLogin?sid=xiaomiio&_json=true"
headers = {
"User-Agent": self._agent,
"Content-Type": "application/x-www-form-urlencoded"
}
cookies = {
"userId": self._username
}
response = self._session.get(url, headers=headers, cookies=cookies)
valid = response.status_code == 200 and "_sign" in self.to_json(response.text)
if valid:
self._sign = self.to_json(response.text)["_sign"]
return valid
def login_step_2(self):
url = "https://account.xiaomi.com/pass/serviceLoginAuth2"
headers = {
"User-Agent": self._agent,
"Content-Type": "application/x-www-form-urlencoded"
}
fields = {
"sid": "xiaomiio",
"hash": (MD5.new(str.encode(self._password)).hexdigest() + "").upper(),
"callback": "https://sts.api.io.mi.com/sts",
"qs": "%3Fsid%3Dxiaomiio%26_json%3Dtrue",
"user": self._username,
"_sign": self._sign,
"_json": "true"
}
response = self._session.post(url, headers=headers, params=fields)
valid = response.status_code == 200 and "ssecurity" in self.to_json(response.text)
if valid:
json_resp = self.to_json(response.text)
self._ssecurity = json_resp["ssecurity"]
self._userId = json_resp["userId"]
self._cUserId = json_resp["cUserId"]
self._passToken = json_resp["passToken"]
self._location = json_resp["location"]
self._code = json_resp["code"]
return valid
def login_step_3(self):
headers = {
"User-Agent": self._agent,
"Content-Type": "application/x-www-form-urlencoded"
}
response = self._session.get(self._location, headers=headers)
if response.status_code == 200:
self._serviceToken = response.cookies.get("serviceToken")
return response.status_code == 200
def login(self):
self._session.cookies.set("sdkVersion", "accountsdk-18.8.15", domain="mi.com")
self._session.cookies.set("sdkVersion", "accountsdk-18.8.15", domain="xiaomi.com")
self._session.cookies.set("deviceId", self._device_id, domain="mi.com")
self._session.cookies.set("deviceId", self._device_id, domain="xiaomi.com")
if self.login_step_1():
if self.login_step_2():
if self.login_step_3():
return True
else:
print("Unable to get service token.")
else:
print("Invalid login or password.")
else:
print("Invalid username.")
return False
def get_devices(self, country):
url = self.get_api_url(country) + "/home/device_list"
params = {
"data": '{"getVirtualModel":false,"getHuamiDevices":0}'
}
return self.execute_api_call(url, params)
def execute_api_call(self, url, params):
headers = {
"Accept-Encoding": "gzip",
"User-Agent": self._agent,
"Content-Type": "application/x-www-form-urlencoded",
"x-xiaomi-protocal-flag-cli": "PROTOCAL-HTTP2"
}
cookies = {
"userId": str(self._userId),
"yetAnotherServiceToken": str(self._serviceToken),
"serviceToken": str(self._serviceToken),
"locale": "en_GB",
"timezone": "GMT+02:00",
"is_daylight": "1",
"dst_offset": "3600000",
"channel": "MI_APP_STORE"
}
millis = round(time.time() * 1000)
nonce = self.generate_nonce(millis)
signed_nonce = self.signed_nonce(nonce)
signature = self.generate_signature(url.replace("/app", ""), signed_nonce, nonce, params)
fields = {
"signature": signature,
"_nonce": nonce,
"data": params["data"]
}
response = self._session.post(url, headers=headers, cookies=cookies, params=fields)
if response.status_code == 200:
return response.json()
return None
def get_api_url(self, country):
return "https://" + ("" if country == "cn" else (country + ".")) + "api.io.mi.com/app"
def signed_nonce(self, nonce):
hash_object = SHA256.new()
hash_object.update(base64.b64decode(self._ssecurity) + base64.b64decode(nonce))
return base64.b64encode(hash_object.digest()).decode('utf-8')
@staticmethod
def generate_nonce(millis):
nonce_bytes = os.urandom(8) + (int(millis / 60000)).to_bytes(4, byteorder='big')
return base64.b64encode(nonce_bytes).decode()
@staticmethod
def generate_agent():
agent_id = "".join(map(lambda i: chr(i), [random.randint(65, 69) for _ in range(13)]))
return f"Android-7.1.1-1.0.0-ONEPLUS A3010-136-{agent_id} APP/xiaomi.smarthome APPV/62830"
@staticmethod
def generate_device_id():
return "".join(map(lambda i: chr(i), [random.randint(97, 122) for _ in range(6)]))
@staticmethod
def generate_signature(url, signed_nonce, nonce, params):
signature_params = [url.split("com")[1], signed_nonce, nonce]
for k, v in params.items():
signature_params.append(f"{k}={v}")
signature_string = "&".join(signature_params)
signature = hmac.new(base64.b64decode(signed_nonce), msg=signature_string.encode(), digestmod=hashlib.sha256)
return base64.b64encode(signature.digest()).decode()
@staticmethod
def to_json(response_text):
return json.loads(response_text.replace("&&&START&&&", ""))
username = input("username:")
password = getpass(prompt='Password:')
country = input("Country (one of: ru, us, tw, sg, cn, de) Leave empty to check all available:")
while country not in ["", "ru", "us", "tw", "sg", "cn", "de"]:
print("Invalid country provided. Valid values: ru, us, tw, sg, cn, de")
print("Country:")
country = input()
print()
countries = ["cn", "de", "us", "ru", "tw", "sg"]
if not country == "":
countries = [country]
connector = XiaomiCloudConnector(username, password)
print("Logging in...")
logged = connector.login()
if logged:
print("Logged in.")
log = open("xiaomi_extractor.log", "a")
print()
for current_country in countries:
devices = connector.get_devices(current_country)
if devices is not None:
if len(devices["result"]["list"]) == 0:
print(f"No devices found for country \"{current_country}\".")
continue
print(f"Devices found for country \"{current_country}\":")
for device in devices["result"]["list"]:
print(" ---------------------------------------------")
log.write(" ---------------------------------------------")
if "name" in device:
print(" NAME: " + device["name"])
log.write(" NAME: " + device["name"])
print(json.dumps(device, indent=6))
log.write(json.dumps(device, indent=6))
print()
print()
else:
print("Unable to get devices.")
log.close()
else:
print("Unable to log in.")
print()
print("Press ENTER to finish")
input()