且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取CMD.EXE的当前工作目录

更新时间:2023-01-27 15:14:33

未经检验的,一个可能的方法:

Untested, a possible approach:

创建与使用GetThreadStartInformation()找到缓冲区的地址,然后使用GetCurrentDirectory来填充它的DllMain的DLL。这应该是确定的,因为这两方面的功能都在kernel32中,这始终是present。你需要有一定的结构有返回成功/失败。

Create a DLL with a DllMain that uses GetThreadStartInformation() to find the address of the buffer, and then uses GetCurrentDirectory to populate it. This should be OK, because both of those functions are in kernel32, which is always present. You will need to have some structure there to return success/failure.

  1. 在得到一个处理cmd.exe进程。
  2. 分配一些内存出现(VirtualAllocEx的)
  3. 把路径在内存中的DLL。 (WriteProcessMemory的)
  4. 加载您的DLL到cmd.exe的地址空间。 (CreateRemoteThread的与调用LoadLibrary的入口点,参数是先前分配的内存。)
  5. 在WaitForSingleObject的后跟GetExit codeThread(),为您提供了cmd.exe进程DLL的HMODULE。
  6. ReadProcessMemory来获取当前目录。
  7. 从cmd.exe的地址空间中卸载您的DLL。 CreateRemote螺纹与FreeLibrary则的入口点,该参数是HMODULE。
  8. WaitForSingleObject的等待DLL来卸载。

广泛的草图:细节保留为锻炼; Tibial!风险:分配内存的CMD.EXE的地址空间,改变其状态。必须小心以称为DllMain中的功能。

Broad sketch: Details left as an excercise! Risks: Allocates memory in the cmd.exe address space, changes its state. Care must be taken with the functions called in DllMain.