File: api_rest.go

package info (click to toggle)
bettercap 2.33.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,668 kB
  • sloc: sh: 154; makefile: 76; python: 52; ansic: 9
file content (311 lines) | stat: -rw-r--r-- 8,730 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package api_rest

import (
	"context"
	"fmt"
	"net/http"
	"sync"
	"time"

	"github.com/bettercap/bettercap/session"
	"github.com/bettercap/bettercap/tls"

	"github.com/bettercap/recording"

	"github.com/gorilla/mux"
	"github.com/gorilla/websocket"

	"github.com/evilsocket/islazy/fs"
)

type RestAPI struct {
	session.SessionModule
	server       *http.Server
	username     string
	password     string
	certFile     string
	keyFile      string
	allowOrigin  string
	useWebsocket bool
	upgrader     websocket.Upgrader
	quit         chan bool

	recClock       int
	recording      bool
	recTime        int
	loading        bool
	replaying      bool
	recordFileName string
	recordWait     *sync.WaitGroup
	record         *recording.Archive
	recStarted     time.Time
	recStopped     time.Time
}

func NewRestAPI(s *session.Session) *RestAPI {
	mod := &RestAPI{
		SessionModule: session.NewSessionModule("api.rest", s),
		server:        &http.Server{},
		quit:          make(chan bool),
		useWebsocket:  false,
		allowOrigin:   "*",
		upgrader: websocket.Upgrader{
			ReadBufferSize:  1024,
			WriteBufferSize: 1024,
		},
		recClock:       1,
		recording:      false,
		recTime:        0,
		loading:        false,
		replaying:      false,
		recordFileName: "",
		recordWait:     &sync.WaitGroup{},
		record:         nil,
	}

	mod.State.Store("recording", &mod.recording)
	mod.State.Store("rec_clock", &mod.recClock)
	mod.State.Store("replaying", &mod.replaying)
	mod.State.Store("loading", &mod.loading)
	mod.State.Store("load_progress", 0)
	mod.State.Store("rec_time", &mod.recTime)
	mod.State.Store("rec_filename", &mod.recordFileName)
	mod.State.Store("rec_frames", 0)
	mod.State.Store("rec_cur_frame", 0)
	mod.State.Store("rec_started", &mod.recStarted)
	mod.State.Store("rec_stopped", &mod.recStopped)

	mod.AddParam(session.NewStringParameter("api.rest.address",
		"127.0.0.1",
		session.IPv4Validator,
		"Address to bind the API REST server to."))

	mod.AddParam(session.NewIntParameter("api.rest.port",
		"8081",
		"Port to bind the API REST server to."))

	mod.AddParam(session.NewStringParameter("api.rest.alloworigin",
		mod.allowOrigin,
		"",
		"Value of the Access-Control-Allow-Origin header of the API server."))

	mod.AddParam(session.NewStringParameter("api.rest.username",
		"",
		"",
		"API authentication username."))

	mod.AddParam(session.NewStringParameter("api.rest.password",
		"",
		"",
		"API authentication password."))

	mod.AddParam(session.NewStringParameter("api.rest.certificate",
		"",
		"",
		"API TLS certificate."))

	tls.CertConfigToModule("api.rest", &mod.SessionModule, tls.DefaultLegitConfig)

	mod.AddParam(session.NewStringParameter("api.rest.key",
		"",
		"",
		"API TLS key"))

	mod.AddParam(session.NewBoolParameter("api.rest.websocket",
		"false",
		"If true the /api/events route will be available as a websocket endpoint instead of HTTPS."))

	mod.AddHandler(session.NewModuleHandler("api.rest on", "",
		"Start REST API server.",
		func(args []string) error {
			return mod.Start()
		}))

	mod.AddHandler(session.NewModuleHandler("api.rest off", "",
		"Stop REST API server.",
		func(args []string) error {
			return mod.Stop()
		}))

	mod.AddParam(session.NewIntParameter("api.rest.record.clock",
		"1",
		"Number of seconds to wait while recording with api.rest.record between one sample and the next one."))

	mod.AddHandler(session.NewModuleHandler("api.rest.record off", "",
		"Stop recording the session.",
		func(args []string) error {
			return mod.stopRecording()
		}))

	mod.AddHandler(session.NewModuleHandler("api.rest.record FILENAME", `api\.rest\.record (.+)`,
		"Start polling the rest API periodically recording each sample in a compressed file that can be later replayed.",
		func(args []string) error {
			return mod.startRecording(args[0])
		}))

	mod.AddHandler(session.NewModuleHandler("api.rest.replay off", "",
		"Stop replaying the recorded session.",
		func(args []string) error {
			return mod.stopReplay()
		}))

	mod.AddHandler(session.NewModuleHandler("api.rest.replay FILENAME", `api\.rest\.replay (.+)`,
		"Start the rest API module in replay mode using FILENAME as the recorded session file, will revert to normal mode once the replay is over.",
		func(args []string) error {
			return mod.startReplay(args[0])
		}))

	return mod
}

type JSSessionRequest struct {
	Command string `json:"cmd"`
}

type JSSessionResponse struct {
	Error string `json:"error"`
}

func (mod *RestAPI) Name() string {
	return "api.rest"
}

func (mod *RestAPI) Description() string {
	return "Expose a RESTful API."
}

func (mod *RestAPI) Author() string {
	return "Simone Margaritelli <evilsocket@gmail.com>"
}

func (mod *RestAPI) isTLS() bool {
	return mod.certFile != "" && mod.keyFile != ""
}

func (mod *RestAPI) Configure() error {
	var err error
	var ip string
	var port int

	if mod.Running() {
		return session.ErrAlreadyStarted(mod.Name())
	} else if err, ip = mod.StringParam("api.rest.address"); err != nil {
		return err
	} else if err, port = mod.IntParam("api.rest.port"); err != nil {
		return err
	} else if err, mod.allowOrigin = mod.StringParam("api.rest.alloworigin"); err != nil {
		return err
	} else if err, mod.certFile = mod.StringParam("api.rest.certificate"); err != nil {
		return err
	} else if mod.certFile, err = fs.Expand(mod.certFile); err != nil {
		return err
	} else if err, mod.keyFile = mod.StringParam("api.rest.key"); err != nil {
		return err
	} else if mod.keyFile, err = fs.Expand(mod.keyFile); err != nil {
		return err
	} else if err, mod.username = mod.StringParam("api.rest.username"); err != nil {
		return err
	} else if err, mod.password = mod.StringParam("api.rest.password"); err != nil {
		return err
	} else if err, mod.useWebsocket = mod.BoolParam("api.rest.websocket"); err != nil {
		return err
	}

	if mod.isTLS() {
		if !fs.Exists(mod.certFile) || !fs.Exists(mod.keyFile) {
			cfg, err := tls.CertConfigFromModule("api.rest", mod.SessionModule)
			if err != nil {
				return err
			}

			mod.Debug("%+v", cfg)
			mod.Info("generating TLS key to %s", mod.keyFile)
			mod.Info("generating TLS certificate to %s", mod.certFile)
			if err := tls.Generate(cfg, mod.certFile, mod.keyFile, false); err != nil {
				return err
			}
		} else {
			mod.Info("loading TLS key from %s", mod.keyFile)
			mod.Info("loading TLS certificate from %s", mod.certFile)
		}
	}

	mod.server.Addr = fmt.Sprintf("%s:%d", ip, port)

	router := mux.NewRouter()

	router.Methods("OPTIONS").HandlerFunc(mod.corsRoute)

	router.HandleFunc("/api/file", mod.fileRoute)

	router.HandleFunc("/api/events", mod.eventsRoute)

	router.HandleFunc("/api/session", mod.sessionRoute)
	router.HandleFunc("/api/session/ble", mod.sessionRoute)
	router.HandleFunc("/api/session/ble/{mac}", mod.sessionRoute)
	router.HandleFunc("/api/session/hid", mod.sessionRoute)
	router.HandleFunc("/api/session/hid/{mac}", mod.sessionRoute)
	router.HandleFunc("/api/session/env", mod.sessionRoute)
	router.HandleFunc("/api/session/gateway", mod.sessionRoute)
	router.HandleFunc("/api/session/interface", mod.sessionRoute)
	router.HandleFunc("/api/session/modules", mod.sessionRoute)
	router.HandleFunc("/api/session/lan", mod.sessionRoute)
	router.HandleFunc("/api/session/lan/{mac}", mod.sessionRoute)
	router.HandleFunc("/api/session/options", mod.sessionRoute)
	router.HandleFunc("/api/session/packets", mod.sessionRoute)
	router.HandleFunc("/api/session/started-at", mod.sessionRoute)
	router.HandleFunc("/api/session/wifi", mod.sessionRoute)
	router.HandleFunc("/api/session/wifi/{mac}", mod.sessionRoute)

	mod.server.Handler = router

	if mod.username == "" || mod.password == "" {
		mod.Warning("api.rest.username and/or api.rest.password parameters are empty, authentication is disabled.")
	}

	return nil
}

func (mod *RestAPI) Start() error {
	if mod.replaying {
		return fmt.Errorf("the api is currently in replay mode, run api.rest.replay off before starting it")
	} else if err := mod.Configure(); err != nil {
		return err
	}

	mod.SetRunning(true, func() {
		var err error

		if mod.isTLS() {
			mod.Info("api server starting on https://%s", mod.server.Addr)
			err = mod.server.ListenAndServeTLS(mod.certFile, mod.keyFile)
		} else {
			mod.Info("api server starting on http://%s", mod.server.Addr)
			err = mod.server.ListenAndServe()
		}

		if err != nil && err != http.ErrServerClosed {
			panic(err)
		}
	})

	return nil
}

func (mod *RestAPI) Stop() error {
	if mod.recording {
		mod.stopRecording()
	} else if mod.replaying {
		mod.stopReplay()
	}

	return mod.SetRunning(false, func() {
		go func() {
			mod.quit <- true
		}()

		ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
		defer cancel()
		mod.server.Shutdown(ctx)
	})
}