int main(int argc, char* argv[]) { DWORD dwProcessId; if (argc != 2) { printf("Usage: %s process\n", argv[0]); return 0; } if (0 == (dwProcessId = getProcessIdByName(argv[1]))) { printf("Not found %s\n", argv[1]); return 0; } if (terminateProcessById(dwProcessId)) { printf("Terminate %s Success\n", argv[1]); } else { printf("Error %ul\n", ::GetLastError()); } return 0; }
int main(int argc, char *argv[]) { if (argc < 3) { fprintf(stderr, "usage:\n" " inject <dllname.dll> <command> [args] ...\n" " inject <dllname.dll> <process-id>\n" " inject <dllname.dll> !<process-name>\n" ); return 1; } BOOL bAttach = FALSE; DWORD dwProcessId = ~0; if (isNumber(argv[2])) { dwProcessId = atol(argv[2]); bAttach = TRUE; } else if (argv[2][0] == '!') { const char *szProcessName = &argv[2][1]; if (!getProcessIdByName(szProcessName, &dwProcessId)) { fprintf(stderr, "error: failed to find process %s\n", szProcessName); return 1; } bAttach = TRUE; fprintf(stderr, "dwProcessId = %lu\n", dwProcessId); } HANDLE hSemaphore = NULL; const char *szDll = argv[1]; if (!USE_SHARED_MEM) { SetEnvironmentVariableA("INJECT_DLL", szDll); } else { hSemaphore = CreateSemaphore(NULL, 1, 1, "inject_semaphore"); if (hSemaphore == NULL) { fprintf(stderr, "error: failed to create semaphore\n"); return 1; } DWORD dwWait = WaitForSingleObject(hSemaphore, 0); if (dwWait == WAIT_TIMEOUT) { fprintf(stderr, "info: waiting for another inject instance to finish\n"); dwWait = WaitForSingleObject(hSemaphore, INFINITE); } if (dwWait != WAIT_OBJECT_0) { fprintf(stderr, "error: failed to enter semaphore gate\n"); return 1; } SetSharedMem(szDll); } BOOL bAttachDwm = FALSE; PROCESS_INFORMATION processInfo; HANDLE hProcess; if (bAttach) { BOOL bRet; HANDLE hToken = NULL; bRet = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken); if (!bRet) { fprintf(stderr, "error: OpenProcessToken returned %u\n", (unsigned)bRet); return 1; } LUID Luid; bRet = LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &Luid); if (!bRet) { fprintf(stderr, "error: LookupPrivilegeValue returned %u\n", (unsigned)bRet); return 1; } TOKEN_PRIVILEGES tp; tp.PrivilegeCount = 1; tp.Privileges[0].Luid = Luid; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; bRet = AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof tp, NULL, NULL); if (!bRet) { fprintf(stderr, "error: AdjustTokenPrivileges returned %u\n", (unsigned)bRet); return 1; } DWORD dwDesiredAccess = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ | PROCESS_TERMINATE; hProcess = OpenProcess( dwDesiredAccess, FALSE /* bInheritHandle */, dwProcessId); if (!hProcess) { logLastError("failed to open process"); return 1; } char szProcess[MAX_PATH]; DWORD dwRet = GetModuleFileNameEx(hProcess, 0, szProcess, sizeof szProcess); assert(dwRet); if (dwRet && stricmp(getBaseName(szProcess), "dwm.exe") == 0) { bAttachDwm = TRUE; } } else { std::string commandLine; char sep = 0; for (int i = 2; i < argc; ++i) { const char *arg = argv[i]; if (sep) { commandLine.push_back(sep); } if (needsQuote(arg)) { quoteArg(commandLine, arg); } else { commandLine.append(arg); } sep = ' '; } STARTUPINFO startupInfo; memset(&startupInfo, 0, sizeof startupInfo); startupInfo.cb = sizeof startupInfo; // Create the process in suspended state if (!CreateProcessA( NULL, const_cast<char *>(commandLine.c_str()), // only modified by CreateProcessW 0, // process attributes 0, // thread attributes TRUE, // inherit handles CREATE_SUSPENDED, NULL, // environment NULL, // current directory &startupInfo, &processInfo)) { DWORD dwLastError = GetLastError(); fprintf(stderr, "error: failed to execute %s (%lu)\n", commandLine.c_str(), dwLastError); if (dwLastError == ERROR_ELEVATION_REQUIRED) { fprintf(stderr, "error: target program requires elevated priviledges and must be started from an Administrator Command Prompt, or UAC must be disabled\n"); } return 1; } hProcess = processInfo.hProcess; } /* * XXX: Mixed architecture don't quite work. See also * http://www.corsix.org/content/dll-injection-and-wow64 */ { typedef BOOL (WINAPI *PFNISWOW64PROCESS)(HANDLE, PBOOL); PFNISWOW64PROCESS pfnIsWow64Process; pfnIsWow64Process = (PFNISWOW64PROCESS) GetProcAddress(GetModuleHandleA("kernel32"), "IsWow64Process"); if (pfnIsWow64Process) { BOOL isParentWow64 = FALSE; BOOL isChildWow64 = FALSE; if (pfnIsWow64Process(GetCurrentProcess(), &isParentWow64) && pfnIsWow64Process(hProcess, &isChildWow64) && isParentWow64 != isChildWow64) { fprintf(stderr, "error: binaries mismatch: you need to use the " #ifdef _WIN64 "32-bits" #else "64-bits" #endif " apitrace binaries to trace this application\n"); TerminateProcess(hProcess, 1); return 1; } } } if (bAttachDwm && IsWindows8OrGreater()) { // Switch to Microsoft Basic Display Driver before injecting, so that // we don't trace with it. devconDisable(DEVCON_CLASS_DISPLAY); Sleep(1000); } const char *szDllName; szDllName = "injectee.dll"; char szDllPath[MAX_PATH]; GetModuleFileNameA(NULL, szDllPath, sizeof szDllPath); getDirName(szDllPath); strncat(szDllPath, szDllName, sizeof szDllPath - strlen(szDllPath) - 1); #if 1 if (!injectDll(hProcess, szDllPath)) { TerminateProcess(hProcess, 1); return 1; } #endif DWORD exitCode; if (bAttach) { if (bAttachDwm) { restartDwmComposition(hProcess); } exitCode = 0; } else { // Start main process thread ResumeThread(processInfo.hThread); // Wait for it to finish WaitForSingleObject(hProcess, INFINITE); if (pSharedMem && !pSharedMem->bReplaced) { fprintf(stderr, "warning: %s was never used: application probably does not use this API\n", szDll); } exitCode = ~0; GetExitCodeProcess(hProcess, &exitCode); CloseHandle(processInfo.hThread); } CloseHandle(hProcess); if (hSemaphore) { ReleaseSemaphore(hSemaphore, 1, NULL); CloseHandle(hSemaphore); } return (int)exitCode; }
void OperatingSystem::killProcess(std::string name){ int index = getProcessIdByName(name); if (index != -1) killProcess(index); }
int main(int argc, char **argv) { DWORD dwProcessId = 0; int c; /* Character of the parsed option. */ debug_options.first_chance = 1; while (1) { int option_index = 0; static const struct option long_options[] = { { "help", 0, NULL, 'h'}, { "version", 0, NULL, 'V'}, { "install", 0, NULL, 'i'}, { "auto", 0, NULL, 'a'}, { "uninstall", 0, NULL, 'u'}, { "process-id", 1, NULL, 'p'}, { "event", 1, NULL, 'e'}, { "tid", 1, NULL, 't'}, { "breakpoint", 0, NULL, 'b'}, { "verbose", 0, NULL, 'v'}, { "debug", 0, NULL, 'd'}, { NULL, 0, NULL, 0} }; c = getopt_long_only(argc, argv, "?hViaup:e:t:vbd", long_options, &option_index); if (c == -1) break; /* Exit from `while (1)' loop. */ switch (c) { case '?': if (optopt != '?') { /* Invalid option. */ char szErrMsg[512]; sprintf(szErrMsg, "Invalid option '%c'", optopt); MessageBoxA( NULL, szErrMsg, PACKAGE, MB_OK | MB_ICONSTOP ); return 1; } /* fall-through */ case 'h': /* Print help and exit. */ help(); return 0; case 'V': /* Print version and exit. */ MessageBoxA( NULL, PACKAGE " " VERSION, PACKAGE, MB_OK | MB_ICONINFORMATION ); return 0; case 'i': /* Install as the default JIT debugger. */ if (uninstall_given) { MessageBoxA( NULL, "conficting options `--uninstall' (`-u') and `--install' (`-i')", PACKAGE, MB_OK | MB_ICONSTOP ); return 0; } install_given = 1; break; case 'a': /* Automatically start. */ if (uninstall_given) { MessageBoxA( NULL, "conficting options `--uninstall' (`-u') and `--auto' (`-a')", PACKAGE, MB_OK | MB_ICONSTOP ); return 0; } auto_given = 1; break; case 'u': /* Uninstall. */ if (install_given) { MessageBoxA( NULL, "conficting options `--install' (`-i') and `--uninstall' (`-u')", PACKAGE, MB_OK | MB_ICONSTOP ); return 0; } if (auto_given) { MessageBoxA( NULL, "conficting options `--auto' (`-a') and `--uninstall' (`-u')", PACKAGE, MB_OK | MB_ICONSTOP ); return 0; } uninstall_given = 1; break; case 'p': /* Attach to the process with the given identifier. */ if (process_id_given) { MessageBoxA( NULL, "`--process-id' (`-p') option redeclared", PACKAGE, MB_OK | MB_ICONSTOP ); return 1; } process_id_given = 1; if (optarg[0] >= '0' && optarg[0] <= '9') { dwProcessId = strtoul(optarg, NULL, 0); } else { debug_options.breakpoint_flag = 1; dwProcessId = getProcessIdByName(optarg); } if (!dwProcessId) { MessageBoxA( NULL, "Invalid process", PACKAGE, MB_OK | MB_ICONSTOP ); return 1; } break; case 'e': /* Signal an event after process is attached. */ if (debug_options.hEvent) { MessageBoxA( NULL, "`--event' (`-e') option redeclared", PACKAGE, MB_OK | MB_ICONSTOP ); return 1; } debug_options.hEvent = (HANDLE) (INT_PTR) atol (optarg); break; case 't': { /* Thread id. Used when debugging WinRT apps. */ debug_options.dwThreadId = strtoul(optarg, NULL, 0); break; } case 'b': /* Treat debug breakpoints as exceptions */ debug_options.breakpoint_flag = 1; break; case 'v': /* Verbose output. */ debug_options.verbose_flag = 1; break; case 'd': /* Debug output. */ debug_options.debug_flag = 1; break; default: /* bug: option not considered. */ { char szErrMsg[512]; sprintf(szErrMsg, "Unexpected option '-%c'", c); MessageBoxA( NULL, szErrMsg, PACKAGE, MB_OK | MB_ICONSTOP ); return 1; } } } if (install_given) { DWORD dwRet = install(0); #if defined(_WIN64) if (dwRet == ERROR_SUCCESS) { dwRet = install(KEY_WOW64_32KEY); } #endif if (dwRet != ERROR_SUCCESS) { MessageBoxA( NULL, dwRet == ERROR_ACCESS_DENIED ? "You must have administrator privileges to install Dr. Mingw as the default application debugger" : "Unexpected error when trying to install Dr. Mingw as the default application debugger", "DrMingw", MB_OK | MB_ICONERROR ); return 1; } MessageBoxA( NULL, "Dr. Mingw has been installed as the default application debugger", "DrMingw", MB_OK | MB_ICONINFORMATION ); return 0; } if (uninstall_given) { DWORD dwRet = uninstall(0); #if defined(_WIN64) if (dwRet == ERROR_SUCCESS) { dwRet = uninstall(KEY_WOW64_32KEY); } #endif if (dwRet != ERROR_SUCCESS) { MessageBoxA( NULL, dwRet == ERROR_ACCESS_DENIED ? "You must have administrator privileges to uninstall Dr. Mingw as the default application debugger" : "Unexpected error when trying to uninstall Dr. Mingw as the default application debugger", "DrMingw", MB_OK | MB_ICONERROR ); return 1; } MessageBoxA( NULL, "Dr. Mingw has been uninstalled", "DrMingw", MB_OK | MB_ICONINFORMATION ); return 0; } if (process_id_given) { SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); if(!ObtainSeDebugPrivilege()) MessageBoxA( NULL, "An error occured while obtaining debug privileges.\nDrMingw will not debug system processes.", "DrMingw", MB_OK | MB_ICONERROR ); createDialog(); _beginthread(debugThread, 0, (void *)(UINT_PTR)dwProcessId); return mainLoop(); } else { help(); } return 0; }