-
Notifications
You must be signed in to change notification settings - Fork 3
/
routes.go
183 lines (161 loc) · 5.01 KB
/
routes.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
)
var templates = template.Must(template.ParseGlob("templates/*"))
const MagentoScan = "Magento Scanner"
const MagentoScanLogin = "Magento Scanner - Login Page"
const MagentoScanAdminLogin = "Magento Scanner - Admin Login Page"
const MagentoScanVersion = "Magento Scanner - Version"
type Page struct {
Title string
Host string
Error bool
Username string
Version string
}
// IndexHandler provides static pages depending on the request. If
// magento_version is requested, return the configured magento_version_text and
// flag the IP. Otherwise, return the index page and check whether to record the
// http.Request.
func IndexHandler(app *App) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
recordAttack(app, r, MagentoScan)
host := fmt.Sprintf("http://%s", app.SensorIP)
p := Page{
Title: app.Config.Magento.SiteName,
Host: host,
}
err := templates.ExecuteTemplate(w, "index.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func fileServe(w http.ResponseWriter, r *http.Request) {
path := fmt.Sprintf("public%s", r.URL.Path)
http.ServeFile(w, r, path)
}
func adminLoginHandler(app *App) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
host := fmt.Sprintf("http://%s", app.SensorIP)
p := Page{
Title: app.Config.Magento.SiteName,
Host: host,
}
var err error
if r.Method == "POST" {
username := r.PostFormValue("name")
password := r.PostFormValue("pass")
recordCredentials(app, r, username, password)
p.Username = username
p.Error = true
err = templates.ExecuteTemplate(w, "admin-login-invalid.html", p)
} else {
recordAttack(app, r, MagentoScanAdminLogin)
err = templates.ExecuteTemplate(w, "admin-login.html", p)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func versionHandler(app *App) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
recordAttack(app, r, MagentoScanVersion)
host := fmt.Sprintf("http://%s", app.SensorIP)
p := Page{
Title: app.Config.Magento.SiteName,
Version: app.Config.Magento.MagentoVersionText,
Host: host,
}
err := templates.ExecuteTemplate(w, "magento_version.txt", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
// TODO: Randomize csrf token
func loginHandler(app *App) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
host := fmt.Sprintf("http://%s", app.SensorIP)
p := Page{
Title: app.Config.Magento.SiteName,
Host: host,
}
var err error
if r.Method == "POST" {
username := r.PostFormValue("name")
password := r.PostFormValue("pass")
recordCredentials(app, r, username, password)
p.Username = username
p.Error = true
err = templates.ExecuteTemplate(w, "login-invalid.html", p)
} else {
recordAttack(app, r, MagentoScanLogin)
err = templates.ExecuteTemplate(w, "login.html", p)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
// routes sets up the necessary http routing for the webapp.
func routes(app *App) *http.ServeMux {
mux := http.NewServeMux()
// TODO: Smart regexes for routing.
mux.HandleFunc("/", IndexHandler(app))
mux.HandleFunc("/magento_version", versionHandler(app))
mux.HandleFunc("/pub/", fileServe)
mux.HandleFunc("/admin_access/", adminLoginHandler(app)) // Any un-covered route
mux.HandleFunc("/customer/account/login/", loginHandler(app)) // Anything containing login
return mux
}
// recordRequest will parse the http.Request and put it into a normalized format
// and then marshal to JSON. This can then be sent on an hpfeeds channel or
// logged to a file directly.
func recordAttack(app *App, r *http.Request, signature string) {
// Populate data to send
payload, err := app.Agave.NewHTTPAttack(signature, r)
if err != nil {
fmt.Println("error:", err)
return
}
// Marshal it to json so we can send it over hpfeeds.
buf, err := json.Marshal(payload)
if err != nil {
fmt.Println("error:", err)
return
}
log.Printf("%s", buf)
// Send to hpfeeds broker
if app.Config.Hpfeeds.Enabled {
app.Publish <- buf
}
}
// recordRequest will parse the http.Request and put it into a normalized format
// and then marshal to JSON. This can then be sent on an hpfeeds channel or
// logged to a file directly.
func recordCredentials(app *App, r *http.Request, username string, password string) {
// Populate data to send
payload, err := app.Agave.NewCredentialAttack(r, username, password)
if err != nil {
fmt.Println("error:", err)
return
}
// Marshal it to json so we can send it over hpfeeds.
buf, err := json.Marshal(payload)
if err != nil {
fmt.Println("error:", err)
return
}
log.Printf("%s", buf)
// Send to hpfeeds broker
if app.Config.Hpfeeds.Enabled {
app.Publish <- buf
}
}