I'm trying to execute shellcode within a Go program, similar to how you can do it with other languages.
Example 1 - Shellcode in C program
Example 2 - http://www.debasish.in/2012/04/execute-shellcode-using-python.html
All methods have broadly similar techniques - assign the shellcode to executable memory via the OS specific allocation (mmap, virtualalloc, etc) and then execute the code by creating a function pointer pointing to that location before executing.
Here is my horrible hacky example at performing the same thing in Go. The shellcode has manipulations performed on it before being passed to the function, so it's format as a []byte is fixed. Saying that mmap expects a file descriptor passed in which is why the horrible "writing to a tmp file" section exists.
func osxExec(shellcode []byte) {
f, err := os.Create("data/shellcode.tmp")
if err != nil {
fmt.Println(err)
}
defer f.Close()
_,_ = f.Write(shellcode)
f.Sync()
b, err := syscall.Mmap(int(f.Fd()), 0, len(shellcode), syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC, syscall.MAP_SHARED)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%p", b)
}
At the end of the code I've got a pointer (slice?) to the code in what I presume is executable memory - but I've got no idea how to cast this address into a function pointer for execution. I asked on some IRC channels but it was suggested that it may not be possible.
Any help is greatly appreciated.
Cheers.
First, you don't need to (currently) use
mmap
at all, since go memory is executable. If you do needmmap
, you can use anonymous memory and forgo the temp file:Otherwise, you can try to use
shellcode
directly, since it's already backed by a contiguous array.As for converting the bytes in
shellcode
to a function, the analog from C would look like:which creates a function value named
f
which can then be called like a normal function.If the shellcode isn't specifically crafted fro Go, and you need to call it from a C stack, it would be easier to do it directly in C using cgo.
There are multiple ways to do it, though I have not tested them myself.
You may want to have a look at https://github.com/nelhage/gojit/blob/master/jit.go It implements a solution based on cgo (which is safer, but slower), and a solution based on a direct call. Look at all the build related functions.