Skip to content

Commit c56d8df

Browse files
committed
Adds HTTPS logic to agnhost netexec
The agnhost image used for testing has a `netexec` path which supports two new flags, `--tls-cert-file` and `--tls-private-key-file`. If the former is provided, the HTTP server will be upgraded to HTTPS, using the certificate (and private key) provided. By default, there are keys already mounted into the container at `/localhost.crt` and `/localhost.key`, which contain PEM-encoded TLS certs with IP SANs for `127.0.0.1` and `[::1]`.
1 parent cf1b72e commit c56d8df

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

test/images/agnhost/netexec/netexec.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ var (
4444
sctpPort = -1
4545
shellPath = "/bin/sh"
4646
serverReady = &atomicBool{0}
47+
certFile = ""
48+
privKeyFile = ""
4749
)
4850

4951
// CmdNetexec is used by agnhost Cobra.
5052
var CmdNetexec = &cobra.Command{
5153
Use: "netexec",
52-
Short: "Creates HTTP, UDP, and (optionally) SCTP servers with various endpoints",
53-
Long: `Starts a HTTP server on given port with the following endpoints:
54+
Short: "Creates HTTP(S), UDP, and (optionally) SCTP servers with various endpoints",
55+
Long: `Starts a HTTP(S) server on given port with the following endpoints:
5456
5557
- /: Returns the request's timestamp.
5658
- /clientip: Returns the request's IP address.
@@ -97,6 +99,10 @@ responding to the same commands as the UDP server.
9799

98100
func init() {
99101
CmdNetexec.Flags().IntVar(&httpPort, "http-port", 8080, "HTTP Listen Port")
102+
CmdNetexec.Flags().StringVar(&certFile, "tls-cert-file", "",
103+
"File containing an x509 certificate for HTTPS. (CA cert, if any, concatenated after server cert)")
104+
CmdNetexec.Flags().StringVar(&privKeyFile, "tls-private-key-file", "",
105+
"File containing an x509 private key matching --tls-cert-file")
100106
CmdNetexec.Flags().IntVar(&udpPort, "udp-port", 8081, "UDP Listen Port")
101107
CmdNetexec.Flags().IntVar(&sctpPort, "sctp-port", -1, "SCTP Listen Port")
102108
}
@@ -125,10 +131,17 @@ func main(cmd *cobra.Command, args []string) {
125131
if sctpPort != -1 {
126132
go startSCTPServer(sctpPort)
127133
}
128-
startHTTPServer(httpPort)
134+
135+
addRoutes()
136+
if len(certFile) > 0 {
137+
// only start HTTPS server if a cert is provided
138+
startHTTPSServer(httpPort, certFile, privKeyFile)
139+
} else {
140+
startHTTPServer(httpPort)
141+
}
129142
}
130143

131-
func startHTTPServer(httpPort int) {
144+
func addRoutes() {
132145
http.HandleFunc("/", rootHandler)
133146
http.HandleFunc("/clientip", clientIPHandler)
134147
http.HandleFunc("/echo", echoHandler)
@@ -141,6 +154,13 @@ func startHTTPServer(httpPort int) {
141154
// older handlers
142155
http.HandleFunc("/hostName", hostNameHandler)
143156
http.HandleFunc("/shutdown", shutdownHandler)
157+
}
158+
159+
func startHTTPSServer(httpsPort int, certFile, privKeyFile string) {
160+
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%d", httpPort), certFile, privKeyFile, nil))
161+
}
162+
163+
func startHTTPServer(httpPort int) {
144164
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil))
145165
}
146166

0 commit comments

Comments
 (0)