Skip to content
Merged
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
10 changes: 10 additions & 0 deletions Confuser.Core/Helpers/InjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ public static IEnumerable<IDnlibDef> Inject(TypeDef typeDef, TypeDef newType, Mo
ctx.DefMap[typeDef] = newType;
PopulateContext(typeDef, ctx);
Copy(typeDef, ctx, false);

// https://github.com/mcpolo99/ConfuserExx/issues/101
if (newType.IsGlobalModuleType) {
foreach (TypeDef nested in newType.NestedTypes.ToList()) {
newType.NestedTypes.Remove(nested);
nested.Visibility = nested.IsNestedPublic ? TypeAttributes.Public : TypeAttributes.NotPublic;
target.Types.Add(nested);
}
}

return ctx.DefMap.Values.Except(new[] { newType }).OfType<IDnlibDef>();
}

Expand Down
4 changes: 2 additions & 2 deletions Confuser.Protections/Compress/Compressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ void PackModules(ConfuserContext context, CompressorContext compCtx, ModuleDef s
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;
dataType.Visibility = TypeAttributes.NotPublic;
dataType.IsSealed = true;
dataType.ClassLayout = new ClassLayoutUser(1, (uint)data.Length);
stubModule.GlobalType.NestedTypes.Add(dataType);
stubModule.Types.Add(dataType);

var dataField = new FieldDefUser("DataField", new FieldSig(dataType.ToTypeSig())) {
IsStatic = true,
Expand Down
4 changes: 2 additions & 2 deletions Confuser.Protections/Constants/InjectPhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ void InjectHelpers(ConfuserContext context, ICompressionService compression, IRu

var dataType = new TypeDefUser("", moduleCtx.Name.RandomName(), context.CurrentModule.CorLibTypes.GetTypeRef("System", "ValueType"));
dataType.Layout = TypeAttributes.ExplicitLayout;
dataType.Visibility = TypeAttributes.NestedPrivate;
dataType.Visibility = TypeAttributes.NotPublic;
dataType.IsSealed = true;
moduleCtx.DataType = dataType;
context.CurrentModule.GlobalType.NestedTypes.Add(dataType);
context.CurrentModule.Types.Add(dataType);
moduleCtx.Name.MarkHelper(dataType, moduleCtx.Marker, (Protection)Parent);

moduleCtx.DataField = new FieldDefUser(moduleCtx.Name.RandomName(), new FieldSig(dataType.ToTypeSig())) {
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 @@ -88,11 +88,11 @@ void InjectHelpers(ConfuserContext context, ICompressionService compression, IRu

var dataType = new TypeDefUser("", moduleCtx.Name.RandomName(), context.CurrentModule.CorLibTypes.GetTypeRef("System", "ValueType"));
dataType.Layout = TypeAttributes.ExplicitLayout;
dataType.Visibility = TypeAttributes.NestedPrivate;
dataType.Visibility = TypeAttributes.NotPublic;
dataType.IsSealed = true;
dataType.ClassLayout = new ClassLayoutUser(1, 0);
moduleCtx.DataType = dataType;
context.CurrentModule.GlobalType.NestedTypes.Add(dataType);
context.CurrentModule.Types.Add(dataType);
moduleCtx.Name.MarkHelper(dataType, moduleCtx.Marker, (Protection)Parent);

moduleCtx.DataField = new FieldDefUser(moduleCtx.Name.RandomName(), new FieldSig(dataType.ToTypeSig())) {
Expand Down
5 changes: 2 additions & 3 deletions Confuser.Renamer/RickRoller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public static void CommenceRickroll(ConfuserContext context, ModuleDef module) {
var nameService = context.Registry.GetService<INameService>();
var injection = Injection.Replace("REPL", EscapeScript(JS));

var globalType = module.GlobalType;
var newType = new TypeDefUser(" ", module.CorLibTypes.Object.ToTypeDefOrRef());
newType.Attributes |= TypeAttributes.NestedPublic;
globalType.NestedTypes.Add(newType);
newType.Attributes |= TypeAttributes.NotPublic;
module.Types.Add(newType);

var trap = new MethodDefUser(
injection,
Expand Down
3 changes: 2 additions & 1 deletion Confuser.Runtime/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ static void Initialize() {

c = Assembly.Load(Lzma.Decompress(o));
AppDomain.CurrentDomain.AssemblyResolve += Handler;
AppDomain.CurrentDomain.ResourceResolve += Handler;
}

static Assembly Handler(object sender, ResolveEventArgs args) {
if (c.FullName == args.Name)
if (c.FullName == args.Name || Array.IndexOf(c.GetManifestResourceNames(), args.Name) != -1)
return c;
return null;
}
Expand Down
8 changes: 8 additions & 0 deletions Tests/Confuser.UnitTest/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ protected async Task Run(string[] inputFileNames, string[] expectedOutput, IEnum
};

foreach (string name in inputFileNames) {
if (IsExternal(name) && !IsAssembly(name))
continue;
var projectModule = new ProjectModule {
Path = Path.Combine(baseDir, GetFileName(name)),
IsExternal = IsExternal(name)
Expand Down Expand Up @@ -177,5 +179,11 @@ private static string GetFileName(string name) {
}

private static bool IsExternal(string name) => name.StartsWith(_externalPrefix, StringComparison.OrdinalIgnoreCase);

private static bool IsAssembly(string name) {
string extension = Path.GetExtension(GetFileName(name));
return extension.Equals(".dll", StringComparison.OrdinalIgnoreCase) ||
extension.Equals(".exe", StringComparison.OrdinalIgnoreCase);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Resource.txt" />
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions Tests/CrossFramework.Console.Net10/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;

namespace CrossFramework.Console {
class Program {
static int Main() {
System.Console.WriteLine("START");
System.Console.WriteLine("Hello from net10.0");
var resourceName = typeof(Program).Assembly.GetManifestResourceNames()
.Single(name => name.EndsWith("Resource.txt", StringComparison.Ordinal));
using (var stream = typeof(Program).Assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream)) {
System.Console.WriteLine(reader.ReadToEnd().Trim());
}
System.Console.WriteLine("END");
return 42;
}
Expand Down
1 change: 1 addition & 0 deletions Tests/CrossFramework.Console.Net10/Resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Resource from net10.0
32 changes: 32 additions & 0 deletions Tests/CrossFramework.Test/ConsoleFrameworkTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Confuser.Core;
using Confuser.Core.Project;
Expand Down Expand Up @@ -85,5 +87,35 @@ public Task Console_Net10_RenameProtection() =>
new SettingItem<Protection>("rename"),
outputDirSuffix: "-console-net10",
checkOutput: false);

[Fact]
[Trait("Category", "CrossFramework")]
[Trait("AppType", "Console")]
[Trait("TFM", "net10.0")]
[Trait("Protection", "resources")]
[Trait("Issue", "https://github.com/mcpolo99/ConfuserExx/issues/101")]
public Task Console_Net10_ResourceProtection() =>
Run(new[] { "CrossFramework.Console.Net10.dll", "external:CrossFramework.Console.Net10.runtimeconfig.json" },
null,
new SettingItem<Protection>("resources"),
outputDirSuffix: "-console-net10-resources",
checkOutput: false,
postProcessAction: outputPath => {
var startInfo = new ProcessStartInfo("dotnet", "CrossFramework.Console.Net10.dll") {
WorkingDirectory = outputPath,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
using var process = Process.Start(startInfo);
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
process.WaitForExit();
Assert.Equal(42, process.ExitCode);
Assert.Empty(error);
Assert.Equal(new[] { "START", "Hello from net10.0", "Resource from net10.0", "END" },
output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
return Task.CompletedTask;
});
}
}