def test_create_message(self): message = SMB2SymbolicLinkErrorResponse() message.set_name(r"C:\temp\folder", r"\??\C:\temp\folder") expected = b"\x58\x00\x00\x00" \ b"\x53\x59\x4d\x4c" \ b"\x0c\x00\x00\xa0" \ b"\x4c\x00" \ b"\x00\x00" \ b"\x1c\x00" \ b"\x24\x00" \ b"\x00\x00" \ b"\x1c\x00" \ b"\x00\x00\x00\x00" \ b"\x43\x00\x3a\x00\x5c\x00\x74\x00" \ b"\x65\x00\x6d\x00\x70\x00\x5c\x00" \ b"\x66\x00\x6f\x00\x6c\x00\x64\x00" \ b"\x65\x00\x72\x00" \ b"\x5c\x00\x3f\x00\x3f\x00\x5c\x00" \ b"\x43\x00\x3a\x00\x5c\x00\x74\x00" \ b"\x65\x00\x6d\x00\x70\x00\x5c\x00" \ b"\x66\x00\x6f\x00\x6c\x00\x64\x00" \ b"\x65\x00\x72\x00" actual = message.pack() assert len(actual) == 92 assert actual == expected
def test_throw_exception_with_symlink_redir(self): symlnk_redir = SMB2SymbolicLinkErrorResponse() symlnk_redir.set_name(r"C:\temp\folder", r"\??\C:\temp\folder") cont_resp = SMB2ErrorContextResponse() cont_resp['error_context_data'] = symlnk_redir error_resp = SMB2ErrorResponse() error_resp['error_data'] = [cont_resp] header = self._get_header(error_resp, NtStatus.STATUS_STOPPED_ON_SYMLINK) try: raise SMBResponseException(header, header['status'].get_value(), header['message_id'].get_value()) except SMBResponseException as exc: assert len(exc.error_details) == 1 err1 = exc.error_details[0] assert isinstance(err1, SMB2SymbolicLinkErrorResponse) exp_resp = "Received unexpected status from the server: " \ "(2147483693) STATUS_STOPPED_ON_SYMLINK: 0x8000002d " \ "- Flag: (0) SYMLINK_FLAG_ABSOLUTE, " \ r"Print Name: C:\temp\folder, " \ r"Substitute Name: \??\C:\temp\folder" assert exc.message == exp_resp assert str(exc) == exp_resp assert exc.status == NtStatus.STATUS_STOPPED_ON_SYMLINK
def test_parse_message(self): actual = SMB2SymbolicLinkErrorResponse() data = b"\x58\x00\x00\x00" \ b"\x53\x59\x4d\x4c" \ b"\x0c\x00\x00\xa0" \ b"\x4c\x00" \ b"\x00\x00" \ b"\x1c\x00" \ b"\x24\x00" \ b"\x00\x00" \ b"\x1c\x00" \ b"\x00\x00\x00\x00" \ b"\x43\x00\x3a\x00\x5c\x00\x74\x00" \ b"\x65\x00\x6d\x00\x70\x00\x5c\x00" \ b"\x66\x00\x6f\x00\x6c\x00\x64\x00" \ b"\x65\x00\x72\x00" \ b"\x5c\x00\x3f\x00\x3f\x00\x5c\x00" \ b"\x43\x00\x3a\x00\x5c\x00\x74\x00" \ b"\x65\x00\x6d\x00\x70\x00\x5c\x00" \ b"\x66\x00\x6f\x00\x6c\x00\x64\x00" \ b"\x65\x00\x72\x00" data = actual.unpack(data) assert len(actual) == 92 assert data == b"" assert actual['symlink_length'].get_value() == 88 assert actual['symlink_error_tag'].get_value() == b"\x53\x59\x4d\x4c" assert actual['reparse_tag'].get_value() == b"\x0c\x00\x00\xa0" assert actual['reparse_data_length'].get_value() == 76 assert actual['unparsed_path_length'].get_value() == 0 assert actual['substitute_name_offset'].get_value() == 28 assert actual['substitute_name_length'].get_value() == 36 assert actual['print_name_offset'].get_value() == 0 assert actual['print_name_length'].get_value() == 28 assert actual['flags'].get_value() == \ SymbolicLinkErrorFlags.SYMLINK_FLAG_ABSOLUTE assert actual['path_buffer'].get_value() == \ b"\x43\x00\x3a\x00\x5c\x00\x74\x00" \ b"\x65\x00\x6d\x00\x70\x00\x5c\x00" \ b"\x66\x00\x6f\x00\x6c\x00\x64\x00" \ b"\x65\x00\x72\x00" \ b"\x5c\x00\x3f\x00\x3f\x00\x5c\x00" \ b"\x43\x00\x3a\x00\x5c\x00\x74\x00" \ b"\x65\x00\x6d\x00\x70\x00\x5c\x00" \ b"\x66\x00\x6f\x00\x6c\x00\x64\x00" \ b"\x65\x00\x72\x00" assert actual.get_print_name() == r"C:\temp\folder" assert actual.get_substitute_name() == r"\??\C:\temp\folder"
def test_resolve_path_different_host(self): b_sub_name = to_bytes(u'\\??\\UNC\\sérver\\sharé2\\foldér', encoding='utf-16-le') b_print_name = to_bytes(u'\\\\sérver\\sharé2\\foldér', encoding='utf-16-le') resp = SMB2SymbolicLinkErrorResponse() resp['unparsed_path_length'] = 0 resp['substitute_name_offset'] = 0 resp['substitute_name_length'] = len(b_sub_name) resp['print_name_offset'] = len(b_sub_name) resp['print_name_length'] = len(b_print_name) resp['flags'] = SymbolicLinkErrorFlags.SYMLINK_FLAG_ABSOLUTE resp['path_buffer'] = b_sub_name + b_print_name link_path = u'\\\\sérver\\sharé\\foldér' expected = u"Encountered symlink at '%s' that points to '\\\\sérver\\sharé2\\foldér' which cannot be " \ u"redirected: Cannot resolve link targets that point to a different host/share" % link_path with pytest.raises(SMBLinkRedirectionError, match=re.escape(to_native(expected))): resp.resolve_path(link_path)
def test_resolve_path(self, unparsed_length, sub_name, print_name, flags, link_path, expected): b_sub_name = to_bytes(sub_name, encoding='utf-16-le') b_print_name = to_bytes(print_name, encoding='utf-16-le') resp = SMB2SymbolicLinkErrorResponse() resp['unparsed_path_length'] = unparsed_length resp['substitute_name_offset'] = 0 resp['substitute_name_length'] = len(b_sub_name) resp['print_name_offset'] = len(b_sub_name) resp['print_name_length'] = len(b_print_name) resp['flags'] = flags resp['path_buffer'] = b_sub_name + b_print_name assert resp.get_print_name() == print_name assert resp.get_substitute_name() == sub_name actual = resp.resolve_path(link_path) assert actual == expected
def test_throw_exception_with_symlink_redir(self): symlnk_redir = SMB2SymbolicLinkErrorResponse() symlnk_redir.set_name(r"C:\temp\folder", r"\??\C:\temp\folder") cont_resp = SMB2ErrorContextResponse() cont_resp['error_context_data'] = symlnk_redir error_resp = SMB2ErrorResponse() error_resp['error_data'] = [cont_resp] header = self._get_header(error_resp, NtStatus.STATUS_STOPPED_ON_SYMLINK) try: raise SMBResponseException(header) except SMBResponseException as exc: assert len(exc.error_details) == 1 err1 = exc.error_details[0] assert isinstance(err1, SMB2SymbolicLinkErrorResponse) exp_resp = "Received unexpected status from the server: The create operation stopped after reaching a " \ "symbolic link. (2147483693) STATUS_STOPPED_ON_SYMLINK: 0x8000002d - Flag: (0) " \ "SYMLINK_FLAG_ABSOLUTE, Print Name: C:\\temp\\folder, Substitute Name: \\??\\C:\\temp\\folder" assert exc.message == exp_resp assert str(exc) == exp_resp assert exc.status == NtStatus.STATUS_STOPPED_ON_SYMLINK