-
Notifications
You must be signed in to change notification settings - Fork 6
/
sunburst_decoder.cs
319 lines (310 loc) · 11.8 KB
/
sunburst_decoder.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Zcg.Tools.Misc
{
class SunBrustDecoder
{
static void Main(string[] args)
{
if (args.Length != 1) { Console.WriteLine("usage: sunbrust_decoder <dga_list.lst>"); return; }
string k = "ph2eifo3n5utg1j8d94qrvbmk0sal76c";
Dictionary<string, string[]> dic = new Dictionary<string, string[]>();
Dictionary<string, List<DateTime>> dic2 = new Dictionary<string, List<DateTime>>();
Dictionary<string, string> dic3 = new Dictionary<string, string>();
Dictionary<string, List<string>> dic4 = new Dictionary<string, List<string>>();
foreach (string s in File.ReadAllLines(args[0]))
{
if (s.Length > 16)
{
string s1 = s.Substring(0, 15);
byte[] data = base32_decode(k, s);
byte[] guid = null;
if ((data[0] & 0x80) == 0x80)
//step 1: first bit set
//OrionImprovementBusinessLayer+CryptoHelper.CreateSecureString with flag=true
//OrionImprovementBusinessLayer+CryptoHelper.GetPreviousString
//domain name fragment sender
{
char c = s[15];
//OrionImprovementBusinessLayer+CryptoHelper.CreateString decoder
int ncount = 0;//OrionImprovementBusinessLayer+CryptoHelper.nCount
if (c >= '0' && c <= '9')
{
ncount = c - '0';
}
else if (c >= 'a' && c <= 'z')
{
ncount = c - 'a' + 10;
}
else
{
Console.WriteLine("err parse ncount: " + s);
}
ncount = ncount - s[0];
while (ncount < 0) { ncount += 36; }
//OrionImprovementBusinessLayer.userId, created by
//OrionImprovementBusinessLayer.GetOrCreateUserID
guid = guid_decode(k, s1);
string dk = BitConverter.ToString(guid).Replace("-", "");
//save all fragments
if (!dic.ContainsKey(dk))
{
dic[dk] = new string[36];
}
string[] d = dic[dk];
d[ncount] = s.Substring(16, s.Length - 16);
}
else
//step 2
//OrionImprovementBusinessLayer+CryptoHelper.CreateSecureString with flag=false
//OrionImprovementBusinessLayer+CryptoHelper.GetNextString
//or OrionImprovementBusinessLayer+CryptoHelper.GetNextStringEx
{
DateTime dt;
if (parse_dga_time(s, out guid, out dt))
{
string dk = BitConverter.ToString(guid).Replace("-", "");
if (!dic2.ContainsKey(dk))
{
dic2[dk] = new List<DateTime>();
}
List<DateTime> ls = dic2[dk];
ls.Add(dt);
}
else
{
Console.WriteLine("err parse time: " + s);
}
}
if (guid != null)
{
string dk = BitConverter.ToString(guid).Replace("-", "");
if (!dic4.ContainsKey(dk))
{
dic4[dk] = new List<string>();
}
dic4[dk].Add(s);
}
}
}
foreach (var kp in dic)
{
bool comp = false;
string s = "";
string r = null;
string[] v = kp.Value;
for (int i = 0; i < v.Length; i++)
{
if (v[i] != null)
{
s += v[i];
}
else
{
break;
}
}
if (v[35] != null)
{
s += v[35];
comp = true;
}
if (comp)
{
if (s.StartsWith("00"))
{
r = Encoding.UTF8.GetString(base32_decode(k, s.Substring(2)));
}
else
{
r = change_char_map(s);
}
}
else
{
r = "not completed request: " + s + ", ";
if (s.StartsWith("00"))
{
r += Encoding.UTF8.GetString(base32_decode(k, s.Substring(2)));
}
else
{
try { r += change_char_map(s); } catch { r += "char map err"; }
}
}
if (dic3.ContainsKey(kp.Key))
{
dic3[kp.Key] = "," + dic3[kp.Key] + r;
}
else
{
dic3[kp.Key] = r;
}
}
StringBuilder sb = new StringBuilder();
foreach (var kp in dic3)
{
sb.AppendFormat("{0},\"{1}\"\r\n", kp.Key, kp.Value);
}
File.WriteAllText("all_domains_dga.txt", sb.ToString());
sb.Clear();
List<DateTime> dts = new List<DateTime>();
foreach (var kp in dic2)
{
sb.AppendFormat("{0},\"", kp.Key);
foreach (DateTime dt in kp.Value)
{
sb.Append(dt);
dts.Add(dt);
sb.Append(",");
}
sb.Append("\"\r\n");
}
File.WriteAllText("all_times_dga.txt", sb.ToString());
sb.Clear();
foreach (var kp in dic2)
{
if (dic.ContainsKey(kp.Key))
{
sb.AppendFormat("{0},\"", kp.Key);
foreach (DateTime dt in kp.Value)
{
sb.Append(dt);
dts.Add(dt);
sb.Append(",");
}
sb.Append("\"\r\n");
}
}
File.WriteAllText("all_times_contains_dga.txt", sb.ToString());
dts.Sort();
sb.Clear();
foreach (DateTime dt in dts)
{
sb.Append(dt + "\r\n");
}
File.WriteAllText("all_times.txt", sb.ToString());
sb.Clear();
foreach (var kp in dic4)
{
sb.AppendFormat("{0},\"", kp.Key);
foreach (string ss in kp.Value)
{
sb.Append(ss);
sb.Append(",");
}
sb.Append("\"");
sb.Append("\r\n");
}
File.WriteAllText("all_domain_mapping.txt", sb.ToString());
}
static string change_char_map(string s)
{
string k = "rq3gsalt6u1iyfzop572d49bnx8cvmkewhj";
string k2 = "0_-.";
string ret = "";
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (c == '0')
{
i++;
c = s[i];
if (k.IndexOf(c) != -1)
{
ret += k2[k.IndexOf(c) % 4];
}
else
{
return "err";
}
}
else if (k.IndexOf(c) != -1)
{
ret += k[(k.IndexOf(c) - 4 + k.Length) % k.Length];
}
else
{
return "err";
}
}
return ret;
}
public static bool parse_dga_time(string s, out byte[] guid, out DateTime dt)
{
try
{
if (s.Length > 18 && s[16] == '0' && s[17] == '0') { dt = DateTime.Now; guid = null; return false; }
byte[] data = base32_decode("ph2eifo3n5utg1j8d94qrvbmk0sal76c", s);
byte[] data2 = new byte[data.Length - 1];
for (int i = 1; i < data2.Length; i++)
{
data2[i - 1] = (byte)(data[i] ^ data[0]);
}
data = data2;
for (int i = 0; i < 8; i++)
{
data[i] ^= data[8 + 2 - i % 2];
}
int i1 = data[8] & 0xf;
int i2 = data[9];
int i3 = data[10];
int dti = (i1 << 16) | (i2 << 8) | i3;
dti >>= 1;
dt = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMinutes(dti * 30);
if (dt > new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc) && dt < DateTime.UtcNow)
{
guid = new byte[8];
for (int i = 0; i < 8; i++) { guid[i] = data[i]; }
return true;
}
guid = null;
dt = DateTime.Now;
return false;
}
catch { dt = DateTime.Now; guid = null; return false; }
}
public static byte[] guid_decode(string k, string s)
{
byte[] data = base32_decode(k, s);
byte[] ret = new byte[8];
for (int i = 1; i < 9; i++)
{
ret[i - 1] = (byte)(data[i] ^ data[0]);
}
return ret;
}
//copy-paste from https://stackoverflow.com/questions/641361/base32-decoding
public static byte[] base32_decode(string ValidChars, string str)
{
int numBytes = str.Length * 5 / 8;
byte[] bytes = new Byte[numBytes];
int bit_buffer;
int currentCharIndex;
int bits_in_buffer;
if (str.Length < 3)
{
bytes[0] = (byte)(ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
return bytes;
}
bit_buffer = (ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
bits_in_buffer = 10;
currentCharIndex = 2;
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)bit_buffer;
bit_buffer >>= 8;
bits_in_buffer -= 8;
while (bits_in_buffer < 8 && currentCharIndex < str.Length)
{
bit_buffer |= ValidChars.IndexOf(str[currentCharIndex++]) << bits_in_buffer;
bits_in_buffer += 5;
}
}
return bytes;
}
}
}