forked from revengsmK/sunlock11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
301 lines (202 loc) · 6.57 KB
/
main.c
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <conio.h>
BOOL setDebugPriv(void){
HANDLE hToken;
LUID Luid;
TOKEN_PRIVILEGES NewState;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken) == FALSE)
return FALSE;
if(LookupPrivilegeValue(NULL, "SeDebugPrivilege", &Luid) == FALSE)
return FALSE;
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = Luid;
NewState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if(AdjustTokenPrivileges(hToken,FALSE,&NewState,sizeof(NewState),NULL,NULL) == 0)
return FALSE;
return TRUE;
}
BOOL IsElevated(void){
BOOL fRet = FALSE;
HANDLE hToken = NULL;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hToken))
{
TOKEN_ELEVATION Elevation;
DWORD cbSize = sizeof( TOKEN_ELEVATION );
if(GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize))
fRet = Elevation.TokenIsElevated;
}
if(hToken){
CloseHandle(hToken);
}
return fRet;
}
void KillProcessByName(const char *filename){
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof(PROCESSENTRY32);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
if (strcmp(pEntry.szExeFile, filename) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
(DWORD) pEntry.th32ProcessID);
if (hProcess != NULL)
{
TerminateProcess(hProcess, 1);
CloseHandle(hProcess);
}
}
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}
// Kills RuntimeBroker.exe process which has SettingsEnvironment.Desktop.dll module loaded
void KillRuntimeBroker(void){
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
HANDLE hSnapshotMod;
HANDLE hProcess;
PROCESSENTRY32 pEntry = {0};
MODULEENTRY32 modEntry = {0};
BOOL hModRes;
pEntry.dwSize = sizeof(PROCESSENTRY32);
modEntry.dwSize = sizeof(MODULEENTRY32);
BOOL hRes = Process32First(hSnapshot, &pEntry);
while(hRes)
{
if(strcmp(pEntry.szExeFile,"RuntimeBroker.exe") == 0)
{
hSnapshotMod = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pEntry.th32ProcessID );
hModRes = Module32First(hSnapshotMod, &modEntry);
while(hModRes)
{
if(strcmp("SettingsEnvironment.Desktop.dll",modEntry.szModule) == 0)
{
hProcess = OpenProcess(PROCESS_TERMINATE,0,(DWORD)pEntry.th32ProcessID);
if(hProcess != NULL)
{
TerminateProcess(hProcess, 1);
CloseHandle(hProcess);
}
}
hModRes = Module32Next(hSnapshotMod, &modEntry);
}
CloseHandle(hSnapshotMod);
}
hRes = Process32Next(hSnapshot, &pEntry);
}
CloseHandle(hSnapshot);
}
// Simple search for an unmasked hex pattern in a buffer
DWORD seekbytes(unsigned char fileBuff[], unsigned char pattern[], DWORD fileSize, SIZE_T numOfBytes){
int i = 0;
fileSize = fileSize - numOfBytes;
if(fileSize > 0 )
{
for( i = 0 ; i <= fileSize; i++)
{
if(memcmp(pattern,fileBuff+i,numOfBytes) == 0)
return i;
}
}
return 0;
}
void banner(void){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
COORD coordScreen = { 0, 0 };
DWORD bytesWritten = 0;
DWORD size = 0;
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
size = consoleInfo.dwSize.X * consoleInfo.dwSize.Y;
SetConsoleTextAttribute(hConsole, 7);
FillConsoleOutputAttribute(hConsole,11,size,coordScreen,&bytesWritten);
}
int main(void){
DWORD bytesWritten = 0;
BOOL retVal;
DWORD openErr = 0;
HANDLE hFile = NULL;
DWORD offset = 0;
DWORD fileSize = 0;
unsigned char pattern[] = {0x48, 0x83, 0xEC, 0x28, 0x48, 0x8D, 0x4C, 0x24, 0x30}; // sub rsp, 28 | lea rcx,qword ptr ss:[rsp+30]
unsigned char patch_bytes[] = { 0xB0, 0x01, 0xC3 }; // mov al,1 | ret
unsigned char *fileBuf = NULL;
char dllPath[MAX_PATH] = "";
char bakPath[MAX_PATH] = "";
banner();
if(IsElevated() == FALSE){
puts("[!] You don't have administrative privileges!\n\nRun this program as administrator.\n");
getch();
return -1;
}
if(setDebugPriv() == FALSE){
puts("[!] Failed to set SeDebugPrivilege!\nAborting...\n");
getch();
return -2;
}
GetWindowsDirectory(dllPath,MAX_PATH);
snprintf(dllPath + strlen(dllPath),MAX_PATH - strlen(dllPath),"%s","\\System32\\SettingsEnvironment.Desktop.dll");
snprintf(bakPath,MAX_PATH,"%s%s",dllPath,".BAK");
// Kill all existing SystemSettings.exe processes
puts("-> Killing all existing SystemSettings.exe processes...\n");
KillProcessByName("SystemSettings.exe");
puts("-> Killing specific RuntimeBroker.exe process...\n");
KillRuntimeBroker();
puts("-> Waiting until all processes terminate...\n");
Sleep(2000);
puts("-> Making backup of original DLL (SettingsEnvironment.Desktop.dll) file...\n");
CopyFile(dllPath,bakPath, TRUE);
hFile = CreateFile(dllPath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
openErr = GetLastError();
if(hFile == INVALID_HANDLE_VALUE)
{
puts("[!] Failed to open file!");
if(openErr == ERROR_ACCESS_DENIED)
puts("[!] Access denied!\n\nPlease run setpm.bat script before running sunlock11!");
getch();
return -3;
}
else
{
puts(" [+] DLL file successfully opened!");
fileSize = GetFileSize(hFile,NULL);
printf(" [+] DLL file size: %d bytes\n",(int)fileSize);
if(fileSize != INVALID_FILE_SIZE)
{
// reserve memory block to load file
fileBuf = GlobalAlloc(GPTR,fileSize);
// load file content to the allocated memory block
if(fileBuf == NULL)
{
puts("[!] Falied to allocate buffer!");
goto cleanup;
}
ReadFile(hFile,fileBuf,fileSize,NULL,NULL);
offset = seekbytes(fileBuf,pattern,fileSize,9);
if(offset == 0)
{
puts("[!] Pattern not found!\nAborting...");
goto cleanup;
}
printf(" [+] DLL file offset: 0x%X\n\n",(unsigned int)offset);
}
if(SetFilePointer(hFile, offset, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER){
puts("[!] Invalid file pointer!");
goto cleanup;
}
// patch DLL
retVal = WriteFile(hFile,&patch_bytes,3,&bytesWritten,NULL);
if(bytesWritten != 3 || retVal == FALSE){
puts("[!] Failed to patch DLL file!");
goto cleanup;
}
puts("-> Successfully patched DLL file!");
}
cleanup:
GlobalFree(fileBuf);
CloseHandle(hFile);
return 0;
}