how to hook API calls ?

Just starting out? Need help? Post your questions and find answers here.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

how to hook API calls ?

Post by newbie »

Hi,

i started in another thread to talk about it but i think it's better to create a new one in the beginner area.

My purpose : to monitor API calls to a particular API, let's say "GetCurrentDirectory". I want to see when the API is called, and by which process/executable.

I tried to do an API hook with "SetWindowsHookEx" but i fail to do it, may be it isn't the good way.

Anyone having a clue or better, a code sample ?

Thanks you if you can help me :)

regards,

newbie.
- Registered PB user -

Using PB 4.00
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

I think the problem could be the hook must be inside a shared DLL and PB can't do that directly. You should modify the assembly and the .def file.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

You can hook in injecting remote thread, or injecting DLL (PB can do this), but for doing a wide hook i thought it was the API i tried to use but i didn't find how.
- Registered PB user -

Using PB 4.00
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

Some parts of the DLL (or eventually all) should be loaded in shared memory so any process can access, that's how you can do system-wide DLLs (I did one in pure ASM to handle keyboard). On MSDN you can find a good explanation. How do you do that with PB ?

From what I know you should modify the generated asm file (/COMMENTED) and add a section that will be then declared as shared. Then you need to set a switch in Lcclnk (personally I used Link that comes with MASM32 but I think Lcclnk supports that). The problem with PB is that it calls Lcclnk directly and you cant set any switch for it.

Ehi Fred: can you enhance the compiler and let pass to it arguments for the linker ? Something like:

pbcompiler test.pb /LCCLNK:argument_for_lcclnk

Also to be able to pass to compiler additional objects or .RES files would be very welcome. I think it's very easy to do. :wink:
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

Seldon wrote:Ehi Fred: can you enhance the compiler and let pass to it
arguments for the linker ? Something like:

pbcompiler test.pb /LCCLNK:argument_for_lcclnk

Also to be able to pass to compiler additional objects or .RES
files would be very welcome. I think it's very easy to do. :wink:
That would be very nice and GPI could stop with his sh*t
to patch the compiler.

@newbie:
SetWindowsHookEx is a function to hook Windows Messages
and stuff, but its not for hooking API calls.
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You can take a look to the following (very very interesting) site: http://help.madshi.net/madCodeHook.htm and you will learn how definitely hook anything on Windows.

About the /LINKER param, it sounds ok. I will add it.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

Thanks you very much for your answers and Fred for his link :D
- Registered PB user -

Using PB 4.00
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Post by Max. »

newbie wrote:Thanks you very much for your answers and Fred for his link :D
Keep us updated on your progress please. :wink:
Athlon64 3800+ · 1 GB RAM · Radeon X800 XL · Win XP Prof/SP1+IE6.0/Firefox · PB 3.94/4.0
Intel Centrino 1.4 MHz · 1.5 GB RAM · Radeon 9000 Mobility · Win XP Prof/SP2+IE6.0/Firefox · PB 3.94/4.0
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

For now when i load my prog it use 100% of CPU and doesn't hook anything but i'm still investigating :wink:
- Registered PB user -

Using PB 4.00
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

newbie wrote:For now when i load my prog it use 100% of CPU and doesn't hook anything but i'm still investigating :wink:
Well, at least it must be doing something 8O :lol:
Paid up PB User !
Proteus
Enthusiast
Enthusiast
Posts: 113
Joined: Wed Sep 17, 2003 8:04 pm
Location: The Netherlands

Post by Proteus »

It's probably doing nothing and looping...

Putting Delay(1) into the loop should help.
P4 2.4GHz, 256 MB, WinXP Pro, onboard video&audio.
The Programmer's Drinking Song:
"99 little bugs in the code,
99 little bugs.
Fix one bug, recompile
100 little bugs in the code."
User avatar
p2hicy
User
User
Posts: 11
Joined: Fri Apr 25, 2003 5:31 pm
Location: Iceland
Contact:

Post by p2hicy »

Any (working) results yet ? :)
Post Reply