RemoteDesktopConnection\src\LogInfo.cs
namespace RDP
{
class LogInfo
{
public string Ipaddress { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}
RemoteDesktopConnection\src\Program.cs
#define debug
using System;
using System.Text.RegularExpressions;
namespace RDP
{
class Program
{
static void Main(string[] args)
{
var info = new LogInfo();
#if debug
Console.WriteLine("please enter ipAddress");
while (true)
{
info.Ipaddress = Console.ReadLine();
if (new Regex(RdpConstant.IpaddressPatten).IsMatch(info.Ipaddress))
{
break;
}
}
Console.WriteLine("please enter username");
info.Username = Console.ReadLine();
Console.WriteLine(info.Username);
Console.WriteLine("please enter password");
info.Password = Console.ReadLine();
#else
info.Ipaddress = "120";
info.Username = "Adm";
info.Password = "wu";
#endif
RdpHandler.Rrocess(info);
}
}
}
RemoteDesktopConnection\src\RdpConstant.cs
namespace RDP
{
class RdpConstant
{
public static readonly string IpaddressPatten= @"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$";
public static readonly string FilePath = "rdp.rdp";
public static readonly string templatePath="../../encryption/TemplateRDP.txt";
}
}
RemoteDesktopConnection\src\encryption\DataProtection.cs
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Security;
namespace RDP
{
[Serializable()]
public sealed class DataProtection
{
[Flags()]
public enum CryptProtectPromptFlags
{
CRYPTPROTECT_PROMPT_ON_UNPROTECT = 0x01,
CRYPTPROTECT_PROMPT_ON_PROTECT = 0x02,
CRYPTPROTECT_PROMPT_RESERVED = 0x04,
CRYPTPROTECT_PROMPT_STRONG = 0x08,
CRYPTPROTECT_PROMPT_REQUIRE_STRONG = 0x10
}
[Flags()]
public enum CryptProtectDataFlags
{
CRYPTPROTECT_UI_FORBIDDEN = 0x01,
CRYPTPROTECT_LOCAL_MACHINE = 0x04,
CRYPTPROTECT_CRED_SYNC = 0x08,
CRYPTPROTECT_AUDIT = 0x10,
CRYPTPROTECT_NO_RECOVERY = 0x20,
CRYPTPROTECT_VERIFY_PROTECTION = 0x40,
CRYPTPROTECT_CRED_REGENERATE = 0x80
}
public static string ProtectData(string data, string name)
{
return ProtectData(data, name,
CryptProtectDataFlags.CRYPTPROTECT_UI_FORBIDDEN | CryptProtectDataFlags.CRYPTPROTECT_LOCAL_MACHINE);
}
public static byte[] ProtectData(byte[] data, string name)
{
return ProtectData(data, name,
CryptProtectDataFlags.CRYPTPROTECT_UI_FORBIDDEN | CryptProtectDataFlags.CRYPTPROTECT_LOCAL_MACHINE);
}
public static string ProtectData(string data, string name, CryptProtectDataFlags flags)
{
byte[] dataIn = Encoding.Unicode.GetBytes(data);
byte[] dataOut = ProtectData(dataIn, name, flags);
if (dataOut != null)
return (Convert.ToBase64String(dataOut));
else
return null;
}
private static byte[] ProtectData(byte[] data, string name, CryptProtectDataFlags dwFlags)
{
byte[] cipherText = null;
// copy data into unmanaged memory
DPAPI.DATA_BLOB din = new DPAPI.DATA_BLOB();
din.cbData = data.Length;
din.pbData = Marshal.AllocHGlobal(din.cbData);
if (din.pbData.Equals(IntPtr.Zero))
throw new OutOfMemoryException("Unable to allocate memory for buffer.");
Marshal.Copy(data, 0, din.pbData, din.cbData);
DPAPI.DATA_BLOB dout = new DPAPI.DATA_BLOB();
try
{
bool cryptoRetval = DPAPI.CryptProtectData(ref din, name, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, dwFlags, ref dout);
if (cryptoRetval)
{
int startIndex = 0;
cipherText = new byte[dout.cbData];
Marshal.Copy(dout.pbData, cipherText, startIndex, dout.cbData);
DPAPI.LocalFree(dout.pbData);
}
else
{
int errCode = Marshal.GetLastWin32Error();
StringBuilder buffer = new StringBuilder(256);
Win32Error.FormatMessage(Win32Error.FormatMessageFlags.FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, errCode, 0, buffer, buffer.Capacity, IntPtr.Zero);
}
}
finally
{
if (!din.pbData.Equals(IntPtr.Zero))
Marshal.FreeHGlobal(din.pbData);
}
return cipherText;
}
internal static void InitPromptstruct(ref DPAPI.CRYPTPROTECT_PROMPTSTRUCT ps)
{
ps.cbSize = Marshal.SizeOf(typeof(DPAPI.CRYPTPROTECT_PROMPTSTRUCT));
ps.dwPromptFlags = 0;
ps.hwndApp = IntPtr.Zero;
ps.szPrompt = null;
}
}
[SuppressUnmanagedCodeSecurityAttribute()]
internal class DPAPI
{
[DllImport("crypt32")]
public static extern bool CryptProtectData(ref DATA_BLOB dataIn, string szDataDescr, IntPtr optionalEntropy, IntPtr pvReserved,
IntPtr pPromptStruct, DataProtection.CryptProtectDataFlags dwFlags, ref DATA_BLOB pDataOut);
[DllImport("crypt32")]
public static extern bool CryptUnprotectData(ref DATA_BLOB dataIn, StringBuilder ppszDataDescr, IntPtr optionalEntropy,
IntPtr pvReserved, IntPtr pPromptStruct, DataProtection.CryptProtectDataFlags dwFlags, ref DATA_BLOB pDataOut);
[DllImport("Kernel32.dll")]
public static extern IntPtr LocalFree(IntPtr hMem);
[StructLayout(LayoutKind.Sequential)]
public struct DATA_BLOB
{
public int cbData;
public IntPtr pbData;
}
[StructLayout(LayoutKind.Sequential)]
public struct CRYPTPROTECT_PROMPTSTRUCT
{
public int cbSize; // = Marshal.SizeOf(typeof(CRYPTPROTECT_PROMPTSTRUCT))
public int dwPromptFlags; // = 0
public IntPtr hwndApp; // = IntPtr.Zero
public string szPrompt; // = null
}
}
internal class Win32Error
{
[Flags()]
public enum FormatMessageFlags : int
{
FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x0100,
FORMAT_MESSAGE_IGNORE_INSERTS = 0x0200,
FORMAT_MESSAGE_FROM_STRING = 0x0400,
FORMAT_MESSAGE_FROM_HMODULE = 0x0800,
FORMAT_MESSAGE_FROM_SYSTEM = 0x1000,
FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x2000,
FORMAT_MESSAGE_MAX_WIDTH_MASK = 0xFF,
}
[DllImport("Kernel32.dll")]
public static extern int FormatMessage(FormatMessageFlags flags, IntPtr source, int messageId, int languageId,
StringBuilder buffer, int size, IntPtr arguments);
}
}
RemoteDesktopConnection\src\encryption\Rdp.cs
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace RDP
{
class RdpHandler
{
public static void Rrocess(LogInfo info) {
if (string.IsNullOrEmpty(info.Username) || string.IsNullOrEmpty(info.Password)) {
throw new ArgumentNullException("username and password can't be empty");
}
var pwstr = BitConverter.ToString(DataProtection.ProtectData(Encoding.Unicode.GetBytes(info.Password), "")).Replace("-", "");
var rdpInfo = String.Format(File.ReadAllText(RdpConstant.templatePath), info.Ipaddress, info.Username, pwstr);
File.WriteAllText(RdpConstant.FilePath,rdpInfo);
_mstsc("mstsc " + RdpConstant.FilePath);
}
private static void _mstsc(String cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(cmd);
}
}
}
RemoteDesktopConnection\src\encryption\TemplateRDP.txt
screen mode id:i:2
use multimon:i:0
desktopwidth:i:1600
desktopheight:i:900
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
networkautodetect:i:1
bandwidthautodetect:i:1
displayconnectionbar:i:1
enableworkspacereconnect:i:0
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
audiomode:i:1
redirectprinters:i:1
redirectcomports:i:0
redirectsmartcards:i:1
redirectclipboard:i:1
redirectposdevices:i:0
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:0
use redirection server name:i:0
rdgiskdcproxy:i:0
kdcproxyname:s:
drivestoredirect:s:
redirectdirectx:i:1
full address:s:{0}
username:s:{1}
password 51:b:{2}
标签:info,IntPtr,CRYPTPROTECT,mstsc,static,远程桌面,public,string,rdp
From: https://www.cnblogs.com/zhuoss/p/17999904