Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


ZwCreateFile

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Tue May 15, 2007 7:52 am    Post subject: ZwCreateFile Reply with quote

UPDATE: ZwCreateFile is returning STATUS_ACCESS_DENIED. Any suggestions?


Ok, well I'm studying drivers and stuff and need a bit of help. Starting simple i'm trying to create a file with a driver. To do this I am trying to use ZwCreateFile.

The code I have written so far now compiles (lol) but when I load the driver the fucking file is nowhere to be found!

I was just wondering if someone could look through this and see what the hell I am doing wrong =\.

Heres my attempt so far:

Code:
#include <ntddk.h>

NTSYSAPI
NTSTATUS
NTAPI ZwCreateFile(
                OUT PHANDLE  FileHandle,
                IN ACCESS_MASK  DesiredAccess,
                IN POBJECT_ATTRIBUTES  ObjectAttributes,
                OUT PIO_STATUS_BLOCK  IoStatusBlock,
                IN PLARGE_INTEGER  AllocationSize,
                IN ULONG  FileAttributes,
                IN ULONG  ShareAccess,
                IN ULONG  CreateDisposition,
                IN ULONG  CreateOptions,
                IN PVOID  EaBuffer,
                IN ULONG  EaLength );

NTSTATUS UnloadDriver( IN PDRIVER_OBJECT DriverObject )
{
   DriverObject->DriverUnload;
   DbgPrint("NOZ3001: Driver unloaded.");

   return STATUS_SUCCESS;
}

NTSTATUS DriverEntry( IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING theRegistryPath )
{
   NTSTATUS ntStatus;
   LARGE_INTEGER AllocationSize;
   OBJECT_ATTRIBUTES DriverFile;
   UNICODE_STRING Directory;
   HANDLE hFileHandle = NULL;

   DbgPrint("NOZ3001: Driver loaded.");

   AllocationSize.HighPart = 500;
   AllocationSize.LowPart = 10;

   RtlInitUnicodeString(&Directory, L"\\??\\E:\\test.txt");
   InitializeObjectAttributes(&DriverFile, &Directory, OBJ_INHERIT, NULL, NULL);
   
   // Create file?
   ntStatus = ZwCreateFile(
      hFileHandle,
      GENERIC_WRITE,
      &DriverFile,
      NULL,
      &AllocationSize,
      0,
      FILE_OVERWRITE_IF,
      0,
      0,
      NULL,
      0);

      DbgPrint("NTSTATUS: %x", ntStatus);

   // Lets leave now the file is created
   // UnloadDriver( DriverObject );

    return STATUS_SUCCESS;

}



Sorry if this is really lame.. Helping me fix this will help me understand a lot more about driver development.


Last edited by Noz3001 on Tue May 15, 2007 9:16 am; edited 6 times in total
Back to top
View user's profile Send private message MSN Messenger
hakkairu
Legendary
Reputation: 0

Joined: 02 Dec 2006
Posts: 1301

PostPosted: Tue May 15, 2007 7:57 am    Post subject: Reply with quote

Hey Noz your off college today right?
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Tue May 15, 2007 7:58 am    Post subject: Reply with quote

Yea, every tuesday
Back to top
View user's profile Send private message MSN Messenger
linden
Master Cheater
Reputation: 0

Joined: 10 Mar 2006
Posts: 319

PostPosted: Tue May 15, 2007 9:18 am    Post subject: Reply with quote

The first parameter should be PHANDLE,
but you have
Code:

HANDLE hFileHandle = NULL;  <----------------

   DbgPrint("NOZ3001: Driver loaded.");

   RtlInitUnicodeString(&Directory, L"\\??\\E:\\test.txt");
   InitializeObjectAttributes(&DriverFile, &Directory, OBJ_INHERIT, NULL, NULL);
   
   // Create file?
   ntStatus = ZwCreateFile(
      hFileHandle,          <----------------
...

you are giving it a NULL pointer...
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Tue May 15, 2007 9:25 am    Post subject: Reply with quote

Thanks for pointing that out linden. That was deadly silly of me.

Now i've fixed it but ZwCreateFile is still returning c0000022 (STATUS_ACCESS_DENIED).

Crying or Very sad Any ideas?
Back to top
View user's profile Send private message MSN Messenger
linden
Master Cheater
Reputation: 0

Joined: 10 Mar 2006
Posts: 319

PostPosted: Tue May 15, 2007 9:27 am    Post subject: Reply with quote

How about setting OBJ_KERNEL_HANDLE flag for InitializeObjectAttributes?

Also, try setting PIO_STATUS_BLOCK.
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Tue May 15, 2007 9:31 am    Post subject: Reply with quote

Same error =(.

All i want to do is create a file =D. Is that too much to ask? Cool
Back to top
View user's profile Send private message MSN Messenger
linden
Master Cheater
Reputation: 0

Joined: 10 Mar 2006
Posts: 319

PostPosted: Tue May 15, 2007 9:39 am    Post subject: Reply with quote

This is a wrapper I wrote for myself so I can use it like it is in usermode. And so far, I never had much trouble with it... Don't know if it would be of any help for you though Wink
Code:

HANDLE CreateFile(
   const PUNICODE_STRING  pusInFileName,
   ACCESS_MASK            dwDesiredAccess,
   DWORD                  dwShareMode,
   PSECURITY_ATTRIBUTES   lpSecurityAttributes,  // I'll ignore this!
   DWORD                  dwCreationDisposition,
   DWORD                  dwFlagsAndAttributes,
   HANDLE                 hTemplateFile
   )
{
   OBJECT_ATTRIBUTES    ObjAtt;
   HANDLE               hFile = NULL;
   IO_STATUS_BLOCK      ioStatus;
   NTSTATUS             NtStatus;

   RtlZeroMemory(&ObjAtt, sizeof(OBJECT_ATTRIBUTES));
   InitializeObjectAttributes(&ObjAtt, pusInFileName, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);

   //KdPrint( ("CreateFile() FilePath=%.*ws\n", pusInFileName->Length / 2, pusInFileName->Buffer) );

   NtStatus = ZwCreateFile(&hFile,
                           dwDesiredAccess | SYNCHRONIZE,
                           &ObjAtt,
                           &ioStatus,
                           NULL,
                           dwFlagsAndAttributes,
                           dwShareMode,
                           dwCreationDisposition,
                           FILE_RANDOM_ACCESS | FILE_SYNCHRONOUS_IO_NONALERT,
                           NULL,
                           0);

   //DbgPrint("CreateFile() NtStatus=0x%08X, FileHandle=0x%08X, AccessMask=0x%08X\n", NtStatus, hFile, dwDesiredAccess);

   if(!NT_SUCCESS(NtStatus)) return INVALID_HANDLE_VALUE;
   
   return hFile;
}
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Tue May 15, 2007 9:48 am    Post subject: Reply with quote

Nice, ill keep that handy. Thanks.

I'm gonna re-boot and try again. I just noticed i commented the unload function out so i have like 50 versions loaded.

(Maybe thats why i've been getting an access error? How stupid)


EDIT: Nope, still access denied.
Back to top
View user's profile Send private message MSN Messenger
linden
Master Cheater
Reputation: 0

Joined: 10 Mar 2006
Posts: 319

PostPosted: Tue May 15, 2007 10:30 am    Post subject: Reply with quote

hmmmm... Sad
This is what I have when I call my wrapper
Code:

   hFile = CreateFile(pusFilePath, FILE_GENERIC_WRITE, 0, NULL, FILE_SUPERSEDE, 0, NULL);
   if( hFile == INVALID_HANDLE_VALUE ){
           .....


So, how about using FILE_GENERIC_WRITE instead of GENERIC_WRITE Question
I see that FILE_GENERIC_WRITE and GENERIC_WRITE are quite different in ntddk.h...
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Tue May 15, 2007 11:24 am    Post subject: Reply with quote

Theres something strange going on. It always returns an error on my PC but on someone elses it worked!? Maybe because I installed XP over windows 2000? (Only difference is that i have a WINNT folder)
Back to top
View user's profile Send private message MSN Messenger
Murder818
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2006
Posts: 942
Location: .SiCk.

PostPosted: Tue May 15, 2007 4:35 pm    Post subject: Reply with quote

Fix by Void Wink

Code:
 HANDLE hFile;
                     IO_STATUS_BLOCK isb;
                     LARGE_INTEGER liSize;
                     OBJECT_ATTRIBUTES oaFileAttributes;
                     UNICODE_STRING usFilePath;
                              //pVirtAddr = (PVOID)paPhysAddr.LowPart;
                              RtlInitUnicodeString(&usFilePath, L"\\DosDevices\\C:\\driverfile.txt");
                     InitializeObjectAttributes(&oaFileAttributes,
                                        &usFilePath,
                                        OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
                                        (HANDLE)NULL,
                                        NULL);
                     liSize.QuadPart = 102400;
                     ntStatus = ZwCreateFile(&hFile,
                                        GENERIC_WRITE,
                                        &oaFileAttributes,
                                        &isb,
                                        &liSize,
                                        FILE_ATTRIBUTE_NORMAL,
                                        FILE_SHARE_WRITE,
                                        FILE_CREATE,
                                        FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
                                        NULL,
                                        0);

_________________
Back to top
View user's profile Send private message AIM Address
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Wed May 16, 2007 5:50 am    Post subject: Reply with quote

LOL, the DosDevices thing fixed it. Thanks all who helped. Thanks linden for being great! Very Happy
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites