Code Injection

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Code Injection

Post by jay lee » 02 Dec 2023, 21:49

In Cheatengine I can use a code injection template to change a function in a running process, how can I do that in ahk?
image.png
image.png (124.9 KiB) Viewed 438 times

[Mod edit: Removed img tags from around inline attached image tags. The img tags are only for URLs to an online image.]

User avatar
lmstearn
Posts: 702
Joined: 11 Aug 2016, 02:32
Contact:

Re: Code Injection

Post by lmstearn » 02 Dec 2023, 23:51

Replacing a function might be possible - it involves detour and trampoline functions - @malcev includes that in this example at
MinHook - API Hooking Library.
Related, see WriteProcessMemory Not working, AhkHook: API hooking with AutoHotkey and Memory Process reading/Writing & Pattern Scans (Array of bytes).
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

Re: Code Injection

Post by jay lee » 03 Dec 2023, 09:04

I found out how to do it!

Code: Select all

replace_instruction(hProcess, address, new_opcode) {
    new_opcode := StrSplit(new_opcode, " ")
    for index, hex_value in new_opcode
        new_opcode[index] := "0x" . hex_value
    
    VarSetCapacity(opcode, new_opcode.MaxIndex(), 0)
    Loop % new_opcode.MaxIndex()
        NumPut(new_opcode[A_Index], opcode, A_Index - 1, "UChar")
    
    if !DllCall("WriteProcessMemory", "Ptr", hProcess, "Ptr", address, "Ptr", &opcode, "UInt", new_opcode.MaxIndex(), "Ptr", 0) {
        MsgBox, Failed to replace instruction.
        return
    }
    return
}
but I dont know how to convert ASM to hex as I mentioned in my newest post

User avatar
lmstearn
Posts: 702
Joined: 11 Aug 2016, 02:32
Contact:

Re: Code Injection

Post by lmstearn » 03 Dec 2023, 12:23

The ASM code has to be assembled to machine code, would the Auto Assembler help there? Check types for 64 bit if compiling for that. It all then has to go into a custom DLL, see here, and here for the mcode bit.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

Post Reply

Return to “Ask for Help (v1)”