-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi.cpp
741 lines (584 loc) · 21.4 KB
/
api.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
COPYRIGHT (C) 2025 ETHAN CHAN
ALL RIGHTS RESERVED. UNAUTHORIZED COPYING, MODIFICATION, DISTRIBUTION, OR USE
OF THIS SOFTWARE WITHOUT PRIOR PERMISSION IS STRICTLY PROHIBITED.
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
PROJECT NAME: DieKnow
FILENAME: src/api.cpp
DESCRIPTION: API functions for DieKnow
AUTHOR: Ethan Chan
DATE: 2024-11-13
VERSION: 1.0.1
Compile with g++ -shared -o api.dll api.cpp -Ofast -fPIC -shared
*/
#include "api.h"
const char* FOLDER_PATH = "C:\\Program Files\\DyKnow\\Cloud";
// Probably subject to change, will have to be updated often
const char* DYK_CLASS_NAME = "WindowsForms10.Window.8.app.0.9fe31_r7_ad1";
const int MAX_DYKNOW_SIZE = 50;
KillMethod default_kill_method = KillMethod::WIN32_API;
std::filesystem::path get_directory() {
HMODULE hModule = nullptr;
if (GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCSTR)&get_directory,
&hModule)) {
char path[MAX_PATH];
if (GetModuleFileNameA(hModule, path, MAX_PATH) > 0) {
return std::filesystem::path(path).parent_path();
} else {
error("Failed to locate DLL directory!\n");
}
}
return {};
}
std::filesystem::path locate_settings() {
auto directory = get_directory();
auto file_path = directory.parent_path().parent_path() / "settings.conf";
if (std::filesystem::exists(file_path)) {
std::cout << "Located settings.conf.\n";
return file_path;
} else {
error("Failed to locate settings.conf!\n"
"It should be located at " +
file_path.string() +
".\n"
);
return {};
}
}
void error(const std::string& message) {
std::cerr << "\033[31m" << "\033[4m"
<< "ERROR" << "\033[24m"
<< ": " << message << "\033[0m";
}
namespace dieknow {
extern "C" {
bool running = false;
int killed = 0;
DK_API int __stdcall dialog(
const char* message,
const char* title,
UINT type) {
return MessageBox(nullptr, message, title, type);
}
}
DK_API uint64_t dyknow_size(const std::string& directory) {
uint64_t total = 0;
WIN32_FIND_DATAA data;
HANDLE find = FindFirstFile((directory + "\\*").c_str(), &data);
if (find == INVALID_HANDLE_VALUE) {
error("Failed to access DyKnow folder!\n");
// validate();
return 0;
}
do {
if (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
continue;
}
std::string filename = data.cFileName;
if ((filename == ".") || (filename == "..")) {
continue;
}
std::string full_path = directory + "\\" + filename;
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// If it's a directory, recursively calculate its size
total += dyknow_size(full_path);
} else {
LARGE_INTEGER file_size;
file_size.LowPart = data.nFileSizeLow;
file_size.HighPart = data.nFileSizeHigh;
total += file_size.QuadPart;
}
} while (FindNextFileA(find, &data) != 0);
FindClose(find);
return total;
}
DK_API void validate() {
/*
Check for the validity of the DyKnow installation. If the DyKnow
installation cannot be found, the application exits.
Settings are loaded.
*/
bool needs_exit = false;
if (!std::filesystem::exists(FOLDER_PATH)) {
needs_exit = true;
std::ostringstream msg;
msg << "A DyKnow installation was not able to be found on your "
<< "device.\n"
<< "Ensure the folder \"" << FOLDER_PATH
<< "\" exists and you have the permissions to access it!\n\n"
<< "Additionally, ensure you have one of the supported DyKnow "
<< "versions. "
<< "You may need to upgrade your DieKnow to a later version.";
MessageBox(nullptr, msg.str().c_str(), "FATAL ERROR", MB_ICONERROR);
} else {
std::cout << "Successfully located DyKnow installation at "
<< FOLDER_PATH << ".\n";
}
auto path = locate_settings();
bool loaded_settings = settings.load(path.string());
if (loaded_settings) {
std::cout << "Successfully loaded DieKnow configuration files.\n";
} else {
error("Failed to load DieKnow configuration files!\n");
needs_exit = true;
}
if (needs_exit) {
error("FATAL: DyKnow validation failed!\n");
std::exit(EXIT_FAILURE);
} else {
std::cout << "Successfully validated DyKnow installation and file "
<< "integrity.\n";
}
uint64_t size = dyknow_size();
uint64_t size_in_mb = size / (1024 * 1024);
std::string color;
if (size_in_mb < 30) {
color = "\033[32m"; // Green
} else if (size_in_mb <= MAX_DYKNOW_SIZE) {
color = "\033[0m"; // Normal (default)
} else {
color = "\033[31m"; // Red
}
std::cout << "DyKnow folder size: " << color
<< comma_separated(size) << "\033[0m" // Reset
<< " bytes (" << comma_separated(size_in_mb)
<< " MBs)\n";
}
bool exists(const char* path) {
/*
Check if a filepath exists.
*/
DWORD ftyp = GetFileAttributesA(path);
if (ftyp == INVALID_FILE_ATTRIBUTES) {
return false;
}
return (ftyp & FILE_ATTRIBUTE_DIRECTORY);
}
DK_API bool close_application_by_exe(const char* exe_name) {
/*
Close a Windows PE executable file given the executable name.
The win32 function `TerminateProcess()` is used, which has looser
privilleges than `taskkill`. It's uncommon that it will require
administrative permissions.
*/
bool terminated = false;
// Create a snapshot of all running process(es)
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
// Break out if the snapshot failed
if (hProcessSnap == INVALID_HANDLE_VALUE) return false;
// Iterate through the process list and terminate them as desired
if (Process32First(hProcessSnap, &pe32)) {
// Populate `pe32` with process snapshot information
do {
// Check if the executable name is the one given as a parameter
if (_stricmp(pe32.szExeFile, exe_name) == 0) {
// Open a HANDLE to the process. This is a little ambiguous as
// it appears we are opening the process.
// Request PROCESS_TERMIANTE right, which allows use to use
// TerminateProcess. This may not work if the process is
// elevated. Once we can obtain a handle to the process, we
// can do anything we want with it under the permissions we
// requested; e.g. terminating it.
HANDLE hProcess = OpenProcess(
PROCESS_TERMINATE, FALSE,
pe32.th32ProcessID);
if (hProcess) {
// Bam, terminated!
TerminateProcess(hProcess, 0);
auto wait = [&]() {
DWORD result = WaitForSingleObject(hProcess, TIMEOUT);
switch (result) {
case WAIT_OBJECT_0:
terminated = true;
std::cout << "Process " << exe_name
<< " terminated successfully.\n";
break;
case WAIT_TIMEOUT:
std::cerr << "WaitForSingleObject timed out!\n";
break;
case WAIT_FAILED:
break;
default:
break;
}
};
std::thread wait_thread(wait);
wait_thread.join();
// Destroy the process handle to avoid memory leaks
CloseHandle(hProcess);
} else {
std::cerr << "Failed to open a handle to the process! "
<< "Error code: " << GetLastError() << "\n";
}
}
} while (Process32Next(hProcessSnap, &pe32));
}
// Destroy the snapshot to avoid memory leaks
CloseHandle(hProcessSnap);
if (terminated) killed++;
return terminated;
}
DK_API int monitor_executables(int interval) {
/*
Begin monitoring and closing of the executables in the given folder path.
A while loop will go through all of the executables in `FOLDER_PATH`. It
will then attempt to terminate them individually. The folder path is
refreshed each iteration of the loop.
An interval that can be specified in settings controls how often the
function is repeated. A low interval may cause high CPU usage while a low
interval may give DyKnow ample time to start back up.
If `FOLDER_PATH` cannot be validated, the `validate()` function is called.
This function returns the amount of executables it terminated in this pass.
> [!IMPORTANT]
> It is best to call this in an independent thread, such as
> `start_monitoring()` as this will run continuously until the variable
> `running` is set to `false`.
*/
int count = 0;
while (running) {
dieknow::sweep();
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
return count;
}
DK_API const char* get_folder_path() {
/*
Retrieve the default DyKnow folder path.
This is made into a function for use with ctypes.
*/
return FOLDER_PATH;
}
// cppcheck-suppress unusedFunction
std::string last_error() {
return std::to_string(GetLastError());
}
int get_kill_method() {
// Scoped enums are not interchangeable with ints!
return static_cast<int>(default_kill_method);
}
void set_kill_method(int value) {
assert((0 <= value) && (value <= 2) &&
"Invalid value for static_cast<KillMethod>!");
default_kill_method = static_cast<KillMethod>(value);
}
DK_API bool execute(const std::string& command) {
STARTUPINFO si = {sizeof(STARTUPINFO)};
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi = {};
if (!CreateProcessA(
nullptr,
const_cast<char*>(command.c_str()),
nullptr,
nullptr,
FALSE,
CREATE_NO_WINDOW,
nullptr,
nullptr,
&si,
&pi
)) {
return false;
}
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return true;
}
DK_API bool taskkill(DWORD identifier, KillMethod method) {
/*
Terminate an executable given a PID passed in `identifier`.
An optional termination method may be specified as the scoped enumerate
`KillMethod`.
* `KillMethod::WIN32_API`: recommended kill method using
`TerminateProcess()`. This is the most reliable and the most performant.
* `KillMethod::SYSTEM`: using OS system's `taskkill` command. Usually will
fail due to lack of permissions.
* `KillMethod::WMIC`: using Windows Management Instrumentation command line.
Usually works but less performant and reliable compared to `WIN32_API`.
*/
switch (method) {
case KillMethod::WIN32_API: {
HANDLE process = OpenProcess(PROCESS_TERMINATE, FALSE, identifier);
if (process) {
// Bam, terminated!
TerminateProcess(process, static_cast<UINT>(-1));
// -1 is the exit code for the process - in this case,
// indicating internal failure.
CloseHandle(process);
return true;
}
return false;
}
case KillMethod::SYSTEM: {
std::string command = "TASKKILL /PID " +
std::to_string(identifier) + "/F";
dieknow::execute(command);
return true;
}
case KillMethod::WMIC: {
std::string command = "wmic process where ProcessId=" +
std::to_string(identifier) + " delete";
dieknow::execute(command);
return true;
}
default: {
return false;
}
}
}
DK_API void sweep() {
/*
Destroy all DyKnow executables in a sweep.
*/
HWND hwnd = FindWindow(
DYK_CLASS_NAME,
nullptr
);
if (!hwnd) {
std::vector<std::string> possible_names;
possible_names.push_back("Do you understand?");
possible_names.push_back("We let your teacher know (I get it!)");
possible_names.push_back("We let your teacher know (I'm not sure!)");
possible_names.push_back(
"We let your teacher know (I don't get it, yet!)"
);
for (const std::string& name : possible_names) {
hwnd = FindWindow(nullptr, name.c_str());
if (hwnd)
break;
}
}
if (!hwnd) {
// error("Failed to find DyKnow window! It may not be running.\n");
return;
}
// Retrieve PID of DyKnow process
DWORD identifier;
GetWindowThreadProcessId(hwnd, &identifier);
dieknow::taskkill(identifier, default_kill_method);
// else {
// error("Failed to enumerate through processes! Error code: " +
// last_error() + "\n");
// }
}
DK_API void start_monitoring(const char* folder_path) {
/*
Begin monitoring executables.
A separate thread is detached from the primary thread. This thread is set
with a lower priority to reduce CPU usage.
See `monitor_executables()`.
*/
if (!running) {
InternetFlags internet_state = dieknow::is_connected();
bool connected = true;
switch (internet_state) {
case InternetFlags::CONNECT_MODEM:
std::cout << "Internet connected via modem.\n";
break;
case InternetFlags::CONNECT_LAN:
std::cout << "Internet connected via LAN.\n";
break;
case InternetFlags::CONNECT_PROXY:
std::cout << "Internet connected via proxy.\n";
break;
case InternetFlags::CONNECT_NONE:
connected = false;
std::cout << "Internet disconnected. Proceeding to start "
<< "monitoring.\n";
break;
}
// We have to do this or the DyKnow web platform will have an error
// message of "Student thumbnail disconnected", which could prompt a
// log to be sent. If we ensure Internet is turned off before, it will
// only display "Student offline", which is very common and unlikely to
// draw suspicion.
if (connected) {
error("Please turn off or disable your Internet before you begin "
"DieKnow! Once started, you can turn back on your \n"
"Internet. Aborting monitoring.\n");
// return;
}
running = true;
int interval = settings.get<int>("interval", 0);
std::thread thread(dieknow::monitor_executables, interval);
HANDLE handle = reinterpret_cast<HANDLE>(thread.native_handle());
std::cout << "Created monitoring std::thread and retrieved HANDLE.\n";
thread.detach();
// int old_interval = settings.get<int>("interval", 0);
// if (old_interval != interval) {
// std::cout << "Monitoring interval updated to " << interval
// << " seconds.\n";
// old_interval = interval;
// }
// SetTimer(nullptr, SWEEP_TIMER_ID, interval * 1000, sweep);
// Reduces CPU usage by prioritizing other applications.
// Other options:
// * IDLE - only run when its the only thread
// * LOWEST
// * BELOW_NORMAL
// * NORMAL - default priority
// * ABOVE_NORMAL
// * HIGHEST
// * TIME_CRITICAL
std::cout << "Monitoring started.\n";
// std::thread asteroids_thread(create, std::ref(running));
// asteroids_thread.detach();
} else {
error("The DieKnow process has already been started!\n");
}
}
DK_API void stop_monitoring() {
/*
Stop monitoring executables.
*/
// Although just a variable is set to false, because the DieKnow process is
// in a separate thread it will finish immediately.
if (running) {
running = false;
std::cout << "Successfully stopped DieKnow process.\n";
} else {
error("The DieKnow process has already been stopped!\n");
}
}
// Both get_killed_count and is_running must be declared as functions as ctypes
// does not support retrieving variables.
DK_API int get_killed_count() {
/*
Retrieve the amount of DyKnow executables killed.
*/
return killed;
}
DK_API bool is_running() {
/*
Check if DieKnow is running or not.
*/
return running;
}
DK_API bool is_monitoring() {
/*
Check if DyKnow is currently running or not using `FindWindow()` with the
class name.
*/
// TODO(eschan145): make it see if it is monitoring rather than running.
HWND window = FindWindow(
DYK_CLASS_NAME,
nullptr
);
return (window != nullptr);
}
DK_API InternetFlags is_connected() {
/*
Check for an Internet connection and return the connection type, one of
`InternetFlags`.
*/
DWORD flags;
bool result = InternetGetConnectedState(&flags, 0);
if (result) {
if (flags & INTERNET_CONNECTION_LAN) [[likely]] {
return InternetFlags::CONNECT_LAN;
}
if (flags & INTERNET_CONNECTION_MODEM) [[unlikely]] {
return InternetFlags::CONNECT_MODEM;
}
if (flags & INTERNET_CONNECTION_PROXY) [[unlikely]] {
return InternetFlags::CONNECT_PROXY;
}
}
return InternetFlags::CONNECT_NONE;
}
DK_API const char* get_executables_in_folder(const char* folder_path) {
/*
Retrieve a printable list of executables in a folder.
Ensures that the DyKnow installation can exist, and checks with
`validate()` if it doesn't.
*/
static std::string result;
result.clear();
bool found_dir = false;
bool found_subfile = false;
for (const auto& entry :
std::filesystem::directory_iterator(folder_path)) {
// cppcheck-suppress useStlAlgorithm
if (entry.is_directory()) {
// If it's a directory, iterate over its subfiles
for (const auto& sub_entry :
std::filesystem::directory_iterator(entry.path())) {
if (sub_entry.is_regular_file() &&
sub_entry.path().extension() == ".exe") {
result += sub_entry.path().filename().string() + "\n";
found_subfile = true;
}
}
if (found_subfile)
found_dir = true;
break;
}
}
if (!found_dir)
validate();
return result.c_str();
}
DK_API int __stdcall bsod() {
/*
Open the Windows Blue Screen of Death via NT API's `NtRaiseHardError`.
Use with caution! Your system will freeze and shut down within a few
seconds, losing any unsaved work.
This function `NtRaiseHardError` is part of the Windows New Technology API
kernel and is completely undocumented. The reason why is to prevent
non-system libraries from messing around with low-level settings they
aren't supposed to.
*/
BOOLEAN bEnabled;
ULONG uResp;
// Load RtlAdjustPrivilege and NtRaiseHardError functions from ntdll.dll
auto RtlAdjustPrivilege = reinterpret_cast<NTSTATUS(WINAPI*)(
ULONG, BOOLEAN, BOOLEAN, PBOOLEAN)>(
GetProcAddress(
GetModuleHandleW(L"ntdll.dll"),
"RtlAdjustPrivilege"
)
);
auto NtRaiseHardError = reinterpret_cast<NTSTATUS(WINAPI*)(
NTSTATUS, ULONG, ULONG, PULONG_PTR, ULONG, PULONG)>(
GetProcAddress(
GetModuleHandleW(L"ntdll.dll"),
"NtRaiseHardError"
)
);
if (!RtlAdjustPrivilege || !NtRaiseHardError) {
if (!RtlAdjustPrivilege) {
std::cerr << "Failed to load RtlAdjustPrivilege from win32's "
<< "nt.dll!\n";
}
if (!NtRaiseHardError) {
std::cerr << "Failed to load NtRaiseHardError from win32's "
<< "nt.dll!\n";
}
std::cerr << "Failed to open Windows BSOD!\n";
return -1;
}
// Enable shutdown privilege for this process
RtlAdjustPrivilege(19, TRUE, FALSE, &bEnabled);
if (bEnabled) {
std::cout << "Successfully loaded permissions with "
<< "RtlAdjustPrivilege().\n";
}
std::cerr << "Successfully initiated Windows BSOD. The system will "
<< "terminate in a few seconds...\n";
// Trigger BSOD
NtRaiseHardError(STATUS_ASSERTION_FAILURE, 0, 0, nullptr, 6, &uResp);
return 0;
}
} // namespace dieknow