| skipped 4 lines |
5 | 5 | | "net/http" |
6 | 6 | | "net/url" |
7 | 7 | | "strings" |
8 | | - | "sync" |
9 | 8 | | |
10 | 9 | | "github.com/google/cel-go/checker/decls" |
11 | 10 | | "github.com/zan8in/afrog/pkg/config" |
| skipped 6 lines |
18 | 17 | | ) |
19 | 18 | | |
20 | 19 | | type Checker struct { |
21 | | - | Options *sync.Pool |
22 | | - | Target *sync.Pool |
23 | | - | PocItem *sync.Pool |
24 | | - | PocHandler *sync.Pool |
25 | | - | OriginalRequest *sync.Pool |
26 | | - | VariableMap *sync.Pool |
27 | | - | Result *sync.Pool |
28 | | - | CustomLib *sync.Pool |
29 | | - | FastClient *sync.Pool |
| 20 | + | Options *config.Options |
| 21 | + | OriginalRequest *http.Request |
| 22 | + | VariableMap map[string]interface{} |
| 23 | + | Result *Result |
| 24 | + | CustomLib *CustomLib |
| 25 | + | FastClient *http2.FastClient |
30 | 26 | | } |
31 | 27 | | |
32 | 28 | | var ReverseCeyeApiKey string |
33 | 29 | | var ReverseCeyeDomain string |
34 | 30 | | |
35 | | - | func NewChecker(options *config.Options, target string, pocItem poc.Poc) *Checker { |
36 | | - | ReverseCeyeApiKey = options.Config.Reverse.Ceye.ApiKey |
37 | | - | ReverseCeyeDomain = options.Config.Reverse.Ceye.Domain |
38 | | - | |
39 | | - | if len(ReverseCeyeApiKey) == 0 || len(ReverseCeyeDomain) == 0 { |
40 | | - | log.Log().Error("Rerverse CeyeApiKey or CeyeDomain is Empty.") |
41 | | - | return nil |
42 | | - | } |
43 | | - | |
44 | | - | return &Checker{ |
45 | | - | Options: &sync.Pool{ |
46 | | - | New: func() interface{} { |
47 | | - | return options |
48 | | - | }, |
49 | | - | }, |
50 | | - | Target: &sync.Pool{ |
51 | | - | New: func() interface{} { |
52 | | - | return target |
53 | | - | }, |
54 | | - | }, |
55 | | - | PocItem: &sync.Pool{ |
56 | | - | New: func() interface{} { |
57 | | - | return &pocItem |
58 | | - | }, |
59 | | - | }, |
60 | | - | PocHandler: &sync.Pool{ |
61 | | - | New: func() interface{} { |
62 | | - | pocHandler := "" |
63 | | - | if strings.Contains(pocItem.Expression, "&&") && !strings.Contains(pocItem.Expression, "||") { |
64 | | - | pocHandler = poc.ALLAND |
65 | | - | } |
66 | | - | if strings.Contains(pocItem.Expression, "||") && !strings.Contains(pocItem.Expression, "&&") { |
67 | | - | pocHandler = poc.ALLOR |
68 | | - | } |
69 | | - | return pocHandler |
70 | | - | }, |
71 | | - | }, |
72 | | - | OriginalRequest: &sync.Pool{ |
73 | | - | New: func() interface{} { |
74 | | - | return &http.Request{} |
75 | | - | }, |
76 | | - | }, |
77 | | - | VariableMap: &sync.Pool{ |
78 | | - | New: func() interface{} { |
79 | | - | return make(map[string]interface{}) |
80 | | - | }, |
81 | | - | }, |
82 | | - | Result: &sync.Pool{ |
83 | | - | New: func() interface{} { |
84 | | - | return &Result{ |
85 | | - | Target: target, |
86 | | - | PocInfo: &pocItem, |
87 | | - | Output: options.Output, |
88 | | - | } |
89 | | - | }, |
90 | | - | }, |
91 | | - | CustomLib: &sync.Pool{ |
92 | | - | New: func() interface{} { |
93 | | - | return NewCustomLib() |
94 | | - | }, |
95 | | - | }, |
96 | | - | FastClient: &sync.Pool{ |
97 | | - | New: func() interface{} { |
98 | | - | return &http2.FastClient{} |
99 | | - | }, |
100 | | - | }, |
101 | | - | } |
102 | | - | } |
103 | | - | |
104 | | - | func (c *Checker) ReleaseVariableMap(vmap map[string]interface{}) { |
105 | | - | if vmap != nil { |
106 | | - | vmap = nil |
107 | | - | c.VariableMap.Put(vmap) |
108 | | - | } |
109 | | - | } |
110 | | - | |
111 | | - | func (c *Checker) ReleaseTarget(r string) { |
112 | | - | if len(r) > 0 { |
113 | | - | r = "" |
114 | | - | c.Target.Put(r) |
115 | | - | } |
116 | | - | } |
117 | | - | |
118 | | - | func (c *Checker) ReleaseHandler(h string) { |
119 | | - | if len(h) > 0 { |
120 | | - | h = "" |
121 | | - | c.Target.Put(h) |
122 | | - | } |
123 | | - | } |
124 | | - | |
125 | | - | func (c *Checker) ReleaseOriginalRequest(o *http.Request) { |
126 | | - | if o != nil { |
127 | | - | *o = http.Request{} |
128 | | - | c.OriginalRequest.Put(o) |
129 | | - | } |
130 | | - | } |
131 | | - | |
132 | 31 | | var FastClientReverse *http2.FastClient // 用于 reverse http client |
133 | 32 | | |
134 | | - | func (c *Checker) Check() (err error) { |
| 33 | + | func (c *Checker) Check(target string, pocItem poc.Poc) (err error) { |
135 | 34 | | |
136 | | - | options := c.Options.Get().(*config.Options) |
137 | | - | defer c.Options.Put(options) |
| 35 | + | options := c.Options |
138 | 36 | | |
139 | | - | fc := c.FastClient.Get().(*http2.FastClient) |
| 37 | + | ReverseCeyeApiKey = options.Config.Reverse.Ceye.ApiKey |
| 38 | + | ReverseCeyeDomain = options.Config.Reverse.Ceye.Domain |
| 39 | + | |
| 40 | + | fc := c.FastClient |
140 | 41 | | fc.Client = http2.New(options) |
141 | 42 | | fc.DialTimeout = options.Config.ConfigHttp.DialTimeout |
142 | | - | defer c.FastClient.Put(fc) |
143 | | - | defer fc.Reset() |
144 | 43 | | |
145 | | - | variableMap := c.VariableMap.Get().(map[string]interface{}) |
146 | | - | defer c.ReleaseVariableMap(variableMap) |
147 | | - | |
148 | | - | target := c.Target.Get().(string) |
149 | | - | defer c.ReleaseTarget(target) |
150 | | - | |
151 | | - | pocItem := c.PocItem.Get().(*poc.Poc) |
152 | | - | defer c.PocItem.Put(pocItem) |
153 | | - | defer pocItem.Reset() |
| 44 | + | variableMap := c.VariableMap |
154 | 45 | | |
155 | | - | result := c.Result.Get().(*Result) |
156 | | - | defer c.Result.Put(result) |
157 | | - | defer result.Reset() |
| 46 | + | result := c.Result |
| 47 | + | result.Target = target |
| 48 | + | result.PocInfo = &pocItem |
158 | 49 | | |
159 | | - | customLib := c.CustomLib.Get().(*CustomLib) |
160 | | - | defer c.CustomLib.Put(customLib) |
161 | | - | defer customLib.Reset() |
| 50 | + | customLib := c.CustomLib |
162 | 51 | | |
163 | | - | originalRequest := c.OriginalRequest.Get().(*http.Request) |
164 | | - | defer c.ReleaseOriginalRequest(originalRequest) |
| 52 | + | originalRequest := c.OriginalRequest |
165 | 53 | | |
166 | | - | pocHandler := c.PocHandler.Get().(string) |
167 | | - | defer c.ReleaseHandler(pocHandler) |
| 54 | + | pocHandler := "" |
| 55 | + | if strings.Contains(pocItem.Expression, "&&") && !strings.Contains(pocItem.Expression, "||") { |
| 56 | + | pocHandler = poc.ALLAND |
| 57 | + | } |
| 58 | + | if strings.Contains(pocItem.Expression, "||") && !strings.Contains(pocItem.Expression, "&&") { |
| 59 | + | pocHandler = poc.ALLOR |
| 60 | + | } |
168 | 61 | | |
169 | 62 | | // update request variablemap |
170 | | - | tempRequest := http2.AcquireProtoRequestPool() |
171 | | - | defer http2.ReleaseProtoRequestPool(tempRequest) |
| 63 | + | // tempRequest := http2.AcquireProtoRequestPool() |
| 64 | + | // defer http2.ReleaseProtoRequestPool(tempRequest) |
172 | 65 | | if pocItem.Transport != "tcp" && pocItem.Transport != "udp" { |
173 | 66 | | if !strings.HasPrefix(target, "http://") && !strings.HasPrefix(target, "https://") { |
174 | 67 | | target = "http://" + target |
| skipped 8 lines |
183 | 76 | | return err |
184 | 77 | | } |
185 | 78 | | |
186 | | - | tempRequest, err = http2.ParseRequest(originalRequest) |
| 79 | + | tempRequest, err := http2.ParseRequest(originalRequest) |
| 80 | + | variableMap["request"] = tempRequest |
| 81 | + | |
187 | 82 | | if err != nil { |
188 | 83 | | log.Log().Error(fmt.Sprintf("ParseRequest err, %s", err.Error())) |
189 | 84 | | result.IsVul = false |
| skipped 8 lines |
198 | 93 | | originalRequest.Header.Set("User-Agent", utils.RandomUA()) |
199 | 94 | | } |
200 | 95 | | } |
201 | | - | variableMap["request"] = tempRequest |
202 | 96 | | |
203 | 97 | | // update set cel and variablemap |
204 | 98 | | if len(pocItem.Set) > 0 { |
| skipped 103 lines |
308 | 202 | | customLib.UpdateCompileOption(key, decls.NewObjectType("proto.Reverse")) |
309 | 203 | | |
310 | 204 | | // if reverse(),initilize a fasthttpclient |
311 | | - | FastClientReverse = c.FastClient.Get().(*http2.FastClient) |
312 | | - | FastClientReverse.DialTimeout = c.Options.Get().(*config.Options).Config.ConfigHttp.DialTimeout |
313 | | - | FastClientReverse.Client = http2.New(c.Options.Get().(*config.Options)) |
| 205 | + | FastClientReverse = c.FastClient |
| 206 | + | FastClientReverse.DialTimeout = c.Options.Config.ConfigHttp.DialTimeout |
| 207 | + | FastClientReverse.Client = http2.New(c.Options) |
314 | 208 | | continue |
315 | 209 | | } |
316 | 210 | | |
| skipped 35 lines |