OLD | NEW |
| (Empty) |
1 // Copyright 2004-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Tool to crash the specified process. | |
17 // Injects a remote thread into the named process and causes | |
18 // this thread to crash. | |
19 // Options: | |
20 // 1. Crash by injecting a thread that tries to execute a non-existent code | |
21 // location. | |
22 // 2. Crash by Raising a specific exception. | |
23 // TODO(omaha): Figure out a way to implement: | |
24 // 3. Crash another thread of the process. | |
25 // 4. Crash another thread when it enters a particular function. | |
26 // 5. Crash another thread when it leaves a particular function. | |
27 // | |
28 // For now the process name and the address of the crash are hard coded. | |
29 | |
30 #include <windows.h> | |
31 #include <wtypes.h> | |
32 #include <tchar.h> | |
33 #include <psapi.h> | |
34 | |
35 const int kMaxProcesses = 1024; | |
36 | |
37 int _tmain(int argc, TCHAR* argv[]) { | |
38 if (2 != argc) { | |
39 wprintf(_T("Incorrect syntax. The single required argument is the ") | |
40 _T("executable to crash.\n")); | |
41 return -1; | |
42 } | |
43 | |
44 const TCHAR* target_process_name = argv[1]; | |
45 | |
46 DWORD num_processes = 0; | |
47 DWORD process_ids[kMaxProcesses] = {0}; | |
48 if (!::EnumProcesses(process_ids, kMaxProcesses, &num_processes)) { | |
49 wprintf(_T("EnumProcesses failed.\n")); | |
50 return -1; | |
51 } | |
52 | |
53 wprintf(_T("Found %u processes.\n"), num_processes); | |
54 bool found = false; | |
55 for (size_t i = 0; i < num_processes && !found; ++i) { | |
56 const DWORD access_rights = PROCESS_CREATE_THREAD | | |
57 PROCESS_QUERY_INFORMATION | | |
58 PROCESS_VM_OPERATION | | |
59 PROCESS_VM_WRITE | | |
60 PROCESS_VM_READ; | |
61 HANDLE process_handle = ::OpenProcess(access_rights, | |
62 FALSE, | |
63 process_ids[i]); | |
64 if (process_handle) { | |
65 TCHAR process_name[1024] = {0}; | |
66 if (::GetProcessImageFileName(process_handle, process_name, 1024) != 0) { | |
67 size_t len = wcslen(target_process_name); | |
68 size_t total_len = wcslen(process_name); | |
69 int offset = total_len - len; | |
70 if (offset >= 0 && | |
71 _wcsicmp(&(process_name[offset]), target_process_name) == 0) { | |
72 found = true; | |
73 wprintf(_T("Found %s %u. Injecting a crash.\n"), | |
74 target_process_name, process_ids[i]); | |
75 DWORD thread_id = 0; | |
76 LPTHREAD_START_ROUTINE func = | |
77 reinterpret_cast<LPTHREAD_START_ROUTINE>(0x12345678); | |
78 void* addr_of_param = reinterpret_cast<void*>(0x2491ed8); | |
79 HANDLE thread_handle = ::CreateRemoteThread(process_handle, | |
80 NULL, | |
81 0, | |
82 func, | |
83 addr_of_param, | |
84 CREATE_SUSPENDED, | |
85 &thread_id); | |
86 if (!thread_handle) { | |
87 HRESULT hr = HRESULT_FROM_WIN32(::GetLastError()); | |
88 wprintf(_T("CreateRemoteThread failed.\n")); | |
89 } | |
90 if (::ResumeThread(thread_handle) == -1) { | |
91 wprintf(_T("Resume thread failed.\n")); | |
92 } else { | |
93 wprintf(_T("Waiting for process to quit.\n")); | |
94 ::WaitForSingleObject(thread_handle, 2000); | |
95 } | |
96 } | |
97 } | |
98 | |
99 ::CloseHandle(process_handle); | |
100 } | |
101 } | |
102 | |
103 if (!found) { | |
104 wprintf(_T("No %s processes found.\n"), target_process_name); | |
105 } else { | |
106 wprintf(_T("Done.\n")); | |
107 } | |
108 | |
109 return 0; | |
110 } | |
111 | |
OLD | NEW |