Skip to content

Commit 638db83

Browse files
authored
Merge pull request #32 from sunnamed434/dev
Bump new version v0.3.1-alpha.6
2 parents d7559fc + dd86400 commit 638db83

File tree

17 files changed

+130
-102
lines changed

17 files changed

+130
-102
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
namespace BitMono.API.Protecting.Contexts
1+
using System.Collections.Generic;
2+
3+
namespace BitMono.API.Protecting.Contexts
24
{
35
public class BitMonoContext
46
{
57
public string ModuleFileName { get; set; }
68
public string OutputPath { get; set; }
79
public string OutputModuleFile { get; set; }
8-
public string DependenciesDirectoryName { get; set; }
10+
public IEnumerable<byte[]> DependenciesData { get; set; }
911
public bool Watermark { get; set; }
1012
}
1113
}

BitMono/BitMono.CLI/Modules/CLIBitMonoModuleFileResolver.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using BitMono.API.Protecting.Resolvers;
22
using NullGuard;
3-
using System;
4-
using System.IO;
53
using System.Linq;
64
using System.Threading.Tasks;
75

BitMono/BitMono.CLI/Program.cs

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Autofac;
22
using BitMono.API.Configuration;
33
using BitMono.API.Protecting;
4+
using BitMono.API.Protecting.Resolvers;
45
using BitMono.CLI.Modules;
56
using BitMono.Core.Configuration.Extensions;
67
using BitMono.Host;
@@ -14,6 +15,7 @@
1415
using System.Collections.Generic;
1516
using System.Diagnostics;
1617
using System.IO;
18+
using System.Linq;
1719
using System.Reflection;
1820
using System.Threading;
1921
using System.Threading.Tasks;
@@ -28,53 +30,73 @@ public class Program
2830

2931
private static async Task Main(string[] args)
3032
{
31-
var moduleFileName = await new CLIBitMonoModuleFileResolver(args).ResolveAsync();
32-
if (string.IsNullOrWhiteSpace(moduleFileName))
33+
try
3334
{
34-
Console.WriteLine("Please, specify file, drag-and-drop it in BitMono CLI");
35-
Console.ReadLine();
36-
return;
37-
}
35+
var moduleFileName = await new CLIBitMonoModuleFileResolver(args).ResolveAsync();
36+
if (string.IsNullOrWhiteSpace(moduleFileName))
37+
{
38+
Console.WriteLine("Please, specify file, drag-and-drop it in BitMono CLI");
39+
Console.ReadLine();
40+
return;
41+
}
3842

39-
var domainBaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
40-
var protectionsFile = Path.Combine(domainBaseDirectory, Protections);
41-
var externalComponentsFile = Path.Combine(domainBaseDirectory, ExternalComponents);
42-
var externalComponentsModuleDefMD = ModuleDefMD.Load(externalComponentsFile);
43-
Assembly.LoadFrom(protectionsFile);
43+
var domainBaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
44+
var protectionsFile = Path.Combine(domainBaseDirectory, Protections);
45+
var externalComponentsFile = Path.Combine(domainBaseDirectory, ExternalComponents);
46+
var externalComponentsModuleDefMD = ModuleDefMD.Load(externalComponentsFile);
47+
Assembly.LoadFrom(protectionsFile);
4448

45-
var moduleFileBaseDirectory = Path.GetDirectoryName(moduleFileName);
46-
var libsDirectoryName = Path.Combine(moduleFileBaseDirectory, "libs");
47-
var outputDirectoryName = Path.Combine(moduleFileBaseDirectory, "output");
48-
Directory.CreateDirectory(libsDirectoryName);
49-
Directory.CreateDirectory(outputDirectoryName);
49+
var moduleFileBaseDirectory = Path.GetDirectoryName(moduleFileName);
50+
var dependenciesDirectoryName = Path.Combine(moduleFileBaseDirectory, "libs");
51+
var outputDirectoryName = Path.Combine(moduleFileBaseDirectory, "output");
52+
Directory.CreateDirectory(dependenciesDirectoryName);
53+
Directory.CreateDirectory(outputDirectoryName);
5054

51-
var serviceProvider = new BitMonoApplication().RegisterModule(new BitMonoModule(configureLogger =>
52-
{
53-
configureLogger.WriteTo.Async(configure => configure.Console(
54-
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}][{SourceContext}] {Message:lj}{NewLine}{Exception}"));
55-
})).Build();
55+
var serviceProvider = new BitMonoApplication().RegisterModule(new BitMonoModule(configureLogger =>
56+
{
57+
configureLogger.WriteTo.Async(configure => configure.Console(
58+
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}][{SourceContext}] {Message:lj}{NewLine}{Exception}"));
59+
})).Build();
5660

57-
var obfuscationConfiguration = serviceProvider.LifetimeScope.Resolve<IBitMonoObfuscationConfiguration>();
58-
var protectionsConfiguration = serviceProvider.LifetimeScope.Resolve<IBitMonoProtectionsConfiguration>();
59-
var appSettingsConfiguration = serviceProvider.LifetimeScope.Resolve<IBitMonoAppSettingsConfiguration>();
60-
var bitMonoContext = await new BitMonoContextCreator(obfuscationConfiguration).CreateAsync(outputDirectoryName, libsDirectoryName);
61-
bitMonoContext.ModuleFileName = moduleFileName;
61+
var obfuscationConfiguration = serviceProvider.LifetimeScope.Resolve<IBitMonoObfuscationConfiguration>();
62+
var protectionsConfiguration = serviceProvider.LifetimeScope.Resolve<IBitMonoProtectionsConfiguration>();
63+
var appSettingsConfiguration = serviceProvider.LifetimeScope.Resolve<IBitMonoAppSettingsConfiguration>();
64+
var dnlibDefFeatureObfuscationAttributeHavingResolver = serviceProvider.LifetimeScope.Resolve<IDnlibDefFeatureObfuscationAttributeHavingResolver>();
65+
var dependencies = Directory.GetFiles(dependenciesDirectoryName);
66+
var dependeciesData = new List<byte[]>();
67+
for (int i = 0; i < dependencies.Length; i++)
68+
{
69+
dependeciesData.Add(File.ReadAllBytes(dependencies[i]));
70+
}
71+
var bitMonoContext = await new BitMonoContextCreator(obfuscationConfiguration).CreateAsync(outputDirectoryName, dependeciesData);
72+
bitMonoContext.ModuleFileName = moduleFileName;
6273

63-
var protections = serviceProvider.LifetimeScope.Resolve<ICollection<IProtection>>();
64-
var moduleFileBytes = File.ReadAllBytes(bitMonoContext.ModuleFileName);
65-
var logger = serviceProvider.LifetimeScope.Resolve<ILogger>().ForContext<Program>();
66-
var protectionSettings = protectionsConfiguration.GetProtectionSettings();
67-
await new BitMonoEngine(serviceProvider, new CLIModuleDefMDWriter(), new ModuleDefMDCreator(moduleFileBytes), logger)
68-
.ObfuscateAsync(bitMonoContext, externalComponentsModuleDefMD, protections, protectionSettings, CancellationToken.Token);
74+
var protections = serviceProvider.LifetimeScope.Resolve<ICollection<IProtection>>().ToList();
75+
var moduleFileBytes = File.ReadAllBytes(bitMonoContext.ModuleFileName);
76+
var logger = serviceProvider.LifetimeScope.Resolve<ILogger>().ForContext<Program>();
77+
var protectionSettings = protectionsConfiguration.GetProtectionSettings();
78+
await new BitMonoEngine(
79+
new CLIModuleDefMDWriter(),
80+
new ModuleDefMDCreator(moduleFileBytes),
81+
dnlibDefFeatureObfuscationAttributeHavingResolver,
82+
obfuscationConfiguration,
83+
logger)
84+
.ObfuscateAsync(bitMonoContext, externalComponentsModuleDefMD, protections, protectionSettings, CancellationToken.Token);
6985

70-
if (obfuscationConfiguration.Configuration.GetValue<bool>(nameof(Obfuscation.OpenFileDestinationInFileExplorer)))
71-
{
72-
Process.Start(bitMonoContext.OutputPath);
73-
}
86+
if (obfuscationConfiguration.Configuration.GetValue<bool>(nameof(Obfuscation.OpenFileDestinationInFileExplorer)))
87+
{
88+
Process.Start(bitMonoContext.OutputPath);
89+
}
7490

75-
await new TipsNotifier(appSettingsConfiguration, logger).NotifyAsync();
91+
await new TipsNotifier(appSettingsConfiguration, logger).NotifyAsync();
7692

77-
await serviceProvider.DisposeAsync();
93+
await serviceProvider.DisposeAsync();
94+
}
95+
catch (Exception ex)
96+
{
97+
Console.WriteLine("Something went wrong! " + ex.ToString());
98+
}
99+
78100
Console.ReadLine();
79101
}
80102
}

BitMono/BitMono.Core/Protecting/Resolvers/DependencyResolver.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ namespace BitMono.Core.Protecting.Resolvers
88
{
99
public class DependencyResolver
1010
{
11-
private readonly ICollection<IProtection> m_Protections;
12-
private readonly ICollection<ProtectionSettings> m_ProtectionSettings;
11+
private readonly List<IProtection> m_Protections;
12+
private readonly IEnumerable<ProtectionSettings> m_ProtectionSettings;
1313
private readonly ILogger m_Logger;
1414

15-
public DependencyResolver(ICollection<IProtection> protections, ICollection<ProtectionSettings> protectionSettings, ILogger logger)
15+
public DependencyResolver(List<IProtection> protections, IEnumerable<ProtectionSettings> protectionSettings, ILogger logger)
1616
{
1717
m_Protections = protections;
1818
m_ProtectionSettings = protectionSettings;
1919
m_Logger = logger.ForContext<DependencyResolver>();
2020
}
2121

22-
public ICollection<IProtection> Sort(out ICollection<string> disabled)
22+
public List<IProtection> Sort(out List<string> disabled)
2323
{
2424
var foundProtections = new List<IProtection>();
2525
var cachedProtections = m_Protections.ToArray().ToList();

BitMono/BitMono.ExternalComponents/Encryptor.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ public static class Encryptor
1212
internal static string Decrypt(byte[] bytes)
1313
{
1414
byte[] decryptedBytes = null;
15-
using (MemoryStream memoryStream = new MemoryStream())
15+
using (var memoryStream = new MemoryStream())
1616
{
17-
using (RijndaelManaged aes = new RijndaelManaged())
17+
using (var aes = new RijndaelManaged())
1818
{
1919
aes.KeySize = 256;
2020
aes.BlockSize = 128;
21-
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(cryptKeyBytes, saltBytes, 1000);
21+
var key = new Rfc2898DeriveBytes(cryptKeyBytes, saltBytes, 1000);
2222
aes.Key = key.GetBytes(aes.KeySize / 8);
2323
aes.IV = key.GetBytes(aes.BlockSize / 8);
2424
aes.Mode = CipherMode.CBC;
2525

26-
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
26+
using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
2727
{
2828
cryptoStream.Write(bytes, 0, bytes.Length);
2929
cryptoStream.Close();
@@ -37,20 +37,20 @@ internal static string Decrypt(byte[] bytes)
3737
}
3838
public static byte[] EncryptContent(string text, byte[] saltBytes, byte[] cryptKeyBytes)
3939
{
40-
byte[] decryptBytes = Encoding.UTF8.GetBytes(text);
40+
var decryptBytes = Encoding.UTF8.GetBytes(text);
4141
byte[] encryptedBytes = null;
42-
using (MemoryStream memoryStream = new MemoryStream())
42+
using (var memoryStream = new MemoryStream())
4343
{
44-
using (RijndaelManaged aes = new RijndaelManaged())
44+
using (var aes = new RijndaelManaged())
4545
{
4646
aes.KeySize = 256;
4747
aes.BlockSize = 128;
48-
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(saltBytes, cryptKeyBytes, 1000);
48+
var key = new Rfc2898DeriveBytes(saltBytes, cryptKeyBytes, 1000);
4949
aes.Key = key.GetBytes(aes.KeySize / 8);
5050
aes.IV = key.GetBytes(aes.BlockSize / 8);
5151
aes.Mode = CipherMode.CBC;
5252

53-
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
53+
using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
5454
{
5555
cryptoStream.Write(decryptBytes, 0, decryptBytes.Length);
5656
cryptoStream.Close();

BitMono/BitMono.GUI/Pages/Obfuscation/Protect.razor.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BitMono.API.Configuration;
22
using BitMono.API.Protecting;
3+
using BitMono.API.Protecting.Resolvers;
34
using BitMono.GUI.API;
45
using BitMono.GUI.Modules;
56
using BitMono.GUI.Shared.Alerting;
@@ -14,8 +15,8 @@ namespace BitMono.GUI.Pages.Obfuscation
1415
{
1516
public partial class Protect
1617
{
17-
private string _dependenciesDirectory;
18-
private string _outputDirectory;
18+
private string _dependenciesDirectoryName;
19+
private string _outputDirectoryName;
1920
private IBrowserFile _obfuscationFile;
2021

2122
[Inject] public ILogger Logger { get; set; }
@@ -49,11 +50,19 @@ public async Task ObfuscateAsync()
4950

5051
var obfuscationConfiguration = ServiceProvider.GetRequiredService<IBitMonoObfuscationConfiguration>();
5152
var appSettingsConfiguration = ServiceProvider.GetRequiredService<IBitMonoAppSettingsConfiguration>();
53+
var dnlibDefFeatureObfuscationAttributeHavingResolver = ServiceProvider.GetRequiredService<IDnlibDefFeatureObfuscationAttributeHavingResolver>();
5254

53-
var bitMonoContext = await new BitMonoContextCreator(obfuscationConfiguration).CreateAsync(_outputDirectory, _dependenciesDirectory);
55+
var dependencies = Directory.GetFiles(_dependenciesDirectoryName);
56+
var dependeciesData = new List<byte[]>();
57+
for (int i = 0; i < dependencies.Length; i++)
58+
{
59+
dependeciesData.Add(File.ReadAllBytes(dependencies[i]));
60+
}
61+
62+
var bitMonoContext = await new BitMonoContextCreator(obfuscationConfiguration).CreateAsync(_outputDirectoryName, dependeciesData);
5463
bitMonoContext.ModuleFileName = _obfuscationFile.Name;
55-
await new BitMonoEngine(ServiceProvider, new GUIModuleDefMDWriter(), new ModuleDefMDCreator(moduleBytes), Logger)
56-
.ObfuscateAsync(bitMonoContext, externalComponentsModuleDefMD, Protections, StoringProtections.Protections);
64+
await new BitMonoEngine(new GUIModuleDefMDWriter(), new ModuleDefMDCreator(moduleBytes), dnlibDefFeatureObfuscationAttributeHavingResolver, obfuscationConfiguration, Logger)
65+
.ObfuscateAsync(bitMonoContext, externalComponentsModuleDefMD, Protections.ToList(), StoringProtections.Protections);
5766

5867
await new TipsNotifier(appSettingsConfiguration, Logger).NotifyAsync();
5968
}
@@ -77,12 +86,12 @@ public async Task ObfuscateFileAsync()
7786
await AlertsContainer.ShowAlertAsync("obfuscation-info", "Please, specify file to be protected!", Alerts.Danger);
7887
return;
7988
}
80-
if (string.IsNullOrWhiteSpace(_dependenciesDirectory))
89+
if (string.IsNullOrWhiteSpace(_dependenciesDirectoryName))
8190
{
8291
await AlertsContainer.ShowAlertAsync("obfuscation-info", "Please, specify dependecies folder!", Alerts.Danger);
8392
return;
8493
}
85-
if (string.IsNullOrWhiteSpace(_outputDirectory))
94+
if (string.IsNullOrWhiteSpace(_outputDirectoryName))
8695
{
8796
await AlertsContainer.ShowAlertAsync("obfuscation-info", "Please, specify output folder!", Alerts.Danger);
8897
return;
@@ -95,13 +104,13 @@ public async Task SelectDependencyFolderAsync(string folder)
95104
{
96105
await hideObfuscationInfoAlert();
97106

98-
_dependenciesDirectory = folder;
107+
_dependenciesDirectoryName = folder;
99108
}
100109
public async Task SelectOutputDirectory(string folder)
101110
{
102111
await hideObfuscationInfoAlert();
103112

104-
_outputDirectory = folder;
113+
_outputDirectoryName = folder;
105114
}
106115

107116
private async Task hideObfuscationInfoAlert()

BitMono/BitMono.Host/Modules/BitMonoModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected override void Load(ContainerBuilder containerBuilder)
6363
});
6464
}
6565

66-
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
66+
var configurationBuilder = new ConfigurationBuilder();
6767
if (m_ConfigureConfigurationBuilder != null)
6868
{
6969
m_ConfigureConfigurationBuilder.Invoke(configurationBuilder);

BitMono/BitMono.Host/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"[Tip]: Mark your method with attribute [MethodImpl(MethodImplOptions.NoInlining)] to ignore obfuscation of your method!",
44
"[Tip]: Mark your Type or method with attribute [Obfuscation(Feature = name)] to ignore obfuscation!",
55
"[Tip]: Open config.json and set Watermark to 'false', to disable watermarking of your file!",
6-
"[Tip]: Drop your libraries in 'Base' directory!"
6+
"[Tip]: Drop your libraries in 'libs' directory!"
77
]
88
}

BitMono/BitMono.Host/protections.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"Name": "StringsEncryption",
3333
"Enabled": true
3434
},
35-
// Currently should be disabled, because it is working so bad
35+
// Currently should be disabled, because of deprecation
3636
{
3737
"Name": "FieldsHiding",
3838
"Enabled": false

BitMono/BitMono.Obfuscation.API/ModuleDefMDCreationResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace BitMono.Obfuscation
55
{
66
public class ModuleDefMDCreationResult
77
{
8-
public AssemblyResolver AssemblyResolver { get; set; }
8+
public IAssemblyResolver AssemblyResolver { get; set; }
99
public ModuleContext ModuleContext { get; set; }
1010
public ModuleCreationOptions ModuleCreationOptions { get; set; }
1111
public ModuleDefMD ModuleDefMD { get; set; }

0 commit comments

Comments
 (0)