Skip to content

Instantly share code, notes, and snippets.

@nomi-san
Created January 15, 2019 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomi-san/60baa8bd1b4defc09325ed162b830942 to your computer and use it in GitHub Desktop.
Save nomi-san/60baa8bd1b4defc09325ed162b830942 to your computer and use it in GitHub Desktop.
Read/write memory trong Go
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