我正在尝试编写一个程序,它将挂钩到应用程序启动并捕获命令行.不知道从哪里开始,因为我在
Windows编程中非常环保.
非常感谢任何帮助
谢谢
你没有提到你喜欢的编程语言,所以我将使用C#作为代码片段.
您可以启动进程并捕获/写入其标准IO流.
以下代码段打开一个进程并捕获其StdOut流:
using (var process = Process.Start(new ProcessStartInfo(FileName = @"yourExecutablePath",UseShellExecute = false,RedirectStandardOutput = true))) using (var stdout = process.StandardOutput) Console.WriteLine(stdout.ReadToEnd());
编辑1
看起来你想挂钩像CreateProcess这样的Windows API.
一种方法是编写内核驱动程序并使用诸如SSTD修补之类的挂钩技术.但编写内核驱动程序IMO非常麻烦.
在某些情况下,您可以使用用户级挂钩.有一些图书馆可以帮助您,包括:EasyHook,Deviare和MS Detour.
编辑2
您也可以使用WMI作为@David Heffernan
建议但它只会在进程启动后通知您(而不是挂钩,这允许您在挂钩函数被调用和/或覆盖函数调用之前运行一些任意代码):
using System.Management; // Run this in another thread and make sure the event watcher gets disposed before exit var start = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace")); start.EventArrived += new EventArrivedEventHandler(delegate (object sender,EventArrivedEventArgs e) { console.WriteLine("Name: {0},Command Line: {1}",e.NewEvent.Properties["ProcessName"].Value,e.NewEvent.Properties["Commandline"].Value); }); start.Start()