-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRdpCrypt.pas
236 lines (203 loc) · 6.42 KB
/
RdpCrypt.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
unit RdpCrypt;
interface
uses
Windows, SysUtils;
const
CRYPTPROTECT_LOCAL_MACHINE = $4;
CRYPTPROTECT_UI_FORBIDDEN = $1;
type
PVOID = Pointer;
{$EXTERNALSYM PVOID}
PPVOID = ^PVOID;
{$EXTERNALSYM PPVOID}
PVOID64 = Pointer;
{$EXTERNALSYM PVOID64}
type
PBOOL = {$IFDEF USE_DELPHI_TYPES} Windows.PBOOL {$ELSE} ^BOOL {$ENDIF};
{$EXTERNALSYM PBOOL}
LPBOOL = {$IFDEF USE_DELPHI_TYPES} Windows.PBOOL {$ELSE} ^BOOL {$ENDIF};
{$EXTERNALSYM LPBOOL}
PBYTE = {$IFDEF USE_DELPHI_TYPES} Windows.PBYTE {$ELSE} ^Byte {$ENDIF};
{$EXTERNALSYM PBYTE}
LPBYTE = {$IFDEF USE_DELPHI_TYPES} Windows.PBYTE {$ELSE} ^Byte {$ENDIF};
{$EXTERNALSYM LPBYTE}
type
_CRYPTOAPI_BLOB = record
cbData: DWORD;
pbData: PBYTE; //LPBYTE
end;
PDATA_BLOB = ^_CRYPTOAPI_BLOB;
DATA_BLOB = _CRYPTOAPI_BLOB;
type
PCRYPTPROTECT_PROMPTSTRUCT = ^CRYPTPROTECT_PROMPTSTRUCT;
{$EXTERNALSYM PCRYPTPROTECT_PROMPTSTRUCT}
_CRYPTPROTECT_PROMPTSTRUCT = record
cbSize: DWORD;
dwPromptFlags: DWORD;
hwndApp: HWND;
szPrompt: LPCWSTR;
end;
{$EXTERNALSYM _CRYPTPROTECT_PROMPTSTRUCT}
CRYPTPROTECT_PROMPTSTRUCT = _CRYPTPROTECT_PROMPTSTRUCT;
{$EXTERNALSYM CRYPTPROTECT_PROMPTSTRUCT}
TCryptProtectPromptStruct = CRYPTPROTECT_PROMPTSTRUCT;
PCryptProtectPromptStruct = PCRYPTPROTECT_PROMPTSTRUCT;
function CryptProtectData(pDataIn: PDATA_BLOB; szDataDescr: LPCWSTR;
pOptionalEntropy: PDATA_BLOB; pvReserved: PVOID;
pPromptStruct: PCRYPTPROTECT_PROMPTSTRUCT; dwFlags: DWORD;
pDataOut: PDATA_BLOB): BOOL; stdcall; external 'Crypt32.dll';
function CryptUnprotectData(pDataIn: PDATA_BLOB; ppszDataDescr: LPWSTR;
pOptionalEntropy: PDATA_BLOB; pvReserved: PVOID;
pPromptStruct: PCRYPTPROTECT_PROMPTSTRUCT; dwFlags: DWORD;
pDataOut: PDATA_BLOB): BOOL; stdcall; external 'Crypt32.dll';
function CryptRDPPassword(sPassword: string): string;
function DecryptRDPPassword(sPasswordHash: string): string;
function BlobDataToHexStr(P: PByte; I: Integer): string;
function PasswordHashToBlobData(sPasswordHash: string): DATA_BLOB;
implementation
{ *********************************************************** }
{ HexToByte: Converts Hex value to Byte }
{ Found this somewhere on the internet }
{ *********************************************************** }
function HexToByte(s: String): Byte;
const
cs = '0123456789ABCDEF';
begin
if (length(s) = 2) and
(AnsiChar(s[1]) in ['0' .. '9', 'A' .. 'F']) and
(AnsiChar(s[2]) in ['0' .. '9', 'A' .. 'F']) then
result := ((pos(s[1], cs) - 1) * 16) + (pos(s[2], cs) - 1)
else
raise EConvertError.CreateFmt('%s is not a Hexformatstring', [s]);
end;
{ *********************************************************** }
{ PasswordHashToBlobData: Converts a RDP password Hash to }
{ a DATA_BLOB structure }
{ sPasswordHash : RDP Password Hash (HEX String }
{ *********************************************************** }
function PasswordHashToBlobData(sPasswordHash: string): DATA_BLOB;
var
Buf: array of Byte;
dwBufSize: Cardinal;
I: Cardinal;
j: Cardinal;
dwHashSize: Cardinal;
begin
dwBufSize := length(sPasswordHash) DIV 2;
dwHashSize := length(sPasswordHash);
SetLength(Buf, dwBufSize);
I := 1;
j := 0;
while I < dwHashSize do
begin
Buf[j] := HexToByte(sPasswordHash[I] + sPasswordHash[I + 1]);
Inc(I, 2);
Inc(j);
end;
GetMem(result.pbData, dwBufSize);
result.cbData := dwBufSize;
result.pbData := PByte(Buf);
end;
{ *********************************************************** }
{ BlobDataToHexStr: Converts a PByte from a DATA_BLOB }
{ to a Hex String so it can be saved in }
{ an RDP file }
{ P : PByte (pbData) from DATA_BLOB }
{ I : Integer (cbData) from DATA_BLOB }
{ *********************************************************** }
function BlobDataToHexStr(P: PByte; I: Integer): string;
var
HexStr: string;
begin
HexStr := '';
while (I > 0) do
begin
Dec(I);
HexStr := HexStr + IntToHex(P^, 2);
Inc(P);
end;
result := HexStr;
end;
{ *********************************************************** }
{ CryptRDPPassword: Converts a plaintext password to }
{ encrypted password hash }
{ an RDP file }
{ sPassword: plaintext password }
{ *********************************************************** }
function CryptRDPPassword(sPassword: string): string;
var
DataIn: DATA_BLOB;
DataOut: DATA_BLOB;
pwDescription: PWideChar;
PwdHash: string;
begin
PwdHash := '';
DataOut.cbData := 0;
DataOut.pbData := nil;
// RDP uses UniCode
DataIn.pbData := Pointer(WideString(sPassword));
DataIn.cbData := length(sPassword) * SizeOf(WChar);
// RDP always sets description to psw
pwDescription := WideString('psw');
if CryptProtectData(@DataIn,
pwDescription,
nil,
nil,
nil,
CRYPTPROTECT_UI_FORBIDDEN, // Never show interface
@DataOut) then
begin
PwdHash := BlobDataToHexStr(DataOut.pbData, DataOut.cbData);
end;
result := PwdHash;
// Cleanup
LocalFree(Cardinal(DataOut.pbData));
LocalFree(Cardinal(DataIn.pbData));
end;
{ *********************************************************** }
{ DecryptRDPPassword: Converts an RDP Password Hash back }
{ to it's original password. }
{ Note that this only works for the user }
{ who encrypted the password (or on the }
{ same computer in case it was encrypted }
{ with the computerkey }
{ sPasswordHash: Password hash (string) }
{ *********************************************************** }
function DecryptRDPPassword(sPasswordHash: string): string;
var
DataIn: DATA_BLOB;
DataOut: DATA_BLOB;
sPassword: string;
pwDecrypted: PWideChar;
pwDescription: PWideChar;
begin
DataIn := PasswordHashToBlobData(sPasswordHash);
DataOut.cbData := 0;
DataOut.pbData := nil;
if CryptUnprotectData(@DataIn,
@pwDescription,
nil,
nil,
nil,
CRYPTPROTECT_UI_FORBIDDEN, // Never show interface
@DataOut) then
begin
GetMem(pwDecrypted, DataOut.cbData);
lstrcpynW(pwDecrypted, PWideChar(DataOut.pbData),
(DataOut.cbData DIV 2) + 1);
sPassword := pwDecrypted;
FreeMem(pwDecrypted);
end
else
begin
raise EConvertError.CreateFmt('Error decrypting: %s',
[SysErrorMessage(GetLastError)]);
end;
result := sPassword;
// Cleanup
if DataOut.cbData > 0 then
begin
LocalFree(Cardinal(DataOut.pbData));
end;
end;
end.