/
vul_csrf.go
108 lines (99 loc) · 2.77 KB
/
vul_csrf.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
package vulinbox
import (
_ "embed"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/utils/lowhttp"
"github.com/yaklang/yaklang/common/yak/yaklib/codec"
"html/template"
"net/http"
)
//go:embed html/vul_csrf_unsafe.html
var unsafeUpdate []byte
//go:embed html/vul_csrf_safe.html
var safeUpdate []byte
var info = "adminPassword"
func (s *VulinServer) registerCsrf() {
var router = s.router
csrfGroup := router.PathPrefix("/csrf").Name("表单 CSRF 保护测试").Subrouter()
csrfRoutes := []*VulInfo{
{
DefaultQuery: "",
Path: "/unsafe",
Title: "没有保护的表单",
Handler: func(writer http.ResponseWriter, request *http.Request) {
if !cookieCheck(writer, request, "/csrf/unsafe") {
return
}
data := map[string]string{
"Info": info,
}
updateInfo(writer, request, data, string(unsafeUpdate))
},
},
{
DefaultQuery: "",
Path: "/safe",
Title: "csrf_token保护的表单",
Handler: func(writer http.ResponseWriter, request *http.Request) {
if !cookieCheck(writer, request, "/csrf/safe") {
return
}
if request.Method == http.MethodPost {
token := request.PostFormValue("csrf_token")
if token != codec.Md5("confidential_cookie_vulinbox") {
writer.Write([]byte("csrf_token check error"))
writer.WriteHeader(http.StatusForbidden)
return
}
}
data := map[string]string{
"Info": info,
"Token": codec.Md5("confidential_cookie_vulinbox"),
}
updateInfo(writer, request, data, string(safeUpdate))
},
},
}
for _, v := range csrfRoutes {
addRouteWithVulInfo(csrfGroup, v)
}
}
func cookieCheck(writer http.ResponseWriter, request *http.Request, location string) bool {
raw, _ := utils.HttpDumpWithBody(request, true)
vulCookie := lowhttp.GetHTTPPacketCookieFirst(raw, "vulCookie")
if vulCookie == "" {
http.SetCookie(writer, &http.Cookie{
Name: "vulCookie",
Value: "confidential_cookie",
})
writer.Header().Set("Location", location)
writer.WriteHeader(302)
return false
}
return true
}
func updateInfo(writer http.ResponseWriter, request *http.Request, data map[string]string, tp string) {
if request.Method == http.MethodPost {
if request.PostFormValue("info") != "" {
info = request.PostFormValue("info")
data["Info"] = info
} else {
writer.Write([]byte("缺少参数"))
writer.WriteHeader(http.StatusBadRequest)
return
}
}
tmpl, err := template.New("csrf").Parse(tp)
if err != nil {
writer.Write([]byte(err.Error()))
writer.WriteHeader(http.StatusInternalServerError)
return
}
err = tmpl.Execute(writer, data)
if err != nil {
writer.Write([]byte(err.Error()))
writer.WriteHeader(http.StatusInternalServerError)
return
}
writer.WriteHeader(http.StatusOK)
}