ÔÆÌ⺣ - רҵÎÄÕ·¶ÀýÎĵµ×ÊÁÏ·ÖÏíÆ½Ì¨

µ±Ç°Î»ÖãºÊ×Ò³ > ÖÐÎÄ·­Òë-EasyHook- The reinvention of Windows API hooking - ͼÎÄ

ÖÐÎÄ·­Òë-EasyHook- The reinvention of Windows API hooking - ͼÎÄ

  • 62 ´ÎÔĶÁ
  • 3 ´ÎÏÂÔØ
  • 2025/5/6 13:31:40

2.1 Global Assembly Cache È«¾Ö³ÌÐò¼¯»º´æ

Currently EasyHook is expecting every injected assembly including all of its dependencies in the Global Assembly Cache (GAC). This is because the CLR will only search for assemblies in directories relative to the current application base directory and the GAC and therefore a target process normally has no access to EasyHook or your injected library. EasyHook is using a reference counter to make sure that multiple installations of the same assemblies from different applications can be managed. The following will register all EasyHook components and the two user assemblies required for injection in the GAC. The first parameter is just an uninterpreted string which should describe what your service is doing. All further parameters are expected to be relative/absolute file paths referring to all assemblies that should be temporarily registered in GAC. Please note that only strongly named assemblies are accepted.

µ±Ç° EasyHookÊÇ»³×Åÿ¸ö×¢Èë³ÌÐò¼¯°üÀ¨ËùÓеÄ×é¼þ¶¼ÔÚÈ«¾Ö³ÌÐò¼¯»º´æÖÐ(GAC).ÕâÊÇÒòΪCLR½«Ö»ÔÚÓ뵱ǰӦÓÃÖ÷Ŀ¼Ïà¹ØµÄµ±Ç°Ä¿Â¼³ÌÐò¼¯ºÍGACÖÐËÑË÷³ÌÐò¼¯,¶øGACÒò´ËÒ»¸öÄ¿±ê½ø³Ìͨ³£²»ÄÜÔÚÄã×¢Èë¿âÖзÃÎÊEasyHook.EasyHookÕýʹÓÃÒýÓüÆÊýÈ·¶¨Í¬Ò»³ÌÐò¼¯²»Í¬µÄÓ¦ÓÃÄܱ»¹ÜÀíºÍ°²×°¡£ÒÔϽ«×¢²áËùÓеÄEasyHook×é¼þºÍÁ½¸öËùÐè×¢ÈëµÄÓû§³ÌÐò¼¯µ½GACÖС£Ê×ÏÈÔÝʱÔÚGACÖÐ×¢²á¡£Çë×¢Ò⣬ֻÓÐÇ¿ÃüÃûÓ¦ÓòſÉÒÔ¡£ Config.Register( \Îļþ·ÃÎʼàÊÓÀàÑÝʾ³ÌÐò.\, \,

\);

It is guaranteed that your libraries will be removed from GAC if the injecting process is being terminated in all common cases. Of course there are some rare exceptions, for example if you shutdown your PC by disconnecting the power cable. In such a case the assemblies will remain in the GAC, forever, which is no bad thing in end-user scenarios but truly during development. You may use the Gacutil.exe that ships with Visual Studio to remove all temporary GAC assemblies.

µ±×¢Èë½ø³ÌÔÚËùÓÐÆÕͨÇé¿öÏÂÖÕֹʱ±£Ö¤ÄãµÄ¿â»á´ÓGACÖÐÒÆ³ý¡£µ±È»»áÓкÜÉÙµÄÒì³£Çé¿ö£¬ÀýÈ磬ֱ½ÓÍ£µçµ¼ÖÂÄãµÄµçÄԹرա£Õâʱ³ÌÐò¼¯»¹»áÔÚGACÖУ¬ÕâÔÚ×îÖÕÓû§µÄÇé¿öÏÂÒ²²»ÔõôÔ㣬µ«ÔÚ¿ª·¢Ê±²»´óºÃ¡£Äã¿ÉÒÔʹÓÃVisual StuidoµÄGacutil.exe¹¤¾ßж³ýËùÓÐÁÙʱµÄGAC³ÌÐò¼¯¡£

Open the ¡°Visual Studio Command Promt¡± as administrator. ÒÔ¹ÜÀíÔ±Éí·Ý´ò¿ª¡°Visual Studio ÃüÁîÐй¤¾ß¡±

1. Run the command: gacutil /uf EasyHook

ÔËÐÐÃüÁî:gacutil /uf EasyHook

2. Run additional commands for each of your assemblies that should be

removed from the GAC¡­

ÔËÐÐÆäËüÐèÒªÒÆÐì³öGAC³ÌÐò¼¯µÄÃüÁî¡£

2.2 Windows Defender

Windows·À»ðǽ

Injection will sometimes make the Windows Defender complain. This does not only apply to EasyHook, but to every library using remote thread creation for injection: ×¢ÈëÓÐʱ»áµ¼ÖÁWindows·À»ðǽµÄ¾¯¸æ¡£Õâ²»½ö½öÊǶÔEasyHook£¬¼¸ºõʹÓÃÔ¶³ÌÏß³Ì×¢ÈëµÄËùÓпⶼ»áÕâÑù£º

Windows Defender Real-Time Protection agent has detected changes. Microsoft recommends you analyze the software that made these changes for potential risks. You can use information about how these programs operate to choose whether to allow them to run or remove them from your computer. Allow changes only if you trust the program or the software publisher. Windows Defender can't undo changes that you allow. For more information please see the following:

Not Applicable

Scan ID: {44726E79-4262-454E-AFED-51A30D34BF67} User: Lynn-PC\\Lynn Name: Unknown ID:

Severity ID: Category ID: Path Found:

process:pid:864;service:EasyHook64Svc;file:D:\\Projects\\EasyHook 2.0.0.0\\Debug\\x64\\EasyHook64Svc.exe Alert Type: Unclassified software Detection Type:

Such warnings are immediately followed by information pointing out that Windows Defender has prevented a malicious attempt. I think this will vanish if you sign all executable binaries of EasyHook with AuthentiCode. Such blocking only occurs when injecting into essential system services.

2.3 Injection ¨C A burden made easy 2.3×¢Èë-¸ºµ£±äµÃ¼òµ¥ÁË

In general, library injection is one of the most complicated parts of any hooking library. But EasyHook goes further. It provides three layers of injection abstraction and your library is the fourth one. The first layer is pure, relocatable assembler code. It launches the second layer, an unmanaged C++ method. The assembler code itself is really stable. It provides extensive error information and is able to unload itself without leaving any resource leaks in the target. The C++ layer starts the managed injection loader and adjusts the target¡¯s

PATH variable by adding the injecting process¡¯ application base directory as first entry. This way you will have access to any file you would also have access to from

your injecting process. The managed injection loader uses NET Reflection and NET Remoting to provide extensive error reports in case of failure and to find a proper entry point in your injection library. It also cares about graceful hook removal and resource cleanup. It is supported to load the same library multiple times into the same target!

Ò»°ãÀ´Ëµ£¬¿â×¢ÈëʱËùÓйҽӿâÖÐ×ÔӵIJ¿·Ö¡£µ«ÊÇEasyHookÈÃËû²»Ò»ÑùÁË¡£ËüÌṩÁË×¢Èë³éÏñµÄÈý¸ö²ã¶øÄãµÄ¿âÔÚµÚËĸö¡£µÚÒ»²ãÊÇ´¿µÄ»ã±à´úÂë¡£ËüÔËÐеڶþ²ã£¬·ÇÍйܵÄC++·½·¨¡£»ã±à´úÂë±¾ÉíÏ൱Îȶ¨¡£ËüÌṩºÜ¶àµÄ´íÎóÐÅÏ¢²¢Äܹ»½«×Ô¼ºÔÚÄ¿±êÖÐж³ý²»»áÁôÏÂ×ÊԴй¶¡£C++²ã¿ªÊ¼¹ÜÀí×¢Èë×°ÔØÆ÷²¢µ÷ÕûÄ¿±ê·¾¶±äÁ¿£¬Í¨¹ýÌí¼Ó½ø³ÌÓ¦ÓÃÖ÷Ŀ¼×÷ΪµÚÒ»¸öÈë¿Ú¡£ÕâÑùÄã¿ÉÄÜ·ÃÎÊÈκÎÄã±»×¢Èë½ø³ÌÄܹ»·ÃÎʵÄÈκÎÎļþ¡£ÍйܵÄ×¢Èë×°ÔØÆ÷ʹÓÃNET·´ÉäºÍNet Remoting½ø¹©ºÜ¶àµÄÔÚ³ö´íʱµÄ´íÎ󱨸沢·¢ÏÖÔÚÄã×¢Èë¿âÖеÄÕýÈ·µÄÈë¿Úµã¡£Ëü»¹¹Ø×¢¹Ò¹³µÄÒÆ³ýºÍ×ÊÔ´µÄÇå³ý¡£ËüÖ§³Ö½«Í¬Ò»¸ö¿â¶à´ÎµÄ×°È뵽ͬһĿ±êÖУ¡

Another complex part is run on host side. It is supported to inject libraries into other terminal sessions, system services and even through WOW64 boundaries. To you, all cases seem the same. EasyHook will automatically select the right injection procedure. If EasyHook has succeeded injection, you can be 99% sure that your library has been successfully loaded and executed. If it fails you can be 99% sure that no resource leaks are left in the target and it remains in a stable, hookable state! Nearly all possible failures are being caught and it would be like a lottery win to see a target getting crashed by library injection!

ÁíÒ»¸ö¸´ÔÓµÄÁÆ·ÖʱÔÚÍйÜÕâ±ßÔËÐС£ËüÖ§³Ö½«¿â×¢Èëµ½ÆäËüÖն˻Ự£¬ÏµÍ³·þÎñÉõÖÁWOW64±ß½ç¡£¶ÔÄãÀ´Ëµ£¬ËùÒÔÓÐÇé¿ö¶¼ÊÇÒ»Ñù¡£EasyHook¿ÉÒÔ×Ô¶¯µÄÑ¡ÔñÕýÈ·µÄ×¢Èë½ø³Ì¡£Èç¹ûEasyHook³É¹¦ ×¢È룬Äã¿É99%È·¶¨ÄãµÄ¿â³É¹¦×°Ôز¢ÔËÐС£Èç¹ûʧ°Ü£¬Ôò99%È·±£Òþ¶¨ºÍ²»Ð¹Â¶¡£¼¸ºõËùÓпÉÄܵĴíÎó¶¼Äܱ»²¶×½µ½£¬Èç¹û×¢Èëʧ°ÜÄÇʱÄúÃþ²ÊÖн±ÁË¡£

Please note that Windows Vista has advanced security for its subsystem services. They are running in a protected environment like the ¡°Protected Media Path¡±. It is not possible to hook such services with EasyHook or any other user-mode library. The following shows the API method that we are talking about: RemoteHooking.Inject( Int32.Parse(args[0]),

\, // 32-Bit version \, // 64-Bit version ChannelName);

The first four parameters are required. If you only want to hook either 32- or 64-Bit targets, you can set the unused path to null. You may either specify a file path that EasyHook will automatically translate to a full qualified assembly name or a partial assembly name like ¡°FileMonInject, PublicKeyToken = 3287453648abcdef¡±. Currently there is only one injection option preventing EasyHook from attaching a debugger to the target but you should only set this option if the target does not like an attached debugger. EasyHook will detach it before injection is completed so in general there is nothing to worry about and it increases injection stability about magnitudes by using the target symbol addresses instead of assuming that the local

ones remain valid in the target!

You can pass as many additional parameters as you like but be aware of that you shall only pass types that are accessible through GAC, otherwise the injected library is not able to deserialize the parameter list. In such a case the exception will be redirected to the host process and you may catch it with a try-catch statement around RemoteHooking.Inject. That¡¯s one of the great advantages!

The injected library will automatically get access to all additional parameters you specify after the fourth one. This way you can easily pass channel names to the target so that your injected library is able to connect to your host. Attention

Keep in mind that the CLR will unload your library only if the target is being terminated. Even if EasyHook releases all associated resources much earlier, you won¡¯t be able to change the injected DLL which implies that the corresponding GAC library is not updateable until the target is terminated. So if you need to change your injected library very frequently (during development) you should always terminate the target after each debugging session. This will ensure that no application depends on the library and it can be removed from the GAC.

2.3.1 Creating an already hooked process 2.3.1´´½¨Ò»¸öÒѾ­´æÔڵĹҹ³½ø³Ì

Sometimes it is necessary to hook a process from the beginning. This is no big deal, just call

RemoteHooking.CreateAndInject instead of Inject. This will execute your library main method before any other instruction. You can resume the newly created process by calling RemoteHooking.WakeUpProcess from your injected library Run method. This only makes sense in conjunction with CreateAndInject, otherwise it will do nothing.

ÓÐʱÐèÒªÔÚ½ø³Ì¿ªÊ¼Ê±¹Ò½Ó¡£ÕâûÓÐʲºÍ´ó²»ÁË¡£²»ÓÃ×¢Èëµ÷ÓÃ

RemoteHooking.CreateAndInject ¼´¿É¡£Õâ»áÔÚÆäËüÖ¸ÁîǰִÐÐÄãµÄ¿âÖ÷·½·¨¡£Äã¿É¼ÌÐøÍ¨¹ýµ÷Óà RemoteHooking.WakeUpProcess ×îд´½¨µÄ½ø³Ì´ÓÄã×¢Èë¿âÔËÐз½·¨ÖС£ÕâÖ»ÔÚCreatAndJnject Á¬½ÓʱÓÐÒâÒ壬²»È»Ã»Ê²Ã´Óᣠ2.4 The injected library entry point ×¢Èë¿âÈë¿Úµã

All injected libraries have to export at least one public class implementing the EasyHook.IEntryPoint interface. The interface itself is empty but identifies your class as entry point. A class marked as entry point this way, is expected to export an instance constructor and a Run instance method having the signature ¡°void Run(IContext, %ArgumentList%)¡± and ¡°.ctor(IContext, %ArgumentList%)¡±. Please note that ¡°%ArgumentList%¡± is a placeholder for additional parameters

ËÑË÷¸ü¶à¹ØÓÚ£º ÖÐÎÄ·­Òë-EasyHook- The reinvention µÄÎĵµ
  • ÊÕ²Ø
  • Î¥¹æ¾Ù±¨
  • °æÈ¨ÈÏÁì
ÏÂÔØÎĵµ10.00 Ôª ¼ÓÈëVIPÃâ·ÑÏÂÔØ
ÍÆ¼öÏÂÔØ
±¾ÎÄ×÷Õߣº...

¹²·ÖÏí92ƪÏà¹ØÎĵµ

Îĵµ¼ò½é£º

2.1 Global Assembly Cache È«¾Ö³ÌÐò¼¯»º´æ Currently EasyHook is expecting every injected assembly including all of its dependencies in the Global Assembly Cache (GAC). This is because the CLR will only search for assemblies in directories relative to the current application base directory and the GAC and therefore a target process normally has no access to EasyHook or your injected library. Eas

¡Á ÓοͿì½ÝÏÂÔØÍ¨µÀ£¨ÏÂÔØºó¿ÉÒÔ×ÔÓɸ´ÖƺÍÅŰ棩
µ¥Æª¸¶·ÑÏÂÔØ
ÏÞÊ±ÌØ¼Û£º10 Ôª/·Ý Ô­¼Û:20Ôª
VIP°üÔÂÏÂÔØ
ÌØ¼Û£º29 Ôª/Ô ԭ¼Û:99Ôª
µÍÖÁ 0.3 Ôª/·Ý ÿÔÂÏÂÔØ150·Ý
ȫվÄÚÈÝÃâ·Ñ×ÔÓɸ´ÖÆ
VIP°üÔÂÏÂÔØ
ÌØ¼Û£º29 Ôª/Ô ԭ¼Û:99Ôª
µÍÖÁ 0.3 Ôª/·Ý ÿÔÂÏÂÔØ150·Ý
ȫվÄÚÈÝÃâ·Ñ×ÔÓɸ´ÖÆ
×¢£ºÏÂÔØÎĵµÓпÉÄÜ¡°Ö»ÓÐĿ¼»òÕßÄÚÈݲ»È«¡±µÈÇé¿ö£¬ÇëÏÂÔØÖ®Ç°×¢Òâ±æ±ð£¬Èç¹ûÄúÒѸ¶·ÑÇÒÎÞ·¨ÏÂÔØ»òÄÚÈÝÓÐÎÊÌ⣬ÇëÁªÏµÎÒÃÇЭÖúÄã´¦Àí¡£
΢ÐÅ£ºfanwen365 QQ£º370150219
Copyright © ÔÆÌ⺣ All Rights Reserved. ËÕICP±¸16052595ºÅ-3 ÍøÕ¾µØÍ¼ ¿Í·þQQ£º370150219 ÓÊÏ䣺370150219@qq.com