From 7dd807502a95825429b9ec0a464f77097a8691a0 Mon Sep 17 00:00:00 2001 From: alsum Date: Tue, 17 Jul 2018 11:05:41 +0200 Subject: [PATCH 1/2] adding set-time func --- zk/base.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/zk/base.py b/zk/base.py index c5d642a..aba3645 100644 --- a/zk/base.py +++ b/zk/base.py @@ -514,3 +514,17 @@ class ZK(object): return True else: raise ZKErrorResponse("Invalid response") + + def set_time(self, time): + """Set the time on the machine""" + command = const.CMD_SET_TIME + command_string = '' + response_size = 8 + chksum = 0 + session_id = self.__sesion_id + reply_id = self.__reply_id + cmd_response = self.__send_command(command,command_string, chksum, session_id, reply_id, response_size) + if cmd_response.get('status'): + return True + else: + raise ZKErrorResponse("Invalid response") \ No newline at end of file From 0cb0d6c03566f113de52a41388cbc5d97db8d901 Mon Sep 17 00:00:00 2001 From: alsum Date: Tue, 17 Jul 2018 11:28:01 +0200 Subject: [PATCH 2/2] handling unauthorized connnections --- zk/base.py | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/zk/base.py b/zk/base.py index aba3645..28e33fe 100644 --- a/zk/base.py +++ b/zk/base.py @@ -10,17 +10,17 @@ from zk.user import User class ZK(object): - is_connect = False __data_recv = None __sesion_id = 0 __reply_id = 0 - def __init__(self, ip, port=4370, timeout=60): + def __init__(self, ip, port=4370, timeout=60,password=0): self.__address = (ip, port) self.__sock = socket(AF_INET, SOCK_DGRAM) self.__sock.settimeout(timeout) + self.__password = password def __create_header(self, command, command_string, checksum, session_id, reply_id): ''' @@ -146,6 +146,11 @@ class ZK(object): response_size = 8 cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size) + self.__sesion_id = unpack('HHHH', self.__data_recv[:8])[2] + if cmd_response.get('code') == const.CMD_ACK_UNAUTH: + command_string = make_commkey(self.__password, self.__sesion_id) + cmd_response = self.__send_command(const.CMD_AUTH, command_string, checksum, self.__sesion_id, + self.__reply_id, response_size) if cmd_response.get('status'): self.is_connect = True # set the session id @@ -527,4 +532,40 @@ class ZK(object): if cmd_response.get('status'): return True else: - raise ZKErrorResponse("Invalid response") \ No newline at end of file + raise ZKErrorResponse("Invalid response") + + +def make_commkey(key, session_id, ticks=50): + """take a password and session_id and scramble them to send to the time + clock. + copied from commpro.c - MakeKey""" + key = int(key) + session_id = int(session_id) + k = 0 + for i in range(32): + if (key & (1 << i)): + k = (k << 1 | 1) + else: + k = k << 1 + k += session_id + + k = pack(b'I', k) + k = unpack(b'BBBB', k) + k = pack( + b'BBBB', + k[0] ^ ord('Z'), + k[1] ^ ord('K'), + k[2] ^ ord('S'), + k[3] ^ ord('O')) + k = unpack(b'HH', k) + k = pack(b'HH', k[1], k[0]) + + B = 0xff & ticks + k = unpack(b'BBBB', k) + k = pack( + b'BBBB', + k[0] ^ B, + k[1] ^ B, + B, + k[3] ^ B) + return k \ No newline at end of file