rva data支持简单加密
parent
6bc9192c13
commit
79bf659e65
|
@ -31,6 +31,7 @@ namespace Obfuz.Emit
|
|||
|
||||
private readonly ModuleDef _module;
|
||||
private readonly IRandom _random;
|
||||
private readonly IEncryptor _encryptor;
|
||||
|
||||
|
||||
class RvaField
|
||||
|
@ -59,10 +60,11 @@ namespace Obfuz.Emit
|
|||
|
||||
private readonly Dictionary<int, TypeDef> _dataHolderTypeBySizes = new Dictionary<int, TypeDef>();
|
||||
|
||||
public ModuleRvaDataAllocator(ModuleDef mod, IRandom random)
|
||||
public ModuleRvaDataAllocator(ModuleDef mod, IRandom random, IEncryptor encryptor)
|
||||
{
|
||||
_module = mod;
|
||||
_random = random;
|
||||
_encryptor = encryptor;
|
||||
}
|
||||
|
||||
private (FieldDef, FieldDef) CreateDataHolderRvaField(TypeDef dataHolderType)
|
||||
|
@ -259,7 +261,9 @@ namespace Obfuz.Emit
|
|||
{
|
||||
field.FillPadding();
|
||||
}
|
||||
field.holderDataField.InitialValue = field.bytes.ToArray();
|
||||
byte[] data = field.bytes.ToArray();
|
||||
_encryptor.EncryptBytes(data, field.minorSecret);
|
||||
field.holderDataField.InitialValue = data;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -274,18 +278,20 @@ namespace Obfuz.Emit
|
|||
{
|
||||
|
||||
private readonly IRandom _random;
|
||||
private readonly IEncryptor _encryptor;
|
||||
private readonly Dictionary<ModuleDef, ModuleRvaDataAllocator> _modules = new Dictionary<ModuleDef, ModuleRvaDataAllocator>();
|
||||
|
||||
public RvaDataAllocator(IRandom random)
|
||||
public RvaDataAllocator(IRandom random, IEncryptor encryptor)
|
||||
{
|
||||
_random = random;
|
||||
_encryptor = encryptor;
|
||||
}
|
||||
|
||||
private ModuleRvaDataAllocator GetModuleRvaDataAllocator(ModuleDef mod)
|
||||
{
|
||||
if (!_modules.TryGetValue(mod, out var allocator))
|
||||
{
|
||||
allocator = new ModuleRvaDataAllocator(mod, _random);
|
||||
allocator = new ModuleRvaDataAllocator(mod, _random, _encryptor);
|
||||
_modules.Add(mod, allocator);
|
||||
}
|
||||
return allocator;
|
||||
|
|
|
@ -13,12 +13,14 @@ namespace Obfuz.Virtualization
|
|||
private readonly RandomDataNodeCreator _nodeCreator;
|
||||
private readonly RvaDataAllocator _rvaDataAllocator;
|
||||
private readonly ConstFieldAllocator _constFieldAllocator;
|
||||
private readonly IEncryptor _encryptor;
|
||||
|
||||
public DefaultDataObfuscator()
|
||||
{
|
||||
_random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5);
|
||||
_encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D });
|
||||
_nodeCreator = new RandomDataNodeCreator(_random);
|
||||
_rvaDataAllocator = new RvaDataAllocator(_random);
|
||||
_rvaDataAllocator = new RvaDataAllocator(_random, _encryptor);
|
||||
_constFieldAllocator = new ConstFieldAllocator(_random);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public class DefaultEncryptor : IEncryptor
|
||||
{
|
||||
private readonly byte[] _key;
|
||||
|
||||
public DefaultEncryptor(byte[] key)
|
||||
{
|
||||
_key = key;
|
||||
}
|
||||
|
||||
public void EncryptBytes(byte[] data, int minorSecret)
|
||||
{
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
data[i] ^= (byte)(_key[i % _key.Length] ^ minorSecret);
|
||||
}
|
||||
}
|
||||
|
||||
public void DecryptBytes(byte[] data, int minorSecret)
|
||||
{
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
data[i] ^= (byte)(_key[i % _key.Length] ^ minorSecret);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,9 +8,11 @@ namespace Obfuz
|
|||
{
|
||||
public static class EncryptionService
|
||||
{
|
||||
private static readonly IEncryptor _encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D });
|
||||
|
||||
public static void DecryptBytes(byte[] data, int minorSecret)
|
||||
{
|
||||
|
||||
_encryptor.EncryptBytes(data, minorSecret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public interface IEncryptor
|
||||
{
|
||||
void EncryptBytes(byte[] data, int minorSecret);
|
||||
void DecryptBytes(byte[] data, int minorSecret);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue