如何在 Windows 上生成进程并查看它使用哪些文件?

发布于 2024-09-17 23:06:57 字数 438 浏览 0 评论 0原文

我想在 Microsoft Windows 上编写一个 C++ 函数,它生成一个进程,除了进程的终止状态之外,还返回该进程读取或写入的所有文件的列表。它不应该需要衍生应用程序的任何合作。

例如,如果生成的程序是 Visual Studio C++ 编译器,则该函数将生成一个列表,其中包含编译器打开的源文件、它读取的所有头文件以及它创建的 .OBJ 文件。如果它还包含程序包含的诸如 .DLL 文件之类的内容,那就没问题了。但同样,无论生成什么程序,它都应该起作用;编译器只是一个例子。

一个转变:如果该进程创建子进程,我还需要监视它们的文件访问。

第二个转折:如果进程尝试打开一个文件,我希望能够让它等到我可以创建该文件,然后才让它恢复并打开该文件。 (我认为这排除了 ETW。)

我知道这听起来可能像是一些可怕的拼凑的成分。但如果我能做到这一点,最终的结果将会非常酷。

I would like to write a C++ function, on Microsoft Windows, that spawns a process and returns, in addition to the process's termination status, a list of all the files the process read or wrote. It should not require any cooperation from the spawned application.

For example, if the program spawned is the Visual Studio C++ compiler, the function would produce a list containing the source file the compiler opened, all header files it read, and the .OBJ file it created. If it also contained things like .DLL files the program contained, that would be fine. But again, it should work regardless of the program spawned; the compiler is just an example.

A twist: if the process creates subprocesses, I need to monitor their file accesses as well.

A second twist: if the process tries to open a file, I would like to be able to make it wait until I can create that file—and only then let it resume and open the file. (I think this rules out ETW.)

I know this probably sounds like an ingredient for some horrible kludge. But if I can get this working, the end result will be really cool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2024-09-24 23:06:57

第二个转折:如果进程尝试打开文件,我希望能够让它等到我可以创建该文件,然后才让它恢复并打开文件

您只需将自己放入 Hack City 并满足该要求- 你是对的,ETW 会是一个更容易的解决方案,但它也无法阻止文件调用。

基本上,您需要执行以下操作:

  1. 创建挂起的进程
  2. 创建两个相反方向的命名管道,其名称众所周知(可能包含进程的 PID)
  3. 挂钩 LoadModule,该挂钩将监视 Kernel32 以获取加载
  4. 当 Kernel32 加载时,挂钩 CreateFileW 和 CreateFileA - 还挂钩 CreateProcessEx 和 ShellExecute
  5. 当 CreateFile 挂钩命中时,您将名称写入其中一个命名管道,然后在另一个命名管道上执行 ReadFile,直到父进程通知您继续。
  6. 当您的 CreateProcessEx 挂钩命中时,您可以从当前进程内部再次执行相同的进程(请记住,您不能让父进程执行 CreateProcess'ing,因为它会弄乱继承的句柄)。
  7. 启动子进程。

请记住,您将注入代码并对内存中的图像进行修复,该图像可能与您的位数不同(即您的应用程序是 64 位,但它正在启动 32 位进程),因此您将必须有 x86 和 amd64 版本的 shim 代码才能注入。我希望通过写这篇冗长的谩骂,您可以说服自己,这实际上是一个糟糕的想法,很难正确实现,并且挂钩 Win32 函数的人会让 Windows 操作系统开发人员感到悲伤。

A second twist: if the process tries to open a file, I would like to be able to make it wait until I can create that file—and only then let it resume and open the file

You just put yourself into Hack City with that requirement - you're right that ETW would've been a far easier solution, but it also has no way to block the file call.

Basically, here's what you're going to have to do:

  1. Create the process suspended
  2. Create two named pipes in opposite directions whose names are well known (perhaps containing the PID of the process)
  3. Hook LoadModule, and the hook will watch for Kernel32 to get loaded
  4. When Kernel32 gets loaded, hook CreateFileW and CreateFileA - also hook CreateProcessEx and ShellExecute
  5. When your CreateFile hook hits, you write the name to one of the named pipes, then execute a ReadFile on the other one until the parent process signals you to continue.
  6. When your CreateProcessEx hook hits, you get to do the same process all over again from inside the current process (remember that you can't have the parent process do the CreateProcess'ing because it'll mess up inherited handles).
  7. Start the child process.

Keep in mind that you'll be injecting code and making fixups to an in-memory image that may be a different bitness than yours (i.e. your app is 64-bit, but it's starting a 32-bit process), so you'll have to have both x86 and amd64 versions of your shim code to inject. I hope by writing this lengthy diatribe you have convinced yourself that this is actually an awful idea that is very difficult to get right and that people who hook Win32 functions make Windows OS developers sad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文