Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
share = Open(smb_tree, "")
share.create(ImpersonationLevel.Impersonation,
DirectoryAccessMask.FILE_READ_ATTRIBUTES |
DirectoryAccessMask.SYNCHRONIZE |
DirectoryAccessMask.FILE_LIST_DIRECTORY,
FileAttributes.FILE_ATTRIBUTE_DIRECTORY,
ShareAccess.FILE_SHARE_READ |
ShareAccess.FILE_SHARE_WRITE |
ShareAccess.FILE_SHARE_DELETE,
CreateDisposition.FILE_OPEN,
CreateOptions.FILE_DIRECTORY_FILE)
try:
paexec_files = share.query_directory("PAExec-*.exe",
FileInformationClass.
FILE_NAMES_INFORMATION)
except SMBResponseException as exc:
if exc.status != NtStatus.STATUS_NO_SUCH_FILE:
raise exc
paexec_files = []
return client, paexec_services, paexec_files
if connection_username is None or connection_password is None and \
not HAS_KERBEROS:
module.fail_json(msg=missing_required_lib("gssapi"),
execption=KERBEROS_IMP_ERR)
win_client = client.Client(server=hostname, username=connection_username,
password=connection_password, port=port,
encrypt=encrypt)
try:
win_client.connect(timeout=connection_timeout)
except SMBAuthenticationError as exc:
module.fail_json(msg='Failed to authenticate over SMB: %s'
% to_text(exc))
except SMBResponseException as exc:
module.fail_json(msg='Received unexpected SMB response when opening '
'the connection: %s' % to_text(exc))
except PDUException as exc:
module.fail_json(msg='Received an exception with RPC PDU message: %s'
% to_text(exc))
except SCMRException as exc:
module.fail_json(msg='Received an exception when dealing with SCMR on '
'the Windows host: %s' % to_text(exc))
except (SMBException, PypsexecException) as exc:
module.fail_json(msg=to_text(exc))
except socket.error as exc:
module.fail_json(msg=to_text(exc))
# create PAExec service and run the process
result['changed'] = True
b_stdin = to_bytes(stdin, encoding='utf-8') if stdin else None
# thread know it can continue before we are blocked by the read
read_msg, read_resp_func = self.pipe.read(0, 1024, send=False)
log.debug("Sending SMB Read request for Output Named Pipe: %s"
% self.name)
request = self.connection.send(read_msg,
sid=self.sid,
tid=self.tid)
self.sent_first = True
try:
log.debug("Reading SMB Read response for Output Named "
"Pipe: %s" % self.name)
pipe_out = read_resp_func(request)
log.debug("Received SMB Read response for Output Named "
"Pipe: %s" % self.name)
self.handle_output(pipe_out)
except SMBResponseException as exc:
# if the error was the pipe was broken exit the loop
# otherwise the error is serious so throw it
close_errors = [
NtStatus.STATUS_PIPE_BROKEN,
NtStatus.STATUS_PIPE_CLOSING,
NtStatus.STATUS_PIPE_EMPTY,
NtStatus.STATUS_PIPE_DISCONNECTED
]
if exc.status in close_errors:
log.debug("%s received for Output Named Pipe: %s, "
"ending thread"
% (str(exc.header['status']), self.name))
break
else:
raise exc
finally:
def read_pipe(pipe, queue):
sent_first = False
while True:
# keep on trying to get the pipe output until we receive a
# STATUS_PIPE_BROKEN
pending_id = None
try:
data = pipe.read(0, 255)
if not sent_first:
queue.put(None) # tells parent thread to read from main pipe
sent_first = True
queue.put(data)
except SMBResponseException as exc:
if not sent_first:
queue.put(None)
sent_first = True
if exc.status == NtStatus.STATUS_PENDING:
# need to poll the receive queue for the final message
pending_id = exc.message_id
elif exc.status == NtStatus.STATUS_PIPE_BROKEN:
break
else:
raise exc
if pending_id:
try:
a = pipe.connection.receive(pending_id)
except SMBResponseException as exc:
if exc.status == NtStatus.STATUS_PIPE_BROKEN:
input_data = PAExecSettingsMsg()
input_data['unique_id'] = self._unique_id
input_data['buffer'] = settings
# write the settings to the main PAExec pipe
pipe_access_mask = FilePipePrinterAccessMask.GENERIC_READ | \
FilePipePrinterAccessMask.GENERIC_WRITE | \
FilePipePrinterAccessMask.FILE_APPEND_DATA | \
FilePipePrinterAccessMask.READ_CONTROL | \
FilePipePrinterAccessMask.SYNCHRONIZE
for i in range(0, 3):
try:
main_pipe = open_pipe(smb_tree, self._exe_file,
pipe_access_mask)
except SMBResponseException as exc:
if exc.status != NtStatus.STATUS_OBJECT_NAME_NOT_FOUND:
raise exc
elif i == 2:
raise PypsexecException("Failed to open main PAExec pipe "
"%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)
req_data += struct.pack("