Skip to content

Commit 4a397ab

Browse files
authored
Merge c029ebe into 755f188
2 parents 755f188 + c029ebe commit 4a397ab

File tree

12 files changed

+669
-12
lines changed

12 files changed

+669
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!--
2+
This file as to be present, otherwise the Microsoft.DiaSymReader.Native package does not copy it's files to the output.
3+
The content of this file does not matter.
4+
-->
5+
<appSettings />

Confuser.Protections/AntiTamper/NormalMode.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,18 @@ public void HandleMD(AntiTamperProtection parent, ConfuserContext context, Prote
100100
}
101101

102102
void WriterEvent(object sender, ModuleWriterEventArgs e) {
103-
var writer = (ModuleWriterBase)sender;
104-
if (e.Event == ModuleWriterEvent.MDEndCreateTables) {
105-
CreateSections(writer);
106-
}
107-
else if (e.Event == ModuleWriterEvent.BeginStrongNameSign) {
108-
EncryptSection(writer);
103+
switch (e.Event)
104+
{
105+
case ModuleWriterEvent.Begin when e.Writer is NativeModuleWriter nativeWriter:
106+
// disable the optimization of the image size for the native writer, so the method bodies can be protected.
107+
nativeWriter.Options = new NativeModuleWriterOptions(nativeWriter.ModuleDefMD, false);
108+
break;
109+
case ModuleWriterEvent.MDEndCreateTables:
110+
CreateSections(e.Writer);
111+
break;
112+
case ModuleWriterEvent.BeginStrongNameSign:
113+
EncryptSection(e.Writer);
114+
break;
109115
}
110116
}
111117

Confuser.Protections/TypeScrambler/ScramblePhase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using dnlib.DotNet;
66

77
namespace Confuser.Protections.TypeScrambler {
8-
internal sealed class ScramblePhase : ProtectionPhase {
8+
sealed class ScramblePhase : ProtectionPhase {
99
public ScramblePhase(TypeScrambleProtection parent) : base(parent) { }
1010

1111
public override ProtectionTargets Targets => ProtectionTargets.Types | ProtectionTargets.Methods;
@@ -30,7 +30,8 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa
3030
foreach (var def in context.CurrentModule.FindDefinitions().WithProgress(context.Logger)) {
3131
switch (def) {
3232
case MethodDef md:
33-
md.ReturnType = rewriter.UpdateSignature(md.ReturnType);
33+
if (md.HasReturnType)
34+
md.ReturnType = rewriter.UpdateSignature(md.ReturnType);
3435
if (md.HasBody) {
3536
rewriter.ProcessBody(md);
3637
}

Confuser.Protections/TypeScrambler/Scrambler/ScannedItem.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ internal bool RegisterGeneric(TypeSig t) {
3535
// Get proper type.
3636
t = SignatureUtils.GetLeaf(t);
3737

38+
// scrambling voids leads to peverify errors, better leave them out.
39+
if (t.ElementType == ElementType.Void)
40+
return false;
41+
3842
if (!Generics.ContainsKey(t)) {
3943
GenericParam newGenericParam;
4044
if (t.IsGenericMethodParameter) {

Confuser.Protections/TypeScrambler/Scrambler/ScannedMethod.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ internal override void Scan() {
3939
}
4040
}
4141

42-
if (TargetMethod.ReturnType != TargetMethod.Module.CorLibTypes.Void) {
42+
if (TargetMethod.HasReturnType) {
4343
RegisterGeneric(TargetMethod.ReturnType);
4444
}
45+
4546
foreach (var param in TargetMethod.Parameters.Where(ProcessParameter))
4647
RegisterGeneric(param.Type);
4748

@@ -89,6 +90,8 @@ private static bool CanScrambleMethod(MethodDef method, bool scramblePublic) {
8990
// PInvoke implementations won't work with this.
9091
if (method.IsPinvokeImpl) return false;
9192

93+
if (method.DeclaringType.IsGlobalModuleType) return false;
94+
9295
return true;
9396
}
9497

@@ -129,7 +132,7 @@ protected override void PrepareGenerics(IEnumerable<GenericParam> scrambleParams
129132
$"{nameof(parameter)}.Type == {nameof(TargetMethod)}.MethodSig.Params[{nameof(parameter)}.MethodSigIndex]");
130133
}
131134

132-
if (TargetMethod.ReturnType != TargetMethod.Module.CorLibTypes.Void)
135+
if (TargetMethod.HasReturnType)
133136
TargetMethod.ReturnType = ConvertToGenericIfAvalible(TargetMethod.ReturnType);
134137

135138
Debug.Assert(TargetMethod.ReturnType == TargetMethod.MethodSig.RetType,

Confuser.Protections/TypeScrambler/Scrambler/ScannedType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ private static bool CanScrambleType(TypeDef type) {
3232
// Delegates are something that shouldn't be touched.
3333
if (type.IsDelegate) return false;
3434

35-
// No entrypoints
36-
if (type.Methods.Any(x => x.Module.EntryPoint == x)) return false;
35+
// No entrypoints or global types
36+
if (type.Methods.Any(x => x.Module.EntryPoint == x) || type.IsGlobalModuleType) return false;
3737

3838
if (type.IsValueType) return false;
3939

Confuser2.sln

Lines changed: 394 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net461</TargetFramework>
5+
<RootNamespace>ClrProtection.Test</RootNamespace>
6+
<IsPackable>false</IsPackable>
7+
<PlatformTarget>x86</PlatformTarget>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\244_ClrProtection\244_ClrProtection.vcxproj" />
12+
<ProjectReference Include="..\Confuser.UnitTest\Confuser.UnitTest.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Confuser.Core;
4+
using Confuser.Core.Project;
5+
using Confuser.UnitTest;
6+
using Xunit;
7+
using Xunit.Abstractions;
8+
9+
namespace ClrProtection.Test {
10+
public class ProtectClrAssemblyTest : TestBase {
11+
public ProtectClrAssemblyTest(ITestOutputHelper outputHelper) : base(outputHelper) { }
12+
13+
[Fact]
14+
[Trait("Category", "Protection")]
15+
[Trait("Protection", "anti tamper")]
16+
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/244")]
17+
public Task AntiTamperProtection() => Run(
18+
"244_ClrProtection.exe",
19+
Array.Empty<String>(),
20+
new SettingItem<Protection>("anti tamper"),
21+
$"_{nameof(AntiTamperProtection)}");
22+
23+
[Fact]
24+
[Trait("Category", "Protection")]
25+
[Trait("Protection", "resources")]
26+
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/244")]
27+
public Task ResourceProtection() => Run(
28+
"244_ClrProtection.exe",
29+
Array.Empty<String>(),
30+
new SettingItem<Protection>("resources"),
31+
$"_{nameof(ResourceProtection)}");
32+
33+
[Fact]
34+
[Trait("Category", "Protection")]
35+
[Trait("Protection", "typescramble")]
36+
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/244")]
37+
public Task TypeScrambleProtection() => Run(
38+
"244_ClrProtection.exe",
39+
Array.Empty<String>(),
40+
new SettingItem<Protection>("typescramble"),
41+
$"_{nameof(TypeScrambleProtection)}");
42+
}
43+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{73f11ee8-f565-479e-8366-bd74ee467ce8}</ProjectGuid>
25+
<RootNamespace>My244ClrProtection</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
28+
</PropertyGroup>
29+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
30+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
31+
<ConfigurationType>Application</ConfigurationType>
32+
<UseDebugLibraries>true</UseDebugLibraries>
33+
<PlatformToolset>v142</PlatformToolset>
34+
<CharacterSet>Unicode</CharacterSet>
35+
<CLRSupport>true</CLRSupport>
36+
</PropertyGroup>
37+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
38+
<ConfigurationType>Application</ConfigurationType>
39+
<UseDebugLibraries>false</UseDebugLibraries>
40+
<PlatformToolset>v142</PlatformToolset>
41+
<WholeProgramOptimization>true</WholeProgramOptimization>
42+
<CharacterSet>Unicode</CharacterSet>
43+
<CLRSupport>true</CLRSupport>
44+
</PropertyGroup>
45+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
46+
<ConfigurationType>Application</ConfigurationType>
47+
<UseDebugLibraries>true</UseDebugLibraries>
48+
<PlatformToolset>v142</PlatformToolset>
49+
<CharacterSet>Unicode</CharacterSet>
50+
<CLRSupport>true</CLRSupport>
51+
</PropertyGroup>
52+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
53+
<ConfigurationType>Application</ConfigurationType>
54+
<UseDebugLibraries>false</UseDebugLibraries>
55+
<PlatformToolset>v142</PlatformToolset>
56+
<WholeProgramOptimization>true</WholeProgramOptimization>
57+
<CharacterSet>Unicode</CharacterSet>
58+
<CLRSupport>true</CLRSupport>
59+
</PropertyGroup>
60+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
61+
<ImportGroup Label="ExtensionSettings">
62+
</ImportGroup>
63+
<ImportGroup Label="Shared">
64+
</ImportGroup>
65+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
66+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
67+
</ImportGroup>
68+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
69+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
70+
</ImportGroup>
71+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
72+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
73+
</ImportGroup>
74+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
75+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
76+
</ImportGroup>
77+
<PropertyGroup Label="UserMacros" />
78+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
79+
<LinkIncremental>true</LinkIncremental>
80+
</PropertyGroup>
81+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
82+
<LinkIncremental>false</LinkIncremental>
83+
</PropertyGroup>
84+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
85+
<LinkIncremental>true</LinkIncremental>
86+
</PropertyGroup>
87+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
88+
<LinkIncremental>false</LinkIncremental>
89+
</PropertyGroup>
90+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
91+
<ClCompile>
92+
<WarningLevel>Level3</WarningLevel>
93+
<SDLCheck>true</SDLCheck>
94+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
95+
<ConformanceMode>false</ConformanceMode>
96+
</ClCompile>
97+
<Link>
98+
<SubSystem>Console</SubSystem>
99+
<GenerateDebugInformation>false</GenerateDebugInformation>
100+
<EntryPointSymbol>main</EntryPointSymbol>
101+
</Link>
102+
</ItemDefinitionGroup>
103+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
104+
<ClCompile>
105+
<WarningLevel>Level3</WarningLevel>
106+
<FunctionLevelLinking>true</FunctionLevelLinking>
107+
<IntrinsicFunctions>true</IntrinsicFunctions>
108+
<SDLCheck>true</SDLCheck>
109+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
110+
<ConformanceMode>false</ConformanceMode>
111+
</ClCompile>
112+
<Link>
113+
<SubSystem>Console</SubSystem>
114+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
115+
<OptimizeReferences>true</OptimizeReferences>
116+
<GenerateDebugInformation>false</GenerateDebugInformation>
117+
<EntryPointSymbol>main</EntryPointSymbol>
118+
</Link>
119+
</ItemDefinitionGroup>
120+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
121+
<ClCompile>
122+
<WarningLevel>Level3</WarningLevel>
123+
<SDLCheck>true</SDLCheck>
124+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
125+
<ConformanceMode>false</ConformanceMode>
126+
</ClCompile>
127+
<Link>
128+
<SubSystem>Console</SubSystem>
129+
<GenerateDebugInformation>false</GenerateDebugInformation>
130+
<EntryPointSymbol>main</EntryPointSymbol>
131+
</Link>
132+
</ItemDefinitionGroup>
133+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
134+
<ClCompile>
135+
<WarningLevel>Level3</WarningLevel>
136+
<FunctionLevelLinking>true</FunctionLevelLinking>
137+
<IntrinsicFunctions>true</IntrinsicFunctions>
138+
<SDLCheck>true</SDLCheck>
139+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
140+
<ConformanceMode>false</ConformanceMode>
141+
</ClCompile>
142+
<Link>
143+
<SubSystem>Console</SubSystem>
144+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
145+
<OptimizeReferences>true</OptimizeReferences>
146+
<GenerateDebugInformation>false</GenerateDebugInformation>
147+
<EntryPointSymbol>main</EntryPointSymbol>
148+
</Link>
149+
</ItemDefinitionGroup>
150+
<ItemGroup>
151+
<ClCompile Include="Program.cpp" />
152+
</ItemGroup>
153+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
154+
<ImportGroup Label="ExtensionTargets">
155+
</ImportGroup>
156+
</Project>

0 commit comments

Comments
 (0)