1
0
mirror of https://github.com/jakeswenson/BitBetter.git synced 2023-10-10 13:36:57 +02:00
This commit is contained in:
Jake Swenson 2017-10-29 12:13:38 -07:00
parent 5c58b0d8fd
commit f7ec8b477d
6 changed files with 179 additions and 66 deletions

View File

@ -9,5 +9,5 @@ cd ./src/bitbetter
dotnet restore
dotnet publish
docker build . -t bitbetter/api # --squash
docker build --pull . -t bitbetter/api # --squash

View File

@ -0,0 +1,5 @@
FROM bitbetter/api
COPY bin/Debug/netcoreapp2.0/publish/* /app/
ENTRYPOINT [ "dotnet", "/app/licenseGen.dll", "--core", "/app/Core.dll", "--cert", "/cert.pfx" ]

View File

@ -1,10 +1,10 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.Loader;
using System.Security.Cryptography.X509Certificates;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using Microsoft.Extensions.CommandLineUtils;
using Newtonsoft.Json;
namespace bitwardenSelfLicensor
{
@ -12,82 +12,162 @@ namespace bitwardenSelfLicensor
{
static int Main(string[] args)
{
string cerFile;
string corePath;
var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication();
var cert = app.Option("-c | --cert", "cert file", CommandOptionType.SingleValue);
var coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue);
if(args.Length >= 2) {
cerFile = args[0];
corePath = args[1];
} else if (args.Length == 1) {
cerFile = args[0];
corePath = "/app/Core.dll";
app.Command("user", config =>
{
var name = config.Argument("Name", "your name");
var email = config.Argument("Email", "your email");
var key = config.Argument("Key", "your key id (optional)");
var help = config.HelpOption("--help | -h | -?");
config.OnExecute(() =>
{
if (!cert.HasValue() || !coreDll.HasValue())
{
app.ShowHelp();
return 1;
}
else {
cerFile = "/newLicensing.cer";
corePath = "/app/Core.dll";
else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value))
{
config.ShowHelp("user");
return 1;
}
var module = ModuleDefinition.ReadModule(new MemoryStream(File.ReadAllBytes(corePath)));
var cert = File.ReadAllBytes(cerFile);
var x = module.Resources.OfType<EmbeddedResource>()
.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");
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, key.Value);
return 0;
});
});
app.Command("org", config =>
{
var name = config.Argument("Name", "your name");
var email = config.Argument("Email", "your email");
var installId = config.Argument("InstallId", "your installation id (GUID)");
var key = config.Argument("Key", "your key id (optional)");
var help = config.HelpOption("--help | -h | -?");
config.OnExecute(() =>
{
if (!cert.HasValue() || !coreDll.HasValue())
{
app.ShowHelp();
return 1;
}
else if (string.IsNullOrWhiteSpace(name.Value) ||
string.IsNullOrWhiteSpace(email.Value) ||
string.IsNullOrWhiteSpace(installId.Value))
{
config.Error.WriteLine("Missing arguments");
config.ShowHelp("org");
return 1;
}
if (!Guid.TryParse(installId.Value, out Guid installationId))
{
config.Error.WriteLine("Unable to parse your installation id as a GUID");
config.ShowHelp("org");
}
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, installationId key.Value);
return 0;
});
});
app.OnExecute(() =>
{
app.ShowHelp();
return 10;
});
app.HelpOption("-? | -h | --help");
try
{
return app.Execute(args);
}
catch (Exception e)
{
Console.Error.WriteLine("Oops: {0}", e);
return 100;
}
}
static void GenerateUserLicense(X509Certificate2 cert, string corePath,
string userName, string email, string key)
{
var core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath);
var type = core.GetType("Bit.Core.Models.Business.UserLicense");
var license = Activator.CreateInstance(type);
void set(string name, object value)
{
type.GetProperty(name).SetValue(license, value);
}
set("LicenseKey", string.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key);
set("Id", Guid.NewGuid());
set("Name", userName);
set("Email", email);
set("MaxStorageGb", short.MaxValue);
set("Premium", true);
set("Version", 1);
set("Issued", DateTime.UtcNow);
set("Refresh", DateTime.UtcNow.AddYears(1).AddMonths(-1));
set("Expires", DateTime.UtcNow.AddYears(1));
set("Trial", false);
set("Hash", Convert.ToBase64String((byte[])type.GetMethod("ComputeHash").Invoke(license, new object[0])));
set("Signature", Convert.ToBase64String((byte[])type.GetMethod("Sign").Invoke(license, new object[] { cert })));
Console.WriteLine(JsonConvert.SerializeObject(license));
}
static void GenerateOrgLicense(X509Certificate2 cert, string corePath,
string userName, string email, Guid instalId, string key)
{
var core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath);
var type = core.GetType("Bit.Core.Models.Business.OrganizationLicense");
var license = Activator.CreateInstance(type);
void set(string name, object value)
{
type.GetProperty(name).SetValue(license, value);
}
set("LicenseKey", string.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key);
set("InstallationId", instalId);
set("Id", Guid.NewGuid());
set("Name", userName);
set("BillingEmail", email);
set("BusinessName", "BitBetter");
set("Enabled", true);
set("Seats", (short)5);
set("MaxCollections", short.MaxValue);
set("MaxStorageGb", short.MaxValue);
set("SelfHost", true);
set("UseGroups", true);
set("UseDirectory", true);
set("UseTotp", true);
set("PlanType", (byte)6);
set("Plan", "Custom");
set("Version", 1);
set("Issued", DateTime.UtcNow);
set("Refresh", DateTime.UtcNow.AddYears(1).AddMonths(-1));
set("Expires", DateTime.UtcNow.AddYears(1));
set("Trial", false);
set("Hash", Convert.ToBase64String((byte[])type.GetMethod("ComputeHash").Invoke(license, new object[0])));
set("Signature", Convert.ToBase64String((byte[])type.GetMethod("Sign").Invoke(license, new object[] { cert })));
Console.WriteLine(JsonConvert.SerializeObject(license));
}
}
}

11
src/licenseGen/build.sh Executable file
View File

@ -0,0 +1,11 @@
#!/bin/bash
script_dir=`cd $(dirname $0); pwd`
cd $script_dir
dotnet restore
dotnet publish
docker build . -t bitbetter/licenseGen # --squash

View File

@ -5,4 +5,10 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
</ItemGroup>
</Project>

11
src/licenseGen/run.sh Executable file
View File

@ -0,0 +1,11 @@
#!/bin/bash
script_dir=`cd $(dirname $0); pwd`
if [ "$#" -ne "1" ]; then
echo "USAGE: $0 <ABSOLUTE PATH TO CERT.PFX>"
exit 1
fi
docker run -it -v "$1:/cert.pfx" bitbetter/licensegen