LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】远程桌面(mstsc.exe)带IP地址/端口/用户名/密码等参数实现无需输入密码而自动登录

admin
2021年6月17日 22:24 本文热度 4757

【C#代码】

Process rdcProcess = new Process();

//使用Powershell自动保存此IP远程桌面的登录用户名和密码

rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe");

rdcProcess.StartInfo.Arguments = "/generic:TERMSRV/192.168.0.217 /user:" + "username" + " /pass:" + "password";

rdcProcess.Start();

//加载远程桌面

rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\mstsc.exe");

rdcProcess.StartInfo.Arguments = "/v: 192.168.0.217:3389 /console";        //ip or name of computer to connect

rdcProcess.Start();

//使用Powershell删除刚刚保存此IP桌面的登录用户名和密码

rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe");

rdcProcess.StartInfo.Arguments = "/delete:TERMSRV/192.168.0.217";

rdcProcess.Start();


该文章在 2021/6/17 22:49:57 编辑过

全部评论3

admin
2021年6月17日 22:43

mstsc保存用户名和密码,实现自动登录远程桌面

1. 生成密码加密的字符串
目前根据网上提供的工具生成加密字符串。百度搜索关键字“rdp 密码生成工具”找出任一款开生成就行了。
原本是打算用c#来写的,年底工作的原因,有空再补上。

2. 在rdp文件中增加password
test.rdp
screen mode id:i:2
use multimon:i:0
desktopwidth:i:1920
desktopheight:i:1080
session bpp:i:32
winposstr:s:0,3,0,0,800,600
compression:i:1
keyboardhook:i:2
audiocapturemode:i:0
videoplaybackmode:i:1
connection type:i:2
displayconnectionbar:i:1
disable wallpaper:i:1
allow font smoothing:i:0
allow desktop composition:i:0
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:0
disable cursor setting:i:0
bitmapcachepersistenable:i:1
full address:s:192.168.1.101
audiomode:i:0
redirectprinters:i:1
redirectcomports:i:0
redirectsmartcards:i:1
redirectclipboard:i:1
redirectposdevices:i:0
redirectdirectx:i:1
autoreconnection enabled:i:1
authentication level:i:2
prompt for credentials:i:0
negotiate security layer:i:1
remoteapplicationmode:i:0
alternate shell:s:
shell working directory:s:
gatewayhostname:s:
gatewayusagemethod:i:4
gatewaycredentialssource:i:4
gatewayprofileusagemethod:i:0
promptcredentialonce:i:1
use redirection server name:i:0
password b:01000000D08C9DDF0115D1118C7A00C04FC297EB01000000F9A314A95F5EBE41B579FC4CDD40DFE504000000080000007000730077000000106600000001000020000000D0EB70938EA9F30E60CF101EFEFD6BA329B169AB7908F1C986C38AD84E790E72000000000E800000000200002100000095679C4EFC19C8D1376E17CEB7766175E486B461F417D2E7933E792458FCF3372000000093FDA94662FA67647815EB619C0245C068B0E0B3BC7E5C7D2F8CB35F94B555E4400000001D98F83DDEBC09A0152075835C82C62D13B941BA5362C60FEA92E35415AA29BAD1C089B2E6FA67F510822E48B631E327972CAC838C68BB2286171C371D8A3E48
full address 需要远程的地址;
password 远程的密码

3. 编写启动脚本
mstsc E:/test.rdp /console /v: 192.168.1.101:80
admin
2021年6月17日 22:53
 

远程桌面,RDP文件密码加密、解密算法(C#)


背景:由于项目需要,使用RDP文件来远程登录,需要实现点击rdp文件就可以自动连接远程桌面,并且实现自动登录功能!自动登录!自动登录!
自动登录:密码需要经过加密,本文的核心!!!废话少说,看代码!

1、首先添加引用是必须的!
      using System;
      using System.Security.Cryptography;

2、核心算法
    using System;
    using System.Security.Cryptography;

     static byte[] s_aditionalEntropy = null;         //附加的加密因子,自定义

        private void test()
        {
            string plainText = "qweR+-*yuioP0";
            byte[] secret = Encoding.Unicode.GetBytes(plainText);
            byte[] encryptedSecret = Protect(secret);
            Console.WriteLine("The encrypted byte array is:");
            string res = string.Empty;
            foreach(byte b in encryptedSecret)
            {
                res += b.ToString("X2");                          //炒鸡坑爹的,转换16进制的一定要用2位,不然就像我一样被坑了半个多月
            }
            
            Console.WriteLine("加密之后的密码:" + res);
            PrintValues(encryptedSecret);

            // Decrypt the data and store in a byte array.
            byte[] originalData = Unprotect(encryptedSecret);
            Console.WriteLine("{0}The original data is:", Environment.NewLine);
            string str = Encoding.Default.GetString(originalData);
            Console.WriteLine("解密之后的密码: " + str);
            PrintValues(originalData);
        }

        //加密方法
        public static byte[] Protect(byte[] data)
        {
            try
            {
                // Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted
                //  only by the same current user.
                return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("Data was not encrypted. An error occurred.");
                Console.WriteLine(e.ToString());
                return null;
            }
        }

        //解密方法
        public static byte[] Unprotect(byte[] data)
        {
            try
            {
                //Decrypt the data using DataProtectionScope.CurrentUser.
                return ProtectedData.Unprotect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("Data was not decrypted. An error occurred.");
                Console.WriteLine(e.ToString());
                return null;
            }
        }

        public static void PrintValues(Byte[] myArr)
        {
            foreach (Byte i in myArr)
            {
                Console.Write("\t{0}", i);
            }
            Console.WriteLine();
        }

3、总结!
     生成的字节数组直接转换成16进制输出,添加到rdp文件的密码里面就会解析不出来。
     坑爹的转换成16进制时需要占位2位,不然就出错,弄了N久才搞定,发文共勉!转载请注明出处,谢谢!

4、附RDP文件的一些注释(转)
screen mode id:i:1 
desktopwidth:i:1280 
desktopheight:i:750 
session bpp:i:24 
winposstr:s:2,3,188,8,1062,721 
full address:s:MyServer    //服务器地址 一般填IP地址
compression:i:1 
keyboardhook:i:2 
audiomode:i:0 
redirectdrives:i:0 
redirectprinters:i:0 
redirectcomports:i:0 
redirectsmartcards:i:0 
displayconnectionbar:i:1 
autoreconnection enabled:i:1  
username:s:MyUserName   //用户名
domain:s:MyDomain    //域名 可以不填。
alternate shell:s: 
shell working directory:s: 
password 51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB0100000052A9E191EA75A948B359790578C9371A0000000008000000700073007700000003660000A8000000100000000A1DCCD2E50775CA25EC3857164B34DC0000000004800000A000000010000000FCE1A645B9B61AA450946BB6F955058108020000D83591CA47562D6DDAA689F050AE145039EBE22E00D1D3AEAA98373C7B63C3E8E7149072DF989EA43EFCE20513AD3D27B11BE7F17066A688E1DCE828AF85460AAC327B38E90776DB962888E4393D19637578984B19A187AAD95F6D2726ADE7DD315FF56C15FF5B3031014EDDCC3C24D1B81779AFDB006EE575F5BEFB8D2D2138D9D9D642BBB251CC5ED7226968764856EC660A646BACE748A13D6002A9A537AA70710615650B9387EED66DE28BD57B304BBDD7B581B943DA628EB0289E30A8BA784B76F7885BECCAB4FEF7820E97EE3C6E036EEAF6EAA669288DF2FCACC9BEC045C907EBBDE87AFB8CC6B07A600BD63AC891B61D95C2265DD9FD5E635D61BFBF5EDC28311375066611C610FB533D64515B643C82F57D9B183B05C156D91BC0974D38E546022B139E82452E6F1EDF76E52F732C3904E5E433F8F3D488DB0698427DBB0791A9F207F8CB6654CB8410BAF4A59C4F9E821E589ABC1E6E6E1D432181B690408F6884FE1007895A4D26D4A5A2C7458EE747DA35D44AC9FB08AB5477EA3E7CCDB3E37EE20FAFD0D0CF9584E420598B7003B347943AC28048F45E0FD21AD08148FFADCE0E7877219259A7BE722FFAE845A429BA2CF0A71F2D19EA7495530FABDB5106E8D404A38A7E6394C38457640EA7398C5D55F0C4D342CC6A39C77E10A2A5145AEA40B14F5C7C3760334D83C9BE748383FADE231248537353817D51F7B44F61B406ABC61400000071C354139F458B02D978015F785B97F7F6B307380    //这么一长串就是密码了,“password 51:b:”后面改为你自己生成的密码串
disable wallpaper:i:1 
disable full window drag:i:1 
disable menu anims:i:1 
disable themes:i:0 
disable cursor setting:i:0 
bitmapcachepersistenable:i:1

该评论在 2021/6/17 22:53:52 编辑过
admin
2021年6月17日 23:46

最近由于项目需要,需要做一个rdp文件上成,然后可以直接连远程桌面的功能,在度娘和谷叔搜索一番,所得甚少。闲话少说,来点干货:


看看系统给提供的是啥样的


我们要关心得是 用户名和密码,其他参数可以慢慢了解,可是这个密码是怎么加密的呢?

使用的是一个win32里面一个叫crypt32.dll的CryptProtectData方法,好了,关键的时候来咯~~~~

 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

internal struct DATA_BLOB
{
    public int cbData;
    public IntPtr pbData;
}
 
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct CRYPTPROTECT_PROMPTSTRUCT
{
    public int cbSize;
    public int dwPromptFlags;
    public IntPtr hwndApp;
    public string szPrompt;
}
 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool CryptProtectData(ref DATA_BLOB pDataIn, string szDataDescr, ref DATA_BLOB pOptionalEntropy, IntPtr pvReserved, ref CRYPTPROTECT_PROMPTSTRUCT pPromptStruct, int dwFlags, ref DATA_BLOB pDataOut);

引用一下win32程序为我们生成密码。

private static string Encrypt(string password)
 2 {
 3     byte[] bytes = Encoding.Unicode.GetBytes(password);
 4     DATA_BLOB dATA_BLOB = default(DATA_BLOB);
 5     DATA_BLOB dATA_BLOB2 = default(DATA_BLOB);
 6     DATA_BLOB dATA_BLOB3 = default(DATA_BLOB);
 7     dATA_BLOB.cbData = bytes.Length;
 8     dATA_BLOB.pbData = Marshal.AllocHGlobal(bytes.Length);
 9     Marshal.Copy(bytes, 0, dATA_BLOB.pbData, bytes.Length);
10     dATA_BLOB3.cbData = 0;
11     dATA_BLOB3.pbData = IntPtr.Zero;
12     dATA_BLOB2.cbData = 0;
13     dATA_BLOB2.pbData = IntPtr.Zero;
14     CRYPTPROTECT_PROMPTSTRUCT cRYPTPROTECT_PROMPTSTRUCT = new CRYPTPROTECT_PROMPTSTRUCT
15     {
16         cbSize = Marshal.SizeOf(typeof(CRYPTPROTECT_PROMPTSTRUCT)),
17         dwPromptFlags = 0,
18         hwndApp = IntPtr.Zero,
19         szPrompt = null
20     };
21     if (CryptProtectData(ref dATA_BLOB, "psw", ref dATA_BLOB3, IntPtr.Zero, ref cRYPTPROTECT_PROMPTSTRUCT, 1, ref dATA_BLOB2))
22     {
23          if (IntPtr.Zero != dATA_BLOB.pbData)
24          {
25               Marshal.FreeHGlobal(dATA_BLOB.pbData);
26          }
27          if (IntPtr.Zero != dATA_BLOB3.pbData)
28          {
29               Marshal.FreeHGlobal(dATA_BLOB3.pbData);
30          }
31          byte[] array = new byte[dATA_BLOB2.cbData];
32          Marshal.Copy(dATA_BLOB2.pbData, array, 0, dATA_BLOB2.cbData);
33          return BitConverter.ToString(array).Replace("-", string.Empty);
34     }
35     return string.Empty;
36 }

有密码了,替换掉开始另存为的文件里的密码,我们自己的rdp文件就有咯!

附一个文件内容方法

private static void rdpProfile(string filename, string address, string username, string password, string colordepth)

{

    if (File.Exists(filename))

    {

        File.Delete(filename);

    }

    using (StreamWriter streamWriter = new StreamWriter(filename, true))

    {

        streamWriter.WriteLine("screen mode id:i:2");

        streamWriter.WriteLine("desktopwidth:i:0");

        streamWriter.WriteLine("desktopheight:i:0");

        streamWriter.WriteLine("session bpp:i:" + colordepth);

        streamWriter.WriteLine("winposstr:s:0,1,0,0,1234,792");

        streamWriter.WriteLine("compression:i:1");

        streamWriter.WriteLine("keyboardhook:i:2");

        streamWriter.WriteLine("audiocapturemode:i:0");

        streamWriter.WriteLine("videoplaybackmode:i:1");

        streamWriter.WriteLine("connection type:i:6");

        streamWriter.WriteLine("displayconnectionbar:i:1");

        streamWriter.WriteLine("disable wallpaper:i:1");

        streamWriter.WriteLine("allow font smoothing:i:1");

        streamWriter.WriteLine("allow desktop composition:i:1");

        streamWriter.WriteLine("disable full window drag:i:1");

        streamWriter.WriteLine("disable menu anims:i:1");

        streamWriter.WriteLine("disable themes:i:1");

        streamWriter.WriteLine("disable cursor setting:i:0");

        streamWriter.WriteLine("bitmapcachepersistenable:i:0");

        streamWriter.WriteLine("full address:s:" + address);

        streamWriter.WriteLine("audiomode:i:0");

        streamWriter.WriteLine("redirectprinters:i:0");

        streamWriter.WriteLine("redirectcomports:i:0");

        streamWriter.WriteLine("redirectsmartcards:i:0");

        streamWriter.WriteLine("redirectclipboard:i:1");

        streamWriter.WriteLine("redirectposdevices:i:0");

        streamWriter.WriteLine("redirectdirectx:i:1");

        streamWriter.WriteLine("drivestoredirect:s:");

        streamWriter.WriteLine("autoreconnection enabled:i:1");

        streamWriter.WriteLine("authentication level:i:2");

        streamWriter.WriteLine("prompt for credentials:i:0");

        streamWriter.WriteLine("negotiate security layer:i:1");

        streamWriter.WriteLine("remoteapplicationmode:i:0");

        streamWriter.WriteLine("alternate shell:s:");

        streamWriter.WriteLine("shell working directory:s:");

        streamWriter.WriteLine("gatewayhostname:s:");

        streamWriter.WriteLine("gatewayusagemethod:i:4");

        streamWriter.WriteLine("gatewaycredentialssource:i:4");

        streamWriter.WriteLine("gatewayprofileusagemethod:i:0");

        streamWriter.WriteLine("promptcredentialonce:i:1");

        streamWriter.WriteLine("use redirection server name:i:0");

        streamWriter.WriteLine("use multimon:i:0");

        if (!string.IsNullOrEmpty(username))

        {

            streamWriter.WriteLine("username:s:" + username);

        }

        if (!string.IsNullOrEmpty(password))

        {

            streamWriter.WriteLine("password 51:b:" + password);

        }

    }

}


该评论在 2021/6/17 23:51:49 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved