spray/internal/pool.go

257 lines
5.3 KiB
Go
Raw Normal View History

2022-09-08 15:57:17 +08:00
package internal
import (
"context"
"github.com/chainreactors/logs"
"github.com/chainreactors/spray/pkg"
2022-09-15 19:27:07 +08:00
"github.com/chainreactors/words"
2022-09-08 15:57:17 +08:00
"github.com/panjf2000/ants/v2"
"github.com/valyala/fasthttp"
2022-09-08 15:57:17 +08:00
"net/http"
"sync"
2022-09-15 19:27:07 +08:00
"time"
2022-09-08 15:57:17 +08:00
)
var (
CheckStatusCode func(int) bool
CheckRedirect func(string) bool
2022-09-08 15:57:17 +08:00
CheckWaf func(*http.Response) bool
)
2022-09-15 19:27:07 +08:00
func NewPool(ctx context.Context, config *pkg.Config, outputCh chan *baseline) (*Pool, error) {
2022-09-19 14:42:29 +08:00
pctx, cancel := context.WithCancel(ctx)
2022-09-08 15:57:17 +08:00
pool := &Pool{
Config: config,
ctx: pctx,
client: pkg.NewClient(config.Thread, 2),
worder: words.NewWorder(config.Wordlist),
outputCh: outputCh,
tempCh: make(chan *baseline, config.Thread),
wg: &sync.WaitGroup{},
checkPeriod: 100,
errPeriod: 10,
2022-09-08 15:57:17 +08:00
}
switch config.Mod {
case pkg.PathSpray:
pool.genReq = func(s string) (*fasthttp.Request, error) {
return pool.BuildPathRequest(s)
2022-09-08 15:57:17 +08:00
}
case pkg.HostSpray:
pool.genReq = func(s string) (*fasthttp.Request, error) {
return pool.BuildHostRequest(s)
2022-09-08 15:57:17 +08:00
}
}
p, _ := ants.NewPoolWithFunc(config.Thread, func(i interface{}) {
unit := i.(*Unit)
req, err := pool.genReq(unit.path)
if err != nil {
logs.Log.Error(err.Error())
return
}
var bl *baseline
2022-09-19 14:42:29 +08:00
resp, err := pool.client.Do(pctx, req)
2022-09-08 15:57:17 +08:00
if err != nil {
//logs.Log.Debugf("%s request error, %s", strurl, err.Error())
pool.errorCount++
bl = &baseline{Err: err}
} else {
defer fasthttp.ReleaseResponse(resp)
defer fasthttp.ReleaseRequest(req)
//defer resp.Body.Close() // 必须要关闭body ,否则keep-alive无法生效
2022-09-20 18:09:06 +08:00
if err = pool.PreCompare(resp); err == nil || unit.source == CheckSource {
2022-09-08 15:57:17 +08:00
// 通过预对比跳过一些无用数据, 减少性能消耗
bl, err = NewBaseline(req.URI(), resp)
2022-09-15 19:27:07 +08:00
} else if err == ErrWaf {
cancel()
2022-09-08 15:57:17 +08:00
} else {
bl = NewInvalidBaseline(req.URI(), resp)
2022-09-08 15:57:17 +08:00
}
}
switch unit.source {
2022-09-20 18:09:06 +08:00
case CheckSource:
if pool.baseline == nil {
//初次check覆盖baseline
pool.baseline = bl
} else if bl.Err != nil {
logs.Log.Warn("maybe ip banned by waf")
} else if !pool.baseline.Equal(bl) {
logs.Log.Warn("maybe trigger risk control")
}
2022-09-08 15:57:17 +08:00
case WordSource:
// 异步进行性能消耗较大的深度对比
pool.tempCh <- bl
2022-09-08 15:57:17 +08:00
}
//todo connectivity check
pool.bar.Done()
2022-09-08 15:57:17 +08:00
pool.wg.Done()
})
pool.pool = p
go pool.Comparing()
2022-09-08 15:57:17 +08:00
return pool, nil
}
type Pool struct {
//url string
//thread int
*pkg.Config
client *pkg.Client
pool *ants.PoolWithFunc
bar *pkg.Bar
2022-09-19 14:42:29 +08:00
ctx context.Context
2022-09-08 15:57:17 +08:00
//baseReq *http.Request
baseline *baseline
outputCh chan *baseline
tempCh chan *baseline
reqCount int
errorCount int
checkPeriod int
errPeriod int
genReq func(s string) (*fasthttp.Request, error)
2022-09-08 15:57:17 +08:00
//wordlist []string
2022-09-15 19:27:07 +08:00
worder *words.Worder
wg *sync.WaitGroup
2022-09-08 15:57:17 +08:00
}
func (p *Pool) check() {
p.wg.Add(1)
2022-09-20 18:09:06 +08:00
_ = p.pool.Invoke(newUnit(pkg.RandPath(), CheckSource))
2022-09-08 15:57:17 +08:00
//}
p.wg.Wait()
}
func (p *Pool) Init() error {
p.check()
2022-09-08 15:57:17 +08:00
// todo 分析baseline
// 检测基本访问能力
if p.baseline != nil && p.baseline.Err != nil {
return p.baseline.Err
}
p.baseline.Collect()
2022-09-08 15:57:17 +08:00
if p.baseline.RedirectURL != "" {
CheckRedirect = func(redirectURL string) bool {
if redirectURL == p.baseline.RedirectURL {
2022-09-08 15:57:17 +08:00
// 相同的RedirectURL将被认为是无效数据
return false
} else {
// path为3xx, 且与baseline中的RedirectURL不同时, 为有效数据
return true
2022-09-08 15:57:17 +08:00
}
}
}
return nil
}
2022-09-15 19:27:07 +08:00
func (p *Pool) Run(ctx context.Context) {
Loop:
for {
select {
case u, ok := <-p.worder.C:
if !ok {
break Loop
}
p.reqCount++
p.wg.Add(1)
if p.reqCount%p.checkPeriod == 0 {
go p.check()
} else if p.reqCount%p.errPeriod == 0 {
go p.check()
}
_ = p.pool.Invoke(newUnit(u, WordSource))
case <-time.NewTimer(time.Duration(p.DeadlineTime) * time.Second).C:
2022-09-15 19:27:07 +08:00
break Loop
case <-ctx.Done():
break Loop
2022-09-19 14:42:29 +08:00
case <-p.ctx.Done():
break Loop
2022-09-15 19:27:07 +08:00
}
2022-09-08 15:57:17 +08:00
}
2022-09-20 18:09:06 +08:00
p.bar.Close()
2022-09-08 15:57:17 +08:00
p.wg.Wait()
}
func (p *Pool) PreCompare(resp *fasthttp.Response) error {
if !CheckStatusCode(resp.StatusCode()) {
2022-09-15 19:27:07 +08:00
return ErrBadStatus
2022-09-08 15:57:17 +08:00
}
if CheckRedirect != nil && !CheckRedirect(string(resp.Header.Peek("Location"))) {
2022-09-15 19:27:07 +08:00
return ErrRedirect
2022-09-08 15:57:17 +08:00
}
//if CheckWaf != nil && !CheckWaf(resp) {
// return ErrWaf
//}
2022-09-08 15:57:17 +08:00
2022-09-15 19:27:07 +08:00
return nil
2022-09-08 15:57:17 +08:00
}
func (p *Pool) RunWithWord(words []string) {
}
func (p *Pool) Comparing() {
for bl := range p.tempCh {
if p.baseline.Equal(bl) {
// 如果是同一个包则设置为无效包
bl.IsValid = false
p.outputCh <- bl
continue
}
bl.Collect()
if p.EnableFuzzy && p.baseline.FuzzyEqual(bl) {
bl.IsValid = false
p.outputCh <- bl
continue
}
p.outputCh <- bl
}
}
func (p *Pool) BuildPathRequest(path string) (*fasthttp.Request, error) {
req := fasthttp.AcquireRequest()
req.SetRequestURI(p.BaseURL + path)
return req, nil
}
func (p *Pool) BuildHostRequest(host string) (*fasthttp.Request, error) {
req := fasthttp.AcquireRequest()
req.SetRequestURI(p.BaseURL)
req.SetHost(host)
return req, nil
}
2022-09-08 15:57:17 +08:00
type sourceType int
const (
2022-09-20 18:09:06 +08:00
CheckSource sourceType = iota + 1
2022-09-08 15:57:17 +08:00
WordSource
WafSource
)
//var sourceMap = map[int]string{
//
//}
func newUnit(path string, source sourceType) *Unit {
return &Unit{path: path, source: source}
}
type Unit struct {
path string
source sourceType
//callback func()
}