ConfuserEx/Confuser.DynCipher/Utils.cs

83 строки
2.5 KiB
C#
Исходник Обычный вид История

2014-05-27 15:45:37 +04:00
using System.IO;
2014-03-09 19:36:33 +04:00
using Confuser.DynCipher.Generation;
2014-03-05 20:36:15 +04:00
2014-05-27 15:45:37 +04:00
namespace Confuser.DynCipher {
public static class MathsUtils {
private const ulong MODULO32 = 0x100000000;
2014-03-05 20:36:15 +04:00
2014-05-27 15:45:37 +04:00
public static ulong modInv(ulong num, ulong mod) {
ulong a = mod, b = num % mod;
ulong p0 = 0, p1 = 1;
while (b != 0) {
if (b == 1) return p1;
p0 += (a / b) * p1;
a = a % b;
2014-03-05 20:36:15 +04:00
2014-05-27 15:45:37 +04:00
if (a == 0) break;
if (a == 1) return mod - p0;
2014-03-05 20:36:15 +04:00
2014-05-27 15:45:37 +04:00
p1 += (b / a) * p0;
b = b % a;
}
return 0;
}
2014-03-09 19:01:34 +04:00
2014-05-27 15:45:37 +04:00
public static uint modInv(uint num) {
return (uint) modInv(num, MODULO32);
}
2014-03-09 19:36:33 +04:00
2014-05-27 15:45:37 +04:00
public static byte modInv(byte num) {
return (byte) modInv(num, 0x100);
}
}
public static class CodeGenUtils {
public static byte[] AssembleCode(x86CodeGen codeGen, x86Register reg) {
var stream = new MemoryStream();
using (var writer = new BinaryWriter(stream)) {
/*
2014-03-09 19:36:33 +04:00
* mov eax, esp
* push ebx
* push edi
* push esi
* sub eax, esp
* cmp eax, 24 ; determine the bitness of platform
* je n
* mov eax, [esp + 4] ; 32 bits => argument in stack
* push eax
* jmp z
* n: push ecx ; 64 bits => argument in register
* z: XXX
* pop esi
* pop edi
* pop ebx
* pop ret
*
*/
2014-05-27 15:45:37 +04:00
writer.Write(new byte[] { 0x89, 0xe0 });
writer.Write(new byte[] { 0x53 });
writer.Write(new byte[] { 0x57 });
writer.Write(new byte[] { 0x56 });
writer.Write(new byte[] { 0x29, 0xe0 });
writer.Write(new byte[] { 0x83, 0xf8, 0x18 });
writer.Write(new byte[] { 0x74, 0x07 });
writer.Write(new byte[] { 0x8b, 0x44, 0x24, 0x10 });
writer.Write(new byte[] { 0x50 });
writer.Write(new byte[] { 0xeb, 0x01 });
writer.Write(new byte[] { 0x51 });
2014-03-09 19:36:33 +04:00
2014-05-27 15:45:37 +04:00
foreach (x86Instruction i in codeGen.Instructions)
writer.Write(i.Assemble());
2014-03-09 19:36:33 +04:00
2014-05-27 15:45:37 +04:00
if (reg != x86Register.EAX)
writer.Write(x86Instruction.Create(x86OpCode.MOV, new x86RegisterOperand(x86Register.EAX), new x86RegisterOperand(reg)).Assemble());
2014-03-09 19:36:33 +04:00
2014-05-27 15:45:37 +04:00
writer.Write(new byte[] { 0x5e });
writer.Write(new byte[] { 0x5f });
writer.Write(new byte[] { 0x5b });
writer.Write(new byte[] { 0xc3 });
}
return stream.ToArray();
}
}
}