diff --git a/Confuser.Core/ConfuserEngine.cs b/Confuser.Core/ConfuserEngine.cs index b7fbbdb99..baabf6703 100644 --- a/Confuser.Core/ConfuserEngine.cs +++ b/Confuser.Core/ConfuserEngine.cs @@ -320,6 +320,10 @@ static void BeginModule(ConfuserContext context) { context.CurrentModuleWriterListener = new ModuleWriterListener(); context.CurrentModuleWriterListener.OnWriterEvent += (sender, e) => context.CheckCancellation(); context.CurrentModuleWriterOptions = new ModuleWriterOptions(context.CurrentModule, context.CurrentModuleWriterListener); + + if (!context.CurrentModule.IsILOnly) + context.RequestNative(); + var snKey = context.Annotations.Get(context.CurrentModule, Marker.SNKey); context.CurrentModuleWriterOptions.InitializeStrongNameSigning(context.CurrentModule, snKey); diff --git a/Confuser.Protections/AntiTamper/JITMode.cs b/Confuser.Protections/AntiTamper/JITMode.cs index a99b38999..2d1c34794 100644 --- a/Confuser.Protections/AntiTamper/JITMode.cs +++ b/Confuser.Protections/AntiTamper/JITMode.cs @@ -151,7 +151,7 @@ public void HandleMD(AntiTamperProtection parent, ConfuserContext context, Prote } void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; if (e.WriterEvent == ModuleWriterEvent.MDBeginWriteMethodBodies) { context.Logger.Debug("Extracting method bodies..."); CreateSection(writer); @@ -162,7 +162,7 @@ void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { } } - void CreateSection(ModuleWriter writer) { + void CreateSection(ModuleWriterBase writer) { // move some PE parts to separate section to prevent it from being hashed var peSection = new PESection("", 0x60000020); bool moved = false; @@ -172,15 +172,18 @@ void CreateSection(ModuleWriter writer) { peSection.Add(writer.StrongNameSignature, alignment); moved = true; } - if (writer.ImportAddressTable != null) { - alignment = writer.TextSection.Remove(writer.ImportAddressTable).Value; - peSection.Add(writer.ImportAddressTable, alignment); - moved = true; - } - if (writer.StartupStub != null) { - alignment = writer.TextSection.Remove(writer.StartupStub).Value; - peSection.Add(writer.StartupStub, alignment); - moved = true; + var managedWriter = writer as ModuleWriter; + if (managedWriter != null) { + if (managedWriter.ImportAddressTable != null) { + alignment = writer.TextSection.Remove(managedWriter.ImportAddressTable).Value; + peSection.Add(managedWriter.ImportAddressTable, alignment); + moved = true; + } + if (managedWriter.StartupStub != null) { + alignment = writer.TextSection.Remove(managedWriter.StartupStub).Value; + peSection.Add(managedWriter.StartupStub, alignment); + moved = true; + } } if (moved) writer.Sections.Add(peSection); @@ -228,7 +231,7 @@ void CreateSection(ModuleWriter writer) { newSection.Add(new ByteArrayChunk(new byte[4]), 4); } - void EncryptSection(ModuleWriter writer) { + void EncryptSection(ModuleWriterBase writer) { Stream stream = writer.DestinationStream; var reader = new BinaryReader(writer.DestinationStream); stream.Position = 0x3C; diff --git a/Confuser.Protections/AntiTamper/NormalMode.cs b/Confuser.Protections/AntiTamper/NormalMode.cs index 9fd9d570a..f40947fb0 100644 --- a/Confuser.Protections/AntiTamper/NormalMode.cs +++ b/Confuser.Protections/AntiTamper/NormalMode.cs @@ -100,7 +100,7 @@ public void HandleMD(AntiTamperProtection parent, ConfuserContext context, Prote } void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; if (e.WriterEvent == ModuleWriterEvent.MDEndCreateTables) { CreateSections(writer); } @@ -109,7 +109,7 @@ void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { } } - void CreateSections(ModuleWriter writer) { + void CreateSections(ModuleWriterBase writer) { var nameBuffer = new byte[8]; nameBuffer[0] = (byte)(name1 >> 0); nameBuffer[1] = (byte)(name1 >> 8); @@ -141,15 +141,18 @@ void CreateSections(ModuleWriter writer) { peSection.Add(writer.StrongNameSignature, alignment); moved = true; } - if (writer.ImportAddressTable != null) { - alignment = writer.TextSection.Remove(writer.ImportAddressTable).Value; - peSection.Add(writer.ImportAddressTable, alignment); - moved = true; - } - if (writer.StartupStub != null) { - alignment = writer.TextSection.Remove(writer.StartupStub).Value; - peSection.Add(writer.StartupStub, alignment); - moved = true; + var managedWriter = writer as ModuleWriter; + if (managedWriter != null) { + if (managedWriter.ImportAddressTable != null) { + alignment = writer.TextSection.Remove(managedWriter.ImportAddressTable).Value; + peSection.Add(managedWriter.ImportAddressTable, alignment); + moved = true; + } + if (managedWriter.StartupStub != null) { + alignment = writer.TextSection.Remove(managedWriter.StartupStub).Value; + peSection.Add(managedWriter.StartupStub, alignment); + moved = true; + } } if (moved) writer.Sections.Add(peSection); @@ -169,7 +172,7 @@ void CreateSections(ModuleWriter writer) { newSection.Add(new ByteArrayChunk(new byte[4]), 4); } - void EncryptSection(ModuleWriter writer) { + void EncryptSection(ModuleWriterBase writer) { Stream stream = writer.DestinationStream; var reader = new BinaryReader(writer.DestinationStream); stream.Position = 0x3C; diff --git a/Confuser.Protections/Compress/ExtractPhase.cs b/Confuser.Protections/Compress/ExtractPhase.cs index de97d3ecd..14c092965 100644 --- a/Confuser.Protections/Compress/ExtractPhase.cs +++ b/Confuser.Protections/Compress/ExtractPhase.cs @@ -65,7 +65,7 @@ public ResourceRecorder(CompressorContext ctx, ModuleDef module) { public void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { if (e.WriterEvent == ModuleWriterEvent.MDEndAddResources) { - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; ctx.ManifestResources = new List>(); Dictionary stringDict = writer.MetaData.StringsHeap.GetAllRawData().ToDictionary(pair => pair.Key, pair => pair.Value); foreach (RawManifestResourceRow resource in writer.MetaData.TablesHeap.ManifestResourceTable) diff --git a/Confuser.Protections/Compress/StubProtection.cs b/Confuser.Protections/Compress/StubProtection.cs index 5c682604f..0fb6fedc0 100644 --- a/Confuser.Protections/Compress/StubProtection.cs +++ b/Confuser.Protections/Compress/StubProtection.cs @@ -90,7 +90,7 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa context.CurrentModuleWriterListener.OnWriterEvent += (sender, e) => { if (e.WriterEvent == ModuleWriterEvent.MDBeginCreateTables) { // Add key signature - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; var prot = (StubProtection)Parent; uint blob = writer.MetaData.BlobHeap.Add(prot.ctx.KeySig); uint rid = writer.MetaData.TablesHeap.StandAloneSigTable.Add(new RawStandAloneSigRow(blob)); diff --git a/Confuser.Protections/Constants/x86Mode.cs b/Confuser.Protections/Constants/x86Mode.cs index 36a6c4e67..7a02d1ef3 100644 --- a/Confuser.Protections/Constants/x86Mode.cs +++ b/Confuser.Protections/Constants/x86Mode.cs @@ -122,7 +122,7 @@ public void Compile(CEContext ctx) { } void InjectNativeCode(object sender, ModuleWriterListenerEventArgs e) { - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; if (e.WriterEvent == ModuleWriterEvent.MDEndWriteMethodBodies) { codeChunk = writer.MethodBodies.Add(new MethodBody(code)); } diff --git a/Confuser.Protections/ControlFlow/x86Predicate.cs b/Confuser.Protections/ControlFlow/x86Predicate.cs index 99023c193..57d492a71 100644 --- a/Confuser.Protections/ControlFlow/x86Predicate.cs +++ b/Confuser.Protections/ControlFlow/x86Predicate.cs @@ -92,7 +92,7 @@ public void Compile(CFContext ctx) { } void InjectNativeCode(object sender, ModuleWriterListenerEventArgs e) { - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; if (e.WriterEvent == ModuleWriterEvent.MDEndWriteMethodBodies) { codeChunk = writer.MethodBodies.Add(new MethodBody(code)); } diff --git a/Confuser.Protections/InvalidMetadataProtection.cs b/Confuser.Protections/InvalidMetadataProtection.cs index df55d47ca..bee3b4043 100644 --- a/Confuser.Protections/InvalidMetadataProtection.cs +++ b/Confuser.Protections/InvalidMetadataProtection.cs @@ -70,7 +70,7 @@ void Randomize(MDTable table) where T : IRawRow { } void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; if (e.WriterEvent == ModuleWriterEvent.MDEndCreateTables) { // These hurts reflection @@ -112,13 +112,13 @@ void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { Randomize(writer.MetaData.TablesHeap.ManifestResourceTable); //Randomize(writer.MetaData.TablesHeap.GenericParamConstraintTable); - writer.Options.MetaDataOptions.TablesHeapOptions.ExtraData = random.NextUInt32(); - writer.Options.MetaDataOptions.TablesHeapOptions.UseENC = false; - writer.Options.MetaDataOptions.MetaDataHeaderOptions.VersionString += "\0\0\0\0"; + writer.TheOptions.MetaDataOptions.TablesHeapOptions.ExtraData = random.NextUInt32(); + writer.TheOptions.MetaDataOptions.TablesHeapOptions.UseENC = false; + writer.TheOptions.MetaDataOptions.MetaDataHeaderOptions.VersionString += "\0\0\0\0"; - writer.Options.MetaDataOptions.OtherHeapsEnd.Add(new RawHeap("#Strings", new byte[1])); - writer.Options.MetaDataOptions.OtherHeapsEnd.Add(new RawHeap("#Blob", new byte[1])); - writer.Options.MetaDataOptions.OtherHeapsEnd.Add(new RawHeap("#Schema", new byte[1])); + writer.TheOptions.MetaDataOptions.OtherHeapsEnd.Add(new RawHeap("#Strings", new byte[1])); + writer.TheOptions.MetaDataOptions.OtherHeapsEnd.Add(new RawHeap("#Blob", new byte[1])); + writer.TheOptions.MetaDataOptions.OtherHeapsEnd.Add(new RawHeap("#Schema", new byte[1])); } else if (e.WriterEvent == ModuleWriterEvent.MDOnAllTablesSorted) { writer.MetaData.TablesHeap.DeclSecurityTable.Add(new RawDeclSecurityRow( diff --git a/Confuser.Protections/ReferenceProxy/StrongMode.cs b/Confuser.Protections/ReferenceProxy/StrongMode.cs index 53d66cefa..2fdc7666d 100644 --- a/Confuser.Protections/ReferenceProxy/StrongMode.cs +++ b/Confuser.Protections/ReferenceProxy/StrongMode.cs @@ -328,7 +328,7 @@ public override void Finalize(RPContext ctx) { } void EncodeField(object sender, ModuleWriterListenerEventArgs e) { - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; if (e.WriterEvent == ModuleWriterEvent.MDMemberDefRidsAllocated) { Dictionary> keyFuncs = keyAttrs .Where(entry => entry != null) diff --git a/Confuser.Protections/ReferenceProxy/x86Encoding.cs b/Confuser.Protections/ReferenceProxy/x86Encoding.cs index ff07efb33..dc734c316 100644 --- a/Confuser.Protections/ReferenceProxy/x86Encoding.cs +++ b/Confuser.Protections/ReferenceProxy/x86Encoding.cs @@ -69,7 +69,7 @@ void Compile(RPContext ctx, out Func expCompiled, out MethodDef native } void InjectNativeCode(object sender, ModuleWriterListenerEventArgs e) { - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; if (e.WriterEvent == ModuleWriterEvent.MDEndWriteMethodBodies) { for (int n = 0; n < nativeCodes.Count; n++) nativeCodes[n] = new Tuple( diff --git a/Confuser.Protections/Resources/MDPhase.cs b/Confuser.Protections/Resources/MDPhase.cs index 1bcf9c51a..baff3d165 100644 --- a/Confuser.Protections/Resources/MDPhase.cs +++ b/Confuser.Protections/Resources/MDPhase.cs @@ -24,7 +24,7 @@ public void Hook() { } void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { - var writer = (ModuleWriter)sender; + var writer = (ModuleWriterBase)sender; if (e.WriterEvent == ModuleWriterEvent.MDBeginAddResources) { ctx.Context.CheckCancellation(); ctx.Context.Logger.Debug("Encrypting resources..."); @@ -37,8 +37,8 @@ void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { // move resources string asmName = ctx.Name.RandomName(RenameMode.Letters); PublicKey pubKey = null; - if (writer.Options.StrongNameKey != null) - pubKey = PublicKeyBase.CreatePublicKey(writer.Options.StrongNameKey.PublicKey); + if (writer.TheOptions.StrongNameKey != null) + pubKey = PublicKeyBase.CreatePublicKey(writer.TheOptions.StrongNameKey.PublicKey); var assembly = new AssemblyDefUser(asmName, new Version(0, 0), pubKey); assembly.Modules.Add(new ModuleDefUser(asmName + ".dll")); ModuleDef module = assembly.ManifestModule; @@ -53,7 +53,7 @@ void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) { } byte[] moduleBuff; using (var ms = new MemoryStream()) { - module.Write(ms, new ModuleWriterOptions { StrongNameKey = writer.Options.StrongNameKey }); + module.Write(ms, new ModuleWriterOptions { StrongNameKey = writer.TheOptions.StrongNameKey }); moduleBuff = ms.ToArray(); }