Ejemplo n.º 1
0
        public async Task <string> SpawnClient(Form f, string nextusername, string nextpassword, string nextserver)
        {
            AxMsRdpClient9NotSafeForScripting rdpClient = new AxMsRdpClient9NotSafeForScripting();

            f.Controls.Add(rdpClient);
            rdpClient.Size = new System.Drawing.Size(1, 1);
            rdpClient.CreateControl();
            rdpClient.OnConnected     += Rdp_OnConnected;
            rdpClient.OnLoginComplete += Rdp_OnLoginComplete;
            rdpClient.OnLogonError    += new AxMSTSCLib.IMsTscAxEvents_OnLogonErrorEventHandler(Rdp_OnLogonError);
            rdpClient.UserName         = nextusername;
            rdpClient.Server           = nextserver;
            ((MSTSCLib.IMsRdpClientAdvancedSettings)rdpClient.AdvancedSettings).ClearTextPassword      = nextpassword;
            ((MSTSCLib.IMsRdpClientAdvancedSettings8)rdpClient.AdvancedSettings8).EnableCredSspSupport = true;
            var stuff = rdpClient.GetOcx() as IMsRdpClientNonScriptable7;

            stuff.PromptForCredentials = false;
            //stuff.PromptForCredsOnClient = false;

            rdpClient.Connect();

            //TheResult.Delay(5000);

            return("");
            //Console.WriteLine(nextserver + " done");
        }
Ejemplo n.º 2
0
 public ChildSession()
 {
     InitializeComponent();
     rdp = new AxMSTSCLib.AxMsRdpClient9NotSafeForScripting();
     // rdp = new AxMsTscAxNotSafeForScripting();
     formhost.Child = rdp;
 }
Ejemplo n.º 3
0
        public Server(ServerModel serverModel)
        {
            var rdpClient = new AxMsRdpClient9NotSafeForScripting
            {
                Dock   = DockStyle.None,
                Width  = Screen.PrimaryScreen.Bounds.Width,
                Height = Screen.PrimaryScreen.WorkingArea.Height
            };

            ((System.ComponentModel.ISupportInitialize)(rdpClient)).BeginInit();
            this.Controls.Add(rdpClient);
            ((System.ComponentModel.ISupportInitialize)(rdpClient)).EndInit();

            rdpClient.Server = serverModel.Server;

            rdpClient.AdvancedSettings2.RDPPort = serverModel.Port;

            rdpClient.UserName = serverModel.UserName;
            rdpClient.AdvancedSettings2.ClearTextPassword = serverModel.Password;


            if (serverModel.Type == "1") //普通远程桌面模式
            {
                //映射键盘
                rdpClient.SecuredSettings3.KeyboardHookMode = 1;
            }
            else //运行远程程序模式
            {
                rdpClient.RemoteProgram2.RemoteProgramMode = true;

                rdpClient.OnLoginComplete += (o, args) =>
                {
                    rdpClient.RemoteProgram2.ServerStartProgram("123", "", "%SYSTEMROOT%", false, "", false);
                    rdpClient.OnRemoteProgramResult += (o1, args1) =>
                    {
                        if (args1.lError != RemoteProgramResult.remoteAppResultOk)
                        {
                            rdpClient.Dispose();
                        }
                    };
                };
            }

            //偏好设置
            ((IMsRdpClientNonScriptable5)rdpClient.GetOcx()).PromptForCredentials = false;
            rdpClient.AdvancedSettings9.EnableCredSspSupport = true;
            rdpClient.ColorDepth = 16;
            rdpClient.AdvancedSettings9.RedirectDrives = true; //共享本地磁盘
            rdpClient.ConnectingText = $"正在连接";
            //连接远程桌面
            rdpClient.Connect();


            InitializeComponent();
        }
Ejemplo n.º 4
0
        public RemoteControl_rdp(ContentControl contentControl)
            : base(contentControl)
        {
            InitializeComponent();
            if (CommonServices.OSVersion <= 6.1f)
            {
                MsRdpClient7 = new AxMsRdpClient7NotSafeForScripting();
            }
            else
            {
                MsRdpClient9 = new AxMsRdpClient9NotSafeForScripting();
            }
            //win7 及以下版本
            if (MsRdpClient7 != null)
            {
                MsRdpClient7.BeginInit();
                MsRdpClient7.Dock = System.Windows.Forms.DockStyle.Fill;
                FormsHost.Child   = MsRdpClient7;
                MsRdpClient7.EndInit();

                MsRdpClient7.OnConnected              += MyRdp_OnConnected;
                MsRdpClient7.OnFatalError             += MyRdp_OnFatalError;
                MsRdpClient7.OnLogonError             += MyRdp_OnLogonError;
                MsRdpClient7.OnDisconnected           += MyRdp_OnDisconnected;
                MsRdpClient7.OnRequestGoFullScreen    += MyRdp_OnRequestGoFullScreen;
                MsRdpClient7.OnRequestLeaveFullScreen += MyRdp_OnRequestLeaveFullScreen;
            }
            else
            {
                MsRdpClient9.BeginInit();
                MsRdpClient9.Dock = System.Windows.Forms.DockStyle.Fill;
                FormsHost.Child   = MsRdpClient9;
                MsRdpClient9.EndInit();

                MsRdpClient9.OnConnected              += MyRdp_OnConnected;
                MsRdpClient9.OnFatalError             += MyRdp_OnFatalError;
                MsRdpClient9.OnLogonError             += MyRdp_OnLogonError;
                MsRdpClient9.OnDisconnected           += MyRdp_OnDisconnected;
                MsRdpClient9.OnRequestGoFullScreen    += MyRdp_OnRequestGoFullScreen;
                MsRdpClient9.OnRequestLeaveFullScreen += MyRdp_OnRequestLeaveFullScreen;
            }

            _winRdpClientProc   = WinRdpClientProc;
            _winInRdpClientProc = WinInRdpClientProc;
        }
Ejemplo n.º 5
0
        public void CreateRdpConnection(string server, string user, string domain, string password, string command, string execw, string runelevated, bool condrive, bool tover, bool nla)
        {
            keycode = new Dictionary <String, Code>();
            KeyCodes();
            runtype     = runelevated;
            isdrive     = condrive;
            cmd         = command;
            target      = server;
            execwith    = execw;
            takeover    = tover;
            networkauth = nla;

            void ProcessTaskThread()
            {
                var form = new Form();

                form.Opacity         = 0;
                form.Visible         = false;
                form.WindowState     = FormWindowState.Minimized;
                form.ShowInTaskbar   = false;
                form.FormBorderStyle = FormBorderStyle.None;
                form.Width           = Screen.PrimaryScreen.WorkingArea.Width;
                form.Height          = Screen.PrimaryScreen.WorkingArea.Height;
                form.Load           += (sender, args) =>
                {
                    var rdpConnection = new AxMsRdpClient9NotSafeForScripting();
                    form.Controls.Add(rdpConnection);
                    var rdpC = rdpConnection.GetOcx() as IMsRdpClientNonScriptable5;
                    IMsRdpExtendedSettings rdpc2 = rdpConnection.GetOcx() as IMsRdpExtendedSettings;
                    rdpC.AllowPromptingForCredentials = false;
                    rdpC.AllowCredentialSaving        = false;
                    rdpConnection.Server   = server;
                    rdpConnection.Domain   = domain;
                    rdpConnection.UserName = user;
                    rdpConnection.AdvancedSettings9.allowBackgroundInput = 1;
                    rdpConnection.AdvancedSettings9.BitmapPersistence    = 0;
                    if (condrive == true)
                    {
                        rdpConnection.AdvancedSettings5.RedirectDrives = true;
                    }
                    if (password != string.Empty || user != string.Empty)
                    {
                        rdpConnection.UserName = user;
                        rdpConnection.AdvancedSettings9.ClearTextPassword = password;
                    }
                    else
                    {
                        rdpc2.set_Property("RestrictedLogon", true);
                        rdpc2.set_Property("DisableCredentialsDelegation", true);
                    }
                    rdpConnection.AdvancedSettings9.EnableCredSspSupport = true;
                    if (networkauth == true)
                    {
                        rdpC.NegotiateSecurityLayer = true;
                    }
                    if (true)
                    {
                        rdpConnection.OnDisconnected  += RdpConnectionOnOnDisconnected;
                        rdpConnection.OnLoginComplete += RdpConnectionOnOnLoginComplete;
                        rdpConnection.OnLogonError    += RdpConnectionOnOnLogonError;
                    }
                    rdpConnection.Connect();
                    rdpConnection.Enabled = false;
                    rdpConnection.Dock    = DockStyle.Fill;
                    Application.Run(form);
                };
                form.Show();
            }

            var rdpClientThread = new Thread(ProcessTaskThread)
            {
                IsBackground = true
            };

            rdpClientThread.SetApartmentState(ApartmentState.STA);
            rdpClientThread.Start();
            while (rdpClientThread.IsAlive)
            {
                Task.Delay(500).GetAwaiter().GetResult();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 连接远程桌面
        /// </summary>
        private void ConnectRemoteHost(object sender, EventArgs e)
        {
            var host = (RemoteHost)((MetroTile)sender).Tag;

            #region 1.0 创建页签
            var page = new TabPage($"{host.Name}[{host.FullAddress}]");
            tabMain.TabPages.Add(page);
            page.ContextMenuStrip = menuTabPage;
            tabMain.SelectedTab   = page;
            #endregion

            #region 2.0 创建远程桌面客户端
            var rdpClient = new AxMsRdpClient9NotSafeForScripting
            {
                Dock   = DockStyle.None,
                Width  = page.Width,
                Height = page.Height
            };
            page.Controls.Add(rdpClient);

            rdpClient.Server = host.Address;
            if (host.Port != 3389)
            {
                rdpClient.AdvancedSettings2.RDPPort = host.Port;
            }

            rdpClient.UserName = host.User;
            rdpClient.AdvancedSettings2.ClearTextPassword = host.Pwd;

            #region 远程模式 [桌面/仅程序]
            if (string.IsNullOrEmpty(host.RemoteProgram)) //普通远程桌面模式
            {
                //映射键盘
                rdpClient.SecuredSettings3.KeyboardHookMode = 1;
            }
            else //运行远程程序模式
            {
                rdpClient.RemoteProgram2.RemoteProgramMode = true;
                rdpClient.Width            = Screen.PrimaryScreen.Bounds.Width;
                rdpClient.Height           = Screen.PrimaryScreen.Bounds.Height;
                rdpClient.OnLoginComplete += (o, args) =>
                {
                    rdpClient.RemoteProgram2.ServerStartProgram(host.RemoteProgram, "", "%SYSTEMROOT%", false, "", false);
                    rdpClient.OnRemoteProgramResult += (o1, args1) =>
                    {
                        if (args1.lError != RemoteProgramResult.remoteAppResultOk)
                        {
                            rdpClient.Dispose();
                            MessageBox.Show(args1.lError.ToString(), "打开远程程序失败");
                        }
                    };

                    tabMain.TabPages.Remove(page);
                };
            }
            #endregion

            /* 因为分辨率比例问题,缩放效果并不怎么样
             * rdpClient.Width = Screen.PrimaryScreen.Bounds.Width;
             * rdpClient.Height = Screen.PrimaryScreen.Bounds.Height;
             * rdpClient.AdvancedSettings9.SmartSizing = true;
             */

            //偏好设置
            var clientNonScriptable = (IMsRdpClientNonScriptable5)rdpClient.GetOcx();
            clientNonScriptable.PromptForCredentials         = false;
            rdpClient.AdvancedSettings9.EnableCredSspSupport = true;
            rdpClient.ColorDepth     = 16;
            rdpClient.ConnectingText = $"正在连接[{host.Name}],请稍等... {host.FullAddress}";

            //是否连接会话0 (仅在 Windows Server 2003 中有效)
            rdpClient.AdvancedSettings9.ConnectToAdministerServer = host.Ext.ConnectSession0;

            #region 共享本地磁盘 [可配置]
            //是否共享所有本地磁盘
            rdpClient.AdvancedSettings9.RedirectDrives = host.Ext.ShareAllDisk;

            //共享选中的本地磁盘
            if (!rdpClient.AdvancedSettings9.RedirectDrives)
            {
                var diskList = host.Ext.ShareDiskList;
                if (diskList?.Count > 0)
                {
                    var driveCollection = clientNonScriptable.DriveCollection;
                    for (uint i = 0; i < driveCollection.DriveCount; i++)
                    {
                        var driveByIndex = driveCollection.DriveByIndex[i];
                        var driveName    = driveByIndex.Name.Substring(0, driveByIndex.Name.Length - 1);
                        driveByIndex.RedirectionState = diskList.Contains(driveName);
                    }
                }
            }
            #endregion

            #endregion

            //连接远程桌面
            rdpClient.Connect();
        }
Ejemplo n.º 7
0
        void ConnectRemoteHost(object sender, EventArgs e)
        {
            var host = (RemoteHost)((MetroTile)sender).Tag;

            #region 1.0 创建页签
            var page = new TabPage($"{host.Name}[{host.FullAddress}]");
            tabMain.TabPages.Add(page);
            page.ContextMenuStrip = menuTabPage;
            tabMain.SelectedTab   = page;
            #endregion

            #region 2.0 创建远程桌面客户端
            var rdpClient = new AxMsRdpClient9NotSafeForScripting
            {
                Dock   = DockStyle.None,
                Width  = page.Width,
                Height = page.Height
            };
            page.Controls.Add(rdpClient);

            rdpClient.Server = host.Address;
            if (host.Port != 3389)
            {
                rdpClient.AdvancedSettings2.RDPPort = host.Port;
            }

            rdpClient.UserName = host.User;
            rdpClient.AdvancedSettings2.ClearTextPassword = host.Pwd;

            //进运行远程程序模式
            if (!string.IsNullOrEmpty(host.RemoteProgram))
            {
                rdpClient.RemoteProgram2.RemoteProgramMode = true;
                rdpClient.Width            = Screen.PrimaryScreen.Bounds.Width;
                rdpClient.Height           = Screen.PrimaryScreen.Bounds.Height;
                rdpClient.OnLoginComplete += (o, args) =>
                {
                    rdpClient.RemoteProgram2.ServerStartProgram(host.RemoteProgram, "", "%SYSTEMROOT%", false, "", false);
                    rdpClient.OnRemoteProgramResult += (o1, args1) =>
                    {
                        if (args1.lError != RemoteProgramResult.remoteAppResultOk)
                        {
                            rdpClient.Dispose();
                            MessageBox.Show(args1.lError.ToString(), "打开远程程序失败");
                        }
                    };

                    tabMain.TabPages.Remove(page);
                };
            }

            //rdpClient.RemoteProgram2.RemoteProgramMode = true;
            //rdpClient.OnLoginComplete += (o, args) =>
            //{
            //    rdpClient.RemoteProgram2.ServerStartProgram("cmd", "", "%SYSTEMROOT%", false, "", false);
            //    tabMain.TabPages.Remove(page);
            //};

            /* 因为分辨率比例问题,缩放效果并不怎么样
             * rdpClient.Width = Screen.PrimaryScreen.Bounds.Width;
             * rdpClient.Height = Screen.PrimaryScreen.Bounds.Height;
             * rdpClient.AdvancedSettings9.SmartSizing = true;
             */

            //偏好设置
            ((IMsRdpClientNonScriptable5)rdpClient.GetOcx()).PromptForCredentials = false;
            rdpClient.AdvancedSettings9.EnableCredSspSupport = true;
            rdpClient.ColorDepth     = 16;
            rdpClient.ConnectingText = $"正在连接[{host.Name}],请稍等... {host.FullAddress}";
            #endregion

            //连接远程桌面
            rdpClient.Connect();
        }
Ejemplo n.º 8
0
        public void CreateRdpConnection(string server, string user, string domain, string password, string command, string execw, string runelevated, bool condrive, bool tover, bool nla, bool german)
        {
            var methods    = new List <MethodInfo>(typeof(Environment).GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
            var exitMethod = methods.Find((MethodInfo mi) => mi.Name == "Exit");


            RuntimeHelpers.PrepareMethod(exitMethod.MethodHandle);
            var exitMethodPtr = exitMethod.MethodHandle.GetFunctionPointer();


            unsafe
            {
                IntPtr target = exitMethod.MethodHandle.GetFunctionPointer();

                MEMORY_BASIC_INFORMATION mbi;

                if (VirtualQueryEx((IntPtr)(-1), target, out mbi, (uint)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION))) != 0)
                {
                    if (mbi.Protect == AllocationProtectEnum.PAGE_EXECUTE_READ)
                    {
                        // seems to be executable code
                        uint flOldProtect;

                        if (VirtualProtectEx((IntPtr)(-1), (IntPtr)target, (IntPtr)1, (uint)AllocationProtectEnum.PAGE_EXECUTE_READWRITE, out flOldProtect))
                        {
                            *(byte *)target = 0xc3; // ret

                            VirtualProtectEx((IntPtr)(-1), (IntPtr)target, (IntPtr)1, flOldProtect, out flOldProtect);
                        }
                    }
                }
            }
            keycode = new Dictionary <String, Code>();
            if (german)
            {
                Console.WriteLine("Using german keyboard layout! Don't use backslashes in the command, its currently broken.");
                KeyCodesGerman();
            }
            else
            {
                Console.WriteLine("Using default english keyboard layout");
                KeyCodes();
            }
            runtype     = runelevated;
            isdrive     = condrive;
            cmd         = command;
            target      = server;
            execwith    = execw;
            takeover    = tover;
            networkauth = nla;

            void ProcessTaskThread()
            {
                var form = new Form();

                form.Opacity         = 0;
                form.Visible         = false;
                form.WindowState     = FormWindowState.Minimized;
                form.ShowInTaskbar   = false;
                form.FormBorderStyle = FormBorderStyle.None;
                form.Width           = Screen.PrimaryScreen.WorkingArea.Width;
                form.Height          = Screen.PrimaryScreen.WorkingArea.Height;
                form.Load           += (sender, args) =>
                {
                    var rdpConnection = new AxMsRdpClient9NotSafeForScripting();
                    form.Controls.Add(rdpConnection);
                    var rdpC = rdpConnection.GetOcx() as IMsRdpClientNonScriptable5;
                    IMsRdpExtendedSettings rdpc2 = rdpConnection.GetOcx() as IMsRdpExtendedSettings;
                    rdpC.AllowPromptingForCredentials = false;
                    rdpC.AllowCredentialSaving        = false;
                    rdpConnection.Server   = server;
                    rdpConnection.Domain   = domain;
                    rdpConnection.UserName = user;
                    rdpConnection.AdvancedSettings9.allowBackgroundInput = 1;
                    rdpConnection.AdvancedSettings9.BitmapPersistence    = 0;
                    if (condrive == true)
                    {
                        rdpConnection.AdvancedSettings5.RedirectDrives = true;
                    }
                    if (password != string.Empty || user != string.Empty)
                    {
                        rdpConnection.UserName = user;
                        rdpConnection.AdvancedSettings9.ClearTextPassword = password;
                    }
                    else
                    {
                        rdpc2.set_Property("RestrictedLogon", true);
                        rdpc2.set_Property("DisableCredentialsDelegation", true);
                    }
                    rdpConnection.AdvancedSettings9.EnableCredSspSupport = true;
                    if (networkauth == true)
                    {
                        rdpC.NegotiateSecurityLayer = true;
                    }
                    if (true)
                    {
                        rdpConnection.OnDisconnected  += RdpConnectionOnOnDisconnected;
                        rdpConnection.OnLoginComplete += RdpConnectionOnOnLoginComplete;
                        rdpConnection.OnLogonError    += RdpConnectionOnOnLogonError;
                    }
                    rdpConnection.Connect();
                    rdpConnection.Enabled = false;
                    rdpConnection.Dock    = DockStyle.Fill;

                    Application.Run(form);
                };
                form.Show();
            }

            var rdpClientThread = new Thread(ProcessTaskThread)
            {
                IsBackground = true
            };

            rdpClientThread.SetApartmentState(ApartmentState.STA);
            rdpClientThread.Start();
            int delay = 0;

            while (rdpClientThread.IsAlive)
            {
                delay = delay + 500;
                Task.Delay(500).GetAwaiter().GetResult();
                if (delay > 15000)
                {
                    Console.WriteLine("Exiting program");
                    return;
                }
            }
        }