def respond(self, response_adu): """ Send response ADU back to client. :param response_adu: A bytearray containing the response of an ADU. """ log.info('--> {0} - {1}.'.format(self.client_address[0], hexlify(response_adu))) self.request.sendall(response_adu)
def respond(self, response_adu): """ Send response ADU back to client. :param response_adu: A bytearray containing the response of an ADU. """ log.info('--> {0} - {1}.'.format(self.client_address[0], hexlify(response_adu))) digest = getHash(response_adu) response_adu = aes_cbc_encrypt(response_adu, self.obtained_key, self.iv, digest) print('\nEncrypted Response ADU:\t-------->\t', response_adu, "\n\n") response_adu_bytes = response_adu.encode('utf-8') self.request.sendall(response_adu_bytes)
def process(self, request_adu): """ Process request ADU and return response. :param request_adu: A bytearray containing the ADU request. :return: A bytearray containing the response of the ADU request. """ log.debug('Lenght of received ADU is {0}.'.format(len(request_adu))) log.info('<-- {0} - {1}.'.format(self.client_address[0], hexlify(request_adu))) try: transaction_id, protocol_id, _, unit_id = \ unpack_mbap(request_adu[:7]) function = function_factory(request_adu[7:]) results = function.execute(unit_id, self.server.route_map) try: # ReadFunction's use results of callbacks to build response # PDU... response_pdu = function.create_response_pdu(results) except TypeError: # ...other functions don't. response_pdu = function.create_response_pdu() except ModbusError as e: function_code = get_function_code_from_request_pdu(request_adu[7:]) response_pdu = pack_exception_pdu(function_code, e.error_code) except Exception as e: log.exception('Could not handle request: {0}.'.format(e)) function_code = get_function_code_from_request_pdu(request_adu[7:]) response_pdu = \ pack_exception_pdu(function_code, ServerDeviceFailureError.error_code) response_mbap = pack_mbap(transaction_id, protocol_id, len(response_pdu) + 1, unit_id) log.debug('Response MBAP {0}'.format(response_mbap)) log.debug('Response PDU {0}'.format(response_pdu)) response_adu = response_mbap + response_pdu return response_adu
def respond(self, response_adu): """ Send response ADU back to client. :param response_adu: A bytearray containing the response of an ADU. """ log.info('--> {0} - {1}.'.format(self.client_address[0], ba.hexlify(response_adu))) salt_key = b'6ea920bb0e8ad58ebf2dbf5634f14f26' salt_iv = b'4893ed0de93bf86871845c06e2bef676' def readfile ( file ) : with open (file ,'rb') as f: content = f.read () f.close() return content dh = readfile("umodbus/secrets/my_dh_file") backend = default_backend() hkdf_key = HKDF( algorithm=hashes.SHA256(), length=16, salt=ba.unhexlify(salt_key), info=b"keygen", backend=backend) hkdf_iv = HKDF( algorithm=hashes.SHA256(), length=16, salt=ba.unhexlify(salt_iv), info=b"ivgen", backend=backend) key = hkdf_key.derive(dh) iv = hkdf_iv.derive(dh) aes = algorithms.AES(key) mode = modes.CBC(iv) cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend) def enc_msg(plain): padder = padding.PKCS7(128).padder() padded_data = padder.update(ba.hexlify(plain)) padded_data += padder.finalize() encryptor = cipher.encryptor() ct = encryptor.update(padded_data) + encryptor.finalize() # hmac h = hmac.HMAC(key, hashes.SHA256(), backend=backend) h.update(plain) mac = h.finalize() # length of this mac is 32 return(ct+mac) self.request.sendall(enc_msg(response_adu))