/
server.go
145 lines (112 loc) · 3.04 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
// Package statichttp serves simple HTTP server
package statichttp
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/evilsocket/islazy/tui"
"github.com/muraenateam/muraena/session"
)
const (
// Name of this module
Name = "static.http"
// Description of this module
Description = "Expose 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
Enabled bool
mux *http.ServeMux
address string
listeningPort int
Protocol string
ListeningHost string
LocalPath string
URLPath string
}
// 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(what string) {
}
// 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),
Enabled: s.Config.StaticServer.Enabled,
}
if !m.Enabled {
m.Debug("is disabled")
return
}
config := s.Config.StaticServer
m.Protocol = "http://"
m.ListeningHost = "localhost"
m.listeningPort = config.Port
m.LocalPath = config.LocalPath
m.URLPath = config.URLPath
// Enable static server module
if err = m.Start(); err != nil {
m.Debug("Dying")
return
}
m.Info("I'm alive and kicking at %s", tui.Bold(tui.Green(m.address)))
return
}
func (module *StaticHTTP) configure() error {
module.address = fmt.Sprintf("127.0.0.1:%d", module.listeningPort)
module.mux = http.NewServeMux()
path := http.Dir(module.LocalPath)
module.Debug("[Static Server] Requested resource: %s", path)
fileServer := http.FileServer(FileSystem{path})
module.mux.Handle(module.URLPath, http.StripPrefix(strings.TrimRight(module.URLPath, "/"), fileServer))
return nil
}
// Start runs the Static HTTP server module
func (module *StaticHTTP) Start() (err error) {
if err := module.configure(); err != nil {
return err
}
go http.ListenAndServe(module.address, module.mux)
return nil
}
// 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
}
func (module *StaticHTTP) MakeDestinationURL(URL *url.URL) (destination string) {
destination = ""
if strings.HasPrefix(URL.Path, module.URLPath) {
destination = fmt.Sprintf("%s%s:%d", module.Protocol, module.ListeningHost, module.listeningPort)
}
return
}