From dfd6e21230ced8565b2e866ebfc80e600c030b69 Mon Sep 17 00:00:00 2001 From: Jake Swenson Date: Thu, 19 Oct 2017 09:51:05 -0700 Subject: [PATCH] Initial commit --- .gitignore | 6 +++ Program.cs | 91 +++++++++++++++++++++++++++++++++++ bitwardenSelfLicensor.csproj | 12 +++++ bitwardenSelfLicensor.sln | 22 +++++++++ cert.cert | Bin 0 -> 1194 bytes 5 files changed, 131 insertions(+) create mode 100644 .gitignore create mode 100644 Program.cs create mode 100644 bitwardenSelfLicensor.csproj create mode 100644 bitwardenSelfLicensor.sln create mode 100644 cert.cert diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..826ef38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea/ +bin/ +obj/ +*.dll +*.pem +.vscode/ \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..6a3decf --- /dev/null +++ b/Program.cs @@ -0,0 +1,91 @@ +using System; +using System.IO; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using Mono.Cecil; +using Mono.Cecil.Cil; +using Mono.Cecil.Rocks; + +namespace bitwardenSelfLicensor +{ + class Program + { + static void Main(string[] args) + { + string cerFile; + string corePath; + + if(args.Length >= 2) { + cerFile = args[0]; + corePath = args[1]; + } else if (args.Length == 1) { + cerFile = args[0]; + corePath = "/app/Core.dll"; + } + else { + cerFile = "/newLicensing.cer"; + corePath = "/app/Core.dll"; + } + + + var module = ModuleDefinition.ReadModule(new MemoryStream(File.ReadAllBytes(corePath))); + var cert = File.ReadAllBytes(cerFile); + + var x = module.Resources.OfType() + .Where(r => r.Name.Equals("Bit.Core.licensing.cer")) + .First(); + + Console.WriteLine(x.Name); + + var e = new EmbeddedResource("Bit.Core.licensing.cer", x.Attributes, cert); + + module.Resources.Add(e); + module.Resources.Remove(x); + + var services = module.Types.Where(t => t.Namespace == "Bit.Core.Services"); + + + var type = services.First(t => t.Name == "LicensingService"); + + var licensingType = type.Resolve(); + + var existingCert = new X509Certificate2(x.GetResourceData()); + + Console.WriteLine($"Existing Cert Thumbprin: {existingCert.Thumbprint}"); + X509Certificate2 certificate = new X509Certificate2(cert); + + Console.WriteLine($"New cert Thumbprint: {certificate.Thumbprint}"); + + var ctor = licensingType.GetConstructors().Single(); + + + var rewriter = ctor.Body.GetILProcessor(); + + var instToReplace = + ctor.Body.Instructions.Where(i => i.OpCode == OpCodes.Ldstr + && string.Equals((string)i.Operand, existingCert.Thumbprint, StringComparison.InvariantCultureIgnoreCase)) + .FirstOrDefault(); + + if(instToReplace != null) { + rewriter.Replace(instToReplace, Instruction.Create(OpCodes.Ldstr, certificate.Thumbprint)); + } + else { + Console.WriteLine("Cant find inst"); + } + + // foreach (var inst in ctor.Body.Instructions) + // { + // Console.Write(inst.OpCode.Name + " " + inst.Operand?.GetType() + " = "); + // if(inst.OpCode.FlowControl == FlowControl.Call) { + // Console.WriteLine(inst.Operand); + // } + // else if(inst.OpCode == OpCodes.Ldstr) { + // Console.WriteLine(inst.Operand); + // } + // else {Console.WriteLine();} + // } + + module.Write("modified.dll"); + } + } +} diff --git a/bitwardenSelfLicensor.csproj b/bitwardenSelfLicensor.csproj new file mode 100644 index 0000000..c296861 --- /dev/null +++ b/bitwardenSelfLicensor.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp2.0 + + + + + + + diff --git a/bitwardenSelfLicensor.sln b/bitwardenSelfLicensor.sln new file mode 100644 index 0000000..5bb438c --- /dev/null +++ b/bitwardenSelfLicensor.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.0.0 +MinimumVisualStudioVersion = 10.0.0.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bitwardenSelfLicensor", "bitwardenSelfLicensor.csproj", "{583CBED9-EA04-4E5A-BE5B-31370052D4E2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {583CBED9-EA04-4E5A-BE5B-31370052D4E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {583CBED9-EA04-4E5A-BE5B-31370052D4E2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {583CBED9-EA04-4E5A-BE5B-31370052D4E2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {583CBED9-EA04-4E5A-BE5B-31370052D4E2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/cert.cert b/cert.cert new file mode 100644 index 0000000000000000000000000000000000000000..740465ba03136bd88550356c9efa1cca5a620f6a GIT binary patch literal 1194 zcmV;b1XcSmf&`{8f&z{L2>`7)J#?njWSlS!1_>&LNQUlw}8?+FaSo=aOK&ww^8Nu*8UgItalZ1eyNhR#$V!-A>%kCAl;XJ$YY(!)*7= zo;!1M*TfXzEpugaqXhOL#ZLy(6Rq?`g1OQ6|t+JgD=mbM+AXiG}hu;}^ zY-lRXz)O0kU7~yn2G}kisPQbJ+bhX~#RY0XXv<%C$nY7Drz1!?{$$^H)!8pmj6v1x z^JG{``Z5yh%lxn!&pIgj*$1WyJ=dT(s|&}vpx06mrEd$KMbM@=go+CO6VN<2KuQ5l z?}^D9Syn(m@I*VGwC^Z=mV4_jtI4nS5lfPy&sRSRuw!V%Fb>hc8fP34K_m$W$9vHq zoVM_%j(r<@Nd!oPrgH!_Q0T_stR(Q4B~ED|t>_`ZKgH20)V17g1{(dpi0~dar*ZCX zwn-)-6Oyd$x7&&tx+z6^)Ur)30IHq#RV7ZR65)*nZ{SXkTgXqjh5!e25Io?Hqj#PG zA?mebMozGnKYAI1iXW>LQlLL=fbkDrNd^nOvzfw#7a0SJic2hq>tQ92C9pT-#a4I97yDRmUn>=nx6ggq{pyQuWXi5~*l>Q2j zx`=NCnH%&WHUjbsi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