Beispiel #1
0
        void KeyboardHookDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.PrintScreen && !ctrl)
            {
                e.SuppressKeyPress = true;
            }
            else if (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey)
            {
                ctrl = true;
            }
            else if (e.KeyCode == Keys.LShiftKey || e.KeyCode == Keys.RShiftKey)
            {
                shift = true;
            }
            else if (e.KeyCode == Keys.LMenu || e.KeyCode == Keys.RMenu)
            {
                alt = true;
            }
            else if (e.KeyCode == Keys.K && ctrl && alt)
            {
                this.Close();
            }
            else if (e.KeyCode == Keys.E && ctrl && alt)
            {
                MessageBox.Show("Desktop handle: " + Desktop.ToString());
            }
            else if (e.KeyCode == Keys.T && ctrl && alt)
            {
                WinAPI.SetWindowPos(this.Handle, WinAPI.HWND_TOPMOST, 0, 0, 0, 0, WinAPI.SWP_NOMOVE | WinAPI.SWP_NOSIZE | WinAPI.SWP_SHOWWINDOW);
            }
            else if (e.KeyCode == Keys.V && ctrl && alt)
            {
                if (File.Exists(cleanup))
                {
                    WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                    si.lpDesktop = DesktopName;
                    //si.dwFlags |= 0x00000020;
                    WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                    WinAPI.CreateProcess(null, cleanup + " -view", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                }
            }
            else if (e.KeyCode == Keys.Escape && ctrl && shift) //Task manager wont open by default so we'll have to do it manually and supress it
            {
                e.SuppressKeyPress = true;

                if (Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2 &&
                    File.Exists(@"C:\Windows\System32\Taskmgr.exe"))
                {
                    WinAPI.ShellExecute(this.Handle, "runas", @"C:\Windows\System32\Taskmgr.exe", null, null, WinAPI.ShowCommands.SW_SHOWNORMAL);
                }
                else if (File.Exists(@"C:\Windows\System32\taskmgr.exe"))
                {
                    WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                    si.lpDesktop = DesktopName;
                    //si.dwFlags |= 0x00000020;
                    WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                    WinAPI.CreateProcess(null, @"C:\Windows\System32\taskmgr.exe", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                }
            }
        }
Beispiel #2
0
        public DesktopAgent(IntPtr Process, IntPtr Desktop, string location, Taskbar tb, string dname)
        {
            DesktopName = dname;
            cleanup     = location + "cleanup.exe";
            if (Process == IntPtr.Zero)
            {
                this.Close();
            }
            if (Desktop == IntPtr.Zero)
            {
                this.Close();
            }
            this.Desktop = Desktop;
            InitializeComponent();

            //CenterToScreen();
            this.FormBorderStyle = FormBorderStyle.None;

            //this.TopMost = true;
            //this.TopLevel = true;
            this.ShowInTaskbar = false;

            this.tb = tb;
            //Console.WriteLine("{0}:{1}, {2}:{3}", tb.Bounds.Left, tb.Bounds.Top, tb.Bounds.Width, tb.Bounds.Height);
            //this.Left = tb.Bounds.Left;
            //SetSize(tb.Bounds.Width, tb.Bounds.Height);

#if HOOKS_ENABLED
            Gma.UserActivityMonitor.HookManager.KeyDown += KeyboardHookDown;
            Gma.UserActivityMonitor.HookManager.KeyUp   += KeyboardHookUp;

            FormClosing += delegate
            {
                Gma.UserActivityMonitor.HookManager.KeyDown -= KeyboardHookDown;
                Gma.UserActivityMonitor.HookManager.KeyUp   -= KeyboardHookUp;
            };
#endif

            //this.Opacity = 0;

            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += delegate
            {
                uint code = 1;
                try
                {
                    while (!this.IsDisposed)
                    {
                        Thread.Sleep(500);
                        if (!WinAPI.GetExitCodeProcess(Process, out code) || code != 259)
                        {
                            break;
                        }
                    }
                }
                catch { }

                if (File.Exists(cleanup))
                {
                    IntPtr hProc = IntPtr.Zero;

                    WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                    si.lpDesktop = DesktopName;
                    //si.dwFlags |= 0x00000020;
                    WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                    WinAPI.CreateProcess(null, cleanup + " -flag", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                    hProc = pi.hProcess;
                    //if (!this.IsDisposed) this.Invoke((Action)delegate { this.Close(); });
                    try
                    {
                        while (!this.IsDisposed)
                        {
                            Thread.Sleep(500);
                            if (!WinAPI.GetExitCodeProcess(hProc, out code) || code != 259)
                            {
                                break;
                            }
                        }
                    }
                    catch { }
                }
                else
                {
                    ERROR = 1;
                }
                if (!this.IsDisposed)
                {
                    this.Invoke((Action) delegate { this.Close(); });
                }
            };
            bg.RunWorkerAsync();

            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Tick    += delegate { Update(); };
            timer.Interval = 1000;
            timer.Start();

            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }
Beispiel #3
0
        public DesktopAgent(IntPtr Process, IntPtr Desktop, string location, Taskbar tb, string dname)
        {
            DesktopName = dname;
            cleanup = location + "cleanup.exe";
            if (Process == IntPtr.Zero) this.Close();
            if (Desktop == IntPtr.Zero) this.Close();
            this.Desktop = Desktop;
            InitializeComponent();

            //CenterToScreen();
            this.FormBorderStyle = FormBorderStyle.None;

            //this.TopMost = true;
            //this.TopLevel = true;
            this.ShowInTaskbar = false;

            this.tb = tb;
            //Console.WriteLine("{0}:{1}, {2}:{3}", tb.Bounds.Left, tb.Bounds.Top, tb.Bounds.Width, tb.Bounds.Height);
            //this.Left = tb.Bounds.Left;
            //SetSize(tb.Bounds.Width, tb.Bounds.Height);

#if HOOKS_ENABLED
            Gma.UserActivityMonitor.HookManager.KeyDown += KeyboardHookDown;
            Gma.UserActivityMonitor.HookManager.KeyUp += KeyboardHookUp;

            FormClosing += delegate
            {
                Gma.UserActivityMonitor.HookManager.KeyDown -= KeyboardHookDown;
                Gma.UserActivityMonitor.HookManager.KeyUp -= KeyboardHookUp;
            };
#endif

            //this.Opacity = 0;

            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += delegate
            {
                uint code = 1;
                try
                {
                    while (!this.IsDisposed)
                    {
                        Thread.Sleep(500);
                        if (!WinAPI.GetExitCodeProcess(Process, out code) || code != 259) { break; }
                    }
                }
                catch { }

                if (File.Exists(cleanup))
                {
                    IntPtr hProc = IntPtr.Zero;

                    WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                    si.lpDesktop = DesktopName;
                    //si.dwFlags |= 0x00000020;
                    WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                    WinAPI.CreateProcess(null, cleanup + " -flag", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                    hProc = pi.hProcess;
                    //if (!this.IsDisposed) this.Invoke((Action)delegate { this.Close(); });
                    try
                    {
                        while (!this.IsDisposed)
                        {
                            Thread.Sleep(500);
                            if (!WinAPI.GetExitCodeProcess(hProc, out code) || code != 259) { break; }
                        }
                    }
                    catch { }
                }
                else ERROR = 1;
                if(!this.IsDisposed) this.Invoke((Action)delegate { this.Close(); });
            };
            bg.RunWorkerAsync();

            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Tick += delegate { Update(); };
            timer.Interval = 1000;
            timer.Start();

            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }
Beispiel #4
0
        void KeyboardHookDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.PrintScreen && !ctrl) e.SuppressKeyPress = true;
            else if (e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey) ctrl = true;
            else if (e.KeyCode == Keys.LShiftKey || e.KeyCode == Keys.RShiftKey) shift = true;
            else if (e.KeyCode == Keys.LMenu || e.KeyCode == Keys.RMenu) alt = true;
            else if (e.KeyCode == Keys.K && ctrl && alt) this.Close();
            else if (e.KeyCode == Keys.E && ctrl && alt)
                MessageBox.Show("Desktop handle: " + Desktop.ToString());
            else if (e.KeyCode == Keys.T && ctrl && alt)
                WinAPI.SetWindowPos(this.Handle, WinAPI.HWND_TOPMOST, 0, 0, 0, 0, WinAPI.SWP_NOMOVE | WinAPI.SWP_NOSIZE | WinAPI.SWP_SHOWWINDOW);
            else if (e.KeyCode == Keys.V && ctrl && alt)
            {
                if (File.Exists(cleanup))
                {
                    WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                    si.lpDesktop = DesktopName;
                    //si.dwFlags |= 0x00000020;
                    WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                    WinAPI.CreateProcess(null, cleanup + " -view", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                }
            }
            else if (e.KeyCode == Keys.Escape && ctrl && shift) //Task manager wont open by default so we'll have to do it manually and supress it
            {
                e.SuppressKeyPress = true;

                if(Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2 &&
                   File.Exists(@"C:\Windows\System32\Taskmgr.exe"))
                {
                    WinAPI.ShellExecute(this.Handle, "runas", @"C:\Windows\System32\Taskmgr.exe", null, null, WinAPI.ShowCommands.SW_SHOWNORMAL);
                }
                else if (File.Exists(@"C:\Windows\System32\taskmgr.exe"))
                {
                    WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                    si.lpDesktop = DesktopName;
                    //si.dwFlags |= 0x00000020;
                    WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                    WinAPI.CreateProcess(null, @"C:\Windows\System32\taskmgr.exe", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                }
            }
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Please specify a file to run");
                return;
            }

            if (!File.Exists(args[0]))
            {
                Console.WriteLine("The file you specified could not be found");
                return;
            }

            StringBuilder sb       = new StringBuilder();
            string        procline = String.Format("\"{0}\"", String.Join("\" \"", args));
            string        ext      = Path.GetExtension(args[0]).ToLower();

            //if (Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2)
            {
                int i = 0;
                for (; i < 10; i++)
                {
                    if (ext == ".dll")
                    {
                        procline = String.Format("{0} {1}", @"rundll32", procline);
                        break;
                    }
                    else if (ext != ".exe")
                    {
                        string file = "";
                        if (!ResolveExtension(ext, ref file))
                        {
                            break;
                        }
                        procline = String.Format("\"{0}\" {1}", file, procline);
                        ext      = Path.GetExtension(file).ToLower();
                    }
                    else
                    {
                        break;
                    }
                }
                if (i == 10)
                {
                    Console.WriteLine("Could not locate default program");
                    return;
                }
            }

            Taskbar tb = new Taskbar(); //Get this first so that if we crash we wont be stuck in desktop limbo!

            IntPtr hOldDesktop = WinAPI.GetThreadDesktop(WinAPI.GetCurrentThreadId());

            IntPtr hNewDesktop = WinAPI.CreateDesktop("securedesktop",
                                                      IntPtr.Zero, IntPtr.Zero, 0, (uint)WinAPI.DESKTOP_ACCESS.GENERIC_ALL, IntPtr.Zero);

            int       ERROR = -1;
            IntPtr    hProc = IntPtr.Zero;
            Exception da_ex = null, sd_ex = null;

            try
            {
                WinAPI.SwitchDesktop(hNewDesktop);

                BackgroundWorker bg = new BackgroundWorker();
                DesktopAgent     sf = null;
                bg.DoWork += delegate
                {
                    WinAPI.SetThreadDesktop(hNewDesktop);
                    try
                    {
                        WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                        si.lpDesktop = "securedesktop";
                        si.dwFlags  |= 0x00000020;
                        WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                        bool cpdone = WinAPI.CreateProcess(null, procline, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                        hProc = pi.hProcess;

                        if (cpdone)
                        {
                            sf = new DesktopAgent(hProc, hNewDesktop, Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\", tb);

                            Application.Run(sf);
                            ERROR = sf.ERROR;
                        }
                        else
                        {
                            ERROR = 4;
                        }
                    }
                    catch (Exception e) { ERROR = 2; da_ex = e; }
                    finally { workdone = true; }
                };
                bg.RunWorkerAsync();

                while (!workdone)
                {
                    System.Threading.Thread.Sleep(100);
                    //if(sf != null && !sf.IsDisposed) WinAPI.SetWindowPos(sf.Handle, new IntPtr(-1), tb.Bounds.Left, tb.Bounds.Top, tb.Bounds.Width, tb.Bounds.Height, 0);
                }
            }
            catch (Exception e) { ERROR = 3; sd_ex = e; }
            finally
            {
                WinAPI.SwitchDesktop(hOldDesktop);

                if (hProc != IntPtr.Zero)
                {
                    WinAPI.TerminateProcess(hProc, 0);
                }
                WinAPI.CloseDesktop(hNewDesktop);
            }

            switch (ERROR)
            {
            case 1:
                MessageBox.Show("The desktop agent could not locate the cleanup binary, it is unsafe to continue to use Secure Desktop until the problem is corrected by redownloading or updating Secure Desktop.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;

            case 2:
                if (da_ex != null)
                {
                    MessageBox.Show("The desktop agent crashed;\r\n" + da_ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                break;

            case 3:
                if (sd_ex != null)
                {
                    MessageBox.Show("Secure Desktop crashed;\r\n" + sd_ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                break;

            case 4:
                MessageBox.Show(String.Format("Failed to start process with error code '{0:X8}'", Marshal.GetLastWin32Error()), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;
            }
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Please specify a file to run");
                return;
            }

            if (!File.Exists(args[0]))
            {
                Console.WriteLine("The file you specified could not be found");
                return;
            }

            StringBuilder sb = new StringBuilder();
            string procline = String.Format("\"{0}\"", String.Join("\" \"", args));
            string ext = Path.GetExtension(args[0]).ToLower();

            //if (Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2)
            {
                int i = 0;
                for (; i < 10; i++)
                {
                    if (ext == ".dll")
                    {
                        procline = String.Format("{0} {1}", @"rundll32", procline);
                        break;
                    }
                    else if (ext != ".exe")
                    {
                        string file = "";
                        if (!ResolveExtension(ext, ref file)) break;
                        procline = String.Format("\"{0}\" {1}", file, procline);
                        ext = Path.GetExtension(file).ToLower();
                    }
                    else break;
                }
                if (i == 10)
                {
                    Console.WriteLine("Could not locate default program");
                    return;
                }
            }

            Taskbar tb = new Taskbar(); //Get this first so that if we crash we wont be stuck in desktop limbo!

            IntPtr hOldDesktop = WinAPI.GetThreadDesktop(WinAPI.GetCurrentThreadId());

            IntPtr hNewDesktop = WinAPI.CreateDesktop("securedesktop",
            IntPtr.Zero, IntPtr.Zero, 0, (uint)WinAPI.DESKTOP_ACCESS.GENERIC_ALL, IntPtr.Zero);

            int ERROR = -1;
            IntPtr hProc = IntPtr.Zero;
            Exception da_ex = null, sd_ex = null;
            try
            {
                WinAPI.SwitchDesktop(hNewDesktop);

                BackgroundWorker bg = new BackgroundWorker();
                DesktopAgent sf = null;
                bg.DoWork += delegate
                {
                    WinAPI.SetThreadDesktop(hNewDesktop);
                    try
                    {
                        WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                        si.lpDesktop = "securedesktop";
                        si.dwFlags |= 0x00000020;
                        WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                        bool cpdone = WinAPI.CreateProcess(null, procline, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                        hProc = pi.hProcess;

                        if (cpdone)
                        {
                            sf = new DesktopAgent(hProc, hNewDesktop, Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\", tb);

                            Application.Run(sf);
                            ERROR = sf.ERROR;
                        }
                        else
                            ERROR = 4;
                    }
                    catch (Exception e) { ERROR = 2; da_ex = e; }
                    finally { workdone = true; }
                };
                bg.RunWorkerAsync();

                while (!workdone)
                {
                    System.Threading.Thread.Sleep(100);
                    //if(sf != null && !sf.IsDisposed) WinAPI.SetWindowPos(sf.Handle, new IntPtr(-1), tb.Bounds.Left, tb.Bounds.Top, tb.Bounds.Width, tb.Bounds.Height, 0);
                }
            }
            catch (Exception e) { ERROR = 3; sd_ex = e; }
            finally
            {
                WinAPI.SwitchDesktop(hOldDesktop);

                if (hProc != IntPtr.Zero) WinAPI.TerminateProcess(hProc, 0);
                WinAPI.CloseDesktop(hNewDesktop);
            }

            switch (ERROR)
            {
                case 1:
                    MessageBox.Show("The desktop agent could not locate the cleanup binary, it is unsafe to continue to use Secure Desktop until the problem is corrected by redownloading or updating Secure Desktop.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                case 2:
                    if (da_ex != null)
                        MessageBox.Show("The desktop agent crashed;\r\n" + da_ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                case 3:
                    if (sd_ex != null)
                        MessageBox.Show("Secure Desktop crashed;\r\n" + sd_ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                case 4:
                    MessageBox.Show(String.Format("Failed to start process with error code '{0:X8}'", Marshal.GetLastWin32Error()), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
            }
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            Process[] processes = Process.GetProcessesByName("SecureDesktop.exe");
            if (processes.Length > 0)
            {
                Console.WriteLine("Secure Desktop is already running (proc)");
                return;
            }
            if (File.Exists("securedesktop.lock"))
            {
                Console.WriteLine("Secure Desktop is already running (lock)");
                return;
            }
            if (args.Length < 1)
            {
                Console.WriteLine("Please specify a file to run");
                return;
            }

            if (!File.Exists(args[0]))
            {
                Console.WriteLine("The file you specified could not be found");
                return;
            }

            File.Create("securedesktop.lock").Close();

            StringBuilder sb       = new StringBuilder();
            string        procline = String.Format("\"{0}\"", String.Join("\" \"", args));
            string        ext      = Path.GetExtension(args[0]).ToLower();

            //if (Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2)
            {
                int i = 0;
                for (; i < 10; i++)
                {
                    if (ext == ".dll")
                    {
                        procline = String.Format("{0} {1}", @"rundll32", procline);
                        break;
                    }
                    else if (ext != ".exe")
                    {
                        string file = "";
                        if (!ResolveExtension(ext, ref file))
                        {
                            break;
                        }
                        procline = String.Format("\"{0}\" {1}", file, procline);
                        ext      = Path.GetExtension(file).ToLower();
                    }
                    else
                    {
                        break;
                    }
                }
                if (i == 10)
                {
                    Console.WriteLine("Could not locate default program");
                    return;
                }
            }

            /* Entropy collection */
            int[] entropy = new int[ISAAC.SIZE];
            int   ei      = 0;

            WinAPI.MEMORYSTATUSEX memStatus = new WinAPI.MEMORYSTATUSEX();
            if (WinAPI.GlobalMemoryStatusEx(memStatus))
            {
                entropy[0] = (int)memStatus.ullAvailPhys;
                entropy[1] = (int)memStatus.ullAvailVirtual;
                entropy[2] = (int)memStatus.ullAvailPageFile;
                ei         = 2;
            }

            WinAPI.POINT pt;
            if (WinAPI.GetCursorPos(out pt))
            {
                entropy[ei + 1] = pt.X;
                entropy[ei + 2] = pt.Y;
                ei += 2;
            }

            uint spc, bps, nofc, tnoc;

            if (WinAPI.GetDiskFreeSpace(null, out spc, out bps, out nofc, out tnoc))
            {
                entropy[ei + 1] = (int)spc;
                entropy[ei + 2] = (int)bps;
                entropy[ei + 3] = (int)nofc;
                entropy[ei + 4] = (int)tnoc;
                ei += 4;
            }

            ISAAC csprng = new ISAAC(entropy);

            for (int i = 0; i < 3; i++)
            {
                csprng.Isaac();
            }

            StringBuilder desktopname = new StringBuilder(16);
            const int     min         = 0x61;
            const int     max         = 0x7A;
            const int     diff        = max - min;

            for (int i = 0; i < 16; i++)
            {
                desktopname.Append(
                    (char)(((int)Math.Abs(csprng.rsl[i]) % diff) + min)
                    );
            }

            string dname = desktopname.ToString();

            Taskbar tb = new Taskbar(); //Get this first so that if we crash we wont be stuck in desktop limbo!

            IntPtr hOldDesktop = WinAPI.GetThreadDesktop(WinAPI.GetCurrentThreadId());

            IntPtr hNewDesktop = WinAPI.CreateDesktop(dname,
                                                      IntPtr.Zero, IntPtr.Zero, 0, (uint)WinAPI.DESKTOP_ACCESS.CUSTOM_SECURE, IntPtr.Zero);

            int       ERROR = -1;
            IntPtr    hProc = IntPtr.Zero;
            Exception da_ex = null, sd_ex = null;

            try
            {
                WinAPI.SwitchDesktop(hNewDesktop);

                BackgroundWorker bg = new BackgroundWorker();
                DesktopAgent     sf = null;
                bg.DoWork += delegate
                {
                    WinAPI.SetThreadDesktop(hNewDesktop);
                    try
                    {
                        WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                        si.lpDesktop = dname;
                        si.dwFlags  |= 0x00000020;
                        WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                        bool cpdone = WinAPI.CreateProcess(null, procline, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                        hProc = pi.hProcess;

                        if (cpdone)
                        {
                            sf = new DesktopAgent(hProc, hNewDesktop, Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\", tb, dname);

                            Application.Run(sf);
                            ERROR = sf.ERROR;
                        }
                        else
                        {
                            ERROR = 4;
                        }
                    }
                    catch (Exception e) { ERROR = 2; da_ex = e; }
                    finally { workdone = true; }
                };
                bg.RunWorkerAsync();

                while (!workdone)
                {
                    System.Threading.Thread.Sleep(100);
                    //if(sf != null && !sf.IsDisposed) WinAPI.SetWindowPos(sf.Handle, new IntPtr(-1), tb.Bounds.Left, tb.Bounds.Top, tb.Bounds.Width, tb.Bounds.Height, 0);
                }
            }
            catch (Exception e) { ERROR = 3; sd_ex = e; }
            finally
            {
                WinAPI.SwitchDesktop(hOldDesktop);

                if (hProc != IntPtr.Zero)
                {
                    WinAPI.TerminateProcess(hProc, 0);
                }
                WinAPI.CloseDesktop(hNewDesktop);
            }

            switch (ERROR)
            {
            case 1:
                MessageBox.Show("The desktop agent could not locate the cleanup binary, it is unsafe to continue to use Secure Desktop until the problem is corrected by redownloading or updating Secure Desktop.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;

            case 2:
                if (da_ex != null)
                {
                    MessageBox.Show("The desktop agent crashed;\r\n" + da_ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                break;

            case 3:
                if (sd_ex != null)
                {
                    MessageBox.Show("Secure Desktop crashed;\r\n" + sd_ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                break;

            case 4:
                MessageBox.Show(String.Format("Failed to start process with error code '{0:X8}'", Marshal.GetLastWin32Error()), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;
            }

            if (File.Exists("securedesktop.lock"))
            {
                File.Delete("securedesktop.lock");
            }
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            Process[] processes = Process.GetProcessesByName("SecureDesktop.exe");
            if (processes.Length > 0)
            {
                Console.WriteLine("Secure Desktop is already running (proc)");
                return;
            }
            if (File.Exists("securedesktop.lock"))
            {
                Console.WriteLine("Secure Desktop is already running (lock)");
                return;
            }
            if (args.Length < 1)
            {
                Console.WriteLine("Please specify a file to run");
                return;
            }

            if (!File.Exists(args[0]))
            {
                Console.WriteLine("The file you specified could not be found");
                return;
            }

            File.Create("securedesktop.lock").Close();

            StringBuilder sb = new StringBuilder();
            string procline = String.Format("\"{0}\"", String.Join("\" \"", args));
            string ext = Path.GetExtension(args[0]).ToLower();

            //if (Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2)
            {
                int i = 0;
                for (; i < 10; i++)
                {
                    if (ext == ".dll")
                    {
                        procline = String.Format("{0} {1}", @"rundll32", procline);
                        break;
                    }
                    else if (ext != ".exe")
                    {
                        string file = "";
                        if (!ResolveExtension(ext, ref file)) break;
                        procline = String.Format("\"{0}\" {1}", file, procline);
                        ext = Path.GetExtension(file).ToLower();
                    }
                    else break;
                }
                if (i == 10)
                {
                    Console.WriteLine("Could not locate default program");
                    return;
                }
            }

            /* Entropy collection */
            int[] entropy = new int[ISAAC.SIZE];
            int ei = 0;

            WinAPI.MEMORYSTATUSEX memStatus = new WinAPI.MEMORYSTATUSEX();
            if (WinAPI.GlobalMemoryStatusEx(memStatus))
            {
                entropy[0] = (int)memStatus.ullAvailPhys;
                entropy[1] = (int)memStatus.ullAvailVirtual;
                entropy[2] = (int)memStatus.ullAvailPageFile;
                ei = 2;
            }

            WinAPI.POINT pt;
            if (WinAPI.GetCursorPos(out pt))
            {
                entropy[ei + 1] = pt.X;
                entropy[ei + 2] = pt.Y;
                ei += 2;
            }

            uint spc, bps, nofc, tnoc;
            if (WinAPI.GetDiskFreeSpace(null, out spc, out bps, out nofc, out tnoc))
            {
                entropy[ei + 1] = (int)spc;
                entropy[ei + 2] = (int)bps;
                entropy[ei + 3] = (int)nofc;
                entropy[ei + 4] = (int)tnoc;
                ei += 4;
            }

            ISAAC csprng = new ISAAC(entropy);

            for (int i = 0; i < 3; i++) csprng.Isaac();

            StringBuilder desktopname = new StringBuilder(16);
            const int min = 0x61;
            const int max = 0x7A;
            const int diff = max - min;
            for (int i = 0; i < 16; i++)
                desktopname.Append(
                    (char)(((int)Math.Abs(csprng.rsl[i]) % diff) + min)
                    );

            string dname = desktopname.ToString();

            Taskbar tb = new Taskbar(); //Get this first so that if we crash we wont be stuck in desktop limbo!

            IntPtr hOldDesktop = WinAPI.GetThreadDesktop(WinAPI.GetCurrentThreadId());

            IntPtr hNewDesktop = WinAPI.CreateDesktop(dname,
            IntPtr.Zero, IntPtr.Zero, 0, (uint)WinAPI.DESKTOP_ACCESS.CUSTOM_SECURE, IntPtr.Zero);

            int ERROR = -1;
            IntPtr hProc = IntPtr.Zero;
            Exception da_ex = null, sd_ex = null;
            try
            {
                WinAPI.SwitchDesktop(hNewDesktop);

                BackgroundWorker bg = new BackgroundWorker();
                DesktopAgent sf = null;
                bg.DoWork += delegate
                {
                    WinAPI.SetThreadDesktop(hNewDesktop);
                    try
                    {
                        WinAPI.STARTUPINFO si = new WinAPI.STARTUPINFO();
                        si.lpDesktop = dname;
                        si.dwFlags |= 0x00000020;
                        WinAPI.PROCESS_INFORMATION pi = new WinAPI.PROCESS_INFORMATION();
                        bool cpdone = WinAPI.CreateProcess(null, procline, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
                        hProc = pi.hProcess;

                        if (cpdone)
                        {
                            sf = new DesktopAgent(hProc, hNewDesktop, Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\", tb, dname);

                            Application.Run(sf);
                            ERROR = sf.ERROR;
                        }
                        else
                            ERROR = 4;
                    }
                    catch (Exception e) { ERROR = 2; da_ex = e; }
                    finally { workdone = true; }
                };
                bg.RunWorkerAsync();

                while (!workdone)
                {
                    System.Threading.Thread.Sleep(100);
                    //if(sf != null && !sf.IsDisposed) WinAPI.SetWindowPos(sf.Handle, new IntPtr(-1), tb.Bounds.Left, tb.Bounds.Top, tb.Bounds.Width, tb.Bounds.Height, 0);
                }
            }
            catch (Exception e) { ERROR = 3; sd_ex = e; }
            finally
            {
                WinAPI.SwitchDesktop(hOldDesktop);

                if (hProc != IntPtr.Zero) WinAPI.TerminateProcess(hProc, 0);
                WinAPI.CloseDesktop(hNewDesktop);
            }

            switch (ERROR)
            {
                case 1:
                    MessageBox.Show("The desktop agent could not locate the cleanup binary, it is unsafe to continue to use Secure Desktop until the problem is corrected by redownloading or updating Secure Desktop.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                case 2:
                    if (da_ex != null)
                        MessageBox.Show("The desktop agent crashed;\r\n" + da_ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                case 3:
                    if (sd_ex != null)
                        MessageBox.Show("Secure Desktop crashed;\r\n" + sd_ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                case 4:
                    MessageBox.Show(String.Format("Failed to start process with error code '{0:X8}'", Marshal.GetLastWin32Error()), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
            }

            if (File.Exists("securedesktop.lock")) File.Delete("securedesktop.lock");
        }