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 


Pushing messages from driver to user mode application

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Sat Feb 12, 2011 6:58 pm    Post subject: Pushing messages from driver to user mode application Reply with quote

I'm developing an application that needs to be notified on every thread startup on the system. As such I have a driver that uses PsSetCreateThreadNotifyRoutineEx to make a callback. Right now I'm sending information about new threads to DbgView via the kernel debugging APIs. What I want to do is have an event-driven system in my user-mode application (written in C#) that is notified of new threads.

I had my C# application use the NamerPipeServerStream class to create a new named pipe. I signal the driver to say that the named pipe has been created using DeviceIoControl. The named pipe on the user application side performs async reads. When new data comes in, the application shows the data in a message box.

In the driver I use ZwCreateFile to open a handle to the named pipe, then ZwWriteFile to send the data. Unfortunately something seems to be going wrong, since the data never reaches the usermode application.

Here's my code:
Code:
CHAR buffer[1024];
size_t cb;
IO_STATUS_BLOCK pipe_status;
HANDLE hPipe;
// hPipe is set by ZwCreateFile before any of this.
RtlStringCbPrintfA(buffer, sizeof(buffer), "Thread created: %d", ThreadId);
RtlStringCbLengthA(buffer, sizeof(buffer), &cb);
ZwWriteFile(hPipe, NULL, NULL, NULL, &pipe_status, buffer, cb, NULL, NULL);


I've checked that hPipe is a valid handle, and it does exist. I can't see what's going wrong here.

Ideas? Better solutions? I really don't want to resort to a pull message queue for this. It's a shame there's no such thing as ApplicationIoControl to do the inverse of DeviceIoControl!

_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25779
Location: The netherlands

PostPosted: Sat Feb 12, 2011 7:48 pm    Post subject: Reply with quote

What is the return value of ZwWriteFile ?

and the createfile parameters might be wrong (you set it up as a blocking call correct ?)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
NoMercy
Master Cheater
Reputation: 1

Joined: 09 Feb 2009
Posts: 289

PostPosted: Sun Feb 13, 2011 3:44 am    Post subject: Reply with quote

Why don't you call DeviceIOControl, with as inpur buffer a void pointer (void*) and then parse in the driver the string into that pointer, and the usermode apllication has the value to.
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Mon Feb 14, 2011 4:52 pm    Post subject: Reply with quote

@NoMercy
That's a pretty unreliable method, since it relies on a shared memory block. It's a synchronization nightmare. Add in problems caused by GC and unpinned managed memory and it becomes a disaster.


Anyway, I ended up going a different route. My code uses a global event which is acknowledged by the user-mode application, which then follows it up with a DeviceIoControl call to get the parameters of the event.

In order to prevent preemption problems (e.g. second event raised before user-mode app can read event info from DeviceIoControl) I'm using a KEVENT which blocks the process notification callback until an IRP comes in to ask about the event. For safety I set a timeout of 500ms on process create routine event and a timeout of 1000ms on thread create routine event (thread create requires two IRPs from the userland app) so it doesn't hang if you kill the app. The problem is that it seems to sometimes take longer than 1 second to go from ZwSetEvent on the global event, to the user app, back to the driver via DeviceIoControl and KeSetEvent. This seems a little excessive, even for the managed part of it. Any ideas what might be causing this latency?

_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Thu Feb 17, 2011 4:42 am    Post subject: Reply with quote

Ok, so it turns out (for some reason) my wait thread was synchronous between event wake-ups and took quite a while to complete an iteration. Probably related to switching between a native callback and managed code. I created 25 wait threads and that seems to have solved the problem.
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
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