Skip to content

Commit ace2dee

Browse files
committed
Add ConfuserEx core framework
1 parent fcc89e8 commit ace2dee

11 files changed

+677
-13
lines changed

Mdcrypt/MdcContext.cs

-7
This file was deleted.

Mdcrypt/Mdcrypt.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<Nullable>enable</Nullable>
1111
<OutputPath>..\bin\$(Configuration)</OutputPath>
1212
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
13+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1314
</PropertyGroup>
1415
<ItemGroup>
1516
<PackageReference Include="dnlib" Version="3.3.1" />

Mdcrypt/MdcryptContext.cs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using dnlib.DotNet;
5+
6+
namespace Mdcrypt {
7+
/// <summary>
8+
/// Mdcrypt context
9+
/// </summary>
10+
public sealed class MdcryptContext {
11+
/// <summary>
12+
/// Manifest assembly
13+
/// </summary>
14+
public AssemblyDef Assembly { get; }
15+
16+
/// <summary>
17+
/// Manifest module in <see cref="Assembly"/>
18+
/// </summary>
19+
public ModuleDefMD ManifestModule => (ModuleDefMD)Assembly.ManifestModule;
20+
21+
/// <summary>
22+
/// Additional assemblies, which will be merged or embedded
23+
/// </summary>
24+
public IList<AssemblyDef> AdditionalAssemblies { get; } = new List<AssemblyDef>();
25+
26+
/// <summary>
27+
/// Gets the current processing pipeline.
28+
/// </summary>
29+
public ProtectionPipeline Pipeline { get; }
30+
31+
/// <summary>
32+
/// Gets the token used to indicate cancellation
33+
/// </summary>
34+
public CancellationToken CancellationToken { get; }
35+
36+
/// <summary>
37+
/// Constructor
38+
/// </summary>
39+
/// <param name="module">The module to obfuscate and encrypt</param>
40+
/// <param name="cancellationToken">Cancellation token</param>
41+
public MdcryptContext(ModuleDefMD module, CancellationToken cancellationToken) {
42+
if (module is null)
43+
throw new ArgumentNullException(nameof(module));
44+
45+
Assembly = module.Assembly;
46+
Pipeline = new ProtectionPipeline();
47+
CancellationToken = cancellationToken;
48+
}
49+
50+
/// <summary>
51+
/// Throws a System.OperationCanceledException if protection process has been canceled.
52+
/// </summary>
53+
/// <exception cref="OperationCanceledException">The protection process is canceled.</exception>
54+
public void CheckCancellation() {
55+
CancellationToken.ThrowIfCancellationRequested();
56+
}
57+
}
58+
}

Mdcrypt/MdcEngine.cs Mdcrypt/MdcryptEngine.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ namespace Mdcrypt {
44
/// <summary>
55
/// Mdcrypt engine
66
/// </summary>
7-
public sealed class MdcEngine {
8-
private readonly MdcContext _context;
9-
10-
public MdcContext Context => _context;
7+
public sealed class MdcryptEngine {
8+
/// <summary>
9+
/// Context
10+
/// </summary>
11+
public MdcryptContext Context { get; }
1112

1213
/// <summary>
1314
/// Constructor
1415
/// </summary>
1516
/// <param name="context">Context for engine</param>
16-
public MdcEngine(MdcContext context) {
17+
public MdcryptEngine(MdcryptContext context) {
1718
if (context is null)
1819
throw new ArgumentNullException(nameof(context));
1920

20-
_context = context;
21+
Context = context;
2122
}
2223
}
2324
}

Mdcrypt/MdcryptException.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright (c) 2015 Ki
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
22+
23+
using System;
24+
25+
namespace Mdcrypt {
26+
/// <summary>
27+
/// The exception that is thrown when a handled error occurred during the protection process.
28+
/// </summary>
29+
#pragma warning disable CA1032 // Implement standard exception constructors
30+
public class MdcryptException : Exception {
31+
#pragma warning restore CA1032 // Implement standard exception constructors
32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="MdcryptException" /> class.
34+
/// </summary>
35+
/// <param name="innerException">The inner exception, or null if no exception is associated with the error.</param>
36+
public MdcryptException(Exception innerException) : base("Exception occurred during the protection process.", innerException) {
37+
}
38+
}
39+
}

Mdcrypt/MdcryptUtils.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using dnlib.DotNet;
2+
3+
namespace Mdcrypt {
4+
/// <summary>
5+
/// Mdcrypt utilities
6+
/// </summary>
7+
public static class MdcryptUtils {
8+
/// <summary>
9+
/// Load <see cref="ModuleDefMD"/>
10+
/// </summary>
11+
/// <param name="data">.NET module/assembly</param>
12+
/// <param name="assemblyResolver">Assembly resolver</param>
13+
/// <returns></returns>
14+
public static ModuleDefMD LoadModule(byte[] data, out AssemblyResolver assemblyResolver) {
15+
assemblyResolver = new AssemblyResolver();
16+
var context = new ModuleContext(assemblyResolver);
17+
assemblyResolver.EnableTypeDefCache = false;
18+
assemblyResolver.DefaultModuleContext = context;
19+
var options = new ModuleCreationOptions() {
20+
Context = context,
21+
TryToLoadPdbFromDisk = false
22+
};
23+
return ModuleDefMD.Load(data, options);
24+
}
25+
}
26+
}

Mdcrypt/Protection.cs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Copyright (c) 2015 Ki
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
22+
23+
using System;
24+
25+
namespace Mdcrypt {
26+
/// <summary>
27+
/// Various presets of protections.
28+
/// </summary>
29+
public enum ProtectionPreset {
30+
/// <summary> The protection does not belong to any preset. </summary>
31+
None = 0,
32+
33+
/// <summary> The protection provides basic security. </summary>
34+
Minimum = 1,
35+
36+
/// <summary> The protection provides normal security for public release. </summary>
37+
Normal = 2,
38+
39+
/// <summary> The protection provides better security with observable performance impact. </summary>
40+
Aggressive = 3,
41+
42+
/// <summary> The protection provides strongest security with possible incompatibility. </summary>
43+
Maximum = 4
44+
}
45+
46+
/// <summary>
47+
/// Indicates the <see cref="Protection" /> must initialize before the specified protections.
48+
/// </summary>
49+
[AttributeUsage(AttributeTargets.Class)]
50+
public sealed class BeforeProtectionAttribute : Attribute {
51+
/// <summary>
52+
/// Gets the full IDs of the specified protections.
53+
/// </summary>
54+
/// <value>The IDs of protections.</value>
55+
public string[] Ids { get; }
56+
57+
/// <summary>
58+
/// Initializes a new instance of the <see cref="BeforeProtectionAttribute" /> class.
59+
/// </summary>
60+
/// <param name="ids">The full IDs of the specified protections.</param>
61+
public BeforeProtectionAttribute(params string[] ids) {
62+
Ids = ids;
63+
}
64+
}
65+
66+
/// <summary>
67+
/// Indicates the <see cref="Protection" /> must initialize after the specified protections.
68+
/// </summary>
69+
[AttributeUsage(AttributeTargets.Class)]
70+
public sealed class AfterProtectionAttribute : Attribute {
71+
/// <summary>
72+
/// Gets the full IDs of the specified protections.
73+
/// </summary>
74+
/// <value>The IDs of protections.</value>
75+
public string[] Ids { get; }
76+
77+
/// <summary>
78+
/// Initializes a new instance of the <see cref="BeforeProtectionAttribute" /> class.
79+
/// </summary>
80+
/// <param name="ids">The full IDs of the specified protections.</param>
81+
public AfterProtectionAttribute(params string[] ids) {
82+
Ids = ids;
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Base class of Mdcrypt protections.
88+
/// </summary>
89+
/// <remarks>
90+
/// A parameterless constructor must exists in derived classes to enable plugin discovery.
91+
/// </remarks>
92+
public abstract class Protection {
93+
/// <summary>
94+
/// Gets the identifier of component used by users.
95+
/// </summary>
96+
/// <value>The identifier of component.</value>
97+
public abstract string Id { get; }
98+
99+
/// <summary>
100+
/// Gets the name of component.
101+
/// </summary>
102+
/// <value>The name of component.</value>
103+
public abstract string Name { get; }
104+
105+
/// <summary>
106+
/// Gets the description of component.
107+
/// </summary>
108+
/// <value>The description of component.</value>
109+
public abstract string Description { get; }
110+
111+
/// <summary>
112+
/// Gets the preset this protection is in.
113+
/// </summary>
114+
/// <value>The protection's preset.</value>
115+
public abstract ProtectionPreset Preset { get; }
116+
}
117+
}

Mdcrypt/ProtectionParameters.cs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright (c) 2015 Ki
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
22+
23+
using System;
24+
using System.Collections.Generic;
25+
using dnlib.DotNet;
26+
27+
namespace Mdcrypt {
28+
/// <summary>
29+
/// Identifies a member that is a setting item.
30+
/// </summary>
31+
public sealed class ProtectionSettingAttribute : Attribute {
32+
}
33+
34+
/// <summary>
35+
/// Protection settings for a certain component
36+
/// </summary>
37+
public abstract class ProtectionSettings {
38+
}
39+
40+
/// <summary>
41+
/// Parameters of <see cref="Protection" />.
42+
/// </summary>
43+
public sealed class ProtectionParameters {
44+
/// <summary>
45+
/// Gets the targets of protection.
46+
/// Possible targets are module, types, methods, fields, events, properties.
47+
/// </summary>
48+
/// <value>A list of protection targets.</value>
49+
public IList<IDnlibDef> Targets { get; }
50+
51+
/// <summary>
52+
/// Get default settings of the protection.
53+
/// </summary>
54+
public ProtectionSettings DefaultSettings { get; }
55+
56+
/// <summary>
57+
/// Get overridden settings of the specified target.
58+
/// </summary>
59+
public IDictionary<IDnlibDef, ProtectionSettings> OverrideSettings { get; }
60+
61+
/// <summary>
62+
/// Initializes a new instance of the <see cref="ProtectionParameters" /> class.
63+
/// </summary>
64+
/// <param name="targets">The protection targets.</param>
65+
/// <param name="defaultSettings">The protection default settings</param>
66+
internal ProtectionParameters(IList<IDnlibDef> targets, ProtectionSettings defaultSettings) {
67+
if (targets is null)
68+
throw new ArgumentNullException(nameof(targets));
69+
if (defaultSettings is null)
70+
throw new ArgumentNullException(nameof(defaultSettings));
71+
72+
Targets = targets;
73+
DefaultSettings = defaultSettings;
74+
OverrideSettings = new Dictionary<IDnlibDef, ProtectionSettings>();
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)