Skip to content

Commit cf7af09

Browse files
committed
协程池换成ants框架,最多限度利用发挥内存作用
1 parent 7554801 commit cf7af09

File tree

5 files changed

+123
-11
lines changed

5 files changed

+123
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* [x] 命令执行漏洞式支持交互式执行命令
3131
* [ ] 验证url是否存活
3232
* [x] 增加自动更新参数
33+
* [x] 随机User-Agent请求头
3334

3435
………
3536

cmd/commons/core/runner.go

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/SummerSec/SpringExploit/cmd/commons/attack"
66
"github.com/SummerSec/SpringExploit/cmd/commons/utils"
77
"github.com/fatih/structs"
8+
"github.com/panjf2000/ants/v2"
89
log "github.com/sirupsen/logrus"
910
"net/url"
1011
"sync"
@@ -57,35 +58,75 @@ func (r *Runner) Run() {
5758
k := r.options.Thread
5859
var wg sync.WaitGroup
5960
hashmap := structs.Map(&r.options)
61+
defer ants.Release()
62+
// TODO: check if options are valid
63+
if k <= 0 {
64+
k = 500
65+
}
66+
log.Info("Running with ", k, " threads")
67+
pool, err1 := ants.NewPool(k+1, ants.WithPreAlloc(true))
68+
if err1 != nil {
69+
log.Error("Error creating pool")
70+
return
71+
}
72+
log.Info("Total URLs: ", len(urls))
6073
for i < len(urls) {
74+
//TODO 老代码
75+
//for t := 0; t < k; t++ {
76+
// if i == len(urls) {
77+
// break
78+
// }
79+
// if urls[i] != "" {
80+
// log.Debugln("Running attack on: ", urls[i])
81+
// // 通道通信 发送url 并且 i++
82+
// c := make(chan int)
83+
// wg.Add(1)
84+
// go func() {
85+
// log.Debugf("Running go func() %d", t)
86+
// Start(urls[i], hashmap, i, c) // Start k goroutines
87+
// wg.Done()
88+
// }()
89+
// i = <-c
90+
// } else {
91+
// wg.Wait()
92+
// i++
93+
// break
94+
// }
95+
//}
96+
6197
for t := 0; t < k; t++ {
62-
if i == len(urls) {
98+
if i == len(urls)-1 {
6399
break
64100
}
65101
if urls[i] != "" {
66-
log.Debugln("Running attack on: ", urls[i])
67102
// 通道通信 发送url 并且 i++
68-
c := make(chan int)
103+
//c := make(chan int)
104+
log.Debugf("Now Threads: %d", t)
69105
wg.Add(1)
70-
go func() {
71-
log.Debugf("Running go func() %d", t)
72-
Start(urls[i], hashmap, i, c) // Start k goroutines
106+
err := pool.Submit(func() {
107+
log.Debugf("Running Submit %d url is %s %d", t, urls[i], i)
108+
Start2(urls[i], hashmap, i) // Start k goroutines
73109
wg.Done()
74-
}()
75-
i = <-c
110+
})
111+
i++
112+
if err != nil {
113+
log.Error("Error submitting job " + urls[i])
114+
log.Error(err)
115+
}
116+
//i = <-c
76117
} else {
77-
wg.Wait()
78118
i++
79-
break
119+
wg.Wait()
80120
}
81121
}
122+
82123
}
83124

84125
}
85126

86127
func Start(u string, hashmap map[string]interface{}, i int, c chan int) {
87128
log.Info("Runner started")
88-
log.Infoln("testing URL: ", u)
129+
log.Infoln("Pen-testing URL: ", u)
89130
//for k, v := range hashmap {
90131
// log.Debugln("key: ", k, " value: ", v)
91132
//}
@@ -111,3 +152,32 @@ func Start(u string, hashmap map[string]interface{}, i int, c chan int) {
111152
c <- i + 1
112153

113154
}
155+
156+
func Start2(u string, hashmap map[string]interface{}, i int) {
157+
log.Infof("%s Runner started", u)
158+
//log.Infoln("testing URL: ", u)
159+
//for k, v := range hashmap {
160+
// log.Debugln("key: ", k, " value: ", v)
161+
//}
162+
defer func() {
163+
if errs := recover(); errs != nil {
164+
log.Debug("Runner panic: ", errs)
165+
}
166+
}()
167+
168+
r, err := url.Parse(u)
169+
if err != nil {
170+
log.Info("URL parse error")
171+
log.Errorln(err)
172+
return
173+
}
174+
var target string
175+
if r.Path == "" {
176+
target = r.Scheme + "://" + r.Host + "/"
177+
} else {
178+
target = r.Scheme + "://" + r.Host + r.Path
179+
}
180+
attack.Sevice(target, hashmap)
181+
log.Infof("%s Runner finished", u)
182+
183+
}

cmd/test/ants.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/panjf2000/ants/v2"
6+
"sync"
7+
"time"
8+
)
9+
10+
func Task() {
11+
fmt.Println(fmt.Sprintf("%d %s", time.Now().Nanosecond(), "Hello, World!"))
12+
time.Sleep(time.Second * 2)
13+
14+
}
15+
16+
func main() {
17+
defer ants.Release()
18+
pool, err := ants.NewPool(50)
19+
if err != nil {
20+
fmt.Println(err)
21+
return
22+
}
23+
var wg sync.WaitGroup
24+
25+
task := func() {
26+
Task()
27+
wg.Done()
28+
}
29+
30+
for i := 0; i < 100; i++ {
31+
wg.Add(1)
32+
fmt.Println(fmt.Sprintf("%d %s", i, "before submit"))
33+
_ = pool.Submit(task)
34+
}
35+
wg.Wait()
36+
37+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/gosuri/uiprogress v0.0.1 // indirect
2323
github.com/hashicorp/errwrap v1.0.0 // indirect
2424
github.com/hashicorp/go-multierror v1.1.1 // indirect
25+
github.com/panjf2000/ants/v2 v2.5.0 // indirect
2526
github.com/projectdiscovery/blackrock v0.0.0-20210415162320-b38689ae3a2e // indirect
2627
github.com/tj/go-update v2.2.5-0.20200519121640-62b4b798fd68+incompatible
2728
golang.org/x/net v0.0.0-20220111093109-d55c255bac03 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
9797
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
9898
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
9999
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
100+
github.com/panjf2000/ants/v2 v2.5.0 h1:1rWGWSnxCsQBga+nQbA4/iY6VMeNoOIAM0ZWh9u3q2Q=
101+
github.com/panjf2000/ants/v2 v2.5.0/go.mod h1:cU93usDlihJZ5CfRGNDYsiBYvoilLvBF5Qp/BT2GNRE=
100102
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
101103
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
102104
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -129,6 +131,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
129131
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
130132
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
131133
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
134+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
132135
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
133136
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
134137
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=

0 commit comments

Comments
 (0)