This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 245
/
controller.go
289 lines (255 loc) · 7.41 KB
/
controller.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package rule
import (
"fmt"
"github.com/jweny/pocassist/pkg/cel/proto"
log "github.com/jweny/pocassist/pkg/logging"
"github.com/jweny/pocassist/pkg/util"
"github.com/valyala/fasthttp"
"net/http"
"strings"
"sync"
)
const (
AffectContent = "text"
AffectDirectory = "directory"
AffectURL = "url"
AffectAppendParameter = "appendparam"
AffectReplaceParameter = "replaceparam"
AffectServer = "server"
AffectScript = "script"
)
var ControllerPool = sync.Pool{}
func NewController() *PocController {
if v := ControllerPool.Get(); v != nil {
c := v.(*PocController)
return c
}
return new(PocController)
}
func PutController(c *PocController) {
c.Reset()
ControllerPool.Put(c)
}
type PocController struct {
Plugin *Plugin
Request *RequestController
CEL *CelController
Handles []HandlerFunc // 控制整个执行过程
Index int64 // 和middlefunc 配套
abortIndex int64 // 终止的index
ScriptResult *util.ScanResult
Debug bool
Keys map[string]interface{}
}
type controllerContext interface {
Next()
Abort()
IsAborted() bool
GetString(key string) string
Set(key string, value interface{})
Get(key string) (value interface{}, exists bool)
GetPoc() *Poc
Groups(bool) (result bool, err error)
Rules([]Rule, bool) (result bool, err error)
GetPocName() string
GetOriginalReq() *http.Request
SetResult(result *util.ScanResult)
IsDebug() bool
// RegisterHandle(f HandlersChain)
}
func InitPocController(req *RequestController, plugin *Plugin, cel *CelController, handles []HandlerFunc) *PocController {
controller := NewController()
controller.Request = req
controller.Plugin = plugin
controller.CEL = cel
controller.Handles = handles
controller.Debug = false
return controller
}
// 增加插件
func (controller *PocController) AddMiddle(handle HandlerFunc) {
controller.Handles = append(controller.Handles, handle)
}
// 根据原始请求 + rule 生成并发起新的请求
func (controller *PocController) DoSingleRuleRequest(rule *Rule) (*proto.Response, error) {
fastReq := controller.Request.Fast
// fixReq : 根据规则对原始请求进行变形
fixedFastReq := fasthttp.AcquireRequest()
fastReq.CopyTo(fixedFastReq)
curPath := string(fixedFastReq.URI().Path())
affects := controller.Plugin.Affects
switch affects {
// param级
case AffectAppendParameter, AffectReplaceParameter:
for k, v := range rule.Headers {
fixedFastReq.Header.Set(k, v)
}
return util.DoFasthttpRequest(fixedFastReq, rule.FollowRedirects)
// content级
case AffectContent:
return util.DoFasthttpRequest(fixedFastReq, rule.FollowRedirects)
// dir级
case AffectDirectory:
// 目录级漏洞检测 判断是否以 "/"结尾
if curPath != "" && strings.HasSuffix(curPath, "/") {
// 去掉规则中的的"/" 再拼
curPath = fmt.Sprint(curPath, strings.TrimPrefix(rule.Path, "/"))
} else {
curPath = fmt.Sprint(curPath, "/" ,strings.TrimPrefix(rule.Path, "/"))
}
// server级
case AffectServer:
curPath = rule.Path
// url级
case AffectURL:
//curPath = curPath, strings.TrimPrefix(rule.Path, "/"))
default:
}
// 兼容xray: 某些 POC 没有区分path和query
curPath = strings.ReplaceAll(curPath, " ", "%20")
curPath = strings.ReplaceAll(curPath, "+", "%20")
fixedFastReq.URI().DisablePathNormalizing= true
fixedFastReq.URI().Update(curPath)
for k, v := range rule.Headers {
fixedFastReq.Header.Set(k, v)
}
fixedFastReq.Header.SetMethod(rule.Method)
// 处理multipart
contentType := string(fixedFastReq.Header.ContentType())
if strings.HasPrefix(strings.ToLower(contentType),"multipart/form-Data") && strings.Contains(rule.Body,"\n\n") {
multipartBody, err := util.DealMultipart(contentType, rule.Body)
if err != nil {
return nil, err
}
fixedFastReq.SetBody([]byte(multipartBody))
}else {
fixedFastReq.SetBody([]byte(rule.Body))
}
return util.DoFasthttpRequest(fixedFastReq, rule.FollowRedirects)
}
// 单个规则运行
func (controller *PocController) SingleRule(rule *Rule, debug bool) (bool, error) {
// 格式校验
err := rule.Verify()
if err != nil {
return false, err
}
// 替换 set
rule.ReplaceSet(controller.CEL.ParamMap)
// 根据原始请求 + rule 生成并发起新的请求 http
resp, err := controller.DoSingleRuleRequest(rule)
if err != nil {
return false, err
}
controller.CEL.ParamMap["response"] = resp
// 匹配search规则
if rule.Search != "" {
controller.CEL.ParamMap = rule.ReplaceSearch(resp, controller.CEL.ParamMap)
}
// 如果当前rule验证失败,立即释放
out, err := controller.CEL.Evaluate(rule.Expression)
if err != nil {
log.Error("[rule/controller.go:SingleRule cel evaluate error]", err)
return false, err
}
if debug {
controller.Request.Add(resp)
} else {
// 非debug模式下不记录 没有漏洞不记录请求链
if !out {
util.ResponsePut(resp)
return false, nil
} // 如果成功,记如请求链
controller.Request.Add(resp)
}
return out, err
}
// 执行 rules
func (controller *PocController) Rules(rules []Rule, debug bool) (bool, error) {
success := false
for _, rule := range rules {
singleRuleResult, err := controller.SingleRule(&rule, debug)
if err != nil {
log.Error("[rule/controller.go:Rules run error]" , err)
return false, err
}
if !singleRuleResult {
//如果false 直接跳出循环 返回
success = false
break
}
success = true
}
return success, nil
}
// 执行 groups
func (controller *PocController) Groups(debug bool) (bool, error) {
groups := controller.Plugin.JsonPoc.Groups
// groups 就是多个rules 任何一个rules成功 即返回成功
for _, rules := range groups {
rulesResult, err := controller.Rules(rules, debug)
if err != nil || !rulesResult {
continue
}
// groups中一个rules成功 即返回成功
if rulesResult {
return rulesResult, nil
}
}
return false, nil
}
func (controller *PocController) Next() {
for controller.Index < int64(len(controller.Handles)) {
controller.Handles[controller.Index](controller)
controller.Index++
}
}
func (controller *PocController) IsAborted() bool {
return controller.Index <= controller.abortIndex
}
func (controller *PocController) Abort() {
controller.abortIndex = controller.Index + 1
}
func (controller *PocController) Reset() {
controller.Handles = nil
controller.Index = 0
controller.abortIndex = 0
controller.Plugin = nil
controller.CEL.Reset()
controller.Request.Reset()
controller.ScriptResult = nil
controller.Keys = nil
controller.Debug = false
return
}
func (controller *PocController) Set(key string, value interface{}) {
if controller.Keys == nil {
controller.Keys = make(map[string]interface{})
}
controller.Keys[key] = value
}
func (controller *PocController) Get(key string) (value interface{}, exists bool) {
value, exists = controller.Keys[key]
return
}
func (controller *PocController) GetString(key string) (s string) {
if val, ok := controller.Get(key); ok && val != nil {
s, _ = val.(string)
}
return
}
func (controller *PocController) GetPoc() *Poc {
return controller.Plugin.JsonPoc
}
func (controller *PocController) GetPocName() string {
return controller.Plugin.JsonPoc.Name
}
func (controller *PocController) GetOriginalReq() *http.Request {
return controller.Request.Original
}
func (controller *PocController) SetResult(result *util.ScanResult){
controller.ScriptResult = result
}
func (controller *PocController) IsDebug() bool {
return controller.Debug
}