Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions Confuser.Core/ConfuserAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,50 @@ public AssemblyDef Resolve(IAssembly assembly, ModuleDef sourceModule) {
if (assembly is AssemblyDef assemblyDef)
return assemblyDef;

var resolvedAssemblyDef = InternalExactResolver.Resolve(assembly, sourceModule);
return resolvedAssemblyDef ?? InternalFuzzyResolver.Resolve(assembly, sourceModule);
var resolvedAssemblyDef =
InternalExactResolver.Resolve(assembly, sourceModule) ??
InternalFuzzyResolver.Resolve(assembly, sourceModule);

// Remove AssemblyAttributes.PA_NoPlatform
if (null != resolvedAssemblyDef &&
(AssemblyAttributes.PA_Mask & resolvedAssemblyDef.Attributes) == AssemblyAttributes.PA_NoPlatform) {
resolvedAssemblyDef.Attributes =
resolvedAssemblyDef.Attributes & ~AssemblyAttributes.PA_FullMask;
}

if (resolvedAssemblyDef?.Name == "netstandard" && 0 < resolvedAssemblyDef.ManifestModule.ExportedTypes.Count) {
// Move types from AssemblyRef to here
var module = resolvedAssemblyDef.ManifestModule;
var newTypes = new List<TypeDef>();
var allAssemblyRefs = new List<AssemblyDef>();

module.ExportedTypes.Clear();

foreach (var assemblyRef in module.GetAssemblyRefs()) {
var subAss =
InternalExactResolver.Resolve(assemblyRef, module) ??
InternalFuzzyResolver.Resolve(assemblyRef, module);
allAssemblyRefs.Add(subAss);
foreach (var subModule in subAss?.Modules) {
foreach (var defType in subModule.Types) {
newTypes.Add(defType);
}
subModule.Types.Clear();
foreach (var defType in newTypes) {
module.Types.Add(defType);
}
newTypes.Clear();
}
}

// Remove them because their types has been removed.
foreach (var subAss in allAssemblyRefs) {
InternalExactResolver.Remove(subAss);
InternalFuzzyResolver.Remove(subAss);
}
}

return resolvedAssemblyDef;
}

public void Clear() {
Expand Down
11 changes: 10 additions & 1 deletion Confuser.Core/Helpers/InjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,16 @@ public override ITypeDefOrRef Map(ITypeDefOrRef source) {
// check if the assembly reference needs to be fixed.
if (source is TypeRef sourceRef) {
var targetAssemblyRef = TargetModule.GetAssemblyRef(sourceRef.DefinitionAssembly.Name);
if (!(targetAssemblyRef is null) && !string.Equals(targetAssemblyRef.FullName, source.DefinitionAssembly.FullName, StringComparison.Ordinal)) {
if (targetAssemblyRef is null) {
// Handle assemblies referenced by netstandard
var corLibAssemblyRef = TargetModule.CorLibTypes.AssemblyRef;
var corLibAssembly = TargetModule.Context.AssemblyResolver.Resolve(corLibAssemblyRef, TargetModule);
if (null != corLibAssembly?.ManifestModule.GetAssemblyRef(sourceRef.DefinitionAssembly.Name)) {
var fixedTypeRef = new TypeRefUser(sourceRef.Module, sourceRef.Namespace, sourceRef.Name, corLibAssemblyRef);
return Importer.Import(fixedTypeRef);
}
}
else if (!string.Equals(targetAssemblyRef.FullName, source.DefinitionAssembly.FullName, StringComparison.Ordinal)) {
// We got a matching assembly by the simple name, but not by the full name.
// This means the injected code uses a different assembly version than the target assembly.
// We'll fix the assembly reference, to avoid breaking anything.
Expand Down
6 changes: 3 additions & 3 deletions Confuser.Protections/Compress/Compressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void PackModules(ConfuserContext context, CompressorContext compCtx, ModuleDef s
context.Logger.EndProgress();
}

void InjectData(ModuleDef stubModule, MethodDef method, byte[] data) {
void InjectData(ConfuserContext context, ModuleDef stubModule, MethodDef method, byte[] data) {
var dataType = new TypeDefUser("", "DataType", stubModule.CorLibTypes.GetTypeRef("System", "ValueType"));
dataType.Layout = TypeAttributes.ExplicitLayout;
dataType.Visibility = TypeAttributes.NestedPrivate;
Expand All @@ -197,7 +197,7 @@ void InjectData(ModuleDef stubModule, MethodDef method, byte[] data) {
repl.Add(Instruction.Create(OpCodes.Dup));
repl.Add(Instruction.Create(OpCodes.Ldtoken, dataField));
repl.Add(Instruction.Create(OpCodes.Call, stubModule.Import(
typeof(RuntimeHelpers).GetMethod("InitializeArray"))));
context, typeof(RuntimeHelpers), "InitializeArray")));
return repl.ToArray();
});
}
Expand Down Expand Up @@ -254,7 +254,7 @@ void InjectStub(ConfuserContext context, CompressorContext compCtx, ProtectionPa
MutationHelper.InjectKeys(entryPoint,
new[] { 0, 1 },
new[] { encryptedModule.Length >> 2, (int)seed });
InjectData(stubModule, entryPoint, encryptedModule);
InjectData(context, stubModule, entryPoint, encryptedModule);

// Decrypt
MethodDef decrypter = defs.OfType<MethodDef>().Single(method => method.Name == "Decrypt");
Expand Down
2 changes: 1 addition & 1 deletion Confuser.Protections/Constants/EncodePhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa
repl.Add(Instruction.Create(OpCodes.Dup));
repl.Add(Instruction.Create(OpCodes.Ldtoken, moduleCtx.DataField));
repl.Add(Instruction.Create(OpCodes.Call, moduleCtx.Module.Import(
typeof(RuntimeHelpers).GetMethod("InitializeArray"))));
context, typeof(RuntimeHelpers), "InitializeArray")));
return repl.ToArray();
});
}
Expand Down
4 changes: 2 additions & 2 deletions Confuser.Protections/Resources/InjectPhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ void MutateInitializer(REContext moduleCtx, MethodDef decomp) {
repl.Add(Instruction.Create(OpCodes.Dup));
repl.Add(Instruction.Create(OpCodes.Ldtoken, moduleCtx.DataField));
repl.Add(Instruction.Create(OpCodes.Call, moduleCtx.Module.Import(
typeof(RuntimeHelpers).GetMethod("InitializeArray"))));
moduleCtx.Context, typeof(RuntimeHelpers), "InitializeArray")));
return repl.ToArray();
});
moduleCtx.Context.Registry.GetService<IConstantService>().ExcludeMethod(moduleCtx.Context, moduleCtx.InitMethod);
}
}
}
}
18 changes: 18 additions & 0 deletions Confuser.Protections/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Confuser.Core;
using dnlib.DotNet;

namespace Confuser.Protections {
internal static class Utils {
public static IMethod Import(this ModuleDef module, ConfuserContext context, Type classType, string method) {
var corLib = context.Resolver.Resolve(context.CurrentModule?.CorLibTypes.AssemblyRef, context.CurrentModule);
var typeInfo = corLib?.ManifestModule.Find(classType.FullName, true);
return (typeInfo == null) ? module.Import(classType.GetMethod(method)) : module.Import(typeInfo.FindMethod(method));
}
}
}