Jump to content
Tuts 4 You

Search & Replace Patch Source ?


Matrix

Recommended Posts

Heres my own version, just wrote this up based on my memory scanner code. Was rather easy to convert over to a file instead of memory too hehe.

'##=======================================================
'## VB6 Search and Replace Patch Method By Wiccaan
'##=======================================================
'## modFunctions.bas - May 06, 2008
'## Copyright (c) - Wiccaan (wiccaan@comcast.net) 2008
'##=======================================================
'##
'## You are free to edit, distrubute, and use this code as
'## long as you give credit to Wiccaan. Along with that, you
'## do not have permission to remove any credit lines from
'## this file. You do not have permission to use this in
'## any commerical applications.
'##
'##=======================================================Option ExplicitPublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)'//
' @Function: OpenFile
' @Purpose : Opens the given file in binary into a byte array.
'\\
Public Function OpenFile(ByVal strFile As String) As Byte()
On Error Resume Next Dim iFileNum As Integer
Dim bByteArray() As Byte
Dim iFileSize As Long iFileNum = FreeFile Open strFile For Binary As #iFileNum
iFileSize = LOF(iFileNum)
ReDim bByteArray(1 To iFileSize) As Byte
Get #iFileNum, , bByteArray
Close #iFileNum OpenFile = bByteArray
End Function'//
' @Function: SaveFile
' @Purpose : Saves the given bytes to the given file. (Overwrites full file!)
'\\
Public Function SaveFile(ByVal strFile As String, ByRef bArray() As Byte)
On Error Resume Next Dim iFileNum As Integer
iFileNum = FreeFile Open strFile For Binary As #iFileNum
Put #iFileNum, , bArray
Close #iFileNum
End Function'//
' @Function: SearchAndReplace
' @Purpose : Scans the given byte array for the SearchBytes() pattern then replaces
' the bytes, starting from the first byte where the pattern was found.
' FileBytes is a pointer to the opened file bytes. This will edit the
' bytes in the array to be saved later.
'\\
Public Function SearchAndReplace(ByRef FileBytes() As Byte, ByRef SearchBytes() As Byte, ByRef ReplaceBytes() As Byte) As Boolean
On Error GoTo ErrHand Dim iOffset As Long
iOffset = InStrB(1, FileBytes, SearchBytes)
If iOffset > 0 Then Dim x As Long
Dim y As Long
y = 0
For x = iOffset To (iOffset + UBound(ReplaceBytes))
FileBytes(x) = ReplaceBytes(y)
y = y + 1
Next x Else
SearchAndReplace = False
Exit Function
End If SearchAndReplace = True
Exit Function
ErrHand:
SearchAndReplace = False
Exit Function
End Function
'############################################################################
##################################
'##
'## Functions Based On Code From Other Sites
'##
'############################################################################
##################################
'//
' @Function: GetArrayDimensions
' @Purpose : Gets the given arrays size.
'
' Thanks To: http://vbnet.mvps.org/index.html?code/helpers/getarraydims.htm
'\\
Public Function GetArrayDimensions(ByVal arrPtr As Long) As Integer Dim Address As Long
CopyMemory Address, ByVal arrPtr, ByVal 4 If Address <> 0 Then
CopyMemory GetArrayDimensions, ByVal Address, 2
End IfEnd Function
'//
' @Function: VarPtrArray
' @Purpose : Returns the pointer to an array. (Used when calling GetArrayDimensions.)
'
' Thanks To: http://vbnet.mvps.org/index.html?code/helpers/getarraydims.htm
'\\
Public Function VarPtrArray(arr As Variant) As Long CopyMemory VarPtrArray, ByVal VarPtr(arr) + 8, ByVal 4End Function'//
' @Function: ByteArray
' @Purpose : Creates an array of bytes used inside of ScanForBytes that can be passed as a param.
'
' Thanks To: http://www.cpearson.com/excel/VBAArrays.htm
'\\
Public Function ByteArray(ParamArray vArray() As Variant) As Byte() Dim vValues() As Variant '// Original Values
Dim bArray() As Byte '// Converted Array '// Copy Original Array To vValues
vValues = vArray '// Check If The Array Has Values
If (GetArrayDimensions(VarPtrArray(vValues())) > 0) And (UBound(vValues) >= 0) Then
Dim x As Long '// Redim the bArray To The Size Of Our Variant Array
ReDim bArray(UBound(vValues) - LBound(vValues)) As Byte
For x = LBound(vValues) To UBound(vValues)
bArray(x) = CByte(vValues(x)) '// Convert Value To Byte And Store It In New Array
Next x '// Return The New Byte Array
ByteArray = bArray
Else
'// Byte Array Was Empty
Exit Function
End IfEnd Function

To use it, heres an example of patching Notepad:

Private Sub Command1_Click()
On Error Resume Next Dim strFilePath As String
strFilePath = String$(255, Chr$(0))
strFilePath = App.Path & "\notepad.exe" Dim bFileBytes() As Byte
bFileBytes = OpenFile(strFilePath) Dim bPatternFind() As Byte
Dim bPatternReplace() As Byte
bPatternFind = ByteArray(&H54, &H68, &H69, &H73, &H20, &H70, &H72, &H6F, &H67, &H72, &H61, &H6D, &H20, &H63, &H61, &H6E, &H6E, &H6F, &H74, &H20, &H62, &H65, &H20, &H72, &H75, &H6E, &H20, &H69, &H6E, &H20, &H44, &H4F, &H53, &H20, &H6D, &H6F, &H64, &H65)
bPatternReplace = ByteArray(&H73, &H69, &H68, &H54, &H20, &H6D, &H61, &H72, &H67, &H6F, &H72, &H70, &H20, &H74, &H6F, &H6E, &H6E, &H61, &H63, &H20, &H65, &H62, &H20, &H6E, &H75, &H72, &H20, &H6E, &H69, &H20, &H53, &H4F, &H44, &H20, &H65, &H64, &H6F, &H6D) Text1.Text = SearchAndReplace(bFileBytes, bPatternFind, bPatternReplace) SaveFile strFilePath, bFileBytesEnd Sub

What this does is patches the 'This program cannot be run in DOS mode' and replaces it with the reverse of each word making it say 'sihT margorp tonnac eb nur ni SOD edom'. Screenies:

Before:

beforeaw6.png

After:

aftervq6.png

Should be pretty straight forward and easy to understand. Hope it helps :)

Edited by Atomos
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...