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_parse_pdu_fine(self):
connection = Connection(uuid.uuid4(), "server", 445)
session = Session(connection, "user", "password")
api = SCMRApi(session)
response_pdu = ResponsePDU()
response_pdu['packed_drep'] = DataRepresentationFormat()
response_pdu['stub_data'] = b"\x01\x02\x03\x04"
expected = b"\x01\x02\x03\x04"
actual = api._parse_pdu(response_pdu.pack(), 10)
assert actual == expected
def test_marshal_string_none(self):
connection = Connection(uuid.uuid4(), "server", 445)
session = Session(connection, "user", "password")
api = SCMRApi(session)
expected = b"\x00\x00\x00\x00"
actual = api._marshal_string(None)
assert actual == expected
def test_marshal_string_no_padding(self):
connection = Connection(uuid.uuid4(), "server", 445)
session = Session(connection, "user", "password")
api = SCMRApi(session)
expected = b"\x02\x00\x00\x00" \
b"\x00\x00\x00\x00" \
b"\x02\x00\x00\x00" \
b"\x68\x00\x00\x00"
actual = api._marshal_string("h")
assert actual == expected
def test_enumerate_services_small_buffer(self, session):
scmr = SCMRApi(session)
scmr.open()
try:
scmr_handle = scmr.open_sc_manager_w(
session.connection.server_name,
None,
DesiredAccess.SC_MANAGER_CONNECT |
DesiredAccess.SC_MANAGER_CREATE_SERVICE |
DesiredAccess.SC_MANAGER_ENUMERATE_SERVICE)
actual = scmr.enum_services_status_w(scmr_handle,
ServiceType.
SERVICE_INTERACTIVE_PROCESS,
EnumServiceState.
SERVICE_STATE_ALL)
assert len(actual) > 0
assert isinstance(actual[0]['display_name'], string_types)
def test_enumerate_services(self, session):
scmr = SCMRApi(session)
scmr.open()
try:
scmr_handle = scmr.open_sc_manager_w(
session.connection.server_name,
None,
DesiredAccess.SC_MANAGER_CONNECT |
DesiredAccess.SC_MANAGER_CREATE_SERVICE |
DesiredAccess.SC_MANAGER_ENUMERATE_SERVICE)
types = ServiceType.SERVICE_INTERACTIVE_PROCESS | \
ServiceType.SERVICE_KERNEL_DRIVER | \
ServiceType.SERVICE_WIN32_SHARE_PROCESS | \
ServiceType.SERVICE_WIN32_OWN_PROCESS | \
ServiceType.SERVICE_FILE_SYSTEM_DRIVER
actual = scmr.enum_services_status_w(scmr_handle,
types,
def test_marshal_string(self):
connection = Connection(uuid.uuid4(), "server", 445)
session = Session(connection, "user", "password")
api = SCMRApi(session)
expected = b"\x03\x00\x00\x00" \
b"\x00\x00\x00\x00" \
b"\x03\x00\x00\x00" \
b"\x68\x00\x69\x00\x00\x00" \
b"\x00\x00"
actual = api._marshal_string("hi")
assert actual == expected
def test_parse_error_unknown(self):
connection = Connection(uuid.uuid4(), "server", 445)
session = Session(connection, "user", "password")
api = SCMRApi(session)
with pytest.raises(SCMRException) as exc:
api._parse_error(999, "function_name")
assert str(exc.value) == "Exception calling function_name. Code: 999" \
", Msg: ERROR_UNKNOWN"
exe_payload = paexec_out_stream
exe_path = "%s.exe" % svc_name
# Setup SMB connection and session
guid = uuid.uuid4()
connection = Connection(guid, server, port)
try:
connection.connect()
session = Session(connection, username, password)
session.connect()
# open the service manager
scmr_api = SCMRApi(session)
scmr_api.open()
try:
sc_desired_access = DesiredAccess.SC_MANAGER_CONNECT | \
DesiredAccess.SC_MANAGER_CREATE_SERVICE | \
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 | \
DesiredAccess.SERVICE_START | \
DesiredAccess.SERVICE_STOP | \
DesiredAccess.DELETE
# delete and create a brand new service
try:
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 | \
DesiredAccess.SERVICE_STOP | \
DesiredAccess.DELETE
try:
service_handle = scmr_api.open_service_w(scm_handle, svc_name,
svc_desired_access)
except SCMRException as exc:
if exc.return_code != 1060:
raise exc
def open(self):
if self._scmr:
log.debug("Handle for SCMR on %s is already open"
% self.smb_session.connection.server_name)
return
# connect to the SCMR Endpoint
log.info("Opening handle for SCMR on %s"
% self.smb_session.connection.server_name)
self._scmr = SCMRApi(self.smb_session)
self._scmr.open()
self._scmr_handle = self._scmr.open_sc_manager_w(
self.smb_session.connection.server_name,
None,
DesiredAccess.SC_MANAGER_CONNECT |
DesiredAccess.SC_MANAGER_CREATE_SERVICE |
DesiredAccess.SC_MANAGER_ENUMERATE_SERVICE
)