def sirep_send_command(sirep_con_sock, sirep_command, print_printable_data=False, verbose=False): # generate the commands's payload sirep_payload = sirep_command.serialize_sirep() logging.info('Sirep payload hex: %s' % sirep_payload.hex()) if verbose: print("SEND:") hexdump.hexdump(sirep_payload) # Send the Sirep payload logging.debug("Sending Sirep payload") sirep_con_sock.sendall(sirep_payload) # Receive all result records result_record_type = -1 records = [] while True: try: first_int = sirep_con_sock.recv(INT_SIZE) if not first_int: break result_record_type = unpack_uint(first_int) logging.debug("Result record type: %d" % result_record_type) data_size = unpack_uint(sirep_con_sock.recv(INT_SIZE)) if not data_size: break logging.debug("Receiving %d bytes" % data_size) data = sirep_con_sock.recv(data_size) logging.info("Result record data hex: %s" % data[:LOGGING_DATA_TRUNCATION].hex()) if verbose: print("RECV:") hexdump.hexdump(data) # If printable, print result record data as is if print_printable_data: try: data_string = data.decode() logging.info("Result data readable print:") print("---------\n%s\n---------" % data_string) except UnicodeError: pass records.append(first_int + data) except socket.timeout as e: logging.debug( "timeout in command communication. Assuming end of conversation" ) break return records
def __init__(self, raw_data, data_size=None): """Initializes the result buffer representation""" result_type = utils.unpack_uint(raw_data[:INT_SIZE]) result_payload = utils.unpack_bytes(raw_data[INT_SIZE:], data_size=data_size) self.result_type = result_type self.payload_length = len(result_payload) self.result_payload = result_payload self.parsed_kv = self._parse_payload_to_kv(self.result_payload)
def main(args): dst_ip = args.target_device_ip command_type = args.command_type sirep_command_type = getattr(CommandType, command_type) try: command_args = get_command_ctor_arguments(sirep_command_type, args) except: logging.error("Wrong usage. use --help for instructions") sys.exit() # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(3) try: sirep_command_ctor = SIREP_COMMANDS[sirep_command_type] # create the requested sirep command try: sirep_command = sirep_command_ctor(*command_args) except TypeError: logging.error("Wrong usage. use --help for instructions") sys.exit() sirep_connect(sock, dst_ip, verbose=args.vv) sirep_result_buffers = sirep_send_command(sock, sirep_command, print_printable_data=args.v or args.vv, verbose=args.vv) for result_buffer in sirep_result_buffers: result_type_code = unpack_uint(result_buffer[:INT_SIZE]) sirep_result_ctor = RESULT_TYPE_TO_RESULT[result_type_code] sirep_result = sirep_result_ctor(result_buffer) print(sirep_result) finally: logging.debug("Closing socket") sock.close() return True
def _parse_payload_to_kv(result_payload): """Described in parent class""" kv = super(HResultResult, HResultResult)._parse_payload_to_kv(result_payload) kv['HResult'] = utils.unpack_uint(result_payload[:INT_SIZE]) return kv