Add override option to netexec

This commit is contained in:
Tim Allclair 2020-07-06 18:26:22 -07:00 committed by Tim Allclair
parent 9156360234
commit 700006f2d9

View File

@ -47,6 +47,7 @@ var (
serverReady = &atomicBool{0} serverReady = &atomicBool{0}
certFile = "" certFile = ""
privKeyFile = "" privKeyFile = ""
httpOverride = ""
) )
// CmdNetexec is used by agnhost Cobra. // CmdNetexec is used by agnhost Cobra.
@ -114,6 +115,7 @@ func init() {
"File containing an x509 private key matching --tls-cert-file") "File containing an x509 private key matching --tls-cert-file")
CmdNetexec.Flags().IntVar(&udpPort, "udp-port", 8081, "UDP Listen Port") CmdNetexec.Flags().IntVar(&udpPort, "udp-port", 8081, "UDP Listen Port")
CmdNetexec.Flags().IntVar(&sctpPort, "sctp-port", -1, "SCTP Listen Port") CmdNetexec.Flags().IntVar(&sctpPort, "sctp-port", -1, "SCTP Listen Port")
CmdNetexec.Flags().StringVar(&httpOverride, "http-override", "", "Override the HTTP handler to always respond as if it were a GET with this path & params")
} }
// atomicBool uses load/store operations on an int32 to simulate an atomic boolean. // atomicBool uses load/store operations on an int32 to simulate an atomic boolean.
@ -137,7 +139,21 @@ func (a *atomicBool) get() bool {
func main(cmd *cobra.Command, args []string) { func main(cmd *cobra.Command, args []string) {
exitCh := make(chan shutdownRequest) exitCh := make(chan shutdownRequest)
addRoutes(exitCh) if httpOverride != "" {
mux := http.NewServeMux()
addRoutes(mux, exitCh)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
overrideReq, err := http.NewRequestWithContext(r.Context(), "GET", httpOverride, nil)
if err != nil {
http.Error(w, fmt.Sprintf("override request failed: %v", err), http.StatusInternalServerError)
return
}
mux.ServeHTTP(w, overrideReq)
})
} else {
addRoutes(http.DefaultServeMux, exitCh)
}
go startUDPServer(udpPort) go startUDPServer(udpPort)
if sctpPort != -1 { if sctpPort != -1 {
@ -152,20 +168,20 @@ func main(cmd *cobra.Command, args []string) {
} }
} }
func addRoutes(exitCh chan shutdownRequest) { func addRoutes(mux *http.ServeMux, exitCh chan shutdownRequest) {
http.HandleFunc("/", rootHandler) mux.HandleFunc("/", rootHandler)
http.HandleFunc("/clientip", clientIPHandler) mux.HandleFunc("/clientip", clientIPHandler)
http.HandleFunc("/dial", dialHandler) mux.HandleFunc("/dial", dialHandler)
http.HandleFunc("/echo", echoHandler) mux.HandleFunc("/echo", echoHandler)
http.HandleFunc("/exit", func(w http.ResponseWriter, req *http.Request) { exitHandler(w, req, exitCh) }) mux.HandleFunc("/exit", func(w http.ResponseWriter, req *http.Request) { exitHandler(w, req, exitCh) })
http.HandleFunc("/healthz", healthzHandler) mux.HandleFunc("/healthz", healthzHandler)
http.HandleFunc("/hostname", hostnameHandler) mux.HandleFunc("/hostname", hostnameHandler)
http.HandleFunc("/redirect", redirectHandler) mux.HandleFunc("/redirect", redirectHandler)
http.HandleFunc("/shell", shellHandler) mux.HandleFunc("/shell", shellHandler)
http.HandleFunc("/upload", uploadHandler) mux.HandleFunc("/upload", uploadHandler)
// older handlers // older handlers
http.HandleFunc("/hostName", hostNameHandler) mux.HandleFunc("/hostName", hostNameHandler)
http.HandleFunc("/shutdown", shutdownHandler) mux.HandleFunc("/shutdown", shutdownHandler)
} }
func startServer(server *http.Server, exitCh chan shutdownRequest, fn func() error) { func startServer(server *http.Server, exitCh chan shutdownRequest, fn func() error) {