-
Notifications
You must be signed in to change notification settings - Fork 3
/
flooder.py
507 lines (408 loc) · 18.4 KB
/
flooder.py
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
#!/usr/bin/env python3
import argparse
import json
import logging
import os
import random
import signal
import sys
import threading
import time
from decimal import Decimal
import subprocess
import requests
class Flooder:
threads = 10
requests = 10
pid = 0
cpu = []
mem = []
shuffle = True
log = True
json = False
wait = 30
help = "supported methods: GET, POST, PUT, DELETE\n" \
"\n" \
"config example:\n" \
" [\n" \
" {\n" \
" \"url\": \"http://localhost:8000/api/url\",\n" \
" \"type\": \"POST\",\n" \
" \"params\": [\n" \
" {\n" \
" \"name\": \"monkey\",\n" \
" \"value\": \"nuts\"\n" \
" },\n" \
" ...\n" \
" ],\n" \
" \"files\": [\n" \
" {\n" \
" \"name\": \"weasel\",\n" \
" \"value\": \"/users/dragon/archive.zip\"\n" \
" },\n" \
" ...\n" \
" ]\n" \
" },\n" \
" ...\n" \
" ]\n"
_version = "1.10"
_start = 0
_stop = 0
_total = 0
thread_list = []
def __init__(self):
self._config()
self.json = json.load(self.json)
if self.log:
logformat = '%(asctime)-26s %(threadName)-12s %(message)s'
logging.basicConfig(filename=time.strftime("flooder_%d-%m-%Y_%H-%M-%S.log"), format=logformat, level=logging.DEBUG)
logging.getLogger("requests").setLevel(logging.WARNING)
if self._validate_list():
self._start = time.time()
print("[" + format_seconds(int(time.time())) + "] Flood gates opening..")
for c in range(0, self.threads):
t = Thread(args=(self.json, self.requests, self.shuffle, self.log, self.wait,))
self.thread_list.append(t)
t.daemon = False
t.start()
print("[" + format_seconds(int(time.time())) + "] Flood gates open.")
print("[" + format_seconds(int(time.time())) + "] Flooding with " + str(
self.requests * self.threads) + " requests via " + str(self.threads) + " threads (" + str(
self.requests) + " per thread)")
if self.pid > 0 and check_pid(self.pid):
print("[" + format_seconds(int(time.time())) + "] Monitoring PID " + str(self.pid))
else:
self.pid = 0
if self.pid > 0:
time.sleep(0.2)
usage = get_cpumem(self.pid)
self.cpu.append(usage[0])
self.mem.append(usage[1])
wait = True
loop = 0
while wait:
wait = False
time.sleep(0.1)
loop += 1
if loop == 300:
total = 0
for t in self.thread_list:
total += t.count
if self.pid > 0:
usage = get_cpumem(self.pid)
self.cpu.append(usage[0])
self.mem.append(usage[1])
print("[" + format_seconds(int(time.time())) + "] PID " + str(self.pid) + " usage: CPU: " + str(usage[0]) + "% MEM: " + str(usage[1]) + "M")
print("[" + format_seconds(int(time.time())) + "] Progress: " + str(total) + " requests of " + str(self.threads * self.requests))
loop = 0
for t in self.thread_list:
if t.is_alive():
wait = True
if self.pid > 0:
usage = get_cpumem(self.pid)
self.cpu.append(usage[0])
self.mem.append(usage[1])
self._stop = time.time()
self._total = self._stop - self._start
self._report()
return
def _config(self):
parser = argparse.ArgumentParser(prog="Flooder", description="HTTP flooding tool " + self._version,
epilog=self.help, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + self._version)
parser.add_argument('-j', '--json', required=True, type=argparse.FileType('r'), help='requests list (required, json)')
parser.add_argument('-t', '--threads', default=10, type=int, help='number of parallel threads to use (default: 10)')
parser.add_argument('-w', '--wait', default=30, type=int, help='request timeout in seconds (default: 30)')
parser.add_argument('-r', '--requests', default=10, type=int, help='number of requests / thread (default: 10)')
parser.add_argument('-p', '--pid', default=0, type=int, help='PID for usage monitoring (handy when flooding local services)')
parser.add_argument('-ns', '--no-shuffle', default=False, action='store_true', help='disable shuffling of requests list')
parser.add_argument('-nl', '--no-log', default=False, action='store_true', help='disable logging to file')
args = parser.parse_args()
self.json = args.json
self.threads = args.threads
self.requests = args.requests
self.wait = args.wait
self.pid = args.pid
self.shuffle = False
self.log = False
return
def _validate_list(self):
try:
if type(self.json) is not list:
raise Exception("List is not a list")
if len(self.json) == 0:
raise Exception("List is empty")
i = 0
for r in self.json:
if type(r) is not dict:
raise Exception("List entry [" + str(i) + "] is not a dict")
if 'url' not in r:
raise Exception("List entry [" + str(i) + "] has no 'url' property")
if type(r['url']) not in [str]:
raise Exception("List entry [" + str(i) + "] 'url' property is not a string")
if 'type' not in r:
raise Exception("List entry [" + str(i) + "] has no 'type' property")
if type(r['type']) not in [str]:
raise Exception("List entry [" + str(i) + "] 'type' property is not a string")
if r['type'].lower() not in ['get', 'post', 'put', 'delete']:
raise Exception("List entry [" + str(i) + "] 'type' property should be: get, post, put, delete")
if 'params' in r:
if type(r['params']) is not list:
raise Exception("List entry [" + str(i) + "] 'params' property is not a list")
j = 0
for e in r['params']:
if type(e) is not dict:
raise Exception("List entry [" + str(i) + "] 'params' entry [" + str(j) + "] is not a dict")
if 'name' not in e:
raise Exception("List entry [" + str(i) + "] 'params' entry [" + str(j) + "]has no 'name' property")
if type(e['name']) not in [str]:
raise Exception("List entry [" + str(i) + "] 'params' entry [" + str(j) + "] 'name' property is not a string")
if 'value' not in e:
raise Exception("List entry [" + str(i) + "] 'params' entry [" + str(j) + "] has no 'value' property")
if type(e['value']) not in [str]:
raise Exception("List entry [" + str(i) + "] 'params' entry [" + str(j) + "] 'value' property is not a string")
j += 1
i += 1
return True
except Exception as e:
print(e.args[0])
return False
def _report(self):
self.requests_total = 0
self.requests_successful = 0
self.requests_error = 0
self.requests_failed = 0
self.time_total = 0
self.time_successful = 0
self.time_error = 0
self.time_failed = 0
self.successful_average = 0
self.successful_fastest = 0
self.successful_slowest = 0
self.error_average = 0
self.error_fastest = 0
self.error_slowest = 0
self.failed_average = 0
self.failed_fastest = 0
self.failed_slowest = 0
self.cpu_sum = 0
self.cpu_average = 0
self.cpu_ceil = 0
self.cpu_floor = 0
self.mem_sum = 0
self.mem_average = 0
self.mem_ceil = 0
self.mem_floor = 0
self.errors = []
for t in self.thread_list:
self.requests_total += len(t.results)
for r in t.results:
self.time_total += r['time']
if r['status'] == 200:
self.requests_successful += 1
self.time_successful += r['time']
if self.successful_fastest == 0:
self.successful_fastest = r['time']
if self.successful_fastest > r['time']:
self.successful_fastest = r['time']
if self.successful_slowest < r['time']:
self.successful_slowest = r['time']
elif r['status'] > 0:
self.requests_error += 1
self.time_error += r['time']
if self.error_fastest == 0:
self.error_fastest = r['time']
if self.error_fastest > r['time']:
self.error_fastest = r['time']
if self.error_slowest < r['time']:
self.error_slowest = r['time']
elif r['status'] == 0:
self.requests_failed += 1
self.time_failed += r['time']
if self.failed_fastest == 0:
self.failed_fastest = r['time']
if self.failed_fastest > r['time']:
self.failed_fastest = r['time']
if self.failed_slowest < r['time']:
self.failed_slowest = r['time']
for error in t.errors:
if error not in self.errors:
self.errors.append(error)
if self.pid > 0:
for p in self.cpu:
self.cpu_sum += p
if self.cpu_floor == 0:
self.cpu_floor = p
if self.cpu_floor > p:
self.cpu_floor = p
if self.cpu_ceil < p:
self.cpu_ceil = p
for v in self.mem:
self.mem_sum += Decimal(str(v))
if self.mem_floor == 0:
self.mem_floor = v
if self.mem_floor > v:
self.mem_floor = v
if self.mem_ceil < v:
self.mem_ceil = v
self.cpu_ceil = round(self.cpu_ceil, 1)
self.cpu_floor = round(self.cpu_floor, 1)
self.cpu_average = round(self.cpu_sum / len(self.cpu), 1)
self.mem_ceil = round(self.mem_ceil, 1)
self.mem_floor = round(self.mem_floor, 1)
self.mem_average = round(self.mem_sum / len(self.mem), 1)
if self.requests_successful > 0:
self.successful_fastest = '%.06f' % float(self.successful_fastest)
self.successful_slowest = '%.06f' % float(self.successful_slowest)
self.successful_average = '%.06f' % float(self.time_successful / (self.requests_successful * self.threads))
if self.requests_error > 0:
self.error_fastest = '%.06f' % float(self.error_fastest)
self.error_slowest = '%.06f' % float(self.error_slowest)
self.error_average = '%.06f' % float(self.time_error / (self.requests_error * self.threads))
if self.requests_failed > 0:
self.failed_fastest = '%.06f' % float(self.failed_fastest)
self.failed_slowest = '%.06f' % float(self.failed_slowest)
self.failed_average = '%.06f' % float(self.time_failed / (self.requests_failed * self.threads))
self._out()
return
def _out(self):
sep = "-" * 35
out = [
sep,
"Threads: " + str(self.threads).rjust(13),
"Requests: " + str(self.requests).rjust(13), sep,
"Requests total: " + str(self.requests_total).rjust(13),
"Requests successful: " + str(self.requests_successful).rjust(13)
]
if self.requests_error > 0:
out.append("Requests error: " + str(self.requests_error).rjust(13))
if self.requests_failed > 0:
out.append("Requests failed: " + str(self.requests_failed).rjust(13))
if self.requests_successful > 0:
out.append(sep)
out.append("Successful fastest: " + str(self.successful_fastest).rjust(13))
out.append("Successful slowest: " + str(self.successful_slowest).rjust(13))
out.append("Successful average: " + str(self.successful_average).rjust(13))
if self.requests_error > 0:
out.append(sep)
out.append("Error fastest: " + str(self.error_fastest).rjust(13))
out.append("Error slowest: " + str(self.error_slowest).rjust(13))
out.append("Error average: " + str(self.error_average).rjust(13))
if self.requests_failed > 0:
out.append(sep)
out.append("Failed fastest: " + str(self.failed_fastest).rjust(13))
out.append("Failed slowest: " + str(self.failed_slowest).rjust(13))
out.append("Failed average: " + str(self.failed_average).rjust(13))
if self.pid > 0:
out.append(sep)
out.append("CPU: " + str(self.cpu_ceil).rjust(6) + "% " + str(self.cpu_average).rjust(6) + "% " + str(self.cpu_floor).rjust(6) + "%")
out.append("MEM: " + str(self.mem_ceil).rjust(6) + "M " + str(self.mem_average).rjust(6) + "M " + str(self.mem_floor).rjust(6) + "M")
out.append(sep)
out.append("Execution time: " + format_seconds(self._total).rjust(13))
out.append("Time total: " + format_seconds(self.time_total).rjust(13))
out.append("Time successful: " + format_seconds(self.time_successful).rjust(13))
if self.requests_error > 0:
out.append("Time error: " + format_seconds(self.time_error).rjust(13))
if self.requests_failed > 0:
out.append("Time failed: " + format_seconds(self.time_failed).rjust(13))
if len(self.errors) > 0:
out.append(sep)
for error in self.errors:
out.append(error)
for line in out:
print(line)
if self.log:
logging.info(line)
return
class Thread(threading.Thread):
def __init__(self, group=None, target=None, name=None, args=()):
threading.Thread.__init__(self, group=group, target=target, name=name)
args = list(args)
self.stop = False
self._list = args[0]
self._requests = args[1]
self._shuffle = args[2]
self._log = args[3]
self._timeout = args[4]
self.results = []
self.errors = []
self.count = 0
if self._shuffle:
random.shuffle(self._list)
return
@staticmethod
def time():
return int(round(time.time() * 1000))
def run(self):
i = 0
while i < self._requests:
for e in self._list:
if self.stop:
return
self.count += 1
payload = {}
if 'params' in e:
for p in e['params']:
payload.update({p['name']: p['value']})
files = {}
if 'files' in e:
for f in e['files']:
path, name = os.path.split(f['value'])
try:
files[f['name']] = (name, open(f['value'], 'rb'))
except IOError:
if self._log:
logging.error("Unable to read file: " + f['value'])
microseconds = self.time()
req = None
try:
if e['type'] == "get":
req = requests.get(e['url'], timeout=self._timeout)
elif e['type'] == "delete":
req = requests.delete(e['url'], timeout=self._timeout)
elif e['type'] == "put":
req = requests.put(e['url'], data=payload, files=files, timeout=self._timeout)
else:
req = requests.post(e['url'], data=payload, files=files, timeout=self._timeout)
res = {'url': e['url'], 'data': payload, 'status': req.status_code,
'time': (Decimal(req.elapsed.microseconds) / 1000 / 1000), 'content': req.text}
if self._log:
logging.debug(res)
except Exception as ex:
elapsed = Decimal(str(self.time() - microseconds)) / 1000
if req is not None:
elapsed = Decimal(str(req.elapsed.microseconds)) / 1000 / 1000
res = {'url': e['url'], 'data': payload, 'status': 0, 'time': elapsed,
'error': str(ex)}
if self._log:
logging.error(res)
self.results.append(res)
i += 1
if i == self._requests:
return
return
def stop(self):
self.stop = True
def format_seconds(val):
return time.strftime("%H:%M:%S", time.gmtime(float(str(val))))
def check_pid(pid):
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
def get_cpumem(pid):
us = list(subprocess.getoutput("ps -p " + str(pid) + " -o pcpu= -o rss=").split())
us[0] = round(Decimal(str(us[0])), 1)
us[1] = round(Decimal(str(us[1])) / 1024, 1)
return us
def stop_app():
for t in Flooder.thread_list:
t.stop()
print("")
sys.exit()
signal.signal(signal.SIGINT, stop_app)
signal.signal(signal.SIGTERM, stop_app)
if __name__ == '__main__':
Flooder()