Skip to content

Commit

Permalink
Fixed Edge finder, removed command line args, added byte patches, add…
Browse files Browse the repository at this point in the history
…ed alternative debug pattern, etc.
  • Loading branch information
Ceiridge committed Apr 8, 2020
1 parent 862f2f1 commit 84f026d
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 32 deletions.
8 changes: 7 additions & 1 deletion ChromeDevExtWarningPatcher/ChromeDevExtWarningPatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="BytePatch.cs" />
<Compile Include="Patches\BytePatch.cs" />
<Compile Include="DllPatcher.cs" />
<Compile Include="InstallationFinder\Defaults\Brave.cs" />
<Compile Include="InstallationFinder\Defaults\Chrome.cs" />
<Compile Include="InstallationFinder\Defaults\Edge.cs" />
Expand All @@ -90,6 +91,10 @@
<Compile Include="PatcherGui.xaml.cs">
<DependentUpon>PatcherGui.xaml</DependentUpon>
</Compile>
<Compile Include="Patches\BytePatchManager.cs" />
<Compile Include="Patches\BytePatchPattern.cs" />
<Compile Include="Patches\Defaults\Patches.cs" />
<Compile Include="Patches\Defaults\Patterns.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand All @@ -110,5 +115,6 @@
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
54 changes: 54 additions & 0 deletions ChromeDevExtWarningPatcher/DllPatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using ChromeDevExtWarningPatcher.Patches;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ChromeDevExtWarningPatcher {
class DllPatcher {
private string DllPath;

public DllPatcher(string dllPath) {
DllPath = dllPath;
}

public void Patch(BytePatchPattern.WriteToLog log) {
FileInfo dllFile = new FileInfo(DllPath);
if (!dllFile.Exists)
throw new IOException("File not found");

byte[] raw = File.ReadAllBytes(dllFile.FullName);

if(Program.bytePatchManager.PatchBytes(ref raw, IsImageX64(dllFile.FullName), log)) {
File.WriteAllBytes(dllFile.FullName, raw);
log("Patched and saved successfully " + dllFile.FullName);
} else {
log("Error trying to patch " + dllFile.FullName);
}
}

// Taken from https://stackoverflow.com/questions/480696/how-to-find-if-a-native-dll-file-is-compiled-as-x64-or-x86
private static bool IsImageX64(string dllFilePath) {
using (var stream = new FileStream(dllFilePath, FileMode.Open, FileAccess.Read))
using (var reader = new BinaryReader(stream)) {
//check the MZ signature to ensure it's a valid Portable Executable image
if (reader.ReadUInt16() != 23117)
throw new BadImageFormatException("Not a valid Portable Executable image", dllFilePath);

// seek to, and read, e_lfanew then advance the stream to there (start of NT header)
stream.Seek(0x3A, SeekOrigin.Current);
stream.Seek(reader.ReadUInt32(), SeekOrigin.Begin);

// Ensure the NT header is valid by checking the "PE\0\0" signature
if (reader.ReadUInt32() != 17744)
throw new BadImageFormatException("Not a valid Portable Executable image", dllFilePath);

// seek past the file header, then read the magic number from the optional header
stream.Seek(20, SeekOrigin.Current);
ushort magicByte = reader.ReadUInt16();
return magicByte == 0x20B;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Edge : Installation {
public override List<string> FindDllFiles() {
List<string> dllFiles = new List<string>();

AddDllToList(dllFiles, GetLatestDll(new DirectoryInfo(@"C:\Progion"), "msedge.dll"));
AddDllToList(dllFiles, GetLatestDll(new DirectoryInfo(@"C:\Prcation"), "msedge.dll"));
AddDllToList(dllFiles, GetLatestDll(new DirectoryInfo(@"C:\Program Files (x86)\Microsoft\Edge\Application"), "msedge.dll"));
AddDllToList(dllFiles, GetLatestDll(new DirectoryInfo(@"C:\Program Files\Microsoft\Edge\Application"), "msedge.dll"));

return dllFiles;
}
Expand Down
2 changes: 1 addition & 1 deletion ChromeDevExtWarningPatcher/PatcherGui.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<ListBox Height="325" Margin="10,0" Background="{x:Null}" Foreground="#FFCA3E47" FontSize="12" HorizontalAlignment="Center" VerticalAlignment="Top" FontWeight="Normal" Width="184">
<CheckBox x:Name="RemoveExtWarning" Content="Remove extension warning" BorderBrush="#FFCA3E47" Background="White" Foreground="#FFCA3E47" IsChecked="True" ToolTip="This patch removes the warning of developer mode extensions when starting a Chromium browser"/>
<CheckBox x:Name="RemoveDebugWarning" Content="Remove debugging warning" BorderBrush="#FFCA3E47" Background="White" Foreground="#FFCA3E47" IsChecked="True" ToolTip="This patch gets rid of the debugging warning when using chrome.debugger in extensions"/>
<CheckBox Content="Disable Elision" BorderBrush="#FFCA3E47" Background="White" Foreground="#FFCA3E47" IsChecked="True" ToolTip="This patch forces Chromium to show WWW and HTTPS again in the url bar!"/>
<CheckBox x:Name="RemoveElision" Content="Disable Elision" BorderBrush="#FFCA3E47" Background="White" Foreground="#FFCA3E47" IsChecked="True" ToolTip="This patch forces Chromium to show WWW and HTTPS again in the url bar!"/>
</ListBox>
<Button x:Name="PatchBtn" Content="Patch" HorizontalAlignment="Center" Height="27" Margin="9,0,10,5" VerticalAlignment="Bottom" Width="185" Background="#FF525252" BorderBrush="White" FontWeight="Normal" FontSize="14" Click="PatchBtn_Click">
<Button.Foreground>
Expand Down
29 changes: 25 additions & 4 deletions ChromeDevExtWarningPatcher/PatcherGui.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.Win32;
using ChromeDevExtWarningPatcher.Patches.Defaults;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
Expand All @@ -27,15 +29,34 @@ public partial class PatcherGui : Window {
openFile.FilterIndex = 1;
openFile.CheckFileExists = openFile.CheckPathExists = openFile.AddExtension = true;

if(openFile.ShowDialog(this) == true) { // No, I am not a noob, I have to do it like this
if(openFile.ShowDialog(this) == true) { // No, I am not a noob, I have to do it like this and further below
AddChromiumInstallation(openFile.FileName);
}
}

private void PatchBtn_Click(object sender, RoutedEventArgs e) {
foreach(CheckBox installationBox in InstallationList.Items) {
if(installationBox.IsChecked == true) { // No, I am not a noob, I have to do it like this
Program.bytePatchManager.disabledTypes.Clear();
if (RemoveExtWarning.IsChecked == false) {
Program.bytePatchManager.disabledTypes.Add(typeof(RemoveExtensionWarningPatch1));
Program.bytePatchManager.disabledTypes.Add(typeof(RemoveExtensionWarningPatch2));
}
if (RemoveExtWarning.IsChecked == false)
Program.bytePatchManager.disabledTypes.Add(typeof(RemoveDebugWarningPatch));
if (RemoveElision.IsChecked == false)
Program.bytePatchManager.disabledTypes.Add(typeof(RemoveElisionPatch));

foreach (CheckBox installationBox in InstallationList.Items) {
if (installationBox.IsChecked == true) {
string path = installationBox.Content.ToString();

new Thread(() => {
try {
DllPatcher patcher = new DllPatcher(path);
patcher.Patch(Log);
} catch (Exception ex) {
Log("Error while patching " + path + ":" + ex.Message);
}
}).Start();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
namespace ChromeDevExtWarningPatcher
using ChromeDevExtWarningPatcher.Patches;

namespace ChromeDevExtWarningPatcher
{
public class BytePatch
{
public byte origByte, patchByte;
public int offset;
public byte[] pattern;
public BytePatchPattern pattern;

public BytePatch(byte[] pattern, byte origByte, byte patchByte, int offset)
public BytePatch(BytePatchPattern pattern, byte origByte, byte patchByte, int offset)
{
this.pattern = pattern;
this.origByte = origByte;
Expand Down
48 changes: 48 additions & 0 deletions ChromeDevExtWarningPatcher/Patches/BytePatchManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using ChromeDevExtWarningPatcher.Patches.Defaults;
using System;
using System.Collections.Generic;

namespace ChromeDevExtWarningPatcher.Patches {
class BytePatchManager {
private List<BytePatch> BytePatches = new List<BytePatch>();

public List<Type> disabledTypes = new List<Type>();

public BytePatchManager() {
BytePatches.Clear();
BytePatches.Add(new RemoveExtensionWarningPatch1());
BytePatches.Add(new RemoveExtensionWarningPatch2());
BytePatches.Add(new RemoveDebugWarningPatch());
BytePatches.Add(new RemoveElisionPatch());
}

public bool PatchBytes(ref byte[] raw, bool x64, BytePatchPattern.WriteToLog log) {
int patches = 0;

foreach(BytePatch patch in BytePatches) {
if (disabledTypes.Contains(patch.GetType())) {
patches++;
continue;
}
long addr = patch.pattern.FindAddress(raw, x64, log);

if(addr != -1) {
long index = addr + patch.offset;
byte sourceByte = raw[index];

log("Source byte of patch at " + patch.offset + ": " + sourceByte);
if (sourceByte == patch.origByte) {
raw[index] = patch.patchByte;
log(index + " => " + patch.patchByte);
patches++;
} else
log("Source byte unexpected, should be " + patch.origByte + "!");
} else {
log("Couldn't find offset for a patch " + patch.pattern.Name);
}
}

return patches == BytePatches.Count;
}
}
}
41 changes: 41 additions & 0 deletions ChromeDevExtWarningPatcher/Patches/BytePatchPattern.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChromeDevExtWarningPatcher.Patches {
public abstract class BytePatchPattern {
public string Name;
protected List<byte[]> AlternativePatternsX86 = new List<byte[]>();
protected List<byte[]> AlternativePatternsX64 = new List<byte[]>();

public BytePatchPattern(string name) {
Name = name;
}

public delegate void WriteToLog(string str);
public long FindAddress(byte[] raw, bool x64, WriteToLog log) {
foreach (byte[] pattern in (x64 ? AlternativePatternsX64 : AlternativePatternsX86)) {
int patternIndex = 0, patternOffset = 0;

for (int i = 0; i < raw.Length; i++) {
byte chromeByte = raw[i];
byte expectedByte = pattern[patternIndex];

if (expectedByte == 0xFF ? true : (chromeByte == expectedByte))
patternIndex++;
else
patternIndex = 0;

if (patternIndex == pattern.Length) {
patternOffset = i - (patternIndex - 1);
log("Found pattern offset at " + patternOffset);
return patternOffset;
}
}
}

return -1L;
}
}
}
17 changes: 17 additions & 0 deletions ChromeDevExtWarningPatcher/Patches/Defaults/Patches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ChromeDevExtWarningPatcher.Patches.Defaults {
public class RemoveExtensionWarningPatch1 : BytePatch {
public RemoveExtensionWarningPatch1() : base(new RemoveExtensionWarningPattern(), 0x04, 0xFF, 22) { }
}

public class RemoveExtensionWarningPatch2 : BytePatch {
public RemoveExtensionWarningPatch2() : base(new RemoveExtensionWarningPattern(), 0x08, 0xFF, 35) { }
}

public class RemoveDebugWarningPatch : BytePatch {
public RemoveDebugWarningPatch() : base(new RemoveDebugWarningPattern(), 0x41, 0xC3, 0x00) { }
}

public class RemoveElisionPatch : BytePatch {
public RemoveElisionPatch() : base(new RemoveElisionPattern(), 0x56, 0xC3, 0x00) { }
}
}
20 changes: 20 additions & 0 deletions ChromeDevExtWarningPatcher/Patches/Defaults/Patterns.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace ChromeDevExtWarningPatcher.Patches.Defaults {
public class RemoveExtensionWarningPattern : BytePatchPattern {
public RemoveExtensionWarningPattern() : base("Remove Extension Warning") {
AlternativePatternsX64.Add(new byte[] { 0x56, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x89, 0xD6, 0x48, 0x89, 0xD1, 0xE8, 0xFF, 0xFF, 0xFF, 0xFF, 0x89, 0xC1 });
}
}

public class RemoveDebugWarningPattern : BytePatchPattern {
public RemoveDebugWarningPattern() : base("Remove Debug Warning") {
AlternativePatternsX64.Add(new byte[] { 0x41, 0x57, 0x41, 0x56, 0x41, 0x54, 0x56, 0x57, 0x53, 0x48, 0x81, 0xEC, 0xFF, 0xFF, 0xFF, 0xFF, 0x49, 0x89, 0xCC, 0x48, 0x8B, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x31, 0xE0, 0x48, 0x89, 0x84, 0x24, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x8D, 0x4A, 0xFF });
AlternativePatternsX64.Add(new byte[] { 0x41, 0x57, 0x41, 0x56, 0x56, 0x57, 0x53, 0x48, 0x81, 0xEC, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x89, 0xCE, 0x48, 0x8B, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x31, 0xE0, 0x48, 0x89, 0x84, 0x24, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x8D, 0x4A, 0x08 });
}
}

public class RemoveElisionPattern : BytePatchPattern {
public RemoveElisionPattern() : base("Remove Elision") {
AlternativePatternsX64.Add(new byte[] { 0x56, 0x57, 0x53, 0x48, 0x83, 0xEC, 0x40, 0x48, 0x8B, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x31, 0xE0, 0x48, 0x89, 0x44, 0x24, 0xFF, 0xE8, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x85, 0xC0, 0x74, 0x61 });
}
}
}
29 changes: 8 additions & 21 deletions ChromeDevExtWarningPatcher/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using ChromeDevExtWarningPatcher.Patches;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -12,32 +13,18 @@ namespace ChromeDevExtWarningPatcher
{
class Program
{
private static string CHROME_INSTALLATION_FOLDER = @"C:\Program Files (x86)\Google\Chrome\Application";

private static readonly byte[] SHOULDINCLUDEEXTENSION_FUNCTION_PATTERN = { 0x56, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x89, 0xD6, 0x48, 0x89, 0xD1, 0xE8, 0xFF, 0xFF, 0xFF, 0xFF, 0x89, 0xC1 }; // 0xFF is ?
private static readonly byte[] MAYBEADDINFOBAR_FUNCTION_PATTERN = { 0x41, 0x57, 0x41, 0x56, 0x56, 0x57, 0x53, 0x48, 0x81, 0xEC, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x89, 0xCE, 0x48, 0x8B, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x31, 0xE0, 0x48, 0x89, 0x84, 0x24, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x8D, 0x4A, 0x08 }; // 0xFF is ?; debugPatch
private static readonly byte[] SHOULDPREVENTELISION_FUNCTION_PATTERN = { 0x56, 0x57, 0x53, 0x48, 0x83, 0xEC, 0x40, 0x48, 0x8B, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x31, 0xE0, 0x48, 0x89, 0x44, 0x24, 0xFF, 0xE8, 0xFF, 0xFF, 0xFF, 0xFF, 0x48, 0x85, 0xC0, 0x74, 0x61 }; // 0xFF is ?

private static BytePatch[] BYTE_PATCHES = { new BytePatch(SHOULDINCLUDEEXTENSION_FUNCTION_PATTERN, 0x04, 0xFF, 22), new BytePatch(SHOULDINCLUDEEXTENSION_FUNCTION_PATTERN, 0x08, 0xFF, 35), new BytePatch(SHOULDPREVENTELISION_FUNCTION_PATTERN, 0x56, 0xC3, 0x0), new BytePatch(MAYBEADDINFOBAR_FUNCTION_PATTERN, 0x41, 0xC3, 0x0) };

private static readonly BytePatch REMOVAL_PATCH = new BytePatch(new byte[] { }, 0x0, 0x0, int.MinValue);


private static Application guiApp;
private static Window guiWindow;

private static double GetUnixTime(DateTime date)
{
return (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
}
public static BytePatchManager bytePatchManager;

[STAThread]
public static void Main(string[] args)
{
bytePatchManager = new BytePatchManager();
guiApp = new Application();
guiApp.Run(guiWindow = new PatcherGui());

if (ContainsArg(args, "noWarningPatch"))
/*if (ContainsArg(args, "noWarningPatch"))
RemovePatches(SHOULDINCLUDEEXTENSION_FUNCTION_PATTERN);
if (ContainsArg(args, "noWWWPatch"))
RemovePatches(SHOULDPREVENTELISION_FUNCTION_PATTERN);
Expand Down Expand Up @@ -70,10 +57,10 @@ public static void Main(string[] args)
}
if(!ContainsArg(args, "noWait"))
Thread.Sleep(5000); // Wait a bit to let the user see the result
Thread.Sleep(5000); // Wait a bit to let the user see the result*/
}

private static bool BytePatchChrome(FileInfo chromeDll)
/*private static bool BytePatchChrome(FileInfo chromeDll)
{
byte[] chromeBytes = File.ReadAllBytes(chromeDll.FullName);
int patches = 0;
Expand Down Expand Up @@ -195,6 +182,6 @@ private static void RemovePatches(byte[] pattern)
CHROME_INSTALLATION_FOLDER = fullArgs.Substring(0, regMatch.Index + regMatchGroup.Length);
Console.WriteLine("New installation folder set: " + CHROME_INSTALLATION_FOLDER);
}
}
}*/
}
}

0 comments on commit 84f026d

Please sign in to comment.