Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hook into application and process startup in windows?

Tags:

windows

hook

I am trying to write a program that will hook into application startup and capture the commandline. Don't have an idea where to start as I am pretty green in windows programming. Would appreciate any help thanks

like image 530
treefrog Avatar asked Nov 16 '11 19:11

treefrog


People also ask

How do I stop apps from opening on startup?

In either the search box or the Run dialog, type msconfig and press Enter. In the System Configuration window, click the Startup tab. The check boxes to the left of each program name indicate if it runs on startup. Once you've changed the selections, click the Apply button.

How do I stop applications from opening on startup Windows 10?

In the Windows search box, type startup apps (Windows 11) or startup tasks (Windows 10), and press Enter. The window that opens will contain a list of applications that may start when your device boots. To disable an app, toggle the switch to Off.


1 Answers

You didn't mention your prefered programming language, so I'll use C# for example snippets.

You can start a process and capture/write into its standard IO streams.

The following snippet, opens a process and captures its StdOut stream:

using (var process = Process.Start(new ProcessStartInfo(FileName = @"yourExecutablePath", UseShellExecute = false, RedirectStandardOutput = true)))
    using (var stdout = process.StandardOutput)
        Console.WriteLine(stdout.ReadToEnd());

EDIT 1

Looks like you want to hook Windows APIs like CreateProcess.

One way to do so is to write a kernel driver and use hooking techniques such as SSTD patching. But writing a kernel driver IMO is cumbersome.

In some cases you can use user-level hooks. There are a few libraries that might help you with that, including: EasyHook, Deviare, and MS Detour.


EDIT 2

You can also use WMI as @David Heffernan suggested but it will only notify you AFTER the process gets started (as opposed to hooking, which allows you to run some arbitrary code BEFORE the hooked function gets called and/or override the function call):

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()
like image 127
fardjad Avatar answered Sep 28 '22 06:09

fardjad