Skip to content

Commit

Permalink
Added new options (-newconsole, -wait).
Browse files Browse the repository at this point in the history
  • Loading branch information
lowleveldesign committed Jan 28, 2025
1 parent 03651d3 commit 70e665f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- uses: actions/setup-dotnet@main
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x

- name: Build detours
run: nmake
Expand Down
19 changes: 14 additions & 5 deletions withdll/DllInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@

namespace withdll;

sealed record ProcessStartupConfig(List<string> CmdlineArgs, bool Debug, bool NewConsole, bool WaitForExit);

static class DllInjection
{
public static void StartProcessWithDlls(List<string> cmdlineArgs, bool debug, List<string> dllPaths)
public static void StartProcessWithDlls(ProcessStartupConfig config, List<string> dllPaths)
{
unsafe
{
var startupInfo = new STARTUPINFOW() { cb = (uint)sizeof(STARTUPINFOW) };

var cmdline = new Span<char>(ConvertStringListToNullTerminatedArray(cmdlineArgs));
uint createFlags = debug ? (uint)PROCESS_CREATION_FLAGS.DEBUG_ONLY_THIS_PROCESS : 0;
var cmdline = new Span<char>(ConvertStringListToNullTerminatedArray(config.CmdlineArgs));
uint createFlags = (config.Debug ? (uint)PROCESS_CREATION_FLAGS.DEBUG_ONLY_THIS_PROCESS : 0) |
(config.NewConsole ? (uint)PROCESS_CREATION_FLAGS.CREATE_NEW_CONSOLE : 0);

var pcstrs = dllPaths.Select(p => new PCSTR((byte*)Marshal.StringToHGlobalAnsi(p))).ToArray();

Expand All @@ -35,12 +38,18 @@ public static void StartProcessWithDlls(List<string> cmdlineArgs, bool debug, Li
}

PInvokeWin32.CloseHandle(processInfo.hThread);
PInvokeWin32.CloseHandle(processInfo.hProcess);

if (debug)
if (config.Debug)
{
PInvokeWin32.DebugActiveProcessStop(processInfo.dwProcessId);
}

if (config.WaitForExit)
{
PInvokeWin32.WaitForSingleObject(processInfo.hProcess, PInvokeWin32.INFINITE);
}

PInvokeWin32.CloseHandle(processInfo.hProcess);
}
finally
{
Expand Down
1 change: 1 addition & 0 deletions withdll/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ DetourCreateProcessWithDllsW

// constants
MAX_PATH
INFINITE
11 changes: 8 additions & 3 deletions withdll/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics;
using withdll;

var parsedArgs = ParseArgs(["h", "help", "debug"], args);
var parsedArgs = ParseArgs(["h", "help", "debug", "newconsole", "wait"], args);
if (!parsedArgs.TryGetValue("", out var freeArgs))
{
freeArgs = [];
Expand All @@ -23,7 +23,9 @@
}
else
{
DllInjection.StartProcessWithDlls(freeArgs, parsedArgs.ContainsKey("debug"), dllPaths);
DllInjection.StartProcessWithDlls(
new(freeArgs, parsedArgs.ContainsKey("debug"), parsedArgs.ContainsKey("newconsole"), parsedArgs.ContainsKey("wait")),
dllPaths);
}
return 0;
}
Expand Down Expand Up @@ -100,10 +102,13 @@ withdll [options] <new process command line | process ID>
Options:
-h, -help Show this help screen
-d, --dll <path[@<ordinal|name>]> A DLL to inject into the target process (can be set multiple times)
-debug Start process with a debugger (useful when using IFEO) [new processes only]
-wait Wait for the started process to finish [new processes only]
-newconsole Launch process with a new command window [new processes only]
Examples:
withdll -d c:\temp\mydll1.dll -d c:\temp\mydll2.dll notepad.exe test.txt
withdll -d c:\temp\mydll1.dll -d c:\temp\mydll2.dll winver.exe
withdll -d c:\temp\mydll.dll@2 1234
withdll -d c:\temp\mydll.dll@ExportedFunc 1234
""");
Expand Down
6 changes: 3 additions & 3 deletions withdll/withdll.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net9.0-windows</TargetFramework>
<Copyright>2023 Sebastian Solnica</Copyright>
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="Detours.Win32Metadata" Version="4.0.1.16" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.162">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 70e665f

Please sign in to comment.