@@ -44,13 +44,15 @@ var (
44
44
sctpPort = - 1
45
45
shellPath = "/bin/sh"
46
46
serverReady = & atomicBool {0 }
47
+ certFile = ""
48
+ privKeyFile = ""
47
49
)
48
50
49
51
// CmdNetexec is used by agnhost Cobra.
50
52
var CmdNetexec = & cobra.Command {
51
53
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:
54
56
55
57
- /: Returns the request's timestamp.
56
58
- /clientip: Returns the request's IP address.
@@ -97,6 +99,10 @@ responding to the same commands as the UDP server.
97
99
98
100
func init () {
99
101
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" )
100
106
CmdNetexec .Flags ().IntVar (& udpPort , "udp-port" , 8081 , "UDP Listen Port" )
101
107
CmdNetexec .Flags ().IntVar (& sctpPort , "sctp-port" , - 1 , "SCTP Listen Port" )
102
108
}
@@ -125,10 +131,17 @@ func main(cmd *cobra.Command, args []string) {
125
131
if sctpPort != - 1 {
126
132
go startSCTPServer (sctpPort )
127
133
}
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
+ }
129
142
}
130
143
131
- func startHTTPServer ( httpPort int ) {
144
+ func addRoutes ( ) {
132
145
http .HandleFunc ("/" , rootHandler )
133
146
http .HandleFunc ("/clientip" , clientIPHandler )
134
147
http .HandleFunc ("/echo" , echoHandler )
@@ -141,6 +154,13 @@ func startHTTPServer(httpPort int) {
141
154
// older handlers
142
155
http .HandleFunc ("/hostName" , hostNameHandler )
143
156
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 ) {
144
164
log .Fatal (http .ListenAndServe (fmt .Sprintf (":%d" , httpPort ), nil ))
145
165
}
146
166
0 commit comments