30.3. 增加7 Layer防火墙

本文涉及的产品
云防火墙,500元 1000GB
简介:

上面仅仅对于方法控制权限,接下来我们为程序增加7层防火墙功能

		
<?php
/* 
* =====================================
* Website: http://netkiller.github.com
* Author: neo <netkiller@msn.com>
* Email: netkiller@msn.com
* =====================================
*/
class Firewall{

	protected $status;
	protected $policy;
	protected $chain;
	protected $rule;
	protected $match;
	private $debug;
	//$get,$post,$cookie,$server;

	public function __construct() {
		$this->name 	= "Firewall";
	}

	public function __destruct() {
		//print "Destroying " . $this->name . "\n";
	}
	
	public function enable(){
		$this->status = true;
	}
	public function disable(){
		$this->status = false;
	}
	
	public function get(){
		if($this->status){
			$this->chain 	= $_GET;
			return($this);
		}else{
			return($this->status);
		}			
	}
	
	public function post(){
		if($this->status){
			$this->chain 	= $_GET;
			return($this);
		}else{
			return($this->status);
		}
		$this->chain 	= $_POST;
	}
	
	public function cookie() {
		if($this->status){
			$this->chain = $_COOKIE;
			return($this);
		}else{
			return($this->status);
		}
		
	}
	
	public function server(){
		if($this->status){
			$this->chain = $_SERVER;
			return($this);
		}else{
			return($this->status);
		}
	}
	
	public function match($key, $value){
		if($this->debug) print_r($this->chain);
		$this->match = false;
		if(!array_key_exists($this->chain, $key)){
			if($this->chain[$key] == $value){
				$this->match = true;	
			}
		}
		return($this);
	}
	public function policy($p){
		$this->policy = $p;
	}
	public function counter($tm, $cnt){
		return($this);
	}
	public function allow($fun = null){
		if($this->status && $this->match){
			if($fun){
				$fun();
			}
		}
		$this->destroy();
		return($this->status);
	}
	public function deny($fun = null){
		if($this->status && $this->match){
			if($fun){
				$fun();
			}
		}
		$this->destroy();
		return($this->status);
	}
	public function debug($tmp){
		$this->debug = $tmp;
	}
	public function ip($ipaddr){
		return $this->server()->match('REMOTE_ADDR', $ipaddr);
	}
	public function destroy(){
		$this->chain = array();
		$this->match = false;
	}
};

#include_once('firewall.php')
$fw = new Firewall();

$fw->debug(true);
$fw->debug(false);
$fw->enable();
//$fw->disable();
function test(){
	echo 'OK';
};
function allow(){
	echo 'allow';
};
function deny(){
	echo 'deny';
};
//$fw->policy('blacklist');

$fw->ip('192.168.3.17')->allow('allow');
$fw->ip('192.168.3.17')->deny('deny');

$fw->counter('1m',5)->match('id','1000')->deny('test');

/*
$fw->ip('172.16.0.0/24')->allow();
$fw->ip('172.16.0.0','255.255.255.0')->allow();

$fw->header(array('User-Agent' => 'MSIE5'))->deny()
*/
$fw->get()->match('id','1000')->deny('test');
$fw->get()->match('name','chen')->allow('test');
//$fw->get()->match(array('id' => '1000'))->deny();
/*
$fw->post()->data(array('action'=>'/login.php'))->allow()
$fw->cookie()->data(array('userid'=>'test'))->deny()
*/
$fw->server()->match('HTTP_REFERER', 'http://www.mydomain.com/index.html')->allow('test');
$fw->server()->match('REQUEST_METHOD', 'GET')->deny('test');

$fw->disable();
//$fw->destroy();
		
		

这里仅仅给你一个思路,我并没有写完程序。例如控制IP请求次数可以如下实现,请自行改善程序

		
<?php
/* 
* =====================================
* Website: http://netkiller.github.com
* Author: neo <netkiller@msn.com>
* Email: netkiller@msn.com
* =====================================
*/
require 'SharedConfigurations.php';

$single_server = array(
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'database' => 0
);

$multiple_servers = array(
    array(
       'host'     => '127.0.0.1',
       'port'     => 6379,
       'database' => 15,
       'alias'    => 'first',
    ),
    array(
       'host'     => '127.0.0.1',
       'port'     => 6380,
       'database' => 15,
       'alias'    => 'second',
    ),
);


$client = new Predis\Client($single_server, array('prefix' => 'fw:'));

$key=$_SERVER['REMOTE_ADDR'];

if(!$client->exists($key)){
	$client->setex($key, 20, 1);
}else{
	$client->incrby($key,1);
}

$counter = $client->get($key);

if($counter > 10){
	echo 'Deny';
}

print_r($client->get($key));

//var_dump($client->keys('*'));		
		
		





原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
Layer 开启与关闭加载层
Layer 开启与关闭加载层
104 0
|
安全 网络安全
H3C防火墙基础配置操作
H3C防火墙基础配置操作
H3C防火墙基础配置操作
|
域名解析 网络协议 网络安全
实验:iptables防火墙+DNS分离解析
实验考察知识点: iptables防火墙的规则配置 DNS分离解析的配置
465 0
|
网络协议 网络安全
防火墙nat豁免与控制原理讲解
防火墙nat豁免与控制原理讲解
516 0
|
安全 网络协议 数据可视化
防火墙原理与定义
1)官方定义:防火墙,用于保护一个网络免受另一个网络的攻击和入侵行为,其本质是控制 2)网络安全市场,存在一种设备名为ISR,全称叫集成多业务路由器Router 3)现在市面上的防火墙,主要以以下几种形态存在 : ①硬件防火墙(独立) ②防火墙模块(能够集成到支持的设备中) ③虚拟化防火墙,(基于软件层面实现的一种应用) 4)为何虚拟防火墙只是一个趋势,没有成为主流? ①虚拟防火墙性能和稳定性上的限制,目前硬件防火墙在这两点是优于虚拟防火墙的
395 0
防火墙原理与定义
|
Linux 网络安全
iptables防火墙工作原理及简单配置访问策略
iptables只是管理包过滤规则的工具,可以添加或删除包过滤的规则,真正执行包过滤规则的是netfilter netfilter是Linux核心中的一个通用架构,内部提供一些列的表,每个表由若干条链组成,每条链由一条或多条规则组成,也就是我们常说的四表五链 四表
193 0
|
网络协议 Serverless C语言
如何在函数计算中实现无入侵全局网络代理
## 什么场合需要代理? 假设您有一台物理服务器部署在家里,你需要在函数计算中访问这台物理服务器。最简单的办法是直接暴露这个物理服务器到公网环境。那么问题来了,如果直接暴露到公网不设置防火墙,那么任何人都可以直接访问你的机器,这样会有很大风险。
2483 0
|
网络协议 安全 Unix