Skip to content

Commit

Permalink
Switch to []byte for patch()
Browse files Browse the repository at this point in the history
Generalize it for any data, not just strings
  • Loading branch information
optix2000 committed Aug 6, 2021
1 parent e123d28 commit a1b76cc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func watchGGST(noClose bool) {
time.Sleep(10 * time.Second)
continue
}
offset, err := patcher.PatchProc(pid, GGStriveExe, APIOffsetAddr, GGStriveAPIURL, PatchedAPIURL)
offset, err := patcher.PatchProc(pid, GGStriveExe, APIOffsetAddr, []byte(GGStriveAPIURL), []byte(PatchedAPIURL))
if err != nil {
if errors.Is(err, patcher.ErrProcessAlreadyPatched) {
fmt.Printf("GGST with PID %d is already patched at offset 0x%x.\n", pid, offset)
Expand Down
10 changes: 5 additions & 5 deletions patcher/patcher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package patcher

import (
"bytes"
"errors"
"fmt"
"path/filepath"
Expand Down Expand Up @@ -65,7 +66,7 @@ func GetProc(proc string) (uint32, error) {
return 0, ErrProcessNotFound
}

func PatchProc(pid uint32, moduleName string, offsetAddr uintptr, old string, new string) (uintptr, error) {
func PatchProc(pid uint32, moduleName string, offsetAddr uintptr, old []byte, new []byte) (uintptr, error) {
proc, err := windows.OpenProcess(windows.PROCESS_VM_READ|windows.PROCESS_VM_WRITE|windows.PROCESS_VM_OPERATION|windows.PROCESS_QUERY_INFORMATION, false, pid)
if err != nil {
return 0, fmt.Errorf("error in OpenProcess: %w", err)
Expand Down Expand Up @@ -96,8 +97,7 @@ func PatchProc(pid uint32, moduleName string, offsetAddr uintptr, old string, ne
}
}
if module == 0 {
// TODO: Better error handling
return 0, errors.New("couldn't find base module for GGST")
return 0, fmt.Errorf("couldn't find base module for %v", moduleName)
}

// Get Entrypoint so we have an idea where GGST's memory starts
Expand Down Expand Up @@ -125,8 +125,8 @@ func PatchProc(pid uint32, moduleName string, offsetAddr uintptr, old string, ne
return offset, fmt.Errorf("error in ReadProcessMemory: %w", err)
}

if string(buf[:bytesRead]) != old {
if string(buf[:min(bytesRead, uint32(len(new)))]) == new {
if !bytes.Equal(buf[:bytesRead], old) {
if bytes.Equal(buf[:min(bytesRead, uint32(len(new)))], new) {
return offset, ErrProcessAlreadyPatched
}
return offset, fmt.Errorf("%q does not match signature at offset 0x%x", buf[:bytesRead], offset)
Expand Down

0 comments on commit a1b76cc

Please sign in to comment.