Skip to content

Commit

Permalink
Merge pull request #17 from stavroskasidis/feature/custom-caching-obf…
Browse files Browse the repository at this point in the history
…uscated

Feature/custom caching obfuscated
  • Loading branch information
stavroskasidis authored Apr 28, 2022
2 parents 81bbbc7 + 486d02e commit ab48e80
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 39 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ This package attempts to guard against false positives from antiviruses that fla
## What does this package do ?
This package injects some custom MSBuild tasks that do the following during publishing:
1. Obfuscates all client assemblies so that firewalls and antiviruses don't see them as executables. Obfuscation methods supported:
* Using a key to XOR all client assemblies (**default**).
* Using a key to XOR all client assemblies (**default**) .
* **OR**
* Changing the MZ header of all client assemblies to BZ, a custom header (less aggressive - more info [here](https://en.wikipedia.org/wiki/DOS_MZ_executable))
2. Renames the extension of all client assemblies from **.dll** to **.bin**
3. Adds a lib.module.js that contains a `beforeStart` blazor initialization method (more info [here](https://docs.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-6.0#javascript-initializers)), that uses a custom `loadBootResource` function to restore the obfuscation of the assemblies after downloaded, but before loaded by dotnet.wasm
* Changing the MZ header of all client assemblies to BZ, a custom header (less aggressive - more info [here](https://en.wikipedia.org/wiki/DOS_MZ_executable)) .
2. Renames the extension of all client assemblies from **.dll** to **.bin** .
3. Swaps Blazor's default caching mechanism with a custom one that saves the obfuscated assemblies on the cache instead of the unobfuscated ones. This is because some antiviruses are flaging the cached Blazor files that are being saved on the disk by the browser.
4. Adds a lib.module.js that contains a `beforeStart` Blazor initialization method (more info [here](https://docs.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-6.0#javascript-initializers)), that uses a custom `loadBootResource` function to restore the obfuscation of the assemblies after downloaded, but before loaded by dotnet.wasm.

## How to use
1. Add the nuget package in your **Client** (wasm) **AND** your **Server** (if using blazor wasm hosted) projects
1. Add the nuget package in your **Client** (wasm) **AND** your **Server** (if using Blazor wasm hosted) projects
```
dotnet add package BlazorWasmAntivirusProtection
```
Expand All @@ -45,13 +46,13 @@ dotnet publish Server\BlazorHostedSampleApp.Server.csproj -c Release
## Configuration
The following options allow you to customize the tasks executed by this package.
### **Custom dll rename extension**
If you want to use a different extension for renaming dlls, for example ".blz", add the following property in the **published** project's .csproj file (**Server** project if using blazor hosted).
If you want to use a different extension for renaming dlls, for example ".blz", add the following property in the **published** project's .csproj file (**Server** project if using Blazor hosted).
```xml
<RenameDllsTo>blz</RenameDllsTo>
```

### **Disable dll rename**
You can disable dll renaming by adding the following property in the **published** project's .csproj file (**Server** project if using blazor hosted).
You can disable dll renaming by adding the following property in the **published** project's .csproj file (**Server** project if using Blazor hosted).
```xml
<DisableRenamingDlls>true</DisableRenamingDlls>
```
Expand All @@ -73,6 +74,12 @@ You can change the key that is used for the XOR obfuscation adding the following
<XorKey>mykey</XorKey>
```

### **Disable caching**
You can disable boot resources caching by using the following property in your Client project's .csproj file, just as you would in any Blazor project. More info [here](https://docs.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/webassembly?view=aspnetcore-6.0#disable-integrity-checking-for-non-pwa-apps).
```xml
<BlazorCacheBootResources>false</BlazorCacheBootResources>
```

## Samples / Demo
You can find a sample app using this package [here](https://blazor-antivirus-block.azurewebsites.net/).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
$(RestoreAdditionalProjectSources);
$(MSBuildThisFileDirectory)../../../artifacts/nuget
</RestoreAdditionalProjectSources>
<!--<BlazorCacheBootResources>false</BlazorCacheBootResources>-->

<!--<ObfuscationMode>Xor</ObfuscationMode>
<XorKey>{903148E6-E3DC-4DED-A887-0061A8028C1C}</XorKey>-->
Expand All @@ -17,7 +18,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.1" PrivateAssets="all" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.6.0-beta" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.7.0-test" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.1" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.6.0-beta" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.7.0-test" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.2" PrivateAssets="all" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.6.0-beta" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.7.0-test" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.2" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.6.0-beta" />
<PackageReference Include="BlazorWasmAntivirusProtection" Version="1.7.0-test" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ Write-Host "Parameters"
Write-Host "=========="
Write-Host "Version suffix: $VersionSuffix"

Write-Message "Removing existing package from nuget cache ..."
Remove-Item -Recurse -Force $env:USERPROFILE/.nuget/packages/BlazorWasmAntivirusProtection -ErrorAction SilentlyContinue;

Write-Message "Building ..."
dotnet build ../src/BlazorWasmAntivirusProtection.Tasks/BlazorWasmAntivirusProtection.Tasks.csproj -c Release
dotnet build ../src/BlazorWasmAntivirusProtection/BlazorWasmAntivirusProtection.csproj -c Release /p:VersionSuffix="$VersionSuffix"
Expand Down
50 changes: 50 additions & 0 deletions scripts/debug.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Push-Location $PSScriptRoot

function Write-Message{
param([string]$message)
Write-Host
Write-Host $message
Write-Host
}
function Confirm-PreviousCommand {
param([string]$errorMessage)
if ( $LASTEXITCODE -ne 0) {
if( $errorMessage) {
Write-Message $errorMessage
}
exit $LASTEXITCODE
}
}

function Confirm-Process {
param ([System.Diagnostics.Process]$process,[string]$errorMessage)
$process.WaitForExit()
if($process.ExitCode -ne 0){
Write-Host $process.ExitCode
if( $errorMessage) {
Write-Message $errorMessage
}
exit $process.ExitCode
}
}

Write-Message "Removing existing package from nuget cache ..."
Remove-Item -Recurse -Force $env:USERPROFILE/.nuget/packages/BlazorWasmAntivirusProtection -ErrorAction SilentlyContinue;

Write-Message "Kill running dotnet processes"
Get-Process "dotnet" | Stop-Process


Write-Message "Building ..."
dotnet build ../src/BlazorWasmAntivirusProtection.Tasks/BlazorWasmAntivirusProtection.Tasks.csproj -c Debug
dotnet build ../src/BlazorWasmAntivirusProtection/BlazorWasmAntivirusProtection.csproj -c Debug /p:VersionSuffix="test"
Confirm-PreviousCommand

Write-Message "Creating nuget package ..."
dotnet pack ../src/BlazorWasmAntivirusProtection/BlazorWasmAntivirusProtection.csproj -c Debug /p:VersionSuffix="test" -o ../artifacts/nuget
Confirm-PreviousCommand

Write-Message "Build completed successfully"

dotnet clean ../sampleapps/BlazorHostedSampleApp/Server/BlazorHostedSampleApp.Server.csproj -c Release
dotnet publish ../sampleapps/BlazorHostedSampleApp/Server/BlazorHostedSampleApp.Server.csproj -c Release
10 changes: 8 additions & 2 deletions src/BlazorWasmAntivirusProtection.Tasks/ObfuscateDlls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ public class ObfuscateDlls : Task
{
[Required]
public ITaskItem[] PublishBlazorBootStaticWebAsset { get; set; }

[Required]
public string SettingsPath { get; set; }

public bool OriginalBlazorCacheBootResources { get; set; }

public string ObfuscationMode { get; set; } = Tasks.ObfuscationMode.Xor.ToString();
public string XorKey { get; set; } = "antiviruses suck!";

public string XorKey { get; set; } = "blazor is not a virus!!";

[Output]
public ITaskItem[] Extension { get; set; }
Expand Down Expand Up @@ -68,7 +73,8 @@ public override bool Execute()
var settings = JsonSerializer.Serialize(new
{
obfuscationMode = obfuscationMode,
xorKey = XorKey
xorKey = XorKey,
cacheBootResourcesObfuscated = OriginalBlazorCacheBootResources
});
File.WriteAllText(SettingsPath, settings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageProjectUrl>https://github.com/stavroskasidis/BlazorWasmAntivirusProtection</PackageProjectUrl>
<Description>This package attempts to guard against false positives from antiviruses that flag Blazor Wasm as malware</Description>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version>1.6.0</Version>
<Version>1.7.0</Version>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@

<!-- Runs in the client project -->
<Target Name="_CleanOldDlls" BeforeTargets="PrepareForILLink">
<PropertyGroup>
<OriginalBlazorCacheBootResources>$(BlazorCacheBootResources)</OriginalBlazorCacheBootResources>
<BlazorCacheBootResources>false</BlazorCacheBootResources>
</PropertyGroup>
<CleanOldDlls IntermediateLinkDir="$(IntermediateLinkDir)"></CleanOldDlls>
</Target>

<!-- Runs in the client project -->
<Target Name="_ObfuscateDlls">

<ObfuscateDlls PublishBlazorBootStaticWebAsset="@(PublishBlazorBootStaticWebAsset)"
SettingsPath="$(IntermediateOutputPath)avp-settings.json"
OriginalBlazorCacheBootResources="$(OriginalBlazorCacheBootResources)"
ObfuscationMode="$(ObfuscationMode)"
XorKey="$(XorKey)">
<Output TaskParameter="Extension" ItemName="BlazorPublishExtension"/>
Expand Down
Loading

0 comments on commit ab48e80

Please sign in to comment.