Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding retry logic to XorDllWithKey #49

Merged
merged 4 commits into from
Feb 7, 2023
Merged
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
41 changes: 33 additions & 8 deletions src/BlazorWasmAntivirusProtection.Tasks/ObfuscateDlls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace BlazorWasmAntivirusProtection.Tasks
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;

public class ObfuscateDlls : Task
{
Expand All @@ -28,11 +29,11 @@ public class ObfuscateDlls : Task

public override bool Execute()
{
//#if DEBUG
// System.Diagnostics.Debugger.Launch();
//#endif
//#if DEBUG
// System.Diagnostics.Debugger.Launch();
//#endif
if (PublishBlazorBootStaticWebAsset.Length == 0) return true;
if(!Enum.TryParse<ObfuscationMode>(ObfuscationMode, out var obfuscationMode))
if (!Enum.TryParse<ObfuscationMode>(ObfuscationMode, out var obfuscationMode))
{
return false;
}
Expand All @@ -55,7 +56,7 @@ public override bool Execute()
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Changing .dll headers from MZ to BZ finished");

}
else if(obfuscationMode == Tasks.ObfuscationMode.Xor)
else if (obfuscationMode == Tasks.ObfuscationMode.Xor)
{
var key = Encoding.ASCII.GetBytes(XorKey);
Log.LogMessage(MessageImportance.High, "BlazorWasmAntivirusProtection: Xor'ing .dlls");
Expand All @@ -74,7 +75,7 @@ public override bool Execute()
{
obfuscationMode = obfuscationMode,
xorKey = XorKey,
cacheBootResourcesObfuscated = OriginalBlazorCacheBootResources
cacheBootResourcesObfuscated = OriginalBlazorCacheBootResources
});
File.WriteAllText(SettingsPath, settings);

Expand All @@ -96,13 +97,37 @@ void ChangeDllHeaderToBz(string fn)
bw.Write(bz);
}

void XorDllWithKey(string fn,byte[] key)
void XorDllWithKey(string fn, byte[] key)
{
var data = File.ReadAllBytes(fn);
for (int i = 0; i < data.Length; i++)
data[i] = (byte)(data[i] ^ key[i % key.Length]);

File.WriteAllBytes(fn, data);
//Ensure file is not locked
//Some VM based systems are having issues replacing the file
int attempts = 0;
bool complete = false;
do
{
try
{
attempts++;
File.WriteAllBytes(fn, data);
complete = true;
}
catch (Exception)
{
if (attempts > 11)
{
//Tried 10 times, abort
throw;
}
//Sleep for .5 second, hopefully lock will clear
Log.LogMessage(MessageImportance.High, "BlazorWasmAntivirusProtection: File locked, will re-attempt");
Thread.Sleep(500);
}
}
while (!complete);
}
}
}