Skip to content
This repository was archived by the owner on Oct 5, 2019. It is now read-only.

Commit 959e8db

Browse files
author
Zaczero
committed
Added more trash options
1 parent e8c45a2 commit 959e8db

File tree

1 file changed

+81
-24
lines changed

1 file changed

+81
-24
lines changed

SharpLoader/Core/SourceRandomizer.cs

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,30 @@ public string GetRandomString(int length)
3838
return rndStrSb.ToString();
3939
}
4040

41+
private string _getVariableStringLastStr;
42+
private List<string> _getVariableStringGenerated;
4143
private string GetVariableName(string str)
4244
{
45+
// New source file
46+
if (_getVariableStringLastStr != str)
47+
{
48+
_getVariableStringLastStr = str;
49+
_getVariableStringGenerated = new List<string>();
50+
}
51+
4352
string varName;
4453

4554
while (true)
4655
{
4756
varName = GetRandomString(_rnd.Next(8, 16 + 1));
48-
if (!str.Contains(varName))
57+
if (!str.Contains(varName) && _getVariableStringGenerated.All(g => g != varName))
4958
{
5059
break;
5160
}
5261
}
5362

63+
_getVariableStringGenerated.Add(varName);
64+
5465
return varName;
5566
}
5667

@@ -139,7 +150,7 @@ private void Encode(ref string str)
139150
{
140151
if (str[i] == ';')
141152
{
142-
resultSb.Append('\u0000');
153+
resultSb.Append('\0');
143154
continue;
144155
}
145156
if (str[i] == '<')
@@ -234,16 +245,14 @@ private void Trash(ref string str)
234245
// Indexes:
235246
// 0 - string trash
236247
// 1 - value trash
237-
var trashChances = new[] { 1, 3 };
248+
// 2 - object trash
249+
var trashChances = new[] { 25, 60, 15 };
238250
var allTrashChance = trashChances.Sum();
239251

240252
// Add all before values
241253
for (var j = 1; j < trashChances.Length; j++)
242254
{
243-
for (var k = 0; k < j; k++)
244-
{
245-
trashChances[j] += trashChances[k];
246-
}
255+
trashChances[j] += trashChances[j - 1];
247256
}
248257

249258
// Add trash
@@ -259,7 +268,7 @@ private void Trash(ref string str)
259268
var varValue = GetRandomString(_rnd.Next(8, 32 + 1));
260269
string operation;
261270

262-
switch (_rnd.Next(0, 3))
271+
switch (_rnd.Next(0, 6))
263272
{
264273
case 0:
265274
{
@@ -276,6 +285,21 @@ private void Trash(ref string str)
276285
operation = "ToUpper()";
277286
break;
278287
}
288+
case 3:
289+
{
290+
operation = "ToLowerInvariant()";
291+
break;
292+
}
293+
case 4:
294+
{
295+
operation = "ToUpperInvariant()";
296+
break;
297+
}
298+
case 5:
299+
{
300+
operation = "ToCharArray()";
301+
break;
302+
}
279303
default:
280304
{
281305
throw new Exception("invalid switch value");
@@ -323,7 +347,50 @@ private void Trash(ref string str)
323347
}
324348
}
325349

326-
trash += $"{varType} {varName}={varValue}\u0000{varName}{operation}={varChange};";
350+
trash += $"{varType} {varName}={varValue}\0{varName}{operation}={varChange};";
351+
}
352+
else if (rndValue < trashChances[trashChancesIndex++])
353+
{
354+
// object trash
355+
var operation = _rnd.Next(0, 2) == 0 ? "GetHashCode()" : "GetTypeCode()";
356+
357+
switch (_rnd.Next(0, 6))
358+
{
359+
case 0:
360+
{
361+
trash += $"new byte().{operation};";
362+
break;
363+
}
364+
case 1:
365+
{
366+
trash += $"new bool().{operation};";
367+
break;
368+
}
369+
case 2:
370+
{
371+
trash += $"new char().{operation};";
372+
break;
373+
}
374+
case 3:
375+
{
376+
trash += $"new short().{operation};";
377+
break;
378+
}
379+
case 4:
380+
{
381+
trash += $"new int().{operation};";
382+
break;
383+
}
384+
case 5:
385+
{
386+
trash += $"new long().{operation};";
387+
break;
388+
}
389+
default:
390+
{
391+
throw new Exception("invalid switch value");
392+
}
393+
}
327394
}
328395
}
329396

@@ -379,17 +446,7 @@ private void Flow(ref string str)
379446

380447
// Generate variable names
381448
var switchVarName = GetVariableName(str);
382-
string exitLoopVarName;
383-
384-
while (true)
385-
{
386-
exitLoopVarName = GetVariableName(str);
387-
388-
if (exitLoopVarName != switchVarName)
389-
{
390-
break;
391-
}
392-
}
449+
var exitLoopVarName = GetVariableName(str);
393450

394451
var cases = new string[switchValues.Length];
395452

@@ -399,18 +456,18 @@ private void Flow(ref string str)
399456
// Last
400457
if (i + 1 == cases.Length)
401458
{
402-
cases[i] = $"case {switchValues[i]}:{{{blocks[i]}{exitLoopVarName}=false\u0000break\u0000}}";
459+
cases[i] = $"case {switchValues[i]}:{{{blocks[i]}{exitLoopVarName}=false\0break\0}}";
403460
}
404461
// Not last
405462
else
406463
{
407-
cases[i] = $"case {switchValues[i]}:{{{blocks[i]}{switchVarName}={switchValues[i + 1]}\u0000break\u0000}}<block>";
464+
cases[i] = $"case {switchValues[i]}:{{{blocks[i]}{switchVarName}={switchValues[i + 1]}\0break\0}}<block>";
408465
}
409466
}
410467

411468
// Generate output
412469
var caseOutput = cases.Aggregate(string.Empty, (current, c) => current + c);
413-
var output = $"int {switchVarName}={switchValues[0]}\u0000bool {exitLoopVarName}=true\u0000while({exitLoopVarName}){{switch({switchVarName}){{<swap>{caseOutput}<swap/>}}}}";
470+
var output = $"<swap>int {switchVarName}={switchValues[0]};bool {exitLoopVarName}=true;<swap/>while({exitLoopVarName}){{switch({switchVarName}){{<swap>{caseOutput}<swap/>}}}}";
414471

415472
// Remove old
416473
str = str.Remove(tagIndex, tagLength + endTagIndex + endTagLength);
@@ -492,7 +549,7 @@ private void Decode(ref string str)
492549
insideString = !insideString;
493550
}
494551

495-
if (str[i] == '\u0000')
552+
if (str[i] == '\0')
496553
{
497554
str = str.Remove(i, 1).Insert(i, ";");
498555
}

0 commit comments

Comments
 (0)