n00b
n00b

Reputation: 5722

NtOpenFile returns STATUS_OBJECT_NAME_INVALID

Following code tries to spawn a file handle using NtOpenFile :

HANDLE spawnFileHandle(){
HANDLE ret;
IO_STATUS_BLOCK IoStatusBlock;
OBJECT_ATTRIBUTES ObjectAttributes;
ObjectAttributes.SecurityDescriptor=0;
ObjectAttributes.SecurityQualityOfService=0;
ObjectAttributes.RootDirectory=0;
ObjectAttributes.Attributes=0;
ObjectAttributes.Length=sizeof(OBJECT_ATTRIBUTES);

WCHAR stringBuffer[5048];
UNICODE_STRING  string;
string.Buffer = stringBuffer;
lstrcpyW(stringBuffer, L"\\??\\");
lstrcatW(stringBuffer, EXEPath);
string.Length = lstrlenW(stringBuffer)*2; // Edit after comment.
string.MaximumLength = 5048;
ObjectAttributes.ObjectName=&string;
NTSTATUS error=origZwOpenFile(&ret, FILE_READ_DATA, &ObjectAttributes, &IoStatusBlock, FILE_SHARE_READ, 0);
printf("huh %ls %x", stringBuffer, error);
return ret;
}

but it allways returns STATUS_OBJECT_NAME_INVALID, example :

Edit : [HBIP] - Hidden Because Im Paranoid -.-

EXE path : C:\Users\n00b\Desktop\[HBIP]\Debug\[HBIP].exe
huh \??\C:\Users\n00b\Desktop\[HBIP]\Debug\[HBIP].exe c0000033
Spawned Handle : cccccccc

What could be the reason ?

Upvotes: 3

Views: 4857

Answers (1)

avakar
avakar

Reputation: 32635

The UNICODE_STRING structure expects both Length and MaximumLength to be in bytes. Note that these values will always be even.

You're getting STATUS_OBJECT_NAME_INVALID because your Length is an odd number, therefore invalid.

Upvotes: 3

Related Questions