-
Notifications
You must be signed in to change notification settings - Fork 31
/
slapper.go
591 lines (499 loc) · 12.7 KB
/
slapper.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package main
import (
"bufio"
"bytes"
"crypto/tls"
"encoding/base64"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net/http"
"net/textproto"
"os"
"strings"
"sync"
"sync/atomic"
"time"
term "github.com/nsf/termbox-go"
terminal "github.com/wayneashleyberry/terminal-dimensions"
)
const (
statsLines = 3
movingWindowsSize = 10 // seconds
screenRefreshFrequency = 10 // per second
screenRefreshInterval = time.Second / screenRefreshFrequency
reservedWidthSpace = 40
reservedHeightSpace = 3
rateIncreaseStep = 100
rateDecreaseStep = -100
)
var (
requestsSent counter
responsesReceived counter
responses [1024]counter
desiredRate counter
timingsOk [][]counter
timingsBad [][]counter
terminalWidth uint
terminalHeight uint
// plotting vars
plotWidth uint
plotHeight uint
// first bucket is for requests faster then minY,
// last of for ones slower then maxY
buckets uint
logBase float64
minY, maxY float64
startMs float64
)
func resetStats() {
requestsSent.Store(0)
responsesReceived.Store(0)
for _, ok := range timingsOk {
for i := 0; i < len(ok); i++ {
ok[i].Store(0)
}
}
for _, bad := range timingsBad {
for i := 0; i < len(bad); i++ {
bad[i].Store(0)
}
}
for i := 0; i < len(responses); i++ {
responses[i].Store(0)
}
}
type counter int64
func (c *counter) Add(v int64) int64 { return atomic.AddInt64((*int64)(c), v) }
func (c *counter) Load() int64 { return atomic.LoadInt64((*int64)(c)) }
func (c *counter) Store(v int64) { atomic.StoreInt64((*int64)(c), v) }
type targeter struct {
idx counter
requests []request
header http.Header
}
type request struct {
method string
url string
body []byte
}
func newTargeter(targets string, base64body bool) (*targeter, error) {
var f *os.File
var err error
if targets == "" {
f = os.Stdin
} else {
f, err = os.Open(targets)
if err != nil {
return nil, err
}
defer f.Close()
}
trgt := &targeter{}
err = trgt.readTargets(f, base64body)
return trgt, err
}
func (trgt *targeter) readTargets(reader io.Reader, base64body bool) error {
// syntax
// GET <url>\n
// $ <body>\n
// \n
var (
method string
url string
body []byte
)
scanner := bufio.NewScanner(reader)
var lastLine string
var line string
for {
if lastLine != "" {
line = lastLine
lastLine = ""
} else {
ok := scanner.Scan()
if !ok {
break
}
line = strings.TrimSpace(scanner.Text())
}
if line == "" {
continue
}
parts := strings.SplitAfterN(line, " ", 2)
method = strings.TrimSpace(parts[0])
url = strings.TrimSpace(parts[1])
ok := scanner.Scan()
line := strings.TrimSpace(scanner.Text())
if !ok {
body = []byte{}
} else if line == "{}" {
body = []byte{}
} else if !strings.HasPrefix(line, "$ ") {
body = []byte{}
lastLine = line
} else {
line = strings.TrimPrefix(line, "$ ")
if base64body {
var err error
body, err = base64.StdEncoding.DecodeString(line)
if err != nil {
return err
}
} else {
body = []byte(line)
}
}
trgt.requests = append(trgt.requests, request{
method: method,
url: url,
body: body,
})
}
return nil
}
func (trgt *targeter) nextRequest() (*http.Request, error) {
if len(trgt.requests) == 0 {
return nil, errors.New("no requests")
}
idx := int(trgt.idx.Add(1))
st := trgt.requests[idx%len(trgt.requests)]
req, err := http.NewRequest(
st.method,
st.url,
bytes.NewReader(st.body),
)
if err != nil {
return req, err
}
for key, headers := range trgt.header {
for _, header := range headers {
if key == "Host" {
req.Host = header
} else {
req.Header.Add(key, header)
}
}
}
return req, err
}
func attack(trgt *targeter, timeout time.Duration, ch <-chan time.Time, quit <-chan struct{}) {
tr := &http.Transport{
DisableKeepAlives: false,
DisableCompression: true,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 30 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Timeout: timeout,
}
for {
select {
case <-ch:
if request, err := trgt.nextRequest(); err == nil {
requestsSent.Add(1)
start := time.Now()
response, err := client.Do(request)
if err == nil {
_, err = ioutil.ReadAll(response.Body)
response.Body.Close()
}
now := time.Now()
elapsed := now.Sub(start)
elapsedMs := float64(elapsed) / float64(time.Millisecond)
correctedElapsedMs := elapsedMs - startMs
elapsedBucket := int(math.Log(correctedElapsedMs) / math.Log(logBase))
// first bucket is for requests faster then minY,
// last of for ones slower then maxY
if elapsedBucket < 0 {
elapsedBucket = 0
} else if elapsedBucket >= int(buckets)-1 {
elapsedBucket = int(buckets) - 1
} else {
elapsedBucket = elapsedBucket + 1
}
responsesReceived.Add(1)
status := 0
if err == nil {
status = response.StatusCode
}
responses[status].Add(1)
tOk, tBad := getTimingsSlot(now)
if status >= 200 && status < 300 {
tOk[elapsedBucket].Add(1)
} else {
tBad[elapsedBucket].Add(1)
}
}
case <-quit:
return
}
}
}
func reporter(quit <-chan struct{}) {
fmt.Print("\033[H")
for i := 0; i < int(terminalHeight); i++ {
fmt.Println(string(bytes.Repeat([]byte(" "), int(terminalWidth)-1)))
}
var currentRate counter
go func() {
var lastSent int64
for range time.Tick(time.Second) {
curr := requestsSent.Load()
currentRate.Store(curr - lastSent)
lastSent = curr
}
}()
colors := []string{
"\033[38;5;46m", "\033[38;5;47m", "\033[38;5;48m", "\033[38;5;49m", // green
"\033[38;5;149m", "\033[38;5;148m", "\033[38;5;179m", "\033[38;5;176m", // yellow
"\033[38;5;169m", "\033[38;5;168m", "\033[38;5;197m", "\033[38;5;196m", // red
}
colorMultiplier := float64(len(colors)) / float64(buckets)
barWidth := int(plotWidth) - reservedWidthSpace // reserve some space on right and left
ticker := time.Tick(screenRefreshInterval)
for {
select {
case <-ticker:
// scratch arrays
tOk := make([]int64, len(timingsOk))
tBad := make([]int64, len(timingsBad))
// need to understand how long in longest bar,
// also take a change to copy arrays to have consistent view
max := int64(1)
for i := 0; i < len(timingsOk); i++ {
ok := timingsOk[i]
bad := timingsBad[i]
for j := 0; j < len(ok); j++ {
tOk[j] += ok[j].Load()
tBad[j] += bad[j].Load()
if sum := tOk[j] + tBad[j]; sum > max {
max = sum
}
}
}
sent := requestsSent.Load()
recv := responsesReceived.Load()
fmt.Print("\033[H") // clean screen
fmt.Printf("sent: %-6d ", sent)
fmt.Printf("in-flight: %-2d ", sent-recv)
fmt.Printf("\033[96mrate: %4d/%d RPS\033[0m ", currentRate.Load(), desiredRate.Load())
fmt.Print("responses: ")
for status, counter := range responses {
if c := counter.Load(); c > 0 {
if status >= 200 && status < 300 {
fmt.Printf("\033[32m[%d]: %-6d\033[0m ", status, c)
} else {
fmt.Printf("\033[31m[%d]: %-6d\033[0m ", status, c)
}
}
}
fmt.Print("\r\n\r\n")
width := float64(barWidth) / float64(max)
for bkt := uint(0); bkt < buckets; bkt++ {
var label string
if bkt == 0 {
if startMs >= 10 {
label = fmt.Sprintf("<%.0f", startMs)
} else {
label = fmt.Sprintf("<%.1f", startMs)
}
} else if bkt == buckets-1 {
if maxY >= 10 {
label = fmt.Sprintf("%3.0f+", maxY)
} else {
label = fmt.Sprintf("%.1f+", maxY)
}
} else {
beginMs := minY + math.Pow(logBase, float64(bkt-1))
endMs := minY + math.Pow(logBase, float64(bkt))
if endMs >= 10 {
label = fmt.Sprintf("%3.0f-%3.0f", beginMs, endMs)
} else {
label = fmt.Sprintf("%.1f-%.1f", beginMs, endMs)
}
}
widthOk := int(float64(tOk[bkt]) * width)
widthBad := int(float64(tBad[bkt]) * width)
widthLeft := barWidth - widthOk - widthBad
fmt.Printf("%10s ms: [%s%6d%s/%s%6d%s] %s%s%s%s%s \r\n",
label,
"\033[32m",
tOk[bkt],
"\033[0m",
"\033[31m",
tBad[bkt],
"\033[0m",
colors[int(float64(bkt)*colorMultiplier)],
bytes.Repeat([]byte("E"), widthBad),
bytes.Repeat([]byte("*"), widthOk),
bytes.Repeat([]byte(" "), widthLeft),
"\033[0m")
}
case <-quit:
return
}
}
}
func keyPressListener(rateChanger chan<- int64) {
// start keyPress listener
err := term.Init()
if err != nil {
log.Fatal(err)
}
defer term.Close()
keyPressListenerLoop:
for {
switch ev := term.PollEvent(); ev.Type {
case term.EventKey:
switch ev.Key {
case term.KeyCtrlC:
break keyPressListenerLoop
default:
switch ev.Ch {
case 'q':
break keyPressListenerLoop
case 'r':
resetStats()
case 'k': // up
rateChanger <- rateIncreaseStep
case 'j':
rateChanger <- rateDecreaseStep
}
}
case term.EventError:
log.Fatal(ev.Err)
}
}
}
func ticker(rate uint64, quit <-chan struct{}) (<-chan time.Time, chan<- int64) {
ticker := make(chan time.Time, 1)
rateChanger := make(chan int64, 1)
// start main workers
go func() {
desiredRate.Store(int64(rate))
tck := time.NewTicker(time.Duration(1e9 / rate))
for {
select {
case r := <-rateChanger:
tck.Stop()
if newRate := desiredRate.Add(r); newRate > 0 {
tck = time.NewTicker(time.Duration(1e9 / newRate))
} else {
desiredRate.Store(0)
}
case t := <-tck.C:
ticker <- t
case <-quit:
return
}
}
}()
return ticker, rateChanger
}
func getTimingsSlot(now time.Time) ([]counter, []counter) {
n := int(now.UnixNano() / 100000000)
slot := n % len(timingsOk)
return timingsOk[slot], timingsBad[slot]
}
func initializeTimingsBucket(buckets uint) {
timingsOk = make([][]counter, movingWindowsSize*screenRefreshFrequency)
for i := 0; i < len(timingsOk); i++ {
timingsOk[i] = make([]counter, buckets)
}
timingsBad = make([][]counter, movingWindowsSize*screenRefreshFrequency)
for i := 0; i < len(timingsBad); i++ {
timingsBad[i] = make([]counter, buckets)
}
go func() {
for now := range time.Tick(screenRefreshInterval) {
// TODO account for missing ticks
// clean next timing slot which is last one in ring buffer
next := now.Add(screenRefreshInterval)
tOk, tBad := getTimingsSlot(next)
for i := 0; i < len(tOk); i++ {
tOk[i].Store(0)
}
for i := 0; i < len(tBad); i++ {
tBad[i].Store(0)
}
}
}()
}
type arrayFlags []string
func (i *arrayFlags) String() string {
return fmt.Sprintf("+%v", *i)
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var headerFlags arrayFlags
func main() {
workers := flag.Uint("workers", 8, "Number of workers")
timeout := flag.Duration("timeout", 30*time.Second, "Requests timeout")
targets := flag.String("targets", "", "Targets file")
base64body := flag.Bool("base64body", false, "Bodies in targets file are base64-encoded")
rate := flag.Uint64("rate", 50, "Requests per second")
miY := flag.Duration("minY", 0, "min on Y axe (default 0ms)")
maY := flag.Duration("maxY", 100*time.Millisecond, "max on Y axe")
flag.Var(&headerFlags, "H", "HTTP header 'key: value' set on all requests. Repeat for more than one header.")
flag.Parse()
terminalWidth, _ = terminal.Width()
terminalHeight, _ = terminal.Height()
plotWidth = terminalWidth
plotHeight = terminalHeight - statsLines
if plotWidth <= reservedWidthSpace {
log.Fatal("not enough screen width, min 40 characters required")
}
if plotHeight <= reservedHeightSpace {
log.Fatal("not enough screen height, min 3 lines required")
}
minY, maxY = float64(*miY/time.Millisecond), float64(*maY/time.Millisecond)
deltaY := maxY - minY
buckets = plotHeight
logBase = math.Pow(deltaY, 1/float64(buckets-2))
startMs = minY + math.Pow(logBase, 0)
initializeTimingsBucket(buckets)
quit := make(chan struct{}, 1)
ticker, rateChanger := ticker(*rate, quit)
trgt, err := newTargeter(*targets, *base64body)
if err != nil {
log.Fatal(err)
}
if len(headerFlags) > 0 {
headers := strings.Join(headerFlags, "\r\n")
headers += "\r\n\r\n" // Need an extra \r\n at the end
tp := textproto.NewReader(bufio.NewReader(strings.NewReader(headers))) // Never change, Go
mimeHeader, err := tp.ReadMIMEHeader()
if err != nil {
log.Fatal(err)
}
trgt.header = http.Header(mimeHeader)
}
// start attackers
var wg sync.WaitGroup
for i := uint(0); i < *workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
attack(trgt, *timeout, ticker, quit)
}()
}
// start reporter
wg.Add(1)
go func() {
defer wg.Done()
reporter(quit)
}()
keyPressListener(rateChanger)
// bye
close(quit)
wg.Wait()
}