package main import ( "database/sql" "fmt" "io/ioutil" "log" "net/http" "os" "strings" _ "github.com/lib/pq" ) type User struct { ID int Username string Flag string } func connectDB() (*sql.DB, error) { connStr := os.Getenv("DATABASE_URL") if connStr == "" { return nil, fmt.Errorf("DATABASE_URL environment variable is not set") } return sql.Open("postgres", connStr) } func main() { // 1. Endpoint to View Source Code (Crucial for the CTF) http.HandleFunc("/source", func(w http.ResponseWriter, r *http.Request) { content, err := ioutil.ReadFile("main.go") if err != nil { http.Error(w, "Could not read source code.", http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/plain") w.Write(content) }) // 2. Login Page http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { html := ` Secure Login System v2.0

SECURE GATEWAY

Protected by GoWAF™ technology

Developers only:

View Source Code
` w.Header().Set("Content-Type", "text/html") w.Write([]byte(html)) }) // 3. The Hardened (but still vulnerable) Login Endpoint http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } username := r.FormValue("username") password := r.FormValue("password") ua := r.Header.Get("User-Agent") if ua != "Secure-CTF-Browser/1.0" { http.Error(w, "Security Alert: Browser not authorized. Please use 'Secure-CTF-Browser/1.0'", http.StatusForbidden) return } if username == "admin" { http.Error(w, "Direct login as 'admin' is disabled for security reasons.", http.StatusForbidden) return } if strings.Contains(username, " ") || strings.Contains(password, " ") { http.Error(w, "WAF Detection: Spaces are not allowed in input fields.", http.StatusBadRequest) return } db, err := connectDB() if err != nil { http.Error(w, "Database connection failed", http.StatusInternalServerError) log.Println("DB Error:", err) return } defer db.Close() query := fmt.Sprintf("SELECT id, username, flag FROM users WHERE username = '%s' AND password = '%s'", username, password) log.Printf("Executing Query: %s\n", query) var user User err = db.QueryRow(query).Scan(&user.ID, &user.Username, &user.Flag) if err != nil { if err == sql.ErrNoRows { w.WriteHeader(http.StatusUnauthorized) w.Write([]byte("Invalid credentials.")) } else { http.Error(w, "Query error: "+err.Error(), http.StatusInternalServerError) } return } w.WriteHeader(http.StatusOK) fmt.Fprintf(w, `

SYSTEM BREACHED

User: %s

FLAG: %s

`, user.Username, user.Flag) }) port := os.Getenv("PORT") if port == "" { port = "7860" } log.Printf("CTF Hard Mode listening on port %s", port) log.Fatal(http.ListenAndServe(":"+port, nil)) }