Sysinternals Homepage
Forum Home Forum Home > Windows Discussions > Development
  New Posts New Posts RSS Feed: Hooking API RegCreateKeyA,RegCreateKeyW
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Hooking API RegCreateKeyA,RegCreateKeyW

 Post Reply Post Reply Page  123 7>
Author
Message Reverse Sort Order
molotov View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 04 October 2006
Online Status: Offline
Posts: 17492
Post Options Post Options   Quote molotov Quote  Post ReplyReply Direct Link To This Post Topic: Hooking API RegCreateKeyA,RegCreateKeyW
    Posted: 12 May 2009 at 11:09am
I'd suggest you go through the referenced links to get a feel for things.   The WDK includes a sample FSFD, I believe, and you will undoubtedly find some examples by searching...
Daily affirmation:
net helpmsg 4006
Back to Top
zu1u View Drop Down
Newbie
Newbie


Joined: 09 May 2009
Online Status: Offline
Posts: 12
Post Options Post Options   Quote zu1u Quote  Post ReplyReply Direct Link To This Post Posted: 12 May 2009 at 10:17am
ok i'll try driver first. Are there sample drivers that are doing similar to my need? I'm totally new to drivers.
I'm not writing a comercial app.
Back to Top
molotov View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 04 October 2006
Online Status: Offline
Posts: 17492
Post Options Post Options   Quote molotov Quote  Post ReplyReply Direct Link To This Post Posted: 11 May 2009 at 6:46pm
The actual hook id used to get the module loaded doesn't really have to do anything - the HookProc can just be a dummy function.  Once the code is loaded you can detour the functions as the Detours examples show, etc.  There are other techniques for API hooking as well as other ways (AppInit_DLLs, CreateRemoteThread, etc.) to get your code loaded so you can hook the API.

i want to hook createFile from kernel32.dll
Probably, you would be better off hooking ntdll!NtCreateFile, if you're going this route...

Does a filesystem filter driver suit better to this?
One driver, vs. loading code into the address space of each process would seem to be a more direct approach.  Are you writing a commercial app, such that you would license Detours if you went with it?

File System Filter Drivers
Introduction to File System Filter Drivers
Daily affirmation:
net helpmsg 4006
Back to Top
zu1u View Drop Down
Newbie
Newbie


Joined: 09 May 2009
Online Status: Offline
Posts: 12
Post Options Post Options   Quote zu1u Quote  Post ReplyReply Direct Link To This Post Posted: 11 May 2009 at 5:52pm
Originally posted by molotov


however i didnt get the idea of how to hook a function globally
What do you mean by "globally"?  Every time it is called, regardless of the process that calls it?  If so, you will need to get your code loaded into the address space of each process and then devise some mechanism for dealing with what you wish to do once the hooked function is called.

exactly thats what i meant.  I read that i have to load the code in the adress space of all the processes.
The question i have is how can i achieve this in the easiest way?
If i use SetWindowsHookEx(.....,0) it is loaded in all process!? What idHook to use though? WM_GETMESSAGE?
And how am i invoking the detouring just from loading my code into the address space of some process?
Or better not using detouring in that case at all?

Originally posted by molotov


so i can do some preprocessing before any file is opened
It sounds like a filesystem filter driver may be better suited for what you wish to do...

maybe, but what i meant by preprocessing is really just that i want to forward the filepath of the file being openend to another part of the overall app.
Does a filesystem filter driver suit better to this? I'll read about it but if you can give some advice that'd be great
Back to Top
molotov View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 04 October 2006
Online Status: Offline
Posts: 17492
Post Options Post Options   Quote molotov Quote  Post ReplyReply Direct Link To This Post Posted: 11 May 2009 at 4:29pm
Hi zu1u,

however i didnt get the idea of how to hook a function globally
What do you mean by "globally"?  Every time it is called, regardless of the process that calls it?  If so, you will need to get your code loaded into the address space of each process and then devise some mechanism for dealing with what you wish to do once the hooked function is called.

so i can do some preprocessing before any file is opened
It sounds like a filesystem filter driver may be better suited for what you wish to do...
Daily affirmation:
net helpmsg 4006
Back to Top
zu1u View Drop Down
Newbie
Newbie


Joined: 09 May 2009
Online Status: Offline
Posts: 12
Post Options Post Options   Quote zu1u Quote  Post ReplyReply Direct Link To This Post Posted: 11 May 2009 at 4:17pm
i just managed to hook a process with detours as described here.
however i didnt get the idea of how to hook a function globally? Is that what the IPC was for?

In my case i want to hook createFile from kernel32.dll, so i can do some preprocessing before any file is opened.

Can someone point me into the right direction? thx
Back to Top
slhack View Drop Down
Newbie
Newbie


Joined: 21 May 2008
Online Status: Offline
Posts: 33
Post Options Post Options   Quote slhack Quote  Post ReplyReply Direct Link To This Post Posted: 08 June 2008 at 7:48am
Thank you very much! It's very easier now to hook apis. Thanks to all!
Now I have to try a lot of things to create my application LOL
You will hear me in a few days I think, for other problems Big%20smile


Edited by slhack - 08 June 2008 at 7:51am
Back to Top
molotov View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 04 October 2006
Online Status: Offline
Posts: 17492
Post Options Post Options   Quote molotov Quote  Post ReplyReply Direct Link To This Post Posted: 07 June 2008 at 7:09am
Hi Loins,
 
Thanks for sharing your Detours macros - looks like it would make using Detours easier. Smile
Daily affirmation:
net helpmsg 4006
Back to Top
nanothyll View Drop Down
Newbie
Newbie
Avatar

Joined: 12 May 2008
Location: China
Online Status: Offline
Posts: 28
Post Options Post Options   Quote nanothyll Quote  Post ReplyReply Direct Link To This Post Posted: 06 June 2008 at 7:36pm
I think you are using a wrong way for the detours library.

Here is some macro which i created to help you to hook APIs with detours lib:

------------------------------
#define BEGIN_HOOKS()    \
    DetourTransactionBegin();    \
    DetourUpdateThread(GetCurrentThread());

#define END_HOOKS()    \
    DetourTransactionCommit();

//////////////////////////////////////////////////////////////////////////

#define HOOK_ADD(n) DetourAttach(&(PVOID&)True_##n, Fake_##n);
#define HOOK_DEL(n) DetourDetach(&(PVOID&)True_##n, Fake_##n);

//////////////////////////////////////////////////////////////////////////

#define OLD_PROC(n)    True_##n
#define NEW_PROC(n) Fake_##n

//////////////////////////////////////////////////////////////////////////

#define DECLARE_HOOK(n,t,l)    extern t (WINAPI * True_##n)l;    \
extern t WINAPI Fake_##n l;

#define IMPLEMENT_HOOK(n,t,l)    t (WINAPI * True_##n)l = n;    \
                                t WINAPI Fake_##n l

#define ID(n)    FID_##n
-------------------------------------------------------

How to use it:

//////////////////////////////////////////////////////////////////////////
///@function: InstallHooks
///@note: Add APIs which you wanna hooking to the list
//////////////////////////////////////////////////////////////////////////

BOOL InstallHooks (void)
{
    BEGIN_HOOKS()
        HOOK_ADD(CopyFileA)
        HOOK_ADD(ExitProcess)
    END_HOOKS()

    return TRUE;
}

//////////////////////////////////////////////////////////////////////////
///@function: Uninstall Hooks
///@note: please remove the APIs which you hooked to the list
//////////////////////////////////////////////////////////////////////////

void UninstallHooks (void)
{
    BEGIN_HOOKS()
        HOOK_DEL(CopyFileA)
        HOOK_DEL(ExitProcess)
    END_HOOKS()
}

//////////////////////////////////////////////////////////////////////////
/// @function: DllMain
//////////////////////////////////////////////////////////////////////////

BOOL WINAPI DllMain (HINSTANCE hInst, DWORD dwReason, PVOID lpReserved)
{
    static HANDLE hMutex = NULL;

    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
        {
            DisableThreadLibraryCalls(hInst);

            if ((hMutex=CreateMutex(NULL, FALSE, NULL))==NULL) return FALSE;
           
            if (InitEmit(hMutex)==FALSE || InstallHooks()==FALSE)
                return FALSE;
        }
        break;

    case DLL_PROCESS_DETACH:
        {
            UninstallHooks();
            TermEmit();
            if (hMutex) {
                ReleaseMutex(hMutex);
                CloseHandle (hMutex);
            }
        }
        break;
    }

    return TRUE;
}

-------------------------------
in hooks.h declare the hook function:

DECLARE_HOOK (CopyFileA, BOOL, (LPCSTR lpExistingFileName,LPCSTR lpNewFileName,BOOL bFailIfExists))

in hooks.cpp implement it:

IMPLEMENT_HOOK(CopyFileA, BOOL, (LPCSTR lpExistingFileName,LPCSTR lpNewFileName,BOOL bFailIfExists))
{
    pack p(ID(CopyFileA));
    p.add_s1(lpExistingFileName);
    p.add_s2(lpNewFileName);
   
    EMIT(p);
    return OLD_PROC(CopyFileA)(lpExistingFileName, lpNewFileName, bFailIfExists);
}

===========
Very easy isn't it? BTW: I use detours 2.1 express :)
Hope that it is useful to you.
Back to Top
molotov View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 04 October 2006
Online Status: Offline
Posts: 17492
Post Options Post Options   Quote molotov Quote  Post ReplyReply Direct Link To This Post Posted: 06 June 2008 at 9:08am
By "setting up", I meant assigning the members.
 
Yes, since you're passing a mbcs buffer rather than a wide char buffer, you would use the *Ansi function.  If you would have passed a wide char buffer, the *Uni function would have worked.


Edited by molotov - 06 June 2008 at 9:09am
Daily affirmation:
net helpmsg 4006
Back to Top
 Post Reply Post Reply Page  123 7>

Forum Jump Forum Permissions View Drop Down