Last active
January 25, 2021 02:53
-
-
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/
This file contains hidden or 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 | |
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