Skip to content

Instantly share code, notes, and snippets.

@JamesMenetrey
Created April 4, 2019 00:16
Show Gist options
  • Save JamesMenetrey/d12c5b33183136423c8d61f6b2e69b0f to your computer and use it in GitHub Desktop.
Save JamesMenetrey/d12c5b33183136423c8d61f6b2e69b0f to your computer and use it in GitHub Desktop.
Go; Direct Memory Editing
package main
import (
"fmt"
"unsafe"
)
func main() {
var i int = 66
var j int = 1234
fmt.Println(i)
fmt.Println(j)
fmt.Println(&i)
fmt.Println(&j)
fmt.Println("Go unsafe now!")
iUintPtr := (uintptr)(unsafe.Pointer(&i)) // or with a static address such as 0x414020
iRawPtr := unsafe.Pointer(iUintPtr)
fmt.Println(*(*int)(iRawPtr))
jRawPtr := unsafe.Pointer(iUintPtr + 4)
fmt.Println(*(*int)(jRawPtr))
fmt.Println("Let's reassign the variable 'j' in an unusual way !")
*(*int)(jRawPtr) = 4321
fmt.Println(j)
fmt.Println(*(*int)(jRawPtr))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment