Skip to content

Commit

Permalink
Allow specifying kill method to win32, taskkill, or wmic
Browse files Browse the repository at this point in the history
  • Loading branch information
eschan145 committed Jan 19, 2025
1 parent 1940188 commit 1bcdea0
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 13 deletions.
76 changes: 66 additions & 10 deletions src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,72 @@ std::string last_error() {
return std::to_string(GetLastError());
}

bool taskkill(DWORD identifier) {
HANDLE process = OpenProcess(PROCESS_TERMINATE, FALSE, identifier);

if (process) {
// Bam, terminated!
TerminateProcess(process, -1);
CloseHandle(process);
return true;
int get_kill_method() {
// Scoped enums are not interchangeable with ints!
return static_cast<int>(default_kill_method);
}

void set_kill_method(int value) {
default_kill_method = static_cast<KillMethod>(value);
}

void system(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);
}

bool taskkill(DWORD identifier, KillMethod method) {
switch (method) {
case KillMethod::WIN32: {
HANDLE process = OpenProcess(PROCESS_TERMINATE, FALSE, identifier);

if (process) {
// Bam, terminated!
TerminateProcess(process, -1);
CloseHandle(process);
return true;
}
return false;
}

case KillMethod::TASKKILL: {
std::string command = "TASKKILL /PID " +
std::to_string(identifier) + "/F";
system(command);
break;
}

case KillMethod::WMIC: {
std::string command = "wmic process where ProcessId=" +
std::to_string(pid) + " delete";
system(command);
break;
}
}
return false;
}

void sweep() {
Expand Down Expand Up @@ -319,7 +375,7 @@ void sweep() {
DWORD identifier;
GetWindowThreadProcessId(hwnd, &identifier);

dieknow::taskkill(identifier);
dieknow::taskkill(identifier, default_kill_method);

// else {
// error("Failed to enumerate through processes! Error code: " +
Expand Down
16 changes: 15 additions & 1 deletion src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,20 @@ enum class InternetFlags {
CONNECT_NONE
};

enum class KillMethod {
WIN32, // Using TerminateProcess()
SYSTEM, // Using taskkill
WMIC // Using WMIC
};

KillMethod default_kill_method = KillMethod::WIN32;


namespace dieknow {

bool taskkill(DWORD identifier);
void system(const std::string& command);

bool taskkill(DWORD identifier, KillMethod method = KillMethod::WIN32);

void sweep();

Expand All @@ -82,6 +93,9 @@ extern "C" {

// __declspec allows it to be exported and used in ctypes

DK_API int get_kill_method();
DK_API void set_kill_method(int value);

DK_API void validate();
DK_API const char* get_folder_path();
DK_API void start_monitoring(const char* folder_path = FOLDER_PATH);
Expand Down
12 changes: 10 additions & 2 deletions src/dieknow.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@
"machine (as well as its dependencies) and is not missing "
"dependencies if dynamically linked!\n\nRefer to "
"https://learn.microsoft.com/en-us/windows/win32/debug/"
"system-error-codes for more information"
"system-error-codes for more information."
)
sys.stderr.write(f"{RED}{error_message}{RESET}\n")
raise OSError from exc

try:
lib.get_kill_method.argtypes = None
lib.get_kill_method.restype = ctypes.c_int

lib.set_kill_method.argtypes = ctypes.c_int
lib.set_kill_method.restype = None

lib.validate.argtypes = None
lib.validate.restype = None
lib.get_folder_path.argtypes = None
Expand Down Expand Up @@ -115,7 +121,9 @@ def __init__(self):
"stop_monitoring",
"sys",
"wintypes",
"Shell"
"Shell",
"get_kill_method",
"set_kill_method"
}
for var_name, value in globals().items():
if var_name not in exclude and not var_name.startswith("__"):
Expand Down
9 changes: 9 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def main():
killed = dieknow.get_killed_count()
print(f"Executables killed: {killed}")

case "method":
value = input("Enter the new termination method (0-2): ")
if (value >= 0) and (value <= 2):
dieknow.set_kill_method(value);
elif not value:
print(dieknow.get_kill_method())
else:
print("Invalid input!")

case "exit":
if dieknow.status():
dieknow.stop_monitoring()
Expand Down

0 comments on commit 1bcdea0

Please sign in to comment.