From 1d111d11a01e9e18a7a589af14d73d7480800792 Mon Sep 17 00:00:00 2001 From: Rexy Date: Tue, 21 Aug 2018 08:56:49 +0300 Subject: [PATCH 1/5] Add files via upload --- Confuser.Runtime/IntegrityProtection.cs | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Confuser.Runtime/IntegrityProtection.cs diff --git a/Confuser.Runtime/IntegrityProtection.cs b/Confuser.Runtime/IntegrityProtection.cs new file mode 100644 index 000000000..6feb81fcb --- /dev/null +++ b/Confuser.Runtime/IntegrityProtection.cs @@ -0,0 +1,39 @@ +using System; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace Confuser.Runtime +{ + internal static class IntegrityProtection + { + internal static void Initialize() + { + var bas = new StreamReader(typeof(IntegrityProtection).Assembly.Location).BaseStream; + var file = new BinaryReader(bas); + var file2 = File.ReadAllBytes(typeof(IntegrityProtection).Assembly.Location); + + var byt = file.ReadBytes(file2.Length - 32); + var a = Hash(byt); + file.BaseStream.Position = file.BaseStream.Length - 32; + string b = Encoding.ASCII.GetString(file.ReadBytes(32)); + + if (a != b) + throw new BadImageFormatException(); + } + + internal static string Hash(byte[] metin) + { + MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); + byte[] btr = metin; + btr = md5.ComputeHash(btr); + StringBuilder sb = new StringBuilder(); + + foreach (byte ba in btr) + { + sb.Append(ba.ToString("x2").ToLower()); + } + return sb.ToString(); + } + } +} From 1082d72cafb0941bd0aa71aa444cc2409a923312 Mon Sep 17 00:00:00 2001 From: Rexy Date: Tue, 21 Aug 2018 08:57:52 +0300 Subject: [PATCH 2/5] Add files via upload --- Confuser.Protections/IntegrityProtection.cs | 150 ++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 Confuser.Protections/IntegrityProtection.cs diff --git a/Confuser.Protections/IntegrityProtection.cs b/Confuser.Protections/IntegrityProtection.cs new file mode 100644 index 000000000..0ac7923e6 --- /dev/null +++ b/Confuser.Protections/IntegrityProtection.cs @@ -0,0 +1,150 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using Confuser.Core; +using Confuser.Core.Helpers; +using Confuser.Core.Services; +using Confuser.Renamer; +using dnlib.DotNet; +using dnlib.DotNet.Emit; +using dnlib.DotNet.Writer; + +namespace Confuser.Protections { + [BeforeProtection("Ki.AntiTamper")] + internal class IntegrityProtection : Protection { + public const string _Id = "integrity prot"; + public const string _FullId = "Rexy.IP"; + + public override string Name { + get { return "Integrity Protection"; } + } + + public override string Description { + get { return "This protection hashs the module to preventing file modifications."; } + } + + public override string Id { + get { return _Id; } + } + + public override string FullId { + get { return _FullId; } + } + + public override ProtectionPreset Preset { + get { return ProtectionPreset.None; } + } + + protected override void Initialize(ConfuserContext context) { + // + } + + protected override void PopulatePipeline(ProtectionPipeline pipeline) { + pipeline.InsertPreStage(PipelineStage.OptimizeMethods, new IntegrityPhase(this)); + pipeline.InsertPreStage(PipelineStage.EndModule, new HashPhase(this)); + } + + class IntegrityPhase : ProtectionPhase { + public IntegrityPhase(IntegrityProtection parent) + : base(parent) { } + + public override ProtectionTargets Targets { + get { return ProtectionTargets.Modules; } + } + + public override string Name { + get { return ""; } + } + + protected override void Execute(ConfuserContext context, ProtectionParameters parameters) + { + TypeDef rtType = context.Registry.GetService().GetRuntimeType("Confuser.Runtime.IntegrityProtection"); + + var marker = context.Registry.GetService(); + var name = context.Registry.GetService(); + + foreach (ModuleDef module in parameters.Targets.OfType()) + { + IEnumerable members = InjectHelper.Inject(rtType, module.GlobalType, module); + + MethodDef cctor = module.GlobalType.FindStaticConstructor(); + var init = (MethodDef)members.Single(method => method.Name == "Initialize"); + cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init)); + + foreach (IDnlibDef member in members) + name.MarkHelper(member, marker, (Protection)Parent); + } + } + } + } + + internal class HashPhase : ProtectionPhase + { + public HashPhase(ConfuserComponent parent) : base(parent) + { + } + + public override ProtectionTargets Targets => ProtectionTargets.Modules; + + public override string Name => "Hash Phase"; + + protected override void Execute(ConfuserContext context, ProtectionParameters parameters) + { + context.CurrentModuleWriterListener.OnWriterEvent += CurrentModuleWriterListener_OnWriterEvent; + } + + private void CurrentModuleWriterListener_OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) + { + var writer = (ModuleWriterBase)sender; + if (e.WriterEvent == dnlib.DotNet.Writer.ModuleWriterEvent.End) + { + HashFile(writer); + } + } + + internal byte[] ReadFully(Stream input) + { + byte[] buffer = new byte[16 * 1024]; + using (MemoryStream ms = new MemoryStream()) + { + int read; + while ((read = input.Read(buffer, 0, buffer.Length)) > 0) + { + ms.Write(buffer, 0, read); + } + return ms.ToArray(); + } + } + + internal string MD5(byte[] metin) + { + MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); + byte[] btr = metin; + btr = md5.ComputeHash(btr); + StringBuilder sb = new StringBuilder(); + + foreach (byte ba in btr) + { + sb.Append(ba.ToString("x2").ToLower()); + } + return sb.ToString(); + } + + private void HashFile(ModuleWriterBase writer) + { + var st = new StreamReader(writer.DestinationStream); + var a = new BinaryReader(st.BaseStream); + a.BaseStream.Position = 0; + var data = a.ReadBytes((int)(st.BaseStream.Length - 32)); + + var md5 = MD5(data); + + var enc = Encoding.ASCII.GetBytes(md5); + + writer.DestinationStream.Position = writer.DestinationStream.Length - enc.Length; + writer.DestinationStream.Write(enc, 0, enc.Length); + } + } +} \ No newline at end of file From ba8c1c28b515761af10a5c923871510233c46ba5 Mon Sep 17 00:00:00 2001 From: Rexy Date: Tue, 21 Aug 2018 10:56:44 +0300 Subject: [PATCH 3/5] Update IntegrityProtection.cs --- Confuser.Protections/IntegrityProtection.cs | 94 ++++++++++----------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/Confuser.Protections/IntegrityProtection.cs b/Confuser.Protections/IntegrityProtection.cs index 0ac7923e6..0ef90e3d7 100644 --- a/Confuser.Protections/IntegrityProtection.cs +++ b/Confuser.Protections/IntegrityProtection.cs @@ -3,59 +3,59 @@ using System.Linq; using System.Security.Cryptography; using System.Text; -using Confuser.Core; +using Confuser.Core; using Confuser.Core.Helpers; using Confuser.Core.Services; using Confuser.Renamer; -using dnlib.DotNet; +using dnlib.DotNet; using dnlib.DotNet.Emit; using dnlib.DotNet.Writer; -namespace Confuser.Protections { - [BeforeProtection("Ki.AntiTamper")] - internal class IntegrityProtection : Protection { - public const string _Id = "integrity prot"; - public const string _FullId = "Rexy.IP"; - - public override string Name { - get { return "Integrity Protection"; } - } - - public override string Description { - get { return "This protection hashs the module to preventing file modifications."; } - } - - public override string Id { - get { return _Id; } - } - - public override string FullId { - get { return _FullId; } - } - - public override ProtectionPreset Preset { - get { return ProtectionPreset.None; } - } - +namespace Confuser.Protections { + [AfterProtection("Ki.AntiTamper")] + internal class IntegrityProtection : Protection { + public const string _Id = "integrity prot"; + public const string _FullId = "Rexy.IP"; + + public override string Name { + get { return "Integrity Protection"; } + } + + public override string Description { + get { return "This protection hashs the module to preventing file modifications."; } + } + + public override string Id { + get { return _Id; } + } + + public override string FullId { + get { return _FullId; } + } + + public override ProtectionPreset Preset { + get { return ProtectionPreset.None; } + } + protected override void Initialize(ConfuserContext context) { - // + // } - protected override void PopulatePipeline(ProtectionPipeline pipeline) { - pipeline.InsertPreStage(PipelineStage.OptimizeMethods, new IntegrityPhase(this)); - pipeline.InsertPreStage(PipelineStage.EndModule, new HashPhase(this)); - } - - class IntegrityPhase : ProtectionPhase { - public IntegrityPhase(IntegrityProtection parent) - : base(parent) { } - - public override ProtectionTargets Targets { - get { return ProtectionTargets.Modules; } - } - - public override string Name { - get { return ""; } + protected override void PopulatePipeline(ProtectionPipeline pipeline) { + pipeline.InsertPreStage(PipelineStage.OptimizeMethods, new IntegrityPhase(this)); + pipeline.InsertPreStage(PipelineStage.EndModule, new HashPhase(this)); + } + + class IntegrityPhase : ProtectionPhase { + public IntegrityPhase(IntegrityProtection parent) + : base(parent) { } + + public override ProtectionTargets Targets { + get { return ProtectionTargets.Modules; } + } + + public override string Name { + get { return ""; } } protected override void Execute(ConfuserContext context, ProtectionParameters parameters) @@ -76,8 +76,8 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa foreach (IDnlibDef member in members) name.MarkHelper(member, marker, (Protection)Parent); } - } - } + } + } } internal class HashPhase : ProtectionPhase @@ -147,4 +147,4 @@ private void HashFile(ModuleWriterBase writer) writer.DestinationStream.Write(enc, 0, enc.Length); } } -} \ No newline at end of file +} From 89b2ef97e1be75433121e2b55d6a09f6cc296a0b Mon Sep 17 00:00:00 2001 From: Rexy Date: Tue, 21 Aug 2018 10:57:31 +0300 Subject: [PATCH 4/5] Delete IntegrityProtection.cs --- Confuser.Protections/IntegrityProtection.cs | 150 -------------------- 1 file changed, 150 deletions(-) delete mode 100644 Confuser.Protections/IntegrityProtection.cs diff --git a/Confuser.Protections/IntegrityProtection.cs b/Confuser.Protections/IntegrityProtection.cs deleted file mode 100644 index 0ef90e3d7..000000000 --- a/Confuser.Protections/IntegrityProtection.cs +++ /dev/null @@ -1,150 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Security.Cryptography; -using System.Text; -using Confuser.Core; -using Confuser.Core.Helpers; -using Confuser.Core.Services; -using Confuser.Renamer; -using dnlib.DotNet; -using dnlib.DotNet.Emit; -using dnlib.DotNet.Writer; - -namespace Confuser.Protections { - [AfterProtection("Ki.AntiTamper")] - internal class IntegrityProtection : Protection { - public const string _Id = "integrity prot"; - public const string _FullId = "Rexy.IP"; - - public override string Name { - get { return "Integrity Protection"; } - } - - public override string Description { - get { return "This protection hashs the module to preventing file modifications."; } - } - - public override string Id { - get { return _Id; } - } - - public override string FullId { - get { return _FullId; } - } - - public override ProtectionPreset Preset { - get { return ProtectionPreset.None; } - } - - protected override void Initialize(ConfuserContext context) { - // - } - - protected override void PopulatePipeline(ProtectionPipeline pipeline) { - pipeline.InsertPreStage(PipelineStage.OptimizeMethods, new IntegrityPhase(this)); - pipeline.InsertPreStage(PipelineStage.EndModule, new HashPhase(this)); - } - - class IntegrityPhase : ProtectionPhase { - public IntegrityPhase(IntegrityProtection parent) - : base(parent) { } - - public override ProtectionTargets Targets { - get { return ProtectionTargets.Modules; } - } - - public override string Name { - get { return ""; } - } - - protected override void Execute(ConfuserContext context, ProtectionParameters parameters) - { - TypeDef rtType = context.Registry.GetService().GetRuntimeType("Confuser.Runtime.IntegrityProtection"); - - var marker = context.Registry.GetService(); - var name = context.Registry.GetService(); - - foreach (ModuleDef module in parameters.Targets.OfType()) - { - IEnumerable members = InjectHelper.Inject(rtType, module.GlobalType, module); - - MethodDef cctor = module.GlobalType.FindStaticConstructor(); - var init = (MethodDef)members.Single(method => method.Name == "Initialize"); - cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init)); - - foreach (IDnlibDef member in members) - name.MarkHelper(member, marker, (Protection)Parent); - } - } - } - } - - internal class HashPhase : ProtectionPhase - { - public HashPhase(ConfuserComponent parent) : base(parent) - { - } - - public override ProtectionTargets Targets => ProtectionTargets.Modules; - - public override string Name => "Hash Phase"; - - protected override void Execute(ConfuserContext context, ProtectionParameters parameters) - { - context.CurrentModuleWriterListener.OnWriterEvent += CurrentModuleWriterListener_OnWriterEvent; - } - - private void CurrentModuleWriterListener_OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) - { - var writer = (ModuleWriterBase)sender; - if (e.WriterEvent == dnlib.DotNet.Writer.ModuleWriterEvent.End) - { - HashFile(writer); - } - } - - internal byte[] ReadFully(Stream input) - { - byte[] buffer = new byte[16 * 1024]; - using (MemoryStream ms = new MemoryStream()) - { - int read; - while ((read = input.Read(buffer, 0, buffer.Length)) > 0) - { - ms.Write(buffer, 0, read); - } - return ms.ToArray(); - } - } - - internal string MD5(byte[] metin) - { - MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); - byte[] btr = metin; - btr = md5.ComputeHash(btr); - StringBuilder sb = new StringBuilder(); - - foreach (byte ba in btr) - { - sb.Append(ba.ToString("x2").ToLower()); - } - return sb.ToString(); - } - - private void HashFile(ModuleWriterBase writer) - { - var st = new StreamReader(writer.DestinationStream); - var a = new BinaryReader(st.BaseStream); - a.BaseStream.Position = 0; - var data = a.ReadBytes((int)(st.BaseStream.Length - 32)); - - var md5 = MD5(data); - - var enc = Encoding.ASCII.GetBytes(md5); - - writer.DestinationStream.Position = writer.DestinationStream.Length - enc.Length; - writer.DestinationStream.Write(enc, 0, enc.Length); - } - } -} From 35b24e08fe84d9bd25ef6d4d8c629e75ee1ebf53 Mon Sep 17 00:00:00 2001 From: Rexy Date: Tue, 21 Aug 2018 14:57:38 +0300 Subject: [PATCH 5/5] Fix --- Confuser.Protections/IntegrityProtection.cs | 150 ++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 Confuser.Protections/IntegrityProtection.cs diff --git a/Confuser.Protections/IntegrityProtection.cs b/Confuser.Protections/IntegrityProtection.cs new file mode 100644 index 000000000..8e3568ab8 --- /dev/null +++ b/Confuser.Protections/IntegrityProtection.cs @@ -0,0 +1,150 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using Confuser.Core; +using Confuser.Core.Helpers; +using Confuser.Core.Services; +using Confuser.Renamer; +using dnlib.DotNet; +using dnlib.DotNet.Emit; +using dnlib.DotNet.Writer; + +namespace Confuser.Protections { + [BeforeProtection("Ki.AntiTamper")] + internal class IntegrityProtection : Protection { + public const string _Id = "integrity prot"; + public const string _FullId = "Rexy.IP"; + + public override string Name { + get { return "Integrity Protection"; } + } + + public override string Description { + get { return "This protection hashs the module to preventing file modifications."; } + } + + public override string Id { + get { return _Id; } + } + + public override string FullId { + get { return _FullId; } + } + + public override ProtectionPreset Preset { + get { return ProtectionPreset.None; } + } + + protected override void Initialize(ConfuserContext context) { + // + } + + protected override void PopulatePipeline(ProtectionPipeline pipeline) { + pipeline.InsertPreStage(PipelineStage.OptimizeMethods, new IntegrityPhase(this)); + pipeline.InsertPreStage(PipelineStage.EndModule, new HashPhase(this)); + } + + class IntegrityPhase : ProtectionPhase { + public IntegrityPhase(IntegrityProtection parent) + : base(parent) { } + + public override ProtectionTargets Targets { + get { return ProtectionTargets.Modules; } + } + + public override string Name { + get { return ""; } + } + + protected override void Execute(ConfuserContext context, ProtectionParameters parameters) + { + TypeDef rtType = context.Registry.GetService().GetRuntimeType("Confuser.Runtime.IntegrityProtection"); + + var marker = context.Registry.GetService(); + var name = context.Registry.GetService(); + + foreach (ModuleDef module in parameters.Targets.OfType()) + { + IEnumerable members = InjectHelper.Inject(rtType, module.GlobalType, module); + + MethodDef cctor = module.GlobalType.FindStaticConstructor(); + var init = (MethodDef)members.Single(method => method.Name == "Initialize"); + cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init)); + + foreach (IDnlibDef member in members) + name.MarkHelper(member, marker, (Protection)Parent); + } + } + } + } + + internal class HashPhase : ProtectionPhase + { + public HashPhase(ConfuserComponent parent) : base(parent) + { + } + + public override ProtectionTargets Targets => ProtectionTargets.Modules; + + public override string Name => "Hash Phase"; + + protected override void Execute(ConfuserContext context, ProtectionParameters parameters) + { + context.CurrentModuleWriterListener.OnWriterEvent += CurrentModuleWriterListener_OnWriterEvent; + } + + private void CurrentModuleWriterListener_OnWriterEvent(object sender, ModuleWriterListenerEventArgs e) + { + var writer = (ModuleWriterBase)sender; + if (e.WriterEvent == dnlib.DotNet.Writer.ModuleWriterEvent.End) + { + HashFile(writer); + } + } + + internal byte[] ReadFully(Stream input) + { + byte[] buffer = new byte[16 * 1024]; + using (MemoryStream ms = new MemoryStream()) + { + int read; + while ((read = input.Read(buffer, 0, buffer.Length)) > 0) + { + ms.Write(buffer, 0, read); + } + return ms.ToArray(); + } + } + + internal string MD5(byte[] metin) + { + MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); + byte[] btr = metin; + btr = md5.ComputeHash(btr); + StringBuilder sb = new StringBuilder(); + + foreach (byte ba in btr) + { + sb.Append(ba.ToString("x2").ToLower()); + } + return sb.ToString(); + } + + private void HashFile(ModuleWriterBase writer) + { + var st = new StreamReader(writer.DestinationStream); + var a = new BinaryReader(st.BaseStream); + a.BaseStream.Position = 0; + var data = a.ReadBytes((int)(st.BaseStream.Length - 32)); + + var md5 = MD5(data); + + var enc = Encoding.ASCII.GetBytes(md5); + + writer.DestinationStream.Position = writer.DestinationStream.Length - enc.Length; + writer.DestinationStream.Write(enc, 0, enc.Length); + } + } +}