14func StartFileServer(c agent.Config) chan net.Conn {
15 var (
16 fsListener = NewLys()
17 handler = middleware.LoggingMiddleware(http.FileServer(http.Dir("/")))18 fileServer = &http.Server{Handler: handler}
19 )
20 go fileServer.Serve(fsListener)
A path traversal attack (also known as directory traversal attack) aims to access files and directories that are stored outside the web root folder.
Manipulating variables that reference files with "dot-dot (..)" sequences and its variations or using absolute file paths may be possible to access arbitrary files and directories stored on the file system, including application source code or configuration and critical system files.
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
http.Handle(
"/bad/",
http.StripPrefix("/bad/", http.FileServer(http.Dir("/"))), // here, http.Dir("/") allows the attacker to read sensitive files
)
http.HandleFunc("/", HelloServer)
log.Fatal(http.ListenAndServe(":3000", nil))
}
func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
http.Handle(
"/bad/",
http.StripPrefix("/bad/", http.FileServer(http.Dir("/app/static"))),
)
http.HandleFunc("/", HelloServer)
log.Fatal(http.ListenAndServe(":3000", nil))
}
func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}