-
Notifications
You must be signed in to change notification settings - Fork 10
/
kkeps_controller.py
executable file
·482 lines (418 loc) · 15.1 KB
/
kkeps_controller.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#/usr/bin/python
import sys, os, re, socket, time, select, random, getopt
from Crypto.Cipher import AES
#
# This is @0x00string's script to control the KanKun Smartplug without a phone
# Enjoy, feel free to check out my notes and slides on building this
#
# you'll want to modify the applyConfig() function to fit your situation
#
# greets to @zenofex, @exploiteers and the KanKun G+ group
#
def banner():
ascii_art = '''
___ ___ ___ _ _ _
____ / _ \ / _ \ / _ \ | | (_) ( )
/ __ \| | | |_ _| | | | | | |___| |_ _ __ _ _ __ __ _|/ ___
/ / _` | | | \ \/ / | | | | | / __| __| '__| | '_ \ / _` | / __|
| | (_| | |_| |> <| |_| | |_| \__ \ |_| | | | | | | (_| | \__ \
_ _\ \__,_|\___//_/\_\\\\___/ \___/|___/\__|_| |_|_| |_|\__, | |___/
| |/ /\____/ | |/ / / ____| | | __/ | | | |
| ' / __ _ _ __ | ' /_ _ _ __ | | ___ _ __ | |_ _ |___/_ | | | ___ _ __
| < / _` | '_ \| <| | | | '_ \| | / _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|
| . \ (_| | | | | . \ |_| | | | | |___| (_) | | | | |_| | | (_) | | | __/ |
|_|\_\__,_|_| |_|_|\_\__,_|_| |_|\_____\___/|_| |_|\__|_| \___/|_|_|\___|_|
'''
print(ascii_art)
def applyConfig():
##
# you can change these to your liking
##
global IFACE
IFACE = "wlan0"
global RHOST
RHOST = "192.168.1.173"
global RMAC
RMAC = "00:15:61:bd:82:91"
global PASSWORD
PASSWORD = "nopassword"
global NAME
NAME = "lan_phone"
global VERBOSE
VERBOSE = False
global SSID
SSID = "pina"
global WLANKEY
WLANKEY = "pineapplekey"
global INITPASS
INITPASS = "nopassword"
global DEVNAME
DEVNAME = "name"
##
# you'll likely not want to change these
##
global KEY
KEY = "fdsl;mewrjope456fds4fbvfnjwaugfo"
global RPORT
RPORT = 27431
global SOCKET_TIMEOUT
SOCKET_TIMEOUT = 1
global RECV_BUF
RECV_BUF = 1024
return 0
def stdout (t, m):
if (t is "+"):
print "\x1b[32;1m[+]\x1b[0m\t" + m + "\n",
elif (t is "-"):
print "\x1b[31;1m[-]\x1b[0m\t" + m + "\n",
elif (t is "*"):
print "\x1b[34;1m[*]\x1b[0m\t" + m + "\n",
elif (t is "!"):
print "\x1b[33;1m[!]\x1b[0m\t" + m + "\n",
def createDump (input):
d, b, h = '', [], []
u = list(input)
for e in u:
h.append(e.encode("hex"))
if e == '0x0':
b.append('0')
elif 30 > ord(e) or ord(e) > 128:
b.append('.')
elif 30 < ord(e) or ord(e) < 128:
b.append(e)
i = 0
while i < len(h):
if (len(h) - i ) >= 16:
d += ' '.join(h[i:i+16])
d += " "
d += ' '.join(b[i:i+16])
d += "\n"
i = i + 16
else:
d += ' '.join(h[i:(len(h) - 1)])
pad = len(' '.join(h[i:(len(h) - 1)]))
d += ' ' * (56 - pad)
d += ' '.join(b[i:(len(h) - 1)])
d += "\n"
i = i + len(h)
return d
def crypto(switch, input):
k = AES.new(KEY, AES.MODE_ECB)
if input is not None:
if switch == "e":
return k.encrypt(input)
elif switch == "d":
return k.decrypt(input)
class sockobj:
def __init__(self, s=None, proto="udp"):
if s is None:
if proto == "udp":
self.s = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM)
elif proto == "tcp":
self.s = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
self.s.settimeout(SOCKET_TIMEOUT)
else:
self.s = s
def connect(self, rhost, rport):
self.s.connect((rhost,rport))
def tx(self,m):
self.s.send(crypto("e",m))
def rx(self):
try:
m = self.s.recv(RECV_BUF)
return crypto("d",m)
except:
return ""
def txrx(ip,port,m,s):
if s is None:
sock = sockobj()
else:
sock = sockobj(None, "tcp")
sock.connect(ip,port)
sock.tx(m)
ret = sock.rx()
if ret is not "":
return ret
else:
stdout("!","Nothing returned...")
def getPacket(t, e=None, on_time="2015-02-10-11:11:11", off_time="2015-02-10-11:11:11", enabled="n", open_enabled="n", close_enabled="n", timer_id=None):
d,c = "%",""
auth = NAME + d + RMAC + d + PASSWORD + d
if t == "open":
c = auth + "open" + d + "request"
elif t == "check":
c = auth + "check" + d + "request"
elif t == "brmode":
c = auth + "check" + d + "brmode"
elif t == "close":
c = auth + "close" + d + "request"
elif t == "total timer":
c = auth + "check#total" + d + "timer"
elif t == "check timer":
c = auth + "check#" + e + "%timer"
elif t == "heart":
c = auth + os.popen('date +%y-%m-%d-%T').read().rstrip() + d + "heart"
elif t == "confirm":
c = auth + "confirm#"+ e + d + "request"
elif t == "confirm timer":
c = auth + "confirm#"+ e + d + "timer"
elif t == "set timer":
c = auth + "alarm#" + str(random.randint(100,999)) + "#" + enabled + "#" + on_time + "#" + open_enabled + "#" + off_time + "#" + close_enabled +"#" + e + "#set" + d + "timer" + d + "timer"
elif t == "unset timer":
c = auth + "alarm#" + timer_id + "#" + enabled + "#" + on_time + "#" + open_enabled + "#" + off_time + "#" + close_enabled + "#" + e + "#unset" + d + "timer" + d + "timer"
elif t == "wifi config":
c = "phone" + d + SSID + d + WLANKEY + d + INITPASS + d + DEVNAME + d + "GMT" + os.popen('date +%z').read().rstrip()
while len(c) % 16 is not 0:
c = c + "\x00"
return c
def isConfirm(m):
q = re.compile('.*?(\d\d\d\d\d).*?')
p = q.search(m)
if p is not None:
return p.group(1)
else:
return None
def isHack(m):
q = re.compile('.*?(hack).*?')
p = q.search(m)
if p is not None:
return p.group(1)
else:
return None
def isCheck(m):
q = re.compile('.*?%.*?%.*?%(.*?)%rack.*?')
p = q.search(m)
if p is not None:
return p.group(1)
else:
return None
def isTotalTimer(m):
q = re.compile('.*?\%.*?\%.*?\%check#(.*?)#.*?')
p = q.search(m)
if p is not None:
return p.group(1)
else:
return None
def isTimerStatus(m):
q = re.compile('.*?%.*?%.*?%alarm#(.*?)#(.)#(.*?)#(.)#(.*?)#(.)#(.*?)#(.)tack.*?')
p = q.search(m)
if p is not None:
return p
else:
return None
def parseRet(m):
ret = isConfirm(m)
is_confirm = ret
if is_confirm is not None:
stdout("*","confirmation number: " + is_confirm)
ret = None
return is_confirm
ret = isHack(m)
if ret is not None:
stdout("*","Heartbeat ackowledged")
ret = None
return None
ret = isCheck(m)
if ret is not None:
stdout("*","switch is: " + ret)
ret = None
return None
ret = isTotalTimer(m)
if ret is not None:
stdout("*","There are currently " + str(ret) + " timers set")
ret = None
return None
ret = isTimerStatus(m)
if ret is not None:
stdout("timer ID " + id + "\nopen time: " + ret.group(3) + "\nclose time: " + ret.group(5) + "\ny1y2y3: " + ret.group(2) + ret.group(4) + ret.group(6) + "\ndays repeated: " + ret.group(7) + "\nenabled: " + ret.group(8))
return ret
def setRMAC():
RMAC = os.popen("arp -i" + IFACE + " " + RHOST + " | awk {'print $4'}").read().strip()
def sendOp(op, e=None,ont=None,offt=None,y1=None,y2=None,y3=None, tid=None):
switch = None
if op == "set timer" or op == "unset timer":
m = getPacket(op, e, ont, offt, y1,y2,y3, tid)
elif op == "open" or op == "close":
m = getPacket(op)
elif op == "wifi config":
global RHOST
RHOST = "192.168.10.253"
global RPORT
RPORT = 37092
switch = 1
m = getPacket(op, e, ont, y1, y2)
else:
m = getPacket(op)
ret = None
stdout("*","Sending " + op + " packet...")
if VERBOSE is not False:
stdout("*","\n" + createDump(m))
ret = txrx(RHOST, RPORT, m, switch)
if ret is not None:
stdout("+","received reply packet...")
if VERBOSE is not False:
stdout("*","\n" + createDump(ret))
parsed = parseRet(ret)
if parsed is not None:
if op is "set timer" or op is "unset timer":
m = getPacket("confirm timer", parsed)
else:
m = getPacket("confirm", parsed)
stdout("*","sending confirmation #: " + str(parsed))
if VERBOSE is not False:
stdout("*","\n" + createDump(m))
ret = None
ret = txrx(RHOST,RPORT,m,switch)
if ret is not None:
stdout("*","received reply packet...")
if VERBOSE is not False:
stdout("*","\n" + createDump(ret))
parsed = parseRet(ret)
def passwordJack():
key = AES.new(KEY, AES.MODE_ECB, )
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', 27431))
s.setblocking(0)
p = re.compile('(.*?)%(.*?)%(.*?).*?')
while True:
ret = select.select([s],[],[])
m = ret[0][0].recv(1024)
if VERBOSE is not False:
stdout("+","\n" + key.decrypt(m))
q = p.search(m)
if q is not None:
stdout("+","Got possible credentials: name: " + q.group(1) + " mac: " + q.group(2) + " password: " + q.group(3))
def heartbeat():
sendOp("heart")
def check():
sendOp("check")
def checkBRMode():
sendOp("brmode")
def totalTimer():
sendOp("total timer")
def checkTimer(num):
sendOp("check timer", num)
def setTimer(start,stop,enabled,onenable,offenable,repeatstr):
sendOp("set timer", repeatstr,start,stop,enabled,onenable,offenable)
def unsetTimer(num):
timer_info = sendOp("check timer", num, "r")
sendOp("set timer", timer_info[0],timer_info[1],timer_info[2],timer_info[3],timer_info[4],timer_info[5],timer_info[6])
def wifiConfig(ssid,key,initpass="nopassword",devname="name"):
global SSID
SSID = ssid
global WLANKEY
WLANKEY = key
global INITPASS
INITPASS = initpass
global DEVNAME
DEVNAME = devname
sendOp("wifi config",SSID,WLANKEY)
def on():
sendOp("open")
def off():
sendOp("close")
def usage():
banner()
usage_text = '''
Usage: script.py -a on
make sure to vi this script and set the IP to that of your target
arguments:
required:
-a, --action <action name> what action to perform, e.g., -a heart
actions: on, off, heart, check, brmode, totalTimer, checkTimer, setTimer, unsetTimer, wifiConfig
optional:
-v verbose output
actions that take additional arguments:
unsetTimer
script.py -a unsetTimer --num 3
--num a number, this is an argument required for checkTimer and unsetTimer actions
setTimer
script.py -a setTimer --start-time "`date +%y-%m-%d-%T`" --stop-time "2015-02-10-11:22:22" --enabled y [...] --repeatstr "1,2,3"
--start-time <date +%y-%m-%d-%T> this is a date-string
--stop-time ditto
--enabled <y|n> timer enabled?
--on-enabled <y|n> on-time enabled?
--off-enabled <y|n> off-time enabled?
--repeatstr <1,2,4,5,6,7>, setTimer argument, repeat on which days?
wifiConfig
script.py -a wifiConfig --ssid Linksys --key "P@ssw0rd!"
--ssid <string>, the ssid of the network to join the device to
--key <string>, the key for the network to join the device to
--initial-password <string>, the password to set for the device (default is "noPASSWORD")
--device-name <string>, the name to give the device (this doesnt always take and changes with the wind)
'''
print(usage_text)
def main():
if applyConfig() > 0:
usage()
exit(1)
action = None
options, remainder = getopt.getopt(sys.argv[1:], 'a:vh', ['action=','verbose','num=','start-time=','stop-time=','enabled=','on-enabled=','off-enabled=','repeatstr=','ssid=','key=','initial-password=','device-name=','ip=','help'])
for opt, arg in options:
if opt in ('-h', '--help'):
usage()
exit()
elif opt in ('-a', '--action'):
action = arg
elif opt in ('-v', '--verbose'):
global VERBOSE
VERBOSE = True
elif opt in ('num'):
num = arg
elif opt in ('start-time'):
start_time = arg
elif opt in ('stop-time'):
stop_time = arg
elif opt in ('enabled'):
enabled = arg
elif opt in ('on-enabled'):
onenable = arg
elif opt in ('off-enabled'):
offenable = arg
elif opt in ('repeatstr'):
repeatstr = arg
elif opt in ('ssid'):
global SSID
SSID = arg
elif opt in ('key'):
global WLANKEY
WLANKEY = arg
elif opt in ('initial-password'):
global INITPASS
INITPASS = arg
elif opt in ('device-name'):
global DEVNAME
DEVNAME = arg
elif opt in ('ip'):
global RHOST
RHOST = arg
if action == None:
usage()
exit(1)
elif action == "heart":
heartbeat()
elif action == "check":
check()
elif action == "on":
on()
elif action == "off":
off()
elif action == "brmode":
checkBRMode()
elif action == "totalTimer":
totalTimer()
elif action == "checkTimer":
checkTimer(num)
elif action == "setTimer":
setTimer(start_time,stop_time,enabled,onenable,offenable,repeatstr)
elif action == "unsetTimer":
unsetTimer(num)
elif action == "wifiConfig":
wifiConfig(SSID,WLANKEY,INITPASS,DEVNAME)
else:
usage()
exit(1)
if __name__ == "__main__":
main()