-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit1.pas
226 lines (177 loc) · 6.28 KB
/
unit1.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
{ KOL MCK } // Do not remove this line!
{$DEFINE KOL_MCK}
{$ifdef FPC} {$mode delphi} {$endif}
unit Unit1;
interface
uses Windows, Messages, KOL {place your units here->}
{$IFDEF LAZIDE_MCK}, Forms, mirror, Classes, Controls, mckCtrls, mckObjs, Graphics;
{$ELSE} ; {$ENDIF}
type
{ TForm1 }
{$I MCKfakeClasses.inc}
{$IFDEF KOLCLASSES} TForm1 = class; PForm1 = TForm1; {$ELSE OBJECTS} PForm1 = ^TForm1; {$ENDIF CLASSES/OBJECTS}
TForm1 = {$IFDEF KOLCLASSES}class{$ELSE}object{$ENDIF}({$IFDEF LAZIDE_MCK}TForm{$ELSE}TObj{$ENDIF})
Form: PControl;
KOLForm1: TKOLForm;
KOLProject1: TKOLProject;
Label1: TKOLLabel;
Label2: TKOLLabel;
lbLog: TKOLListBox;
MainMenu1: TKOLMainMenu;
pnlContainer: TKOLPanel;
procedure KOLForm1FormCreate(Sender: PObj);
procedure menuExitMenu(Sender: PMenu; Item: Integer);
procedure menuPatchMenu(Sender: PMenu; Item: Integer);
private
{ private declarations }
public
{ public declarations }
procedure Log(S: KOLString);
end;
var
Form1 {$IFDEF KOL_MCK} : PForm1 {$ELSE} : TForm1 {$ENDIF} ;
{$IFDEF KOL_MCK}
procedure NewForm1( var Result: PForm1; AParent: PControl );
{$ENDIF}
implementation
uses
uPatch,
uRegistry;
{$IFDEF KOL_MCK}
{$I unit1_1.inc}
{$ENDIF}
{$O-}
const
opPatch: TOpCode32 = ($FFFF, $A8, $1B, $E9);
type
TRegQueryValueExW = function(hKey:HKEY; lpValueName:LPCWSTR; lpReserved:LPDWORD; lpType:LPDWORD; lpData:pointer;lpcbData:LPDWORD):LONG;
function newCryptVerifySignatureW(hHash: DWORD; pbSignature: PBYTE; dwSigLen: DWORD; hPubKey: DWORD; sDescription: PWideChar; dwFlags: DWORD): BOOL;
begin
Result := True;
end;
function newRegQueryValueExW(hKey:HKEY; lpValueName:LPCWSTR; lpReserved:LPDWORD; lpType:LPDWORD; lpData:pointer;lpcbData:LPDWORD):LONG;
var
c: Byte;
p: PBYTE;
oldRQVE: TRegQueryValueExW;
begin
oldRQVE := TRegQueryValueExW($12345678);
Result := 6; // ERROR_INVALID_HANDLE
if lpValueName <> nil then begin
DWORD(p) := DWORD(lpValueName);
c := 0;
repeat
if p^ = $2D then // '-'
inc(c);
inc(p, 2);
until p^ = 0;
if c = 4 then begin
lpType^ := 3; // REG_BINARY
lpcbData^ := 256; // MAX_LICENSE_LENGTH
Result := 0; // ERROR_SUCCESS
end;
end;
if Result <> 0 then
Result := oldRQVE(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData);
end;
{ TForm1 }
procedure TForm1.menuExitMenu(Sender: PMenu; Item: Integer);
begin
Form.Close;
end;
procedure TForm1.menuPatchMenu(Sender: PMenu; Item: Integer);
const
HEAP_ZERO_MEMORY = $00000008;
HEAP_SHARED_READONLY = $00001000;
var
pHeap: Pointer;
hHeap: THandle;
hCoreDLL: HModule;
totalMem: DWORD;
cur: Pointer;
fNewCryptVerifySignature,
jNewCryptVerifySignature,
fOldCryptVerifySignature: TFuncCode;
aOldCryptVerifySignature,
aNewCryptVerifySignature: Pointer;
fNewRegQueryValueEx,
jNewRegQueryValueEx,
fOldRegQueryValueEx: TFuncCode;
aOldRegQueryValueEx,
aNewRegQueryValueEx: Pointer;
aJumpRegQueryValueEx: Pointer;
tf: TFuncCode;
regType, regSize: DWORD;
begin
SetKMode(TRUE);
SetProcPermissions($FFFFFFFF);
Log('Patching...');
// Ensure "HKCU\Security\Software\Microsoft\Marketplace\Licenses" exists
RegSetDWORD('HKCU\Security\Software\Microsoft\Marketplace\Licenses\dummy', 0);
RegDelete('HKCU\Security\Software\Microsoft\Marketplace\Licenses\dummy');
fNewCryptVerifySignature := GetFunctionCode(@newCryptVerifySignatureW, 1024, opPatch);
fNewRegQueryValueEx := GetFunctionCode(@newRegQueryValueExW, 1024, opPatch);
totalMem := 1024;
hCoreDll := LoadLibrary('coredll.dll');
try
hHeap := HeapCreate(HEAP_SHARED_READONLY, totalMem, totalMem);
if hHeap <> 0 then
try
pHeap := HeapAlloc(hHeap, HEAP_ZERO_MEMORY, totalMem);
if pHeap <> nil then
try
cur := pHeap;
aOldCryptVerifySignature := GetProcAddress(hCoreDLL, 'CryptVerifySignatureW');
aNewCryptVerifySignature := cur;
Move(fNewCryptVerifySignature[0], aNewCryptVerifySignature^, Length(fNewCryptVerifySignature));
jNewCryptVerifySignature := CreateJumpCode(aNewCryptVerifySignature);
DWORD(cur) := DWORD(cur) + Length(fNewCryptVerifySignature);
aOldRegQueryValueEx := GetProcAddress(hCoreDLL, 'RegQueryValueExW');
tf := CreateJumpCode(Pointer(DWORD(aOldRegQueryValueEx) + 8));
SetLength(tf, 16);
Move(tf[0], tf[8], 8);
Move(GetCoreDLLAddress(hCoreDLL, aOldRegQueryValueEx)^, tf[0], 8);
Move(tf[0], cur^, 16);
aJumpRegQueryValueEx := cur;
DWORD(cur) := DWORD(cur) + 16;
aNewRegQueryValueEx := cur;
Move(fNewRegQueryValueEx[0], aNewRegQueryValueEx^, Length(fNewRegQueryValueEx));
jNewRegQueryValueEx := CreateJumpCode(aNewRegQueryValueEx);
DWORD(cur) := DWORD(cur) + Length(fNewRegQueryValueEx);
Move(aJumpRegQueryValueEx, cur^, 4);
DWORD(cur) := DWORD(cur) + 4;
fOldCryptVerifySignature := PatchCode(GetCoreDLLAddress(hCoreDLL, aOldCryptVerifySignature), jNewCryptVerifySignature);
fOldRegQueryValueEx := PatchCode(GetCoreDLLAddress(hCoreDLL, aOldRegQueryValueEx), jNewRegQueryValueEx);
ClearCache;
Log('Waiting...');
Sleep(30000);
Log('Unpatching...');
PatchCode(GetCoreDLLAddress(hCoreDLL, aOldCryptVerifySignature), fOldCryptVerifySignature);
PatchCode(GetCoreDLLAddress(hCoreDLL, aOldRegQueryValueEx), fOldRegQueryValueEx);
ClearCache;
finally
HeapFree(hHeap, 0, pHeap);
end else Log('Error: pHeap == nil :: ' + Int2Str(GetLastError));
finally
HeapDestroy(hHeap);
end else Log('Error: hHeap == 0 :: ' + Int2Str(GetLastError));
finally
FreeLibrary(hCoreDLL);
end;
Log('Done!');
end;
procedure TForm1.KOLForm1FormCreate(Sender: PObj);
begin
pnlContainer.Align := caClient;
end;
procedure TForm1.Log(S: KOLString);
begin
lbLog.Add(S);
lbLog.CurIndex := lbLog.Count - 1;
Form.Invalidate;
Form.Update;
Form.ProcessPaintMessages;
end;
initialization
{$IFNDEF KOL_MCK} {$I unit1.lrs} {$ENDIF}
end.