public override void Lockdown(Prison prison)
        {
            Native.SECURITY_ATTRIBUTES secAttributes = new Native.SECURITY_ATTRIBUTES();
            secAttributes.nLength = Marshal.SizeOf(secAttributes);

            IntPtr windowStation = Native.CreateWindowStation(prison.User.Username, 0, Native.WINDOWS_STATION_ACCESS_MASK.WINSTA_NONE, null);

            IntPtr desktop = IntPtr.Zero;


            lock (windowStationLock)
            {
                IntPtr currentWindowStation = Native.GetProcessWindowStation();
                bool   setOk = Native.SetProcessWindowStation(windowStation);

                if (!setOk)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                Native.CreateDesktop(prison.User.Username, null, null, 0, Native.ACCESS_MASK.DESKTOP_CREATEWINDOW, null);

                prison.ProcessStartupInfo.lpDesktop = string.Format(@"{0}\{0}", prison.User.Username);

                Native.SetProcessWindowStation(currentWindowStation);
            }
        }
        public override void Lockdown(Prison prison)
        {
            Native.SECURITY_ATTRIBUTES secAttributes = new Native.SECURITY_ATTRIBUTES();
            secAttributes.nLength = Marshal.SizeOf(secAttributes);

            IntPtr windowStation = Native.CreateWindowStation(prison.User.Username, 0, Native.WINDOWS_STATION_ACCESS_MASK.WINSTA_NONE, null);

            IntPtr desktop = IntPtr.Zero;

            lock (windowStationLock)
            {
                IntPtr currentWindowStation = Native.GetProcessWindowStation();
                bool setOk = Native.SetProcessWindowStation(windowStation);

                if (!setOk)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                Native.CreateDesktop(prison.User.Username, null, null, 0, Native.ACCESS_MASK.DESKTOP_CREATEWINDOW, null);

                prison.ProcessStartupInfo.lpDesktop = string.Format(@"{0}\{0}", prison.User.Username);

                Native.SetProcessWindowStation(currentWindowStation);
            }
        }
        private SafeFileHandle CreateNamedPipe(string pipeName)
        {
            CommonSecurityDescriptor sd = new CommonSecurityDescriptor(false, false, "D:(A;;GA;;;LS)(A;;GA;;;BA)(A;;GA;;;IU)");

            byte[] sdBytes = new byte[sd.BinaryLength];
            sd.GetBinaryForm(sdBytes, 0);
            GCHandle gcHandle = GCHandle.Alloc(sdBytes, GCHandleType.Pinned);

            Native.SECURITY_ATTRIBUTES securityAttributes = new Native.SECURITY_ATTRIBUTES();
            securityAttributes.nLength              = Marshal.SizeOf(securityAttributes);
            securityAttributes.bInheritHandle       = 0;
            securityAttributes.lpSecurityDescriptor = Marshal.UnsafeAddrOfPinnedArrayElement(sdBytes, 0);

            IntPtr handle = Native.CreateNamedPipe(
                pipeName,
                Native.PIPE_ACCESS_DUPLEX | Native.FILE_FLAG_OVERLAPPED | Native.FILE_FLAG_FIRST_PIPE_INSTANCE,
                Native.PIPE_TYPE_BYTE | Native.PIPE_READMODE_BYTE | Native.PIPE_WAIT,
                1,
                2048,
                2048,
                Native.NMPWAIT_USE_DEFAULT_WAIT,
                securityAttributes);

            gcHandle.Free();

            return(new SafeFileHandle(handle, true));
        }
Example #4
0
        public override void Apply(Prison prison)
        {
            Native.SECURITY_ATTRIBUTES secAttributes = new Native.SECURITY_ATTRIBUTES();
            secAttributes.nLength = Marshal.SizeOf(secAttributes);

            IntPtr windowStation = IntPtr.Zero;

            windowStation = Native.OpenWindowStation(prison.User.Username, false, Native.WINDOWS_STATION_ACCESS_MASK.WINSTA_CREATEDESKTOP);

            int openWinStaStatus = Marshal.GetLastWin32Error();

            // Error 0x2 is ERROR_FILE_NOT_FOUND
            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx
            if (windowStation == IntPtr.Zero && openWinStaStatus != 0x2)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            if (windowStation == IntPtr.Zero && openWinStaStatus == 0x2)
            {
                // TODO SECURITY: change security attributes. the default will give everyone access to the object including other prisons
                windowStation = Native.CreateWindowStation(prison.User.Username, 0, Native.WINDOWS_STATION_ACCESS_MASK.WINSTA_CREATEDESKTOP, null);

                if (windowStation == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }

            lock (windowStationContextLock)
            {
                IntPtr currentWindowStation = Native.GetProcessWindowStation();

                try
                {
                    bool setOk = Native.SetProcessWindowStation(windowStation);

                    if (!setOk)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    // TODO SECURITY: change security attributes. the default will give everyone access to the object including other prisons
                    var desktop = Native.CreateDesktop("Default", null, null, 0, Native.ACCESS_MASK.DESKTOP_CREATEWINDOW, null);

                    if (desktop == IntPtr.Zero)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    prison.desktopName = string.Format(@"{0}\Default", prison.User.Username);
                }
                finally
                {
                    Native.SetProcessWindowStation(currentWindowStation);
                }
            }
        }
        public override void Apply(Prison prison)
        {
            Native.SECURITY_ATTRIBUTES secAttributes = new Native.SECURITY_ATTRIBUTES();
            secAttributes.nLength = Marshal.SizeOf(secAttributes);

            IntPtr windowStation = IntPtr.Zero;

            windowStation = NativeOpenWindowStation(prison.User.Username);

            int openWinStaStatus = Marshal.GetLastWin32Error();

            // Error 0x2 is ERROR_FILE_NOT_FOUND
            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx
            if (windowStation == IntPtr.Zero && openWinStaStatus != 0x2)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            if (windowStation == IntPtr.Zero &&  openWinStaStatus == 0x2)
            {
                // TODO SECURITY: change security attributes. the default will give everyone access to the object including other prisons
                windowStation = NativeCreateWindowStation(prison.User.Username);

                if (windowStation == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }

            lock (windowStationContextLock)
            {
                IntPtr currentWindowStation = NativeGetProcessWindowStation();

                try
                {
                    bool setOk = NativeSetProcessWindowStation(windowStation);

                    if (!setOk)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    // TODO SECURITY: change security attributes. the default will give everyone access to the object including other prisons
                    var desktop = NativeCreateDesktop();

                    if (desktop == IntPtr.Zero)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    prison.desktopName = string.Format(@"{0}\Default", prison.User.Username);
                }
                finally
                {
                    NativeSetProcessWindowStation(currentWindowStation);
                }
            }
        }
Example #6
0
    /// <summary>
    /// Starts the application, injecting Reloaded into it.
    /// </summary>
    public void Start()
    {
        // Start up the process
        Native.STARTUPINFO         startupInfo         = new Native.STARTUPINFO();
        Native.SECURITY_ATTRIBUTES lpProcessAttributes = new Native.SECURITY_ATTRIBUTES();
        Native.SECURITY_ATTRIBUTES lpThreadAttributes  = new Native.SECURITY_ATTRIBUTES();
        Native.PROCESS_INFORMATION processInformation  = new Native.PROCESS_INFORMATION();

        if (_arguments == null)
        {
            _arguments = "";
        }

        bool success = Native.CreateProcessW(null, $"\"{_location}\" {_arguments}", ref lpProcessAttributes,
                                             ref lpThreadAttributes, false, Native.ProcessCreationFlags.CREATE_SUSPENDED,
                                             IntPtr.Zero, Path.GetDirectoryName(_location) !, ref startupInfo, ref processInformation);

        if (!success)
        {
            string windowsErrorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
            throw new ArgumentException($"{Resources.ErrorFailedToStartProcess.Get()} {windowsErrorMessage}");
        }

        // DLL Injection
        var process  = Process.GetProcessById((int)processInformation.dwProcessId);
        var injector = new ApplicationInjector(process);

        try
        {
            injector.Inject();
        }
        catch (Exception)
        {
            Native.ResumeThread(processInformation.hThread);
            throw;
        }

        Native.ResumeThread(processInformation.hThread);
    }
Example #7
0
        private SafeFileHandle CreateNamedPipe(string pipeName)
        {
            CommonSecurityDescriptor sd = new CommonSecurityDescriptor(false, false, "D:(A;;GA;;;LS)(A;;GA;;;BA)(A;;GA;;;IU)");

            byte[] sdBytes = new byte[sd.BinaryLength];
            sd.GetBinaryForm(sdBytes, 0);
            GCHandle gcHandle = GCHandle.Alloc(sdBytes, GCHandleType.Pinned);

            Native.SECURITY_ATTRIBUTES securityAttributes = new Native.SECURITY_ATTRIBUTES();
            securityAttributes.nLength = Marshal.SizeOf(securityAttributes);
            securityAttributes.bInheritHandle = 0;
            securityAttributes.lpSecurityDescriptor = Marshal.UnsafeAddrOfPinnedArrayElement(sdBytes, 0);

            IntPtr handle = Native.CreateNamedPipe(
                pipeName,
                Native.PIPE_ACCESS_DUPLEX | Native.FILE_FLAG_OVERLAPPED | Native.FILE_FLAG_FIRST_PIPE_INSTANCE,
                Native.PIPE_TYPE_BYTE | Native.PIPE_READMODE_BYTE | Native.PIPE_WAIT,
                1,
                2048,
                2048,
                Native.NMPWAIT_USE_DEFAULT_WAIT,
                securityAttributes);

            gcHandle.Free();

            return new SafeFileHandle(handle, true);
        }