【问题标题】:Modifying command line arguments for GetCommandLine()修改 GetCommandLine() 的命令行参数
【发布时间】:2014-07-15 09:42:43
【问题描述】:

我正在尝试修改我的可执行文件的命令行参数,以便 GetCommandLine() 将返回我设置的字符串。由于我想在任何人之前修改命令行值,我已经通过 /ENTRY 开关将我的入口点更改为 testme() 函数,并设置 /NODEFAULTLIB 选项以排除 CRT。使用下面的代码,为什么我可以通过命令行更改字符串缓冲区指针,但不能分配一个全新的缓冲区?

代码:

#include <Windows.h>
#include <winternl.h>

typedef NTSTATUS (WINAPI *PFN_NtQueryInformationProcess)(
    IN HANDLE               ProcessHandle,
    IN PROCESSINFOCLASS       ProcessInformationClass,
    IN PVOID                ProcessInformation,
    IN ULONG                ProcessInformationLength,
    _Out_opt_  PULONG ReturnLength );

int testme()
{
   // Get PEB block address

   PROCESS_BASIC_INFORMATION pbi;
   ULONG result;

   PFN_NtQueryInformationProcess pfnQueryProcess = 
    (PFN_NtQueryInformationProcess) GetProcAddress(LoadLibrary("ntdll"),
        "NtQueryInformationProcess");

  pfnQueryProcess(GetCurrentProcessId(),
    ProcessBasicInformation, &pbi, sizeof(pbi), &result);

  // Modify ProcessParameters->CommandLine

  // This works
  pbi.PebBaseAddress->ProcessParameters->CommandLine.Buffer[0] = L'a';
  pbi.PebBaseAddress->ProcessParameters->CommandLine.Buffer[1] = L' ';
  pbi.PebBaseAddress->ProcessParameters->CommandLine.Buffer[2] = L'b';
  pbi.PebBaseAddress->ProcessParameters->CommandLine.Buffer[3] = L'\0';
  pbi.PebBaseAddress->ProcessParameters->CommandLine.Length = 6;

  // This does not work  

  UNICODE_STRING cmdLine;

  wchar_t wszNewCmdLine[] = L"x y\0";

  cmdLine.Buffer = (wchar_t*)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t)*pbi.PebBaseAddress->ProcessParameters->CommandLine.MaximumLength);
  cmdLine.MaximumLength = pbi.PebBaseAddress->ProcessParameters->CommandLine.MaximumLength;
  cmdLine.Length = sizeof(wszNewCmdLine) - sizeof(L'\0');

  //Copy buffer 
  for(int i=0; i<cmdLine.Length; ++i)
      cmdLine.Buffer[i] = wszNewCmdLine[i];

  pbi.PebBaseAddress->ProcessParameters->CommandLine.Buffer = cmdLine.Buffer;
  pbi.PebBaseAddress->ProcessParameters->CommandLine.Length = cmdLine.Length;
  pbi.PebBaseAddress->ProcessParameters->CommandLine.MaximumLength = cmdLine.MaximumLength;

  // Now testing, pCmdLine returned is "a b", not "x y".
  wchar_t *pCmdLine = GetCommandLine();

  return 0;
}

【问题讨论】:

  • NtQueryInformationProcess() 采用进程句柄,而不是 PID。

标签: c++ windows winapi


【解决方案1】:

很遗憾,GetCommandLineW 没有从 PEB 返回命令行。在BaseDllInitialize 例程中,复制了PEB 命令行结构,从那时起,GetCommandLineW 使用该副本。您需要在内存中找到此副本才能对其进行修改,这似乎非常困难并且也很危险/不可靠。

您可以查看像 Detours 这样的 API 挂钩,但更简单的解决方案可能是首先使用所需的命令行启动可执行文件。如果命令行正确,您可以测试它何时启动,如果不正确,则使用所需的命令行生成另一个自身的副本。

【讨论】:

  • 这也是我的结论,但你的答案是第一位的。谢谢。
【解决方案2】:

经过一番反复试验,我想出了以下几点。我写了一个 C 可执行文件,它只链接kernel32.lib,不链接CRT。在exe 中,我对kernel32.dll 中的GetCommandLineX 函数进行了EAT 修补。然后我加载另一个需要GetCommandLineX 方法作为其功能的一部分的dll(test.dll)。由于kernel32 已修补,加载程序用修补后的函数指针填充 test.dll 的导入表。最后,test.dll 中的方法调用了我的 GetCommandLineX 版本,我可以轻松地更改它们的实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2017-09-04
    相关资源
    最近更新 更多