From f651fb1f0b1c6c7671a3f463bda27bf7633b994a Mon Sep 17 00:00:00 2001 From: Bastian Bloessl Date: Mon, 18 Feb 2019 14:21:29 +0000 Subject: [PATCH 1/2] zigbee_crypt: fix memory leak and some whitespace --- zigbee_crypt/zigbee_crypt.c | 45 ++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/zigbee_crypt/zigbee_crypt.c b/zigbee_crypt/zigbee_crypt.c index 9b46b723..7a24e938 100644 --- a/zigbee_crypt/zigbee_crypt.c +++ b/zigbee_crypt/zigbee_crypt.c @@ -2,12 +2,12 @@ * zigbee_crypt.c * Copyright 2011 steiner * zigbee convenience functions - * + * * alot of this code was "borrowed" from wireshark * packet-zbee-security.c & pzcket-zbee-security.h * function: zbee_sec_ccm_decrypt */ - + // Explaination of Python Build Values http://docs.python.org/c-api/arg.html#Py_BuildValue #include @@ -27,6 +27,7 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) { const char *zigbeeData; int sizeZigbeeData; int i, j; + PyObject *res; char pMIC[ZBEE_SEC_CONST_MICSIZE]; char pEncMIC[ZBEE_SEC_CONST_MICSIZE]; @@ -35,7 +36,7 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) { char cipher_out[ZBEE_SEC_CONST_BLOCKSIZE]; /* Cipher Instance. */ gcry_cipher_hd_t cipher_hd; - + if (!PyArg_ParseTuple(args, "s#s#is#s#", &pZkey, &sizeZkey, &pNonce, &sizeNonce, @@ -48,12 +49,12 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) { PyErr_SetString(PyExc_ValueError, "incorrect key size (must be 16)"); return NULL; } - + if (sizeNonce != ZBEE_SEC_CONST_NONCE_LEN) { PyErr_SetString(PyExc_ValueError, "incorrect nonce size (must be 13)"); return NULL; } - + if ((sizeMIC != 0) && (sizeMIC != 4) && (sizeMIC != 8) && (sizeMIC != 16)) { PyErr_SetString(PyExc_ValueError, "incorrect mic size (must be 0, 4, 8, or 16 bytes)"); return NULL; @@ -63,7 +64,7 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) { memset(pEncMIC, 0, ZBEE_SEC_CONST_MICSIZE); pEncrypted = malloc(sizeUnencryptedData); memset(pEncrypted, 0, sizeUnencryptedData); - + /* Open the cipher in ECB mode. */ if (gcry_cipher_open(&cipher_hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0)) { PyErr_SetString(PyExc_Exception, "gcrypt open AES-128 ECB cipher failed"); @@ -87,7 +88,7 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) { gcry_cipher_close(cipher_hd); return NULL; } - + j = 0; if (sizeZigbeeData > 0) { /* Process L(a) into the cipher block. */ @@ -139,10 +140,10 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) { gcry_cipher_close(cipher_hd); return NULL; } - + gcry_cipher_close(cipher_hd); memcpy(pMIC, cipher_out, sizeMIC); - + /* Create the CCM* counter block A0 */ memset(cipher_in, 0, ZBEE_SEC_CONST_BLOCKSIZE); cipher_in[0] = ZBEE_SEC_CCM_FLAG_L; @@ -187,8 +188,10 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) { } /* Done with the CTR Cipher. */ gcry_cipher_close(cipher_hd); - - return Py_BuildValue("(s#s#)", pEncrypted, sizeUnencryptedData, pEncMIC, sizeMIC); + + res = Py_BuildValue("(s#s#)", pEncrypted, sizeUnencryptedData, pEncMIC, sizeMIC); + free(pEncrypted); + return res; }; static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) { @@ -202,6 +205,7 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) { int sizeEncryptedData; const char *zigbeeData; int sizeZigbeeData; + PyObject *res; char pMIC[ZBEE_SEC_CONST_MICSIZE]; char pUnencMIC[ZBEE_SEC_CONST_MICSIZE]; @@ -220,7 +224,7 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) { PyErr_SetString(PyExc_ValueError, "incorrect key size (must be 16)"); return NULL; } - + if (sizeNonce != ZBEE_SEC_CONST_NONCE_LEN) { PyErr_SetString(PyExc_ValueError, "incorrect nonce size (must be 13)"); return NULL; @@ -233,12 +237,12 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) { memset(pMIC, 0, ZBEE_SEC_CONST_MICSIZE); memcpy(pMIC, pOldMIC, sizeMIC); - + memset(pUnencMIC, 0, ZBEE_SEC_CONST_MICSIZE); - + pUnencrypted = malloc(sizeEncryptedData); memset(pUnencrypted, 0, sizeEncryptedData); - + /* Create the CCM* counter block A0 */ memset(cipher_in, 0, ZBEE_SEC_CONST_BLOCKSIZE); cipher_in[0] = ZBEE_SEC_CCM_FLAG_L; @@ -287,7 +291,7 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) { } /* Done with the CTR Cipher. */ gcry_cipher_close(cipher_hd); - + int i, j; /* Re-open the cipher in ECB mode. */ if (gcry_cipher_open(&cipher_hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0)) { @@ -382,16 +386,17 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) { gcry_cipher_close(cipher_hd); return NULL; } - + gcry_cipher_close(cipher_hd); - + // now use j to indicate whether the MICs match j = 0; if (memcmp(cipher_out, pUnencMIC, sizeMIC) == 0) { j = 1; } - - return Py_BuildValue("(s#i)", pUnencrypted, sizeEncryptedData, j); + res = Py_BuildValue("(s#i)", pUnencrypted, sizeEncryptedData, j); + free(pUnencrypted); + return res; }; static PyMethodDef zigbee_crypt_Methods[] = { From cfda5159f5526ad30a003003daac3ae399f6bd1a Mon Sep 17 00:00:00 2001 From: SrEdeu Date: Mon, 18 Mar 2019 14:02:26 +0100 Subject: [PATCH 2/2] fixing datetime method call --- tools/zbfind | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/zbfind b/tools/zbfind index 884c5ed5..065cd9b4 100644 --- a/tools/zbfind +++ b/tools/zbfind @@ -1020,7 +1020,7 @@ class ZBPoller(Thread): idev = dev details = idev[1] # Update last seen detail - details[0][1] = "Last seen: " + str(datetime.datetime.now()) + details[0][1] = "Last seen: " + str(datetime.now()) if (fc_secenabled != 0): details[0][2] = "Security: Enabled" details[0][3] = "Last Seq Num: " + seq @@ -1036,7 +1036,7 @@ class ZBPoller(Thread): if match == False: # Add this entry to the list - nowstr = str(datetime.datetime.now()) + nowstr = str(datetime.now()) details = [["First seen: " + nowstr]] details[0].append("Last Seen: " + nowstr) if (fc_secenabled != 0):