I had some help with a text-replacement script recently and wondered if it could be used for patching purposes. I usually use HxD, and thought that if it's essentially a glorified notepad, what difference is there between editing an executable in a text editor and a hex editor? I decided to test my theory and put the script to the test, while it can replace the specified strings, it adds information to the executable.
What throws me off here is that if it is used for replacing text in a text document, it only replaces strings and doesn't add bytes to the document unless the new string is longer than the previous one. I believe this is because the script is designed for editing text documents and modifies the header because of this. I used HxD to compare the before and after and sure enough the header gets modified. Here is the script I have currently:
@echo off setlocal enableextensions disabledelayedexpansion set search=OutDir=Old String set replace=OutDir=New String set textFile=Document.txt :PowerShell SET PSScript=%temp%\~tmpStrRplc.ps1 ECHO (Get-Content "%~dp0%textFile%").replace("%search%", "%replace%") ^| Set-Content "%~dp0%textFile%">"%PSScript%" SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0 CD /D "%PowerShellDir%" Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'" EXIT
(The strings I will be replacing are the same size, so the executable's size will be unaltered, and string corruption is not being used.)
I would like to note that I am new to PowerShell, so I'm still learning how it works. I noticed the
%~dp0%textFile% variable. If I am interpreting this variable correctly, it tells the PowerShell script to handle the defined file to be modified as a text document. Going with this logic (if it is correct), is there a similar variable that will either ignore the format or handle the input file as an executable? My script is capable of changing what I define, but breaks the executable, Is patching with my script feasible?