Read/write memory trong Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#cgo CFLAGS: -Wincompatible-pointer-types | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <windows.h> | |
void *open_process(int pid) { | |
return (void*)OpenProcess(PROCESS_ALL_ACCESS, 0, pid); | |
} | |
void close_process(void *proc) { | |
CloseHandle((HANDLE)proc); | |
} | |
int read_memory(void *proc, size_t addr, void *buf, size_t size) { | |
return ReadProcessMemory((HANDLE)proc, (void*)addr, buf, size, NULL); | |
} | |
int write_memory(void *proc, size_t addr, void *buf, size_t size) { | |
return WriteProcessMemory((HANDLE)proc, (void*)addr, buf, size, NULL); | |
} | |
int cast_int(void *ptr) { | |
return *(int*)ptr; | |
} | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"unsafe" | |
) | |
type ptr interface{} | |
func main() { | |
proc := C.open_process(0x01520) // 0x01520- chrome.exe | |
if proc == nil { | |
fmt.Print("cannot open process!\n") // check lỗi | |
} else { | |
addr := C.ulonglong(0x000E29D0) | |
buf := new(int) | |
//val := 110 | |
//C.write_memory(proc, addr, unsafe.Pointer(&val), C.sizeof_int) | |
// | cast về void * | sizeof(int) | |
ret := C.read_memory(proc, addr, unsafe.Pointer(buf), C.sizeof_int) | |
// check lỗi | |
if ret != 0 { | |
fmt.Printf("value: %d\n", *buf) // đọc giá trị int | |
} else { | |
fmt.Printf("cannot read on address: %08x\n", addr) | |
} | |
C.close_process(proc) // đóng process | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment