import sys
import httplib
import urllib
import json
import socket
import hashlib
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
global phone
global group
phone = "???"
group = "???"
def raw_post(host, port, url, postparams, headparams):
# posturl = json.dumps(postparams)
posturl = urllib.urlencode(postparams)
conn = httplib.HTTPConnection(host, port=port)
conn.request("POST", url, posturl, headparams)
response = conn.getresponse()
headers = response.getheaders()
res = response.read()
results = {1: headers, 2: res}
return results
def padding(text):
mod = len(text) % 16
if mod != 0:
text = text +(16 - mod) * chr(16 - mod)
else:
text = text + 16 * chr(16)
return text
def decode_aes(text, key):
text = a2b_hex(text)
obj = AES.new(key, AES.MODE_ECB)
ans = obj.decrypt(text)
return ans
def encode_aes(text, key):
text = padding(text)
obj = AES.new(key, AES.MODE_ECB)
ans = obj.encrypt(text)
return ans
def login_id_get(host, port):
url = '/v1/user/login/id/get'
postparams = {}
postparams["loginAccount"] = phone
postparams["clientType"] = "1"
postparams["src"] = "0"
postparams["appId"] = "1000"
postparams["format"] = "2"
postparams["stamp"] = "20161114143601"
sign = hashlib.sha256(
"/v1/user/login/id/getappId=1000&clientType=1&format=2&loginAccount=" + phone + "&src=0&stamp=201611141436012f39d871a38a4841aab3be3837e39cf4"
).hexdigest()
postparams["sign"] = sign
headparams = {}
headparams["Content-Type"] = "application/x-www-form-urlencoded"
headparams["User-Agent"] = "Dalvik/2.1.0 (Linux; U; Android 5.0.2; ZTE A2015 Build/LRX22G)"
try:
results = raw_post(host, port, url, postparams, headparams)
headers = results[1] # response headers
res = str(results[2]) # response body
res_json = json.loads(res, encoding='utf-8')
errorCode = res_json['errorCode']
result = res_json['result']
#print result
login_id = result['loginId']
login(host, port, login_id)
except httplib.HTTPException as e:
print ':'
print e
print '\n'
sys.exit()
except socket.error as e_socket:
print ':'
print e_socket
print '\n'
sys.exit()
def login(host, port, login_id):
url = '/v1/user/login'
postparams = {}
postparams["loginAccount"] = phone
postparams["clientType"] = "1"
postparams["src"] = "0"
postparams["appId"] = "1000"
postparams["format"] = "2"
postparams["stamp"] = "20161114144901"
postparams["pushToken"] = "0407ed2e6ab"
postparams["pushType"] = "1"
#get hash password
pwd = hashlib.sha256("lijialin").hexdigest()
password = hashlib.sha256(login_id + pwd + "2f39d871a38a4841aab3be3837e39cf4").hexdigest()
postparams["password"] = password
#get sign
sign = hashlib.sha256("/v1/user/loginappId=1000&clientType=1&format=2&loginAccount=" + phone + "&password=" + password + "&pushToken=0407ed2e6ab&pushType=1&src=0&stamp=201611141449012f39d871a38a4841aab3be3837e39cf4").hexdigest()
postparams["sign"] = sign
headparams = {}
headparams["Content-Type"] = "application/x-www-form-urlencoded"
headparams["User-Agent"] = "Dalvik/2.1.0 (Linux; U; Android 5.0.2; ZTE A2015 Build/LRX22G)"
try:
results = raw_post(host, port, url, postparams, headparams)
headers = results[1] # response headers
res = str(results[2]) # response body
res_json = json.loads(res, encoding='utf-8')
errorCode = res_json['errorCode']
print "login errorCode:"#########################
print errorCode
if errorCode == "0":
result = res_json['result']
session_id = result['sessionId']
access_token = result['accessToken']
# for i in range(0, 9999):
# frag = str(i)
# if len(frag) < 4:
# frag = "0" * (4 - len(frag)) + frag
# home_bind(host, port, session_id, access_token, frag)
home_bind(host, port, session_id, access_token, "0364")
#appliance_list_get(host, port, session_id, access_token)
#delete(host, port, session_id, "17592186973172")
except httplib.HTTPException as e:
print ':'
print e
print '\n'
sys.exit()
except socket.error as e_socket:
print ':'
print e_socket
print '\n'
sys.exit()
def home_bind(host, port, session_id, access_token, frag):
url = '/v1/appliance/home/bind'
postparams = {}
postparams["applianceName"] = frag
postparams["src"] = "0"
postparams["format"] = "2"
postparams["applianceType"] = "0xb8"
postparams["stamp"] = "201611145701"
postparams["language"] = "zh_CN"
postparams["modelNumber"] = "1"
postparams["homegroupId"] = group
postparams["sessionId"] = session_id
#get referSn
data_key = decode_aes(access_token, "a979c60f245e8026")[0 : 16]
sn = "0000B811100R1105B1572030" + frag + "0000"
#print sn########
#sn = "0000B811100R1105B157203003640000"
refer_sn = b2a_hex(encode_aes(sn, data_key))
postparams["referSn"] = refer_sn
#get sign
sign = hashlib.sha256(
"/v1/appliance/home/bindapplianceName=" + frag + "&applianceType=0xb8&format=2&homegroupId=" + group + "&language=zh_CN&modelNumber=1&referSn=" + refer_sn + "&sessionId=" + session_id + "&src=0&stamp=2016111457012f39d871a38a4841aab3be3837e39cf4").hexdigest()
postparams["sign"] = sign
headparams = {}
headparams["Content-Type"] = "application/x-www-form-urlencoded"
headparams["User-Agent"] = "Dalvik/2.1.0 (Linux; U; Android 5.0.2; ZTE A2015 Build/LRX22G)"
try:
results = raw_post(host, port, url, postparams, headparams)
headers = results[1] # response headers
res = str(results[2]) # response body
res_json = json.loads(res, encoding='utf-8')
errorCode = res_json['errorCode']
# print res_json
#print "bind errorCode:"###################
print errorCode
if errorCode == "0":
result = res_json['result']
device_id = result['id']
info_bind_get(host, port, device_id, session_id, sn)
#appliance_list_get(port, host, session_id, data_key, device_id)
else:
msg = res_json['msg']
print msg##########
except httplib.HTTPException as e:
print ':'
print e
print '\n'
sys.exit()
except socket.error as e_socket:
print ':'
print e_socket
print '\n'
sys.exit()
def info_bind_get(host, port, device_id, session_id, sn):
url = '/v1/appliance/info/bind/get'
postparams = {}
postparams["applianceId"] = device_id
postparams["src"] = "0"
postparams["format"] = "2"
postparams["stamp"] = "20161114145801"
postparams["sessionId"] = session_id
#get sign
sign = hashlib.sha256(
"/v1/appliance/info/bind/getapplianceId=" + device_id + "&format=2&sessionId=" + session_id + "&src=0&stamp=201611141458012f39d871a38a4841aab3be3837e39cf4").hexdigest()
postparams["sign"] = sign
headparams = {}
headparams["Content-Type"] = "application/x-www-form-urlencoded"
headparams["User-Agent"] = "Dalvik/2.1.0 (Linux; U; Android 5.0.2; ZTE A2015 Build/LRX22G)"
try:
results = raw_post(host, port, url, postparams, headparams)
headers = results[1] # response headers
res = str(results[2]) # response body
res_json = json.loads(res, encoding='utf-8')
errorCode = res_json['errorCode']
if errorCode == "0":
result = res_json['result']
bind_status = result['bindStatus']
online_staus = result['onlineStatus']
print "bindStatus:"
print bind_status##############
print "onlineStatus:"
print online_staus
print result
if online_staus == "1":
print "deviceId:"
print device_id
print "sn:"
print sn
else:
delete(host, port, session_id, device_id)
except httplib.HTTPException as e:
print ':'
print e
print '\n'
sys.exit()
except socket.error as e_socket:
print ':'
print e_socket
print '\n'
sys.exit()
# def appliance_list_get(host, port, session_id, data_key, device_id):
# url = '/v1/appliance/home/list/get'
#
# postparams = {}
# postparams["src"] = "0"
# postparams["format"] = "2"
# postparams["stamp"] = "20161114110349"
# postparams["sessionId"] = session_id
# postparams["language"] = "zh_CN"
#
# # get sign
# sign = hashlib.sha256(
# "/v1/appliance/home/list/getformat=2&language=zh_CN&sessionId=" + session_id + "&src=0&stamp=201611141103492f39d871a38a4841aab3be3837e39cf4").hexdigest()
# postparams["sign"] = sign
#
# headparams = {}
# headparams["Content-Type"] = "application/x-www-form-urlencoded"
# headparams["User-Agent"] = "Dalvik/2.1.0 (Linux; U; Android 5.0.2; ZTE A2015 Build/LRX22G)"
#
# try:
# results = raw_post(host, port, url, postparams, headparams)
#
# headers = results[1] # response headers
# res = str(results[2]) # response body
# res_json = json.loads(res, encoding='utf-8')
# errorCode = res_json['errorCode']
# print "list errorCode:"
# print errorCode##################################
# if errorCode == "0":
# result = res_json['result']
# device = result['homeList'][0]['applianceList'][0]
# print "device:"
# print device ###########################
# if check(device, data_key) == 0:
# delete(host, port, session_id, device_id)
# # print "result:"
# # print device ##############
# else:
# print "appliance errorCode:"
# print errorCode
# print "msg:"
# print res_json['msg']
#
#
# except httplib.HTTPException as e:
# print ':'
# print e
# print '\n'
# sys.exit()
# except socket.error as e_socket:
# print ':'
# print e_socket
# print '\n'
# sys.exit()
def delete(host, port, session_id, device_id):
url = '/v1/appliance/delete'
postparams = {}
postparams["applianceId"] = device_id
postparams["src"] = "0"
postparams["format"] = "2"
postparams["stamp"] = "20161114110349"
postparams["sessionId"] = session_id
postparams["language"] = "zh_CN"
# get sign
sign = hashlib.sha256(
"/v1/appliance/deleteapplianceId=" + device_id + "&format=2&language=zh_CN&sessionId=" + session_id + "&src=0&stamp=201611141103492f39d871a38a4841aab3be3837e39cf4").hexdigest()
postparams["sign"] = sign
headparams = {}
headparams["Content-Type"] = "application/x-www-form-urlencoded"
headparams["User-Agent"] = "Dalvik/2.1.0 (Linux; U; Android 5.0.2; ZTE A2015 Build/LRX22G)"
try:
results = raw_post(host, port, url, postparams, headparams)
headers = results[1] # response headers
res = str(results[2]) # response body
res_json = json.loads(res, encoding='utf-8')
errorCode = res_json['errorCode']
if errorCode == "0":
print "delete 0" #######################
else:
print "delete errorCode:"
print errorCode
print "msg:"
print res_json['msg']
except httplib.HTTPException as e:
print ':'
print e
print '\n'
sys.exit()
except socket.error as e_socket:
print ':'
print e_socket
print '\n'
sys.exit()
def check(device, data_key):
if device["onlineStatus"] == "1":
sn = decode_aes(device["sn"], data_key)[0 : 32]
print sn
print device["id"]
return 1
else:
return 0
def main():
host = '???'
port = 10050
login_id_get(host, port)
# appliance_list_get(host, port, session_id, "111", "17592186973172")
# #encode mode
# text = "0000B811100R1105B157203003640000"
# key = "d4bdd184c4ed4569"
# ans = encode_aes(text, key)
# t = b2a_hex(ans)
# print t
#
# #decode mode
# text = "7b34ac3039b41a872136124bb542985673e9040221dc53e61704362c9fd5c910"
# key = "a979c60f245e8026"
# ans = decode_aes(text, key)[0 : 16]
# print ans
if __name__ == '__main__':
main()