简体   繁体   English

如何使用deviare进行系统调用计数

[英]How to make system calls counter using deviare

I would like to hook all the functions calls of all running processes. 我想挂钩所有正在运行的进程的所有函数调用。 I can hook certain function("ws2_32.dll!recv") of all processes using deviare by: 我可以使用deviare钩住所有进程的某些函数(“ ws2_32.dll!recv”),方法是:

        CreateSpyMgr(out mgr);
        hook = mgr.CreateHook("ws2_32.dll!recv");
        hook.Attach(mgr.get_Processes(0));
        mgr.set_ReportProcessCreation(DeviareCommonLib.ReportMethod._create_process_hook_and_polling, 0);
        hook.set_HookNewProcesses(0, 1);
        hook.OnFunctionCalled += new DHookEvents_OnFunctionCalledEventHandler(hook_OnFunctionCalled);
        hook.Hook();

How can I hook all function calls instead of just one? 如何钩住所有函数调用而不是仅钩住一个? is it possible? 可能吗?

Or should I create hooks collection(of all functions which is way hard) using INktSpyMgr::CreateHooksCollection and add hooks to it, then call hook method and pass the INktHooksEnum object as the parameter. 还是我应该使用INktSpyMgr :: CreateHooksCollection创建(非常困难的所有函数)钩子集合,并向其添加钩子,然后调用钩子方法并传递INktHooksEnum对象作为参数。 Is this the only way to do this? 这是唯一的方法吗?

My aim is to make a tool that counts all system calls for each running process. 我的目标是制作一个工具,该工具可以计算每个运行进程的所有系统调用。 Feel free to give any suggestions. 随时提出任何建议。

First a word of advice: be very very careful about which APIs you hook. 首先要提一个建议:请非常谨慎地钩上哪些API。 If anything you do within your hook method results in a call to one of the APIs you are hooking then you are creating an infinite recursion that could potentially wreck your computer. 如果您在hook方法中执行的任何操作都导致对您所钩连的API的调用,那么您正在创建无限递归,这可能会破坏您的计算机。 Bear that in mind. 记住这一点。 You'll probably want to filter out the API calls for your own process as well, otherwise you'll end up logging entries about the disk access caused by logging entries, and before you know it your memory is full and the hard drive is fully occupied with logging about logging. 您可能还希望过滤出自己的进程的API调用,否则最终将记录关于由日志记录条目引起的磁盘访问的日志记录条目,并且在不知道其内存已满且硬盘驱动器已满的情况下忙于记录日志。

There appears to be nothing in the Deviare API that allows you to create hooks on multiple methods - no wildcards or 'hook everything' calls - so you'll have to enumerate the APIs (see INktModule.ExportedFunctions for some ideas) and hook them. Deviare API中似乎没有任何东西可以让您在多个方法上创建钩子-无需通配符或“钩住所有内容”调用-因此您必须枚举API(请参阅INktModule.ExportedFunctions以获得一些想法)并对其进行钩子。 I'd suggest that you use a hook collection (see INktSpyMgr.CreateHookCollection and INktHooksEnum ) so that you can setup all your hooks and then attach and detach them in one operation. 我建议您使用一个钩子集合(请参阅INktSpyMgr.CreateHookCollectionINktHooksEnum ),以便您可以设置所有钩子,然后在一个操作中附加和分离它们。

As for the logging aspect, give some thought to using a queue of some sort - ConcurrentQueue<T> by preference - to pass the actual logging operations off to another thread. 至于日志记录方面,请考虑使用某种队列-通过优先选择ConcurrentQueue<T> -将实际的日志记录操作传递给另一个线程。 That way you spend a minimum of time in the actual hook function as well as reducing the chances of your hooks causing recursion. 这样,您就可以在实际的钩子函数上花费最少的时间,并减少钩子导致递归的机会。 You'll have to experiment with filtering in the logging thread vs the hook functions to find out which has the smaller performance impact on the system. 您将不得不在日志记录线程中对钩子函数进行试验,以找出对系统性能影响较小的过滤器。

Always make sure you know how much data your program is dealing with and have a plan in place for dealing with the volume of data. 始终确保知道您的程序正在处理多少数据,并制定了处理数据量的计划。 You're going to have to do some serious profiling to find the pain points, then put in plenty of work on reducing the overheads so that your program doesn't mess up the system too badly. 您将必须进行一些认真的分析才能找到要害所在,然后进行大量工作来减少开销,以使您的程序不会严重破坏系统。

Personally I'd start with a small subset of the APIs you ultimately want to monitor, write code that works as well as you can make it, then move up to the full set of APIs. 就我个人而言,我将从一小部分您最终希望监视的API开始,编写可以正常运行的代码,然后逐步使用完整的API集。 Less chance that you'll kill your computer that way. 这样一来,您杀死计算机的机会就更少了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM