/
server.go
165 lines (132 loc) · 3.75 KB
/
server.go
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
// Package statichttp serves simple HTTP server
package statichttp
import (
"fmt"
"log"
"net"
"net/http"
"net/url"
"path/filepath"
"strings"
"sync"
"github.com/evilsocket/islazy/tui"
"github.com/muraenateam/muraena/session"
)
const (
// Name of this module
Name = "static.http"
// Description of this module
Description = "Exposes a simple HTTP server to serve static resources during the MiTM session"
// Author of this module
Author = "Muraena Team"
)
// StaticHTTP module
type StaticHTTP struct {
session.SessionModule
config session.StaticHTTPConfig
mux *http.ServeMux `toml:"-"`
address string `toml:"-"`
}
// Name returns the module name
func (module *StaticHTTP) Name() string {
return Name
}
// Description returns the module description
func (module *StaticHTTP) Description() string {
return Description
}
// Author returns the module author
func (module *StaticHTTP) Author() string {
return Author
}
// Prompt prints module status based on the provided parameters
func (module *StaticHTTP) Prompt() {
module.Raw("No options are available for this module")
}
// Load configures the module by initializing its main structure and variables
func Load(s *session.Session) (m *StaticHTTP, err error) {
m = &StaticHTTP{
SessionModule: session.NewSessionModule(Name, s),
config: s.Config.StaticServer,
}
if !m.config.Enabled {
m.Debug("is disabled")
return
}
// Enable static server module
if err = m.start(); err != nil {
return
}
m.Info("I'm alive and kicking at %s", tui.Bold(tui.Green(m.address)))
return
}
// Debugging wrapper around the file server
func (module *StaticHTTP) logFileServer(handler http.Handler, localPath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
filePath := filepath.Join(localPath, r.URL.Path)
module.Info("Requested %s", filePath)
handler.ServeHTTP(w, r)
}
}
func (module *StaticHTTP) configure() error {
config := module.config
// :0 means "assign an available port"
module.address = fmt.Sprintf("%s:%d", config.ListeningHost, config.ListeningPort)
module.mux = http.NewServeMux()
path := http.Dir(config.LocalPath)
module.Debug("[Static Server] Requested resource: %s", path)
fileServer := http.FileServer(FileSystem{path})
debugFS := module.logFileServer(fileServer, config.LocalPath)
module.mux.Handle(config.URLPath, http.StripPrefix(strings.TrimRight(config.URLPath, "/"), debugFS))
return nil
}
// start runs the Static HTTP server module
func (module *StaticHTTP) start() (err error) {
if err = module.configure(); err != nil {
return
}
listener, err := net.Listen("tcp", module.address)
if err != nil {
log.Fatalf("Error creating listener: %v", err)
}
// Retrieve the actual address & port assigned by the system
module.address = listener.Addr().String()
module.Info("listening on %s", module.address)
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done() // Signal the server is ready
if err := http.Serve(listener, module.mux); err != nil {
module.Error("%v", err)
}
}()
// Wait for the server to start
wg.Wait()
return
}
// FileSystem custom file system handler
type FileSystem struct {
fs http.FileSystem
}
// Open opens file
func (fs FileSystem) Open(path string) (http.File, error) {
f, err := fs.fs.Open(path)
if err != nil {
return nil, err
}
s, err := f.Stat()
if s.IsDir() {
index := strings.TrimSuffix(path, "/") + "/index.html"
if _, err := fs.fs.Open(index); err != nil {
return nil, err
}
}
return f, nil
}
// GetNewDestination returns the destination URL for the given request
func (module *StaticHTTP) GetNewDestination(URL *url.URL) (destination string) {
if strings.HasPrefix(URL.Path, module.config.URLPath) {
destination = fmt.Sprintf("http://%s", module.address)
}
return
}