Instructions are not duplicated anymore.
This commit is contained in:
Martin Karing 2020-12-24 13:58:47 +01:00
Родитель 992fa1414b
Коммит e2f8d09c36
1 изменённых файлов: 10 добавлений и 20 удалений

Просмотреть файл

@ -25,34 +25,19 @@ namespace Confuser.Protections.ControlFlow {
if (block.Instructions[i].OpCode.OpCodeType == OpCodeType.Prefix) {
skipCount = 1;
currentFragment.Add(block.Instructions[i]);
}
if (i + 2 < block.Instructions.Count &&
block.Instructions[i + 0].OpCode.Code == Code.Dup &&
block.Instructions[i + 1].OpCode.Code == Code.Ldvirtftn &&
block.Instructions[i + 2].OpCode.Code == Code.Newobj) {
else if (HasInstructionSeq(block.Instructions, i,Code.Dup, Code.Ldvirtftn, Code.Newobj)) {
skipCount = 2;
currentFragment.Add(block.Instructions[i]);
}
if (i + 4 < block.Instructions.Count &&
block.Instructions[i + 0].OpCode.Code == Code.Ldc_I4 &&
block.Instructions[i + 1].OpCode.Code == Code.Newarr &&
block.Instructions[i + 2].OpCode.Code == Code.Dup &&
block.Instructions[i + 3].OpCode.Code == Code.Ldtoken &&
block.Instructions[i + 4].OpCode.Code == Code.Call) // Array initializer
{
else if (HasInstructionSeq(block.Instructions, i,Code.Ldc_I4, Code.Newarr, Code.Dup, Code.Ldtoken, Code.Call)) { // Array initializer
skipCount = 4;
currentFragment.Add(block.Instructions[i]);
}
if (i + 1 < block.Instructions.Count &&
block.Instructions[i + 0].OpCode.Code == Code.Ldftn &&
block.Instructions[i + 1].OpCode.Code == Code.Newobj) {
else if (HasInstructionSeq(block.Instructions, i,Code.Ldftn, Code.Newobj)) { // Create delegate to function
skipCount = 1;
currentFragment.Add(block.Instructions[i]);
}
currentFragment.Add(block.Instructions[i]);
if (ctx.Intensity > ctx.Random.NextDouble()) {
if (skipCount == -1 && ctx.Intensity > ctx.Random.NextDouble()) {
fragments.AddLast(currentFragment.ToArray());
currentFragment.Clear();
}
@ -64,6 +49,11 @@ namespace Confuser.Protections.ControlFlow {
return fragments;
}
private static bool HasInstructionSeq(List<Instruction> instructions, int offset, params Code[] codes) {
if (offset + codes.Length > instructions.Count) return false;
return !codes.Where((code, i) => instructions[i + offset].OpCode.Code != code).Any();
}
public override void Mangle(CilBody body, ScopeBlock root, CFContext ctx) {
body.MaxStack++;
foreach (InstrBlock block in GetAllBlocks(root)) {
@ -92,4 +82,4 @@ namespace Confuser.Protections.ControlFlow {
}
}
}
}
}