-
Notifications
You must be signed in to change notification settings - Fork 4
/
SimTask.cs
173 lines (163 loc) · 5.89 KB
/
SimTask.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
using System;
using System.Collections.Generic;
using System.Xml;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace Sim
{
class SimTask
{
private XmlNode node;
private int pauseTime = 5000;
private int numLoops = 1;
private bool killChildren = false;
private string errorDir;
private string errorLog;
private List<Process> spawnedProcesses = new List<Process>();
public SimTask(XmlNode _node, string _errorDir)
{
this.node = _node;
errorDir = _errorDir;
ParseNodeConfig();
}
private void ParseNodeConfig()
{
var configNode = node.SelectSingleNode("config");
if (configNode == null)
throw new Exception("Failed to get config handle.");
var name = configNode.SelectSingleNode("name");
string fileName;
if (name == null)
{
fileName = Guid.NewGuid().ToString() + ".log";
} else
{
fileName = name.InnerText + ".log";
}
errorLog = Path.Combine(errorDir, fileName);
var pause = configNode.SelectSingleNode("pause");
if (pause != null)
{
if (!int.TryParse(pause.InnerText, out pauseTime))
{
Console.WriteLine($"[-] Failed to parse integer pause time from pause value of {pause.InnerText}");
}
}
var loop = configNode.SelectSingleNode("loop");
if (loop != null)
{
if (!int.TryParse(loop.InnerText, out numLoops))
{
Console.WriteLine($"[-] Failed to parse integer loop number from loop value of {loop.InnerText}");
}
}
}
private void LogError(Exception ex)
{
if (!File.Exists(errorLog))
{
using (StreamWriter sw = File.CreateText(errorLog))
{
sw.WriteLine(ex.Message);
sw.WriteLine(ex.StackTrace);
}
}
else
{
using (StreamWriter sw = File.AppendText(errorLog))
{
sw.WriteLine(ex.Message);
sw.WriteLine(ex.StackTrace);
}
}
}
public void Run()
{
for (int l = 0; l < numLoops; l++)
{
var actionsNode = node.SelectSingleNode("actions");
if (actionsNode == null)
throw new Exception("Invalid XML schema for task node. Reason: No actions tag defined.");
XmlNode childNode = actionsNode.FirstChild;
while (childNode != null)
{
try
{
switch (childNode.Name.ToLower())
{
case "setclipboard":
string clipValue = childNode.InnerText;
Keyboard.ClipSet(clipValue);
break;
case "getclipboard":
Keyboard.ClipGet();
break;
case "plain":
string keyvalue = childNode.InnerText;
Keyboard.KeyInput(keyvalue);
break;
case "special":
string keyValue = childNode.InnerText;
Keyboard.SkeyInput(keyValue);
break;
case "process":
string process = childNode.InnerText;
UserProcess(process);
break;
case "kill":
killChildren = true;
KillChildren(killChildren);
break;
case "powershell":
string powershell = childNode.InnerText;
UserPowershell(powershell);
break;
case "mount":
string path = childNode.InnerText;
MountDrive(path);
break;
case "sleep":
int.TryParse(childNode.InnerText, out int sleep);
Thread.Sleep(sleep);
break;
default:
break;
}
}
catch (Exception ex)
{
LogError(ex);
}
childNode = childNode.NextSibling;
Thread.Sleep(pauseTime);
}
}
}
private void KillChildren(bool killChildren)
{
if (killChildren == true)
{
foreach (var proc in spawnedProcesses)
{
proc.Kill();
}
}
}
private void UserProcess(string process)
{
var proc = Process.Start(process);
spawnedProcesses.Add(proc);
}
static public void UserPowershell(string powershell)
{
string s = PowerShellRunner.InvokePS(powershell);
Console.WriteLine(s);
}
static public void MountDrive(string path)
{
string argument = "use K: " + path;
Process.Start("net.exe", argument);
}
}
}