From f933d417b7ffa6278b48c2a42d8378808e1610f9 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 5 Jul 2018 18:49:00 +0200 Subject: [PATCH] Testing Windows impl. DLL location and hardening issues --- .../VirtualComputerWindows/Exports.cs | 49 ++++++++++-- .../VirtualComputerWindows/VBFB.cs | 75 +++++++++++++++++++ .../VirtualComputerWindows.csproj | 7 +- 3 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 VirtualComputerWindows/VirtualComputerWindows/VBFB.cs diff --git a/VirtualComputerWindows/VirtualComputerWindows/Exports.cs b/VirtualComputerWindows/VirtualComputerWindows/Exports.cs index 5ad2882..b29a80e 100644 --- a/VirtualComputerWindows/VirtualComputerWindows/Exports.cs +++ b/VirtualComputerWindows/VirtualComputerWindows/Exports.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using VirtualBox; @@ -9,12 +10,50 @@ namespace VirtualComputerWindows { public static class Exports { - [DllExport] - public static VirtualBoxClass Init() + [DllImport(@"C:\Program Files\Oracle\VirtualBox\VBoxRT.dll", CallingConvention = CallingConvention.StdCall)] + private static extern void RTR3InitExe(int argc, string argv, int zeroflags); + /*[DllImport(@"C:\Program Files\Oracle\VirtualBox\VBoxVMM.dll", CallingConvention = CallingConvention.StdCall)] + private static extern int VMMDoHmTest(IntPtr vmstruct);*/ + + private static VirtualBoxClient vbc; + + public static async Task Init() => await Task.Run(() => { - var vbox = new VirtualBoxClass(); - return vbox; - var machine = vbox.Machines.GetValue(0) as IMachine; + try + { + //Environment.SetEnvironmentVariable("VBOX_HOME", @"C:\Program Files\Oracle\VirtualBox\"); + //TODO: Only finds the VBoxVMM.dll when placed in the VirtualBox dir (regardless of working dir) + //Even then there are hardening issues: VERR_SUPDRV_NOT_BUDDING_VM_PROCESS_1 + //https://www.virtualbox.org/svn/vbox/trunk/src/VBox/HostDrivers/Support/win/SUPDrv-win.cpp + vbc = new VirtualBoxClientClass(); + var vbox = vbc.VirtualBox; + RTR3InitExe(0, "", 0); + var ses = vbc.Session; + var machine = vbox.Machines.GetValue(0) as IMachine; + ses.Name = "minecraft"; + machine.LockMachine(ses, LockType.LockType_VM); + Console.WriteLine("Locking..."); + while (ses.State != SessionState.SessionState_Locked) ; + Console.WriteLine("Locked"); + machine = ses.Machine; + Console.WriteLine("Powering up..."); + ses.Console.PowerUp().WaitForCompletion(10000); + Console.WriteLine("Framebuffer attach"); + ses.Console.Display.AttachFramebuffer(0, new VBFB(ses.Console.Display)); + return vbox; + } + catch(Exception e) + { + Console.WriteLine(e); + Console.ReadLine(); + return null; + } + }); + + public static void Main() + { + Init(); + Console.ReadLine(); } } } diff --git a/VirtualComputerWindows/VirtualComputerWindows/VBFB.cs b/VirtualComputerWindows/VirtualComputerWindows/VBFB.cs new file mode 100644 index 0000000..7999b81 --- /dev/null +++ b/VirtualComputerWindows/VirtualComputerWindows/VBFB.cs @@ -0,0 +1,75 @@ +using System; +using VirtualBox; + +namespace VirtualComputerWindows +{ + internal class VBFB : IFramebuffer + { + private IDisplay display; + + public VBFB(IDisplay display) + { + this.display = display; + } + + public void NotifyUpdate(uint aX, uint aY, uint aWidth, uint aHeight) + { + Console.WriteLine("Update: " + aX + " " + aY + " " + aWidth + " " + aHeight); + } + + public void NotifyUpdateImage(uint aX, uint aY, uint aWidth, uint aHeight, Array aImage) + { + Console.WriteLine("UpdateImage: " + aX + " " + aY + " " + aWidth + " " + aHeight); + } + + public void NotifyChange(uint aScreenId, uint aXOrigin, uint aYOrigin, uint aWidth, uint aHeight) + { + Console.WriteLine("Change: " + aXOrigin + " " + aYOrigin + " " + aWidth + " " + aHeight); + display.QuerySourceBitmap(0, out var isd); + var addr = new IntPtr(); + isd.QueryBitmapInfo(addr, out var w, out var h, out var bpp, out var bpl, out var bf); + Console.WriteLine("Bitmap info: " + addr + " " + w + " " + h + " " + bpp + " " + bpl + " " + bf); + } + + public int VideoModeSupported(uint aWidth, uint aHeight, uint aBpp) + { + return 1; + } + + public uint GetVisibleRegion(ref byte aRectangles, uint aCount) + { + return aCount; + } + + public void SetVisibleRegion(ref byte aRectangles, uint aCount) + { + } + + public void ProcessVHWACommand(ref byte aCommand, int aEnmCmd, int aFromGuest) + { + } + + public void Notify3DEvent(uint aType, Array aData) + { + } + + public uint Width => 640; + + public uint Height => 480; + + public uint BitsPerPixel => 32; + + public uint BytesPerLine => 640 * 4; + + public BitmapFormat PixelFormat => BitmapFormat.BitmapFormat_BGRA; + + public uint HeightReduction => 0; + + public IFramebufferOverlay Overlay => null; + + public long WinId => 0; + + //public Array Capabilities => new[] { FramebufferCapabilities.FramebufferCapabilities_UpdateImage }; + public Array Capabilities => new FramebufferCapabilities[] { }; + } +} \ No newline at end of file diff --git a/VirtualComputerWindows/VirtualComputerWindows/VirtualComputerWindows.csproj b/VirtualComputerWindows/VirtualComputerWindows/VirtualComputerWindows.csproj index 39ee28e..fa0964b 100644 --- a/VirtualComputerWindows/VirtualComputerWindows/VirtualComputerWindows.csproj +++ b/VirtualComputerWindows/VirtualComputerWindows/VirtualComputerWindows.csproj @@ -5,7 +5,7 @@ Debug AnyCPU {5E734F17-FF74-4187-A6E4-B7E1DAB272F8} - Library + Exe Properties VirtualComputerWindows VirtualComputerWindows @@ -44,6 +44,7 @@ AnyCPU prompt MinimumRecommendedRules.ruleset + false bin\x64\Release\ @@ -72,6 +73,9 @@ prompt MinimumRecommendedRules.ruleset + + + ..\packages\DllExport.1.5.2\lib\net20\DllExport.dll @@ -93,6 +97,7 @@ +