-
Notifications
You must be signed in to change notification settings - Fork 11
/
routes.go
170 lines (147 loc) · 4.5 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
package main
import (
"encoding/json"
"errors"
"fmt"
"html/template"
"log"
"net/http"
)
var templates = template.Must(template.ParseGlob("templates/*"))
const CVE20196340 = "CVE-2019-6340"
const DrupalScan = "Drupal Scanner"
const DrupalScanLogin = "Drupal Scanner - Login Page"
const DrupalScanChangelog = "Drupal Scanner - CHANGELOG.txt"
var (
Err422 = errors.New("The website encountered an unexpected error. Please try again later.")
)
type Page struct {
Title string
Host string
Error bool
Username string
}
func NotFoundHandler(app App) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
recordAttack(app, r, DrupalScanChangelog)
err := templates.ExecuteTemplate(w, "404.html", getHost(r))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
// IndexHandler provides static pages depending on the request. If
// CHANGELOG.txt is requested, return the appropriate Changelog file 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, DrupalScan)
host := fmt.Sprintf("http://%s", app.SensorIP)
p := Page{
Title: app.Config.Drupal.SiteName,
Host: host,
}
err := templates.ExecuteTemplate(w, "index.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func NodeHandler(app App) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
recordAttack(app, r, CVE20196340)
http.Error(w, Err422.Error(), http.StatusUnprocessableEntity)
}
}
func fileServe(w http.ResponseWriter, r *http.Request) {
path := fmt.Sprintf("public%s", r.URL.Path)
http.ServeFile(w, r, path)
}
// 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.Drupal.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, DrupalScanLogin)
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()
mux.HandleFunc("/", IndexHandler(app))
mux.HandleFunc("/core/", fileServe)
mux.HandleFunc("/sites/", fileServe)
mux.HandleFunc("/logo.svg", fileServe)
mux.HandleFunc("/CHANGELOG.txt", NotFoundHandler(app))
mux.HandleFunc("/node/", NodeHandler(app))
mux.HandleFunc("/user/login", loginHandler(app))
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
}
}
// getHost tries its best to return the request host.
func getHost(r *http.Request) string {
r.URL.Scheme = "http"
r.URL.Host = r.Host
return r.URL.String()
}