This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
taskkill.cs
209 lines (179 loc) · 6.92 KB
/
taskkill.cs
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
// Sources:
// - Kill Remote Process - https://stackoverflow.com/questions/25727323/kill-process-on-remote-machine
// TODO:
// - Kill child processes: https://kv4s.files.wordpress.com/2015/08/killserviceprocesses.jpg
// To Compile:
// C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:taskkill.exe taskkill.cs
using System;
using System.Management;
class TaskKill
{
public static void PrintUsage()
{
Console.WriteLine(@"Kill process by ID or name
USAGE:
taskkill [/S <system> [/U [domain\]<username> /P <password>]] { [/PID <processid>[,...] | /IM <imagename>] }");
}
// Returns an appropriate singular or plural string based on the number of processes
private static string GetNumProcStr(int numProcesses)
{
if (numProcesses == 1)
{
return "1 process";
}
return numProcesses + " processes";
}
public static void Main(string[] args)
{
try
{
if (args.Length == 0)
{
PrintUsage();
return;
}
string system = "127.0.0.1";
string username = "";
string password = "";
string pid_str = "";
string image = "";
bool pid_set = false;
bool image_set = false;
// Parse arguments
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg.ToUpper())
{
case "-S":
case "/S":
i++;
try
{
system = args[i].Trim(new Char[] { '\\', ' ' });
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No system specified");
}
break;
case "-PID":
case "/PID":
i++;
try
{
pid_str = args[i];
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No PID(s) specified");
}
pid_set = true;
break;
case "-IM":
case "/IM":
i++;
try
{
image = args[i];
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No image specified");
}
image_set = true;
break;
case "-U":
case "/U":
i++;
try
{
username = args[i];
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No username specified");
}
break;
case "-P":
case "/P":
i++;
try
{
password = args[i];
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No password specified");
}
break;
case "/?":
default:
PrintUsage();
return;
}
}
// Error out if neither PID nor image are specified, or if both are specified
if (!(pid_set || image_set))
{
throw new ArgumentException("No process specified");
}
else if (pid_set && image_set)
{
throw new ArgumentException("PID and image cannot both be set");
}
ConnectionOptions conn_opts = new ConnectionOptions();
// Apply username and password if specified
if (username.Length > 0 && password.Length > 0)
{
conn_opts.Username = username;
conn_opts.Password = password;
}
else if (username.Length > 0 || password.Length > 0)
{
// Throw an exception if username or password were specified, but not both
throw new ArgumentException("Please specify username and password");
}
ManagementScope scope = new ManagementScope(@"\\" + system + @"\root\cimv2", conn_opts);
// Initialize WMI query
string queryStr = "select * from Win32_process where name = '" + image + "'";
string procID = image;
// Override the query if PID was specified
if (pid_set)
{
queryStr = "select * from Win32_process where ProcessId = " + pid_str.Replace(",", " OR ProcessID = ");
procID = "PID: " + pid_str.Replace(",", ", ");
}
int numProcesses = 0;
SelectQuery query = new SelectQuery(queryStr);
// Execute query within scope and iterate through results
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
ManagementObjectCollection collection = searcher.Get();
Console.WriteLine("Attempting to terminate " + GetNumProcStr(collection.Count));
foreach (ManagementObject process in collection)
{
try
{
process.InvokeMethod("Terminate", null);
numProcesses++;
}
catch (Exception e)
{
throw new Exception(String.Format("PID {0} {1}", process.Properties["ProcessId"].Value, e.Message.Trim().ToLower()));
}
}
}
Console.WriteLine("Terminated " + GetNumProcStr(numProcesses) + " (" + procID + ") on " + system);
}
catch (Exception e)
{
// Catch errors like connection timeouts
Console.Error.WriteLine("[-] ERROR: {0}", e.Message.Trim());
}
finally
{
Console.WriteLine("\nDONE");
}
}
}