1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| package main
import ( "fmt" "io/ioutil" "log" "net/http" "net/url" "strings" )
var ( secret = "xiaomo.wabzsy" banner = "Golang HTTP Server (v2.3.3)" template = `<!DOCTYPE html> <html> <head> <title>San Check</title> </head> <body> <form method="POST"> <input type="text" name="url" /> <input type="submit" value="淦!" /> </form> <pre> %s </pre> </body> <!-- ./sancheck.php --> </html>` )
func SanCheck(input string) error { u, err := url.Parse(input)
if err != nil { return err }
if u.Scheme != "http" { return fmt.Errorf("err: Invalid Scheme [%s]", u.Scheme) }
if u.Opaque != "" { return fmt.Errorf("err: WHAT AER YOU DOING ?!!! (%s)", u.Opaque) }
if u.Hostname() != "127.0.0.1" { return fmt.Errorf("err: Invalid Hostname [%s]", u.Hostname()) }
if u.Port() != "" && u.Port() != "80" { return fmt.Errorf("err: Invalid Port [%s]", u.Port()) }
if u.User == nil { return fmt.Errorf("err: Authorization Required") }
if u.User.Username() != "root" { return fmt.Errorf("err: Invalid Username [%s]", u.User.Username()) }
if password, set := u.User.Password(); !set || password != "P@ssw0rd!" { return fmt.Errorf("err: Invalid Password [%s]", password) }
if u.RequestURI() != "/flag.php" { return fmt.Errorf("err: Invalid RequestURI [%s]", u.RequestURI()) }
if u.Fragment != "" { return fmt.Errorf("err: Invalid Fragment [%s]", u.Fragment) }
if !strings.Contains(u.String(), "'Pwned!'") { return fmt.Errorf("err: San Check failed") }
return nil }
func DefaultHandler(w http.ResponseWriter, req *http.Request) {
input := req.PostFormValue("url")
if input == "" { Response(w, template, "Where is the flag?") return }
if err := SanCheck(input); err == nil { req, _ := http.NewRequest("GET", input, nil) req.Header.Add("Secret", secret) if resp, err := (&http.Client{}).Do(req); err == nil { if body, err := ioutil.ReadAll(resp.Body); err == nil { Response(w, template, string(body)) } _ = resp.Body.Close() } else { Response(w, template, err.Error()) } } else { Response(w, template, err.Error()) } }
func FlagHandler(w http.ResponseWriter, req *http.Request) { if strings.Join(req.Header["Secret"], "") != secret { Forbidden(w) } else if !strings.HasPrefix(req.RemoteAddr, "127.0.0.1") { Forbidden(w) } else { Response(w, "<h1>%s</h1>", readFlag()) } }
func readFlag() string { if bs, err := ioutil.ReadFile("./flag.txt"); err == nil { return string(bs) } else { return fmt.Sprintf("Unable to read flag.txt, please contact technical support.(%s)", err.Error()) } }
func CodeHandler(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.Header().Set("Server", banner) if bs, err := ioutil.ReadFile("main.go"); err == nil { _, _ = w.Write(bs) } else { _, _ = w.Write( []byte( fmt.Sprintf("Unable to read main.go, please contact technical support.(%s)", err.Error(), ), ), ) } }
func Forbidden(w http.ResponseWriter) { w.WriteHeader(403) Response(w, "<h1>%s</h1>", "403 Forbidden") }
func Response(w http.ResponseWriter, tpl string, response string) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Server", banner) if _, err := fmt.Fprintf(w, tpl, response); err != nil { log.Println(err) } }
func main() { http.HandleFunc("/", DefaultHandler) http.HandleFunc("/flag.php", FlagHandler) http.HandleFunc("/sancheck.php", CodeHandler) log.Fatal(http.ListenAndServe(":80", nil)) }
|