forked from jasonacox/tuyapower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan-enc.py
103 lines (91 loc) · 3.07 KB
/
scan-enc.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
#!/usr/bin/python
#
# TuyaScan - Scan for tuya devices on the network broadcasting on UDP port 6666
# ## BETA ## Added additional logic to handle encrypted payloads
#
# Author: Jason A. Cox
# For more information see https://github.com/jasonacox/tuyapower
import base64
from hashlib import md5
import json
import logging
import socket
import sys
import time
import colorsys
import binascii
try:
#raise ImportError
import Crypto
from Crypto.Cipher import AES # PyCrypto
except ImportError:
Crypto = AES = None
import pyaes # https://github.com/ricmoo/pyaes
MAXCOUNT = 10
PORT = 6666
PROTOCOL_VERSION_BYTES_31 = b'3.1'
PROTOCOL_VERSION_BYTES_33 = b'3.3'
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# Enable broadcasting mode
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
print("Scanning on UDP port %s for devices...\n"%PORT)
def appenddevice(newdevice, devices):
for i in devices:
if i['ip'] == newdevice['ip']:
return True
devices.append(newdevice)
return False
devices=[]
count = 0
stop = False
client.bind(("", PORT))
while stop == False:
data, addr = client.recvfrom(4048)
print("MESSAGE from [%s]: %s"%(addr[0],data))
ip = addr[0]
gwId = productKey = version = ""
result = data
try:
result = data[20:-8]
if result.startswith(b'{'):
# this is the regular expected code path
if not isinstance(result, str):
result = result.decode()
result = json.loads(result)
elif result.startswith(PROTOCOL_VERSION_BYTES_31):
# version == 3.1 and got an encrypted payload
result = result[len(PROTOCOL_VERSION_BYTES_31):] # remove version header
result = result[16:] # remove MD5 hexdigest of payload
cipher = AESCipher(None)
result = cipher.decrypt(result)
print('decrypted result=%r', result)
if not isinstance(result, str):
result = result.decode()
result = json.loads(result)
elif result.startswith(PROTOCOL_VERSION_BYTES_33):
# version == 3.3:
cipher = AESCipher(None)
result = cipher.decrypt(result, False)
print('decrypted result=%r', result)
if not isinstance(result, str):
result = result.decode()
result = json.loads(result)
else:
print('Unexpected payload=%r', result)
result = {"ip": ip}
ip = result['ip']
gwId = result['gwId']
productKey = result['productKey']
version = result['version']
except:
print('Unexpected payload=%r', result)
result = {"ip": ip}
if appenddevice(result, devices) == False:
print("FOUND: %s, ID = %s, Key = %s, Version = %s" % (ip,gwId,productKey,version))
else:
count = count + 1
if count > MAXCOUNT:
stop = True
print("\nScan Complete! Found %s devices."%len(devices))
print(devices)