包装可执行文件以诊断其调用

时间:2020-03-06 14:32:00  来源:igfitidea点击:

我有一个Windows可执行文件(whoami),经常崩溃。从另一个过程中调用它以获取有关当前用户和域的详细信息。我想知道失败时会传递什么参数。

有谁知道适当的方法来包装过程并在仍然调用过程的同时将其写为命令行参数以进行记录?

说命令是这样使用的:
'whoami.exe /全部'

我希望有一个脚本而不是whoami.exe(具有相同的文件名),它将将该调用写入日志,然后将调用传递给实际进程。

解决方案

我们没有注意到哪种编程语言。如果那是我们想要的,则无法从.bat文件执行此操作,但是我们可以使用任何编程语言来执行此操作。在C中的示例:

int main(int argc, void **argv)
{
    // dump contents of argv to some log file
    int i=0;
    for (i=0; i<argc; i++)
        printf("Argument #%d: %s\n", argv[i]);
    // run the 'real' program, giving it the rest of argv vector (1+)
    // for example spawn, exec or system() functions can do it
    return 0; // or you can do a blocking call, and pick the return value from the program
}

查找whoami.exe,将其备份,用我们自己的可执行文件替换它,并查看使用其参数执行的操作(也许将它们保存在文本文件中)。

我不认为使用"脚本"会起作用,因为中间件应具有.exe扩展名,以使工作方式有效。

我将编写一个非常小的命令行程序来执行此操作;类似于以下内容(以Delphi / Virtual Pascal编写,因此将产生Win32可执行文件,但任何编译语言都可以):

program PassThrough;

uses
  Dos; // Imports the Exec routine

const
  PassTo = 'Original.exe'; // The program you really want to call

var 
  CommandLine: String;
  i: Integer;
  f: Text;

begin
  CommandLine := '';
  for i := 1 to ParamCount do
    CommandLine := CommandLine + ParamStr(i) + ' ';

  Assign(f,'Passthrough.log');
  Append(f);
  Writeln(f, CommandLine);      // Write a line in the log
  Close(f);

  Exec(PassTo, CommandLine);    // Run the intended program
end.

我们是否不能仅更改调用程序以记录其用于调用流程的参数以及退出代码?
这比尝试深入到whoami.exe更容易

从批处理文件中:

echo Parameters: %* >> logfile.txt
whoami.exe %*

需要注意的是,如果参数包含空格(并且我们使用"进行转义"传递了空格),则可能会遇到问题,因为命令行解析器基本上会转义它们,因此在传递给其他可执行文件之前应重新转义它们。

如果可以重现崩溃,请在崩溃进程终止之前使用"进程资源管理器"查看其命令行。

http://technet.microsoft.com/zh-cn/sysinternals/bb896653.aspx