Skip to content

Instantly share code, notes, and snippets.

@tehmoon
Created May 30, 2018 01:41
Show Gist options
  • Save tehmoon/2402af30a3a05e9fbe5e339e703f8dcb to your computer and use it in GitHub Desktop.
Save tehmoon/2402af30a3a05e9fbe5e339e703f8dcb to your computer and use it in GitHub Desktop.
PoC payload dump exec in golang
package main
/*
The goal of this PoC is to try to exec a payload built within the binary.
I kind of use the same idea as for protobuf, compile the target into executable
then copy the payload inside the program, do the reverse operation and exec it.
It works like this:
- compile your binary to the target arch
- use cryptocli dd -in <binary> -encoders gzip,byte-string -out <temp_file>
- <temp_file> will be a long string of bytes, copy it to the variable payload
- compile the go project
The idea that I had was to actually delete the file as soon as it was executed.
It doesn't work on windows because it shoes the file as busy, so careful cleanup is needed.
Otherwise the PoC works, I would problably won't have enough time but the idea
is to create a package that templatize to a go file directly like protobuf does.
*/
import (
"compress/gzip"
"io"
"io/ioutil"
"strings"
"os"
"os/exec"
)
const payload = ""
func main() {
f, err := ioutil.TempFile("", "")
if err != nil {
panic(err)
}
name := f.Name()
str := strings.NewReader(payload)
reader, err := gzip.NewReader(str)
if err != nil {
os.Remove(name)
panic(err)
}
_, err = io.Copy(f, reader)
if err != nil {
os.Remove(name)
panic(err)
}
// On *nix system make it executable
// err = f.Chmod(0700)
// if err != nil {
// os.Remove(name)
// panic(err)
// }
//
// f.Close()
// On windows system, make it an exe file
// err = os.Rename(name, name + ".exe")
// if err != nil {
// os.Remove(name)
// panic(err)
// }
//
// name = name + ".exe"
cmd := exec.Command(name)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
os.Remove(name)
panic(err)
}
err = os.Remove(name)
if err != nil {
panic(err)
}
err = cmd.Wait()
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment