Skip to content

Instantly share code, notes, and snippets.

@nomi-san

nomi-san/rw_mem.go

Created Jan 15, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.