Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_create_message(self):
message = PAExecMsg()
message['msg_id'] = PAExecMsgId.MSGID_OK
message['unique_id'] = 1234
message['buffer'] = b"\x01\x02\x03\x04"
expected = b"\x04\x00" \
b"\xd2\x04\x00\x00" \
b"\x04\x00\x00\x00" \
b"\x01\x02\x03\x04"
actual = message.pack()
assert len(message) == 14
assert actual == expected
def test_parse_message(self):
actual = PAExecMsg()
data = b"\x04\x00" \
b"\xd2\x04\x00\x00" \
b"\x04\x00\x00\x00" \
b"\x01\x02\x03\x04"
data = actual.unpack(data)
assert len(actual) == 14
assert data == b""
assert actual['msg_id'].get_value() == PAExecMsgId.MSGID_OK
assert actual['unique_id'].get_value() == 1234
assert actual['buffer_length'].get_value() == 4
assert actual['buffer'].get_value() == b"\x01\x02\x03\x04"
actual.check_resp()
def test_parse_message_fail_response(self):
actual = PAExecMsg()
data = b"\x06\x00" \
b"\xd2\x04\x00\x00" \
b"\x08\x00\x00\x00" \
b"\x04\x00\x00\x00\x68\x00\x69\x00"
actual.unpack(data)
with pytest.raises(PAExecException) as exc:
actual.check_resp()
assert str(exc.value) == "Received exception from remote PAExec " \
"service: hi"
assert exc.value.msg_id == PAExecMsgId.MSGID_FAILED
assert exc.value.buffer == b"\x04\x00\x00\x00\x68\x00\x69\x00"
"%s, no more attempts remaining"
% self._exe_file)
log.warning("Main pipe %s does not exist yet on attempt %d. "
"Trying again in 5 seconds"
% (self._exe_file, i + 1))
time.sleep(5)
else:
break
log.info("Writing PAExecSettingsMsg to the main PAExec pipe")
log.info(str(input_data))
main_pipe.write(input_data.pack(), 0)
log.info("Reading PAExecMsg from the PAExec pipe")
settings_resp_raw = main_pipe.read(0, 1024)
settings_resp = PAExecMsg()
settings_resp.unpack(settings_resp_raw)
log.debug(str(settings_resp))
settings_resp.check_resp()
# start the process now
start_msg = PAExecMsg()
start_msg['msg_id'] = PAExecMsgId.MSGID_START_APP
start_msg['unique_id'] = self._unique_id
start_msg['buffer'] = PAExecStartBuffer()
start_buffer = PAExecStartBuffer()
start_buffer['process_id'] = self.pid
start_buffer['comp_name'] = self.current_host.encode('utf-16-le')
start_msg['buffer'] = start_buffer
log.info("Writing PAExecMsg with PAExecStartBuffer to start the "
"remote process")
main_access_mask = FilePipePrinterAccessMask.GENERIC_READ | \
FilePipePrinterAccessMask.GENERIC_WRITE | \
FilePipePrinterAccessMask.FILE_APPEND_DATA | \
FilePipePrinterAccessMask.READ_CONTROL | \
FilePipePrinterAccessMask.SYNCHRONIZE
# connect to the main pipe and read the output
main_pipe = create_pipe(tree, main_name, main_access_mask)
main_pipe.write(input_data, 0)
main_out = main_pipe.read(0, 1024, wait=True)
main_out_resp = PAExecMsg()
main_out_resp.unpack(main_out)
main_out_resp.check_resp()
# send the start process
start_msg = PAExecMsg()
start_msg['msg_id'] = PAExecMsgId.MSGID_START_APP
start_msg['unique_id'] = paexec_id
start_msg_buffer = PAExecStartBuffer()
start_msg_buffer['process_id'] = pid
start_msg_buffer['comp_name'] = current_host.encode('utf-16-le')
start_msg['buffer'] = start_msg_buffer
start_msg_b = start_msg.pack()
main_pipe.write(start_msg_b, 0)
out_access_mask = FilePipePrinterAccessMask.FILE_READ_DATA | \
FilePipePrinterAccessMask.FILE_READ_ATTRIBUTES | \
FilePipePrinterAccessMask.FILE_READ_EA | \
FilePipePrinterAccessMask.READ_CONTROL | \
FilePipePrinterAccessMask.SYNCHRONIZE
stdout_name = "PaExecOut%s%d" % (current_host, pid)
stderr_name = "PaExecErr%s%d" % (current_host, pid)
stdin_name = "PaExecIn%s%d" % (current_host, pid)
# create the pipes for RemCom
main_access_mask = FilePipePrinterAccessMask.GENERIC_READ | \
FilePipePrinterAccessMask.GENERIC_WRITE | \
FilePipePrinterAccessMask.FILE_APPEND_DATA | \
FilePipePrinterAccessMask.READ_CONTROL | \
FilePipePrinterAccessMask.SYNCHRONIZE
# connect to the main pipe and read the output
main_pipe = create_pipe(tree, main_name, main_access_mask)
main_pipe.write(input_data, 0)
main_out = main_pipe.read(0, 1024, wait=True)
main_out_resp = PAExecMsg()
main_out_resp.unpack(main_out)
main_out_resp.check_resp()
# send the start process
start_msg = PAExecMsg()
start_msg['msg_id'] = PAExecMsgId.MSGID_START_APP
start_msg['unique_id'] = paexec_id
start_msg_buffer = PAExecStartBuffer()
start_msg_buffer['process_id'] = pid
start_msg_buffer['comp_name'] = current_host.encode('utf-16-le')
start_msg['buffer'] = start_msg_buffer
start_msg_b = start_msg.pack()
main_pipe.write(start_msg_b, 0)
def __init__(self):
self.fields = OrderedDict([
('msg_id', EnumField(
size=2,
enum_type=PAExecMsgId
)),
('unique_id', IntField(size=4)),
('buffer_length', IntField(
size=4,
default=lambda s: len(s['buffer'])
)),
('buffer', BytesField(
size=lambda s: s['buffer_length'].get_value()
))
])
super(PAExecMsg, self).__init__()
# process is finished so the stdout jobs should be complete
stdout_proc.join()
stderr_proc.join()
while True:
try:
stdout += stdout_queue.get(block=False)
except Empty:
break
while True:
try:
stderr += stderr_queue.get(block=False)
except Empty:
break
resp_msg = PAExecMsg()
resp_msg.unpack(resp)
resp_msg.check_resp()
rc = PAExecReturnBuffer()
rc.unpack(resp_msg['buffer'].get_value())
finally:
tree.disconnect()
# stop and delete the service at the end
scmr_api = SCMRApi(session)
scmr_api.open()
try:
sc_desired_access = DesiredAccess.SC_MANAGER_CONNECT | \
DesiredAccess.SC_MANAGER_ENUMERATE_SERVICE
scm_handle = scmr_api.open_sc_manager_w(server, None, sc_desired_access)
try:
svc_desired_access = DesiredAccess.SERVICE_QUERY_STATUS | \
stderr_pipe.close()
stdin_pipe.close()
log.info("Gettings stdout and stderr from pipe buffer queue")
stdout_out = stdout_pipe.get_output()
stderr_bytes = stderr_pipe.get_output()
else:
stdout_out = None
stderr_bytes = None
log.info("Closing main PAExec pipe")
main_pipe.close()
log.info("Disconnecting from SMB Tree %s" % smb_tree.share_name)
smb_tree.disconnect()
log.info("Unpacking PAExecMsg data from process result")
exe_result = PAExecMsg()
exe_result.unpack(exe_result_raw)
log.debug(str(exe_result))
exe_result.check_resp()
log.debug("Unpacking PAExecReturnBuffer from main PAExecMsg")
rc = PAExecReturnBuffer()
rc.unpack(exe_result['buffer'].get_value())
log.debug(str(rc))
return_code = rc['return_code'].get_value()
log.info("Process finished with exit code: %d" % return_code)
log.debug("RC: %d" % return_code)
return stdout_out, stderr_bytes, return_code
else:
break
log.info("Writing PAExecSettingsMsg to the main PAExec pipe")
log.info(str(input_data))
main_pipe.write(input_data.pack(), 0)
log.info("Reading PAExecMsg from the PAExec pipe")
settings_resp_raw = main_pipe.read(0, 1024)
settings_resp = PAExecMsg()
settings_resp.unpack(settings_resp_raw)
log.debug(str(settings_resp))
settings_resp.check_resp()
# start the process now
start_msg = PAExecMsg()
start_msg['msg_id'] = PAExecMsgId.MSGID_START_APP
start_msg['unique_id'] = self._unique_id
start_msg['buffer'] = PAExecStartBuffer()
start_buffer = PAExecStartBuffer()
start_buffer['process_id'] = self.pid
start_buffer['comp_name'] = self.current_host.encode('utf-16-le')
start_msg['buffer'] = start_buffer
log.info("Writing PAExecMsg with PAExecStartBuffer to start the "
"remote process")
log.debug(str(start_msg))
main_pipe.write(start_msg.pack(), 0)
if not interactive and not asynchronous:
# create a pipe for stdout, stderr, and stdin and run in a separate
# thread