-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
BeaconProgram.cs
199 lines (166 loc) · 6.52 KB
/
BeaconProgram.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
using NtApiDotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace BeaconEye {
public enum Action : int {
NONE,
append,
prepend,
base64,
print,
parameter,
header,
BUILD,
netbios,
_PARAMETER,
_HEADER,
netbiosu,
uri_append,
base64url,
strrep,
mask,
hostheader,
}
public class Statement {
public Statement(Action action, byte[] argument) {
Action = action;
Argument = argument;
}
public Action Action { get; set; }
public byte[] Argument { get; set; }
public override string ToString() {
string action = Action.ToString();
if(Action == Action._HEADER) {
action = "header";
}else if(Action == Action._PARAMETER) {
action = "parameter";
}
return $"{action} {Encoding.ASCII.GetString(Argument)};";
}
}
public class BeaconProgram {
public List<Statement> Statements { get; set; } = new List<Statement>();
byte[] NetBIOSDecode(byte[] source, bool upper) {
byte baseChar = (byte)(upper ? 0x41 : 0x61);
byte[] result = new byte[source.Length / 2];
for (int idx = 0; idx < source.Length; idx += 2){
result[idx/2] = ((byte)(((source[idx] - baseChar) << 4) | (source[idx + 1] - baseChar) & 0xf));
}
return result;
}
byte[] MaskDecode(byte[] source) {
var xorKey = source.Take(4).ToArray();
var result = source.Skip(4).ToArray();
for(int idx = 0; idx< result.Length; ++idx) {
result[idx] ^= xorKey[idx % 4];
}
return result;
}
public byte[] RecoverOutput(byte[] source) {
bool decode = false;
byte[] decoded = source;
bool done = false;
var outputStatements = new List<Statement>();
foreach (var statement in Statements) {
switch (statement.Action) {
case Action.BUILD:
int buildType = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(statement.Argument, 0));
if (buildType == 1) {
decode = true;
}
break;
case Action.print:
if (decode) {
done = true;
outputStatements.Reverse();
}
break;
default:
if (decode) {
outputStatements.Add(statement);
}
break;
}
if (done)
break;
}
foreach(var statement in outputStatements) {
switch (statement.Action) {
case Action.base64url:
decoded = Convert.FromBase64String(Uri.UnescapeDataString(Encoding.ASCII.GetString(decoded)));
break;
case Action.base64:
decoded = Convert.FromBase64String(Encoding.ASCII.GetString(decoded));
break;
case Action.prepend:
decoded = decoded.Skip(statement.Argument.Length).ToArray();
break;
case Action.append:
decoded = decoded.Take(decoded.Length - statement.Argument.Length).ToArray();
break;
case Action.netbiosu:
decoded = NetBIOSDecode(decoded, true);
break;
case Action.netbios:
decoded = NetBIOSDecode(decoded, false);
break;
case Action.mask:
decoded = MaskDecode(decoded);
break;
default:
throw new NotImplementedException($"Statment with action {statement.Action} currently not supported, please open an issue on GitHub");
}
}
return decoded;
}
internal static BeaconProgram Parse(long address, ProcessReader process) {
;
var result = new BeaconProgram();
bool done = false;
while (!done) {
var action = (Action)IPAddress.NetworkToHostOrder(process.ReadMemory<int>((ulong)address));
address += 4;
var actionParameter = new byte[0];
switch (action) {
case Action.NONE:
done = true;
break;
case Action.append:
case Action.prepend:
case Action._HEADER:
case Action.header:
case Action._PARAMETER:
case Action.parameter:
case Action.hostheader:
case Action.uri_append:
case Action.base64url:
int actionParamLen = IPAddress.NetworkToHostOrder(process.ReadMemory<int>((ulong)address));
address += 4;
actionParameter = process.ReadMemory((ulong)address, actionParamLen);
address += actionParamLen;
break;
case Action.BUILD:
actionParameter = process.ReadMemory((ulong)address, 4);
address += 4;
break;
case Action.base64:
case Action.print:
case Action.netbios:
case Action.netbiosu:
case Action.strrep:
case Action.mask:
break;
}
if (action != Action.NONE) {
result.Statements.Add(new Statement(action, actionParameter));
}
}
return result;
}
}
}