/
attack.go
240 lines (213 loc) · 6.05 KB
/
attack.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
package stress
import (
"crypto/md5"
"crypto/tls"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"sync/atomic"
"time"
)
var (
remain int64
)
// Attacker is an attack executor, wrapping an http.Client
type Attacker struct{ client http.Client }
// DefaultAttacker is the default Attacker used by Attack
var DefaultAttacker = NewAttacker()
// NewAttacker returns a pointer to a new Attacker
func NewAttacker() *Attacker {
return &Attacker{http.Client{Transport: &defaultTransport}}
}
// Attack hits the passed Targets (http.Requests) at the rate specified for
// duration time and then waits for all the requests to come back.
// The results of the attack are put into a slice which is returned.
//
// Attack is a wrapper around DefaultAttacker.Attack
func AttackRate(tgts Targets, rate uint64, du time.Duration) Results {
return DefaultAttacker.AttackRate(tgts, rate, du)
}
func AttackConcy(tgts Targets, concurrency uint64, number uint64) Results {
return DefaultAttacker.AttackConcy(tgts, concurrency, number)
}
// Attack attacks the passed Targets (http.Requests) at the rate specified for
// duration time and then waits for all the requests to come back.
// The results of the attack are put into a slice which is returned.
func (a Attacker) AttackRate(tgts Targets, rate uint64, du time.Duration) Results {
hits := int(rate * uint64(du.Seconds()))
resc := make(chan Result)
throttle := time.NewTicker(time.Duration(1e9 / rate))
defer throttle.Stop()
for i := 0; i < hits; i++ {
<-throttle.C
go func(tgt Target) { resc <- a.hit(tgt) }(tgts[i%len(tgts)])
}
results := make(Results, 0, hits)
for len(results) < cap(results) {
results = append(results, <-resc)
}
return results.Sort()
}
func (a *Attacker) hit(tgt Target) (res Result) {
req, err := tgt.Request()
if err != nil {
res.Error = err.Error()
return res
}
res.Timestamp = time.Now()
r, err := a.client.Do(req)
if err != nil {
res.Error = err.Error()
return res
}
res.BytesOut = uint64(req.ContentLength)
res.Code = uint16(r.StatusCode)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
if res.Code >= 300 || res.Code < 200 {
res.Error = fmt.Sprintf("%s %s: %s", tgt.Method, tgt.URL, r.Status)
}
return res
}
res.Latency = time.Since(res.Timestamp)
res.BytesIn = uint64(len(body))
if res.Code >= 300 || res.Code < 200 {
res.Error = fmt.Sprintf("%s %s: %s", tgt.Method, tgt.URL, r.Status)
} else {
if strings.Contains(tgt.File, "md5") {
//fmt.Printf("checking [%s]\n", tgt.File)
kv := strings.Split(tgt.File, ":")
/*
for k, v := range kv {
fmt.Printf("[%d]: %s\n", k, v)
}
*/
if len(kv) == 2 {
if kv[1] != "" && len(kv[1]) == 32 {
//fmt.Println("checking [%s]\n", kv[1])
h := md5.New()
h.Write(body)
rsp_md5 := hex.EncodeToString(h.Sum(nil))
if rsp_md5 != kv[1] {
//fmt.Println("md5 not match!")
res.Code = 250
res.Error = fmt.Sprintf("%s %s: MD5 not matced", tgt.Method, tgt.URL)
}
}
}
}
}
if res.Code >= 250 || res.Code < 200 {
log.Printf("%s\n", res.Error)
}
return res
}
// SetRedirects sets the max amount of redirects the attacker's http client
// will follow.
func (a *Attacker) SetRedirects(redirects int) {
a.client.CheckRedirect = func(_ *http.Request, via []*http.Request) error {
if len(via) > redirects {
return fmt.Errorf("Stopped after %d redirects", redirects)
}
return nil
}
}
// SetTimeout sets the client side timeout for each request the attacker makes.
func (a *Attacker) SetTimeout(timeout time.Duration) {
tr := a.client.Transport.(*http.Transport)
tr.ResponseHeaderTimeout = timeout
a.client.Transport = tr
}
func (a Attacker) AttackConcy(tgts Targets, concurrency uint64, number uint64) Results {
retsc := make(chan Results)
atomic.StoreInt64(&remain, int64(number))
if concurrency > number {
concurrency = number
}
var i uint64
for i = 0; i < concurrency; i++ {
go func(tgts Targets) { retsc <- a.shoot(tgts) }(tgts)
}
results := make(Results, 0, number)
for i = 0; i < concurrency; i++ {
results = append(results, <-retsc...)
}
return results.Sort()
}
func (a *Attacker) shoot(tgts Targets) Results {
results := make(Results, 0, 1)
req_remain := atomic.LoadInt64(&remain)
for req_remain > 0 {
atomic.AddInt64(&remain, -1)
var res Result
tgt := tgts[int(req_remain)%len(tgts)]
req, err := tgt.Request()
if err != nil {
res.Error = err.Error()
results = append(results, res)
req_remain = atomic.LoadInt64(&remain)
continue
}
res.Timestamp = time.Now()
r, err := a.client.Do(req)
if err != nil {
res.Error = err.Error()
results = append(results, res)
req_remain = atomic.LoadInt64(&remain)
continue
}
res.BytesOut = uint64(req.ContentLength)
res.Code = uint16(r.StatusCode)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
if res.Code >= 300 || res.Code < 200 {
res.Error = fmt.Sprintf("%s %s: %s", tgt.Method, tgt.URL, r.Status)
}
results = append(results, res)
req_remain = atomic.LoadInt64(&remain)
continue
}
res.Latency = time.Since(res.Timestamp)
res.BytesIn = uint64(len(body))
if res.Code >= 300 || res.Code < 200 {
res.Error = fmt.Sprintf("%s %s: %s", tgt.Method, tgt.URL, r.Status)
} else {
if strings.Contains(tgt.File, "md5") {
//fmt.Printf("checking [%s]\n", tgt.File)
kv := strings.Split(tgt.File, ":")
/*
for k, v := range kv {
fmt.Printf("[%d]: %s\n", k, v)
}
*/
if len(kv) == 2 {
if kv[1] != "" && len(kv[1]) == 32 {
//fmt.Println("checking [%s]\n", kv[1])
h := md5.New()
h.Write(body)
rsp_md5 := hex.EncodeToString(h.Sum(nil))
if rsp_md5 != kv[1] {
//fmt.Println("md5 not match!")
res.Code = 250
res.Error = fmt.Sprintf("%s %s: MD5 not matced", tgt.Method, tgt.URL)
}
}
}
}
}
if res.Code >= 250 || res.Code < 200 {
log.Printf("%s\n", res.Error)
}
results = append(results, res)
req_remain = atomic.LoadInt64(&remain)
}
return results
}
var defaultTransport = http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}