Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is an amazing tool, but I found a little bug in your code! #122

Open
l0kihardt opened this issue Jun 12, 2020 · 3 comments
Open

This is an amazing tool, but I found a little bug in your code! #122

l0kihardt opened this issue Jun 12, 2020 · 3 comments
Labels
needs-testing Fix needs testing

Comments

@l0kihardt
Copy link

l0kihardt commented Jun 12, 2020

The bug happens when your mother tongue is not English.
When I send the mapi.RopQueryRowsRequest request to the Exchange server with property tags mapi.PidTagDisplayName and mapi.PidTagFolderID.

	setColumns.PropertyTags = make([]mapi.PropertyTag, 2)
	setColumns.PropertyTags[0] = mapi.PidTagDisplayName
	setColumns.PropertyTags[1] = mapi.PidTagFolderID

The response looks like this.
Screenshot_20200612_145513
The response data itself looks correct with folderID and folderName, but when ruler tries to read these tags with mapi.UnmarshalRops, bug happens.
If the language setting of the current user is not English, the end of the folderName will be 0x00 0x00, bug if the language setting of the current user is English, the end of the folderName will be 0x00 0x00 0x00.
So the code should be like this, otherwise a NULL byte will be eaten.

//ReadUnicodeString read and return a unicode string
func ReadUnicodeString(pos int, buff []byte) ([]byte, int) {
	//stupid hack as using bufio and ReadString(byte) would terminate too early
	//would terminate on 0x00 instead of 0x0000
	index := bytes.Index(buff[pos:], []byte{0x00, 0x00})
	if buff[pos+index+2] == 0x00 {
		index++ //unicode string end with 0x00 0x00 0x00
	}
	if index == -1 {
		return nil, 0
	}
	str := buff[pos : pos+index]
	return []byte(str), pos + index + 2
}

Also, When I send the mapi.RopQueryRowsRequest request to the Exchange Server 2016. I found that there is only one flag in the response.
So the code in the function (queryRows *RopQueryRowsResponse) Unmarshal(resp []byte, properties []PropertyTag) (int, error) should be like this


	flag, pos = utils.ReadByte(pos, resp)

	for k := 0; k < int(queryRows.RowCount); k++ {
		trow := PropertyRow{}
		//check if has flag (is flaggedpropertyrow)
		for _, property := range properties {

			if flag == 0x01 {
				trow.Flag, pos = utils.ReadByte(pos, resp)
			}
			if trow.Flag != 0x00 {
[...]
			} else if property.PropertyType == PtypString {
				trow.ValueArray, pos = utils.ReadUnicodeString(pos, resp)
				rows[k] = append(rows[k], trow)
			} else if property.PropertyType == PtypBinary {
@staaldraad
Copy link
Collaborator

Thanks for trying it out and the kind words @l0kihardt!

It seems like the ReadUnicodeString bug might be playing a role in this bug report as well: #121 (comment)

I'll have a look at these asap, but your suggestions make sense 😃

Thanks again 🙏

@staaldraad
Copy link
Collaborator

Reading through this it looks like the flag bug was likely because of the ReadUnicodeString bug

        } else if property.PropertyType == PtypString {
                   trow.ValueArray, pos = utils.ReadUnicodeString(pos, resp)
                   rows[k] = append(rows[k], trow)
-                 if len(trow.ValueArray) > 0 { //empty string means no extra null byte.
-                            pos++
-                 }

The flag needs to be set in the loop as it should be part of the PropertyRow (page 70): https://interoperability.blob.core.windows.net/files/MS-OXCDATA/%5BMS-OXCDATA%5D-080627.pdf
I worked off of the RopQueryRows Response documented here: https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxcrops/45a36946-5db6-4342-9403-80958fbe537c

@l0kihardt
Copy link
Author

Yeah, I think you are correct. I found that the response of the form display request has different formats...
Some of the delimiters are 0x00 0x00 0x00 0x00, and others are 0x00 0x00 0x00 0x01
Screenshot_20200616_081602
But as the picture I uploaded before shows, there is only two NULL byte as the delimiters.
Then if you use the same parse program, there will be some bug.

@staaldraad staaldraad added the needs-testing Fix needs testing label May 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-testing Fix needs testing
Projects
None yet
Development

No branches or pull requests

2 participants