Add InitializedDataCreator class

This commit is contained in:
de4dot 2011-12-29 08:22:41 +01:00
parent 959b6c8229
commit b930e8fd97
2 changed files with 82 additions and 0 deletions

View File

@ -107,6 +107,7 @@
<Compile Include="deobfuscators\IDeobfuscatedFile.cs" />
<Compile Include="deobfuscators\IDeobfuscator.cs" />
<Compile Include="deobfuscators\IDeobfuscatorInfo.cs" />
<Compile Include="deobfuscators\InitializedDataCreator.cs" />
<Compile Include="deobfuscators\InlinedMethodsFinder.cs" />
<Compile Include="deobfuscators\ISimpleDeobfuscator.cs" />
<Compile Include="deobfuscators\MethodCollection.cs" />

View File

@ -0,0 +1,81 @@
/*
Copyright (C) 2011 de4dot@gmail.com
This file is part of de4dot.
de4dot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
de4dot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using Mono.Cecil;
using de4dot.blocks;
namespace de4dot.code.deobfuscators {
class InitializedDataCreator {
ModuleDefinition module;
Dictionary<long, TypeDefinition> sizeToArrayType = new Dictionary<long, TypeDefinition>();
TypeDefinition ourType;
TypeReference valueType;
int unique = 0;
public InitializedDataCreator(ModuleDefinition module) {
this.module = module;
}
void createOurType() {
if (ourType != null)
return;
var attrs = TypeAttributes.NotPublic | TypeAttributes.AutoLayout |
TypeAttributes.Class | TypeAttributes.AnsiClass;
ourType = new TypeDefinition("", string.Format("<PrivateImplementationDetails>{0}", Guid.NewGuid().ToString("B")), attrs, module.TypeSystem.Object);
ourType.MetadataToken = DotNetUtils.nextTypeDefToken();
module.Types.Add(ourType);
}
TypeDefinition getArrayType(long size) {
createOurType();
TypeDefinition arrayType;
if (sizeToArrayType.TryGetValue(size, out arrayType))
return arrayType;
if (valueType == null)
valueType = DotNetUtils.findOrCreateTypeReference(module, module.TypeSystem.Corlib as AssemblyNameReference, "System", "ValueType", false);
var attrs = TypeAttributes.NestedPrivate | TypeAttributes.ExplicitLayout |
TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.AnsiClass;
arrayType = new TypeDefinition("", string.Format("__StaticArrayInitTypeSize={0}", size), attrs, valueType);
arrayType.MetadataToken = DotNetUtils.nextTypeDefToken();
ourType.NestedTypes.Add(arrayType);
sizeToArrayType[size] = arrayType;
arrayType.ClassSize = (int)size;
arrayType.PackingSize = 1;
arrayType.IsValueType = true;
return arrayType;
}
public FieldDefinition create(byte[] data) {
var arrayType = getArrayType(data.LongLength);
var attrs = FieldAttributes.Assembly | FieldAttributes.Static;
var field = new FieldDefinition(string.Format("field_{0}", unique++), attrs, arrayType);
field.MetadataToken = DotNetUtils.nextFieldToken();
ourType.Fields.Add(field);
var iv = new byte[data.Length];
Array.Copy(data, iv, data.Length);
field.InitialValue = iv;
return field;
}
}
}