Skip to content

Commit

Permalink
Merge pull request #49 from sykesbPragmatics/issue48
Browse files Browse the repository at this point in the history
Adding retry logic to XorDllWithKey
  • Loading branch information
stavroskasidis authored Feb 7, 2023
2 parents 0ed9c6d + 85e93de commit 20d0d8f
Showing 1 changed file with 33 additions and 8 deletions.
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);
}
}
}

0 comments on commit 20d0d8f

Please sign in to comment.