优化--force的逻辑, 开启--force时将关闭check

This commit is contained in:
M09Ic 2022-11-17 05:40:02 +08:00
parent 11a8b6168c
commit 32bf598c28
4 changed files with 89 additions and 71 deletions

View File

@ -16,6 +16,7 @@ type Option struct {
InputOptions InputOptions
OutputOptions OutputOptions
RequestOptions RequestOptions
ModeOptions
MiscOptions MiscOptions
} }
@ -50,9 +51,15 @@ type RequestOptions struct {
Headers []string `long:"header"` Headers []string `long:"header"`
Method string `long:"method"` Method string `long:"method"`
Cookie string `long:"cookie"` Cookie string `long:"cookie"`
Force bool `long:"force"` SimhashDistance int `long:"distance" default:"5"`
SimhashDistance int `long:"distance"` }
CheckOnly bool `long:"--check-only"`
type ModeOptions struct {
Force bool `long:"force"`
CheckOnly bool `long:"check-only"`
CheckPeriod int `long:"check-period" default:"100"`
ErrPeriod int `long:"error-period" default:"10"`
BreakThreshold int `long:"error-threshold" default:"20"`
} }
type MiscOptions struct { type MiscOptions struct {
@ -73,20 +80,23 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
} }
var err error var err error
r := &Runner{ r := &Runner{
Progress: uiprogress.New(), Progress: uiprogress.New(),
Threads: opt.Threads, Threads: opt.Threads,
PoolSize: opt.PoolSize, PoolSize: opt.PoolSize,
Mod: opt.Mod, Mod: opt.Mod,
Timeout: opt.Timeout, Timeout: opt.Timeout,
Deadline: opt.Deadline, Deadline: opt.Deadline,
Offset: opt.Offset, Offset: opt.Offset,
Limit: opt.Limit, Limit: opt.Limit,
URLList: make(chan string), URLList: make(chan string),
OutputCh: make(chan *pkg.Baseline, 100), OutputCh: make(chan *pkg.Baseline, 100),
FuzzyCh: make(chan *pkg.Baseline, 100), FuzzyCh: make(chan *pkg.Baseline, 100),
Fuzzy: opt.Fuzzy, Fuzzy: opt.Fuzzy,
Force: opt.Force, Force: opt.Force,
CheckOnly: opt.CheckOnly, CheckOnly: opt.CheckOnly,
CheckPeriod: opt.CheckPeriod,
ErrPeriod: opt.ErrPeriod,
BreakThreshold: opt.BreakThreshold,
} }
err = pkg.LoadTemplates() err = pkg.LoadTemplates()
@ -108,7 +118,10 @@ func (opt *Option) PrepareRunner() (*Runner, error) {
} }
if opt.Force { if opt.Force {
breakThreshold = 999999 // 如果开启了force模式, 将关闭check机制, err积累到一定数量自动退出机制
r.BreakThreshold = max
r.CheckPeriod = max
r.ErrPeriod = max
} }
// prepare url // prepare url

View File

@ -19,8 +19,7 @@ var (
CheckRedirect func(string) bool CheckRedirect func(string) bool
CheckWaf func([]byte) bool CheckWaf func([]byte) bool
) )
var max = 2147483647
var breakThreshold int = 20
func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) { func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
pctx, cancel := context.WithCancel(ctx) pctx, cancel := context.WithCancel(ctx)
@ -34,8 +33,6 @@ func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
tempCh: make(chan *pkg.Baseline, config.Thread), tempCh: make(chan *pkg.Baseline, config.Thread),
wg: sync.WaitGroup{}, wg: sync.WaitGroup{},
initwg: sync.WaitGroup{}, initwg: sync.WaitGroup{},
checkPeriod: 100,
errPeriod: 10,
reqCount: 1, reqCount: 1,
failedCount: 1, failedCount: 1,
} }
@ -49,7 +46,7 @@ func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
pool.wg.Add(1) pool.wg.Add(1)
_ = pool.pool.Invoke(newUnit(pkg.RandPath(), CheckSource)) _ = pool.pool.Invoke(newUnit(pkg.RandPath(), CheckSource))
if pool.failedCount > breakThreshold { if pool.failedCount > pool.BreakThreshold {
// 当报错次数超过上限是, 结束任务 // 当报错次数超过上限是, 结束任务
pool.recover() pool.recover()
pool.cancel() pool.cancel()
@ -64,7 +61,7 @@ func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
pool.wg.Add(1) pool.wg.Add(1)
_ = pool.pool.Invoke(newUnit(pkg.RandHost(), CheckSource)) _ = pool.pool.Invoke(newUnit(pkg.RandHost(), CheckSource))
if pool.failedCount > breakThreshold { if pool.failedCount > pool.BreakThreshold {
// 当报错次数超过上限是, 结束任务 // 当报错次数超过上限是, 结束任务
pool.recover() pool.recover()
pool.cancel() pool.cancel()
@ -109,7 +106,7 @@ func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
return return
case CheckSource: case CheckSource:
if bl.Err != "" { if bl.Err != "" {
logs.Log.Warnf("[check.error] maybe ip had banned by waf, break (%d/%d), error: %s", pool.failedCount, breakThreshold, bl.Err) logs.Log.Warnf("[check.error] maybe ip had banned by waf, break (%d/%d), error: %s", pool.failedCount, pool.BreakThreshold, bl.Err)
pool.failedBaselines = append(pool.failedBaselines, bl) pool.failedBaselines = append(pool.failedBaselines, bl)
} else if i := pool.base.Compare(bl); i < 1 { } else if i := pool.base.Compare(bl); i < 1 {
if i == 0 { if i == 0 {
@ -128,10 +125,10 @@ func NewPool(ctx context.Context, config *pkg.Config) (*Pool, error) {
// 异步进行性能消耗较大的深度对比 // 异步进行性能消耗较大的深度对比
pool.tempCh <- bl pool.tempCh <- bl
pool.reqCount++ pool.reqCount++
if pool.reqCount%pool.checkPeriod == 0 { if pool.reqCount%pool.CheckPeriod == 0 {
pool.reqCount++ pool.reqCount++
go pool.check() go pool.check()
} else if pool.failedCount%pool.errPeriod == 0 { } else if pool.failedCount%pool.ErrPeriod == 0 {
pool.failedCount++ pool.failedCount++
go pool.check() go pool.check()
} }
@ -168,8 +165,6 @@ type Pool struct {
tempCh chan *pkg.Baseline // 待处理的baseline tempCh chan *pkg.Baseline // 待处理的baseline
reqCount int reqCount int
failedCount int failedCount int
checkPeriod int
errPeriod int
failedBaselines []*pkg.Baseline failedBaselines []*pkg.Baseline
base *pkg.Baseline base *pkg.Baseline
baselines map[int]*pkg.Baseline baselines map[int]*pkg.Baseline

View File

@ -17,28 +17,31 @@ var BlackStatus = []int{400, 404, 410}
var FuzzyStatus = []int{403, 500, 501, 502, 503} var FuzzyStatus = []int{403, 500, 501, 502, 503}
type Runner struct { type Runner struct {
URLList chan string URLList chan string
Wordlist []string Wordlist []string
Headers http.Header Headers http.Header
Fns []func(string) string Fns []func(string) string
Threads int Threads int
PoolSize int PoolSize int
Pools *ants.PoolWithFunc Pools *ants.PoolWithFunc
poolwg sync.WaitGroup poolwg sync.WaitGroup
Timeout int Timeout int
Mod string Mod string
Probes []string Probes []string
OutputCh chan *pkg.Baseline OutputCh chan *pkg.Baseline
FuzzyCh chan *pkg.Baseline FuzzyCh chan *pkg.Baseline
Fuzzy bool Fuzzy bool
OutputFile *files.File OutputFile *files.File
FuzzyFile *files.File FuzzyFile *files.File
Force bool Force bool
Progress *uiprogress.Progress Progress *uiprogress.Progress
Offset int Offset int
Limit int Limit int
Deadline int Deadline int
CheckOnly bool CheckPeriod int
ErrPeriod int
BreakThreshold int
CheckOnly bool
} }
func (r *Runner) Prepare(ctx context.Context) error { func (r *Runner) Prepare(ctx context.Context) error {
@ -55,15 +58,18 @@ func (r *Runner) Prepare(ctx context.Context) error {
r.Pools, err = ants.NewPoolWithFunc(r.PoolSize, func(i interface{}) { r.Pools, err = ants.NewPoolWithFunc(r.PoolSize, func(i interface{}) {
u := i.(string) u := i.(string)
config := &pkg.Config{ config := &pkg.Config{
BaseURL: u, BaseURL: u,
Wordlist: r.Wordlist, Wordlist: r.Wordlist,
Thread: r.Threads, Thread: r.Threads,
Timeout: r.Timeout, Timeout: r.Timeout,
Headers: r.Headers, Headers: r.Headers,
Mod: pkg.ModMap[r.Mod], Mod: pkg.ModMap[r.Mod],
Fns: r.Fns, Fns: r.Fns,
OutputCh: r.OutputCh, OutputCh: r.OutputCh,
FuzzyCh: r.FuzzyCh, FuzzyCh: r.FuzzyCh,
CheckPeriod: r.CheckPeriod,
ErrPeriod: r.ErrPeriod,
BreakThreshold: r.BreakThreshold,
} }
if config.Mod == pkg.PathSpray { if config.Mod == pkg.PathSpray {
@ -84,6 +90,7 @@ func (r *Runner) Prepare(ctx context.Context) error {
if err != nil { if err != nil {
logs.Log.Error(err.Error()) logs.Log.Error(err.Error())
if !r.Force { if !r.Force {
// 如果没开启force, init失败将会关闭pool
pool.cancel() pool.cancel()
r.poolwg.Done() r.poolwg.Done()
return return

View File

@ -19,15 +19,18 @@ var ModMap = map[string]SprayMod{
} }
type Config struct { type Config struct {
BaseURL string BaseURL string
Wordlist []string Wordlist []string
Thread int Thread int
Timeout int Timeout int
Method string CheckPeriod int
Mod SprayMod ErrPeriod int
Headers http.Header BreakThreshold int
ClientType int Method string
Fns []func(string) string Mod SprayMod
OutputCh chan *Baseline Headers http.Header
FuzzyCh chan *Baseline ClientType int
Fns []func(string) string
OutputCh chan *Baseline
FuzzyCh chan *Baseline
} }