Skip to content

Instantly share code, notes, and snippets.

@brunocalza
Last active January 25, 2021 02:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brunocalza/b9ff4ad46c27926e5e4f078133d0de79 to your computer and use it in GitHub Desktop.
Save brunocalza/b9ff4ad46c27926e5e4f078133d0de79 to your computer and use it in GitHub Desktop.
This a full example of how PROT_EXEC works in mmap. More about it at https://brunocalza.me/2021/01/10/discovering-and-exploring-mmap-using-go/
package main
import (
"fmt"
"unsafe"
"github.com/edsrzf/mmap-go"
)
func main() {
code := []byte{
0x48, 0xc7, 0x44, 0x24, 0x10, 0x00, 0x00, 0x00, 0x00,
0x48, 0x8b, 0x44, 0x24, 0x08,
0x48, 0xff, 0xc0,
0x48, 0x89, 0x44, 0x24, 0x10,
0xc3,
}
memory, err := mmap.MapRegion(nil, len(code), mmap.EXEC|mmap.RDWR, mmap.ANON, 0)
if err != nil {
panic(err)
}
copy(memory, code)
memory_ptr := &memory
ptr := unsafe.Pointer(&memory_ptr)
inc := *(*func(int) int)(ptr)
fmt.Println(inc(10)) // Prints 11
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment