Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Brovan/Core/Emulation/BinaryEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ public struct BinaryEmulatorSettings
/// </summary>
public string RawProgramArguments;

public string WorkingDirectory;

/// <summary>
/// Parsed arguments passed to the emulated process, excluding argv[0].
/// </summary>
Expand Down Expand Up @@ -370,6 +372,7 @@ public int Compare(MemoryRegion x, MemoryRegion y)
public bool Debug { get; set; }

public string RawProgramArguments { get; }
public string WorkingDirectory { get; }
public string[] ProgramArguments { get; }

public int IPRegister { get; private set; }
Expand Down Expand Up @@ -528,6 +531,7 @@ public BinaryEmulator(BinaryFile Binary, BinaryEmulatorSettings Settings)
this.Settings = Settings;
Debug = Settings.Debug;
RawProgramArguments = Settings.RawProgramArguments ?? string.Empty;
WorkingDirectory = Settings.WorkingDirectory;
ProgramArguments = Settings.ProgramArguments?.ToArray() ?? Array.Empty<string>();

if (_binary.Architecture == BinaryArchitecture.x64)
Expand Down Expand Up @@ -567,6 +571,7 @@ public BinaryEmulator(IGuestEnvironment Guest, BinaryEmulatorSettings Settings,
this.Settings = Settings;
Debug = Settings.Debug;
RawProgramArguments = Settings.RawProgramArguments ?? string.Empty;
WorkingDirectory = Settings.WorkingDirectory;
ProgramArguments = Settings.ProgramArguments?.ToArray() ?? Array.Empty<string>();

if (Guest is GenericGuest Generic)
Expand Down Expand Up @@ -2396,6 +2401,9 @@ public bool RunMlfqScheduler(uint BaseQuantumInstructions = 200000, int Levels =
{
SchedulerTick++;

if (WinHelper != null)
OS.Windows.RemoteProcessRequests.Drain(this);

bool ThreadOrderChanged = ThreadOrder.Count != KnownThreadOrderCount;
bool AgingDue = AgingThresholdSlices > 0 && SchedulerTick % AgingThresholdSlices == 0;
if (AgingDue)
Expand Down
44 changes: 38 additions & 6 deletions Brovan/Core/Emulation/Guests/WindowsGuest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using System.Text;
using Brovan.Core.Emulation.OS.Windows;
using Brovan.Core.Emulation.OS.Windows.Win32k;
using Brovan.Core.Helpers;
using static Brovan.Core.Emulation.OS.Windows.WinSysHelper;
using static Brovan.Core.Helpers.BinaryHelpers;
Expand Down Expand Up @@ -829,6 +830,7 @@ public ulong AllocateAndInitializeTEB(BinaryEmulator Instance, EmulatedThread Th
Instance._emulator.WriteMemory(Teb + Wow64InfoOffset + 0x0C, 0u);
Instance._emulator.WriteMemory(Teb + Wow64TlsWow64InfoOffset, (uint)(Teb + Wow64InfoOffset));

Win32kDpi.ApplyThreadContext(Instance, Teb);
return Teb;
}

Expand Down Expand Up @@ -859,6 +861,8 @@ public ulong AllocateAndInitializeTEB(BinaryEmulator Instance, EmulatedThread Th
SameTebFlags |= TEB_SAME_TEB_FLAG_SKIP_LOADER_INIT;
Instance._emulator.WriteMemory(Teb + TebSameTebFlagsOffset64, SameTebFlags, 2);
Instance._emulator.WriteMemory(Teb + 0x180C, (uint)0u);

Win32kDpi.ApplyThreadContext(Instance, Teb);
return Teb;
}

Expand Down Expand Up @@ -1157,6 +1161,36 @@ private void EnsureDirectBlobStandardHandles()
}
}

private static void JoinGuestSession(BinaryEmulator Instance, WinModule MainModule)
{
string ImageName = MainModule?.Name;
if (string.IsNullOrEmpty(ImageName))
ImageName = Path.GetFileName(Instance._binary?.Location ?? string.Empty);

GuestSessionRegistry.Join(
Instance.WinHelper.PID,
(uint)Instance._binary.Architecture,
ImageName,
ExitCode =>
{
Instance.TriggerEventMessage($"[!] Another process in the session asked this one to stop with exit code 0x{ExitCode:X}.", LogFlags.Important);
Instance.WinHelper.HideDesktopWindow();
Instance.StopEmulation();
});
}

private static string ResolveStartupDirectory(BinaryEmulator Instance, string ImagePath)
{
string Directory = Instance.WorkingDirectory;
if (string.IsNullOrWhiteSpace(Directory))
Directory = Path.GetDirectoryName(ImagePath);

if (string.IsNullOrWhiteSpace(Directory))
Directory = "C:\\";

return Directory.EndsWith("\\", StringComparison.Ordinal) ? Directory : Directory + "\\";
}

public void PrepareWinEnvironment(BinaryEmulator Instance, WinModule MainModule)
{
Instance.EnsureInstructionHook();
Expand All @@ -1171,6 +1205,8 @@ public void PrepareWinEnvironment(BinaryEmulator Instance, WinModule MainModule)
EnsureDirectBlobStandardHandles();

WinSyscallTable = HelperFunctions.BuildWinSyscallTable(Instance._binary.Architecture);
Win32kDpi.SeedFromImage(Instance, MainModule);
JoinGuestSession(Instance, MainModule);

ulong PageSize = 0x2000;
PEB = Instance.MapUniqueAddress(PageSize, MemoryProtection.ReadWrite);
Expand Down Expand Up @@ -1215,9 +1251,7 @@ public void PrepareWinEnvironment(BinaryEmulator Instance, WinModule MainModule)
string ImagePath = IsPeImage
? Instance._binary.Location
: (!string.IsNullOrWhiteSpace(Instance._binary.Location) ? Instance._binary.Location : (!string.IsNullOrWhiteSpace(MainModule.Path) ? MainModule.Path : (!string.IsNullOrWhiteSpace(MainModule.Name) ? MainModule.Name : "blob.bin")));
string CurrentDir = Path.GetDirectoryName(ImagePath) ?? "C:\\";
if (string.IsNullOrWhiteSpace(CurrentDir)) CurrentDir = "C:\\";
if (!CurrentDir.EndsWith("\\")) CurrentDir += "\\";
string CurrentDir = ResolveStartupDirectory(Instance, ImagePath);
string DesktopInfo = "Winsta0\\Default";
string WindowTitle = ImagePath;
string CommandLine = GeneralHelper.QuoteCommandLineArg(ImagePath);
Expand Down Expand Up @@ -1335,9 +1369,7 @@ private ulong BuildProcessParameters32(BinaryEmulator Instance, WinModule MainMo
string ImagePath = Instance._binary.Location;
if (string.IsNullOrWhiteSpace(ImagePath))
ImagePath = !string.IsNullOrWhiteSpace(MainModule.Path) ? MainModule.Path : (!string.IsNullOrWhiteSpace(MainModule.Name) ? MainModule.Name : "app.exe");
string CurrentDir = Path.GetDirectoryName(ImagePath) ?? "C:\\";
if (string.IsNullOrWhiteSpace(CurrentDir)) CurrentDir = "C:\\";
if (!CurrentDir.EndsWith("\\")) CurrentDir += "\\";
string CurrentDir = ResolveStartupDirectory(Instance, ImagePath);
string DesktopInfo = "Winsta0\\Default";
string WindowTitle = ImagePath;
string CommandLine = GeneralHelper.QuoteCommandLineArg(ImagePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public static partial class X11
[LibraryImport("libX11.so.6")]
public static partial int XDisplayHeight(IntPtr display, int screen);

[LibraryImport("libX11.so.6")]
public static partial IntPtr XResourceManagerString(IntPtr display);

[LibraryImport("libX11.so.6")]
public static partial nuint XWhitePixel(IntPtr display, int screen);

Expand Down
Loading
Loading