-
Notifications
You must be signed in to change notification settings - Fork 2
/
RaxWaf.php
270 lines (227 loc) · 7.03 KB
/
RaxWaf.php
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
<?php
namespace rax;
/**
* 根据宝塔规则改写的php防火墙,后期更新规则请进群940586873
*
* @Author : 9rax.dev@gmail.com
* @DateTime: 2020/7/2 15:26
* @Notice : 九锐网(9rax.com)旗下的开源作品。
*/
class RaxWaf
{
static $config = [
//开启防火墙
'enable' => true,
//开启记录
'log' => true,
//日志目录
'log_path' => __DIR__ . DIRECTORY_SEPARATOR . 'waf_log' . DIRECTORY_SEPARATOR,
//规则目录
'rule_path' => __DIR__ . DIRECTORY_SEPARATOR . 'waf_rule' . DIRECTORY_SEPARATOR,
//拦截消息
'deny_message' => 'php waf hit ! ',
//注入多少次后屏蔽
'deny_num' => 3,
];
static $is_cli;
static $rules;
static $instance;
/**
* @var callable
*/
static $handle;
private static $deny_ips = [];
static function init($config = [])
{
if (!self::$instance) {
$config && self::$config = array_merge(self::$config, $config);
if (!is_dir(self::$config['log_path'])) mkdir(self::$config['log_path'], 755, true);
self::$instance = new self();
self::$is_cli = PHP_SAPI === 'cli' ? true : false;
if (!is_file(self::$config['rule_path'] . 'deny_ips.json')) {
touch(self::$config['rule_path'] . 'deny_ips.json');
}
self::$deny_ips = json_decode(self::$config['rule_path'] . 'deny_ips.json', true) ? json_decode(self::$config['rule_path'] . 'deny_ips.json', true) : [];
}
}
static function getDenyIps()
{
if (!self::$instance) self::init();
return self::$deny_ips;
}
static function setDenyIps($ips = [])
{
self::$deny_ips = $ips;
}
function __construct()
{
self::$rules['args'] = self::parserRule('args.json');
self::$rules['post'] = self::parserRule('post.json');
self::$rules['url'] = self::parserRule('url.json');
}
/**
* 解析规则
*
* @param $json
*
* @Author : 9rax.dev@gmail.com
* @DateTime: 2020/7/5 10:34
*/
private static function parserRule($json, $mutil = true)
{
$res = [];
$content = json_decode(file_get_contents(self::$config['rule_path'] . $json));
foreach ($content as $k => $item) {
if (is_string($k)) {
$res[$k] = self::fixRegex($item);
} elseif (isset($item[3])) {
if ($item[0]) $res[$item[2]] = self::fixRegex($item[1]);
}
}
return $res;
}
/**
* saveDenyIps
*
* @param $ips
*
* @Author : 9rax.dev@gmail.com
* @DateTime: 2020/7/7 19:22
*/
static function saveDenyIps($ips)
{
$ips && is_array($ips) && file_put_contents(self::$config['rule_path'] . 'deny_ips.json', json_encode($ips, JSON_UNESCAPED_UNICODE));
}
/**
* 监听请求
*
* @param mixed $urlOrData
* @param array $data
* @param string $remark
*
* @return bool
* @Author : 9rax.dev@gmail.com
* @DateTime: 2020/7/5 13:29
*/
static function check($ip, $urlOrData, $data = [], $remark = '')
{
if (!self::$instance) {
self::init();
}
if (!self::$config['enable']) {
return false;
}
if (in_array($ip, self::$deny_ips)) {
if (!self::$handle && !self::$is_cli) {
die(self::$config['deny_message']);
}
return false;
}
$deny = false;
$data = is_array($urlOrData) ? $urlOrData : $data;
$url = '';
//url地址和参数过滤
if (is_string($urlOrData) && self::$rules['url']) {
$url = $urlOrData;
$rules = array_merge(self::$rules['url'], self::$rules['args']);
foreach ($rules as $name => $regex) {
//var_dump($name,"/{$regex}/i",$url,preg_match("/{$regex}/i",$url));
if (preg_match("/{$regex}/i", $url) || preg_match("/{$regex}/i", urldecode($url))) {
self::log($url, $ip, 'URL&GET HIT', $regex, $name, $data, $remark);
$deny = true;
break;
}
}
}
//var_dump($data);
if ($data) {
$remark = is_string($data) ? $data : $remark;
$rules = self::$rules['post'];
$values = '';
$type = 'DATA HIT';
if (is_array($data)) {
$values=self::arr2str($data);
foreach ($rules as $name => $regex) {
if (preg_match("/{$regex}/i", $values) || preg_match("/{$regex}/i", htmlspecialchars_decode($values))) {
self::log($url, $ip, $type, $regex, $name, $data, $remark);
$deny = true;
break;
}
}
}
}
if (!self::$handle && !self::$is_cli) {
die(self::$config['deny_message']);
} else if (is_callable(self::$handle)) {
call_user_func(self::$handle, $ip);
}
return $deny;
}
/**
* log
*
* @param $url
* @param $type
* @param $rule
* @param string $name
* @param array $data
* @param string $remark
*
* @Author : 9rax.dev@gmail.com
* @DateTime: 2020/7/5 13:33
*/
private static function log($url, $ip, $type, $rule, $name = '', $data = [], $remark = '')
{
$log = "TIME:" . date('Y-m-d H:i:s', time()) . ";TYPE:{$type};RULE:{$rule};IP:{$ip};";
$url && $log .= "URL:{$url};";
$name && $log .= "NAME:{$name};";
$remark && $log .= "REMARK:{$remark};";
$data && $log = $log . PHP_EOL . 'CHECK_DATA:' . json_encode($data, JSON_UNESCAPED_UNICODE);
$log .= PHP_EOL . PHP_EOL;
self::logOutput($log);
}
/**
* 日志记录到文件
*
* @param $str
*
* @Author : 9rax.dev@gmail.com
* @DateTime: 2020/7/5 13:33
*/
private static function logOutput($str)
{
//数据类型检测
$filename = self::$config['log_path'] . DIRECTORY_SEPARATOR . date("Y-m-d") . ".log";
file_put_contents($filename, $str, FILE_APPEND);
}
/**
* 修正正则表达式
*
* @param $regex
*
* @Author : 9rax.dev@gmail.com
* @DateTime: 2020/7/5 15:18
*/
private static function fixRegex($regex)
{
$regex = preg_replace_callback('/([\\\\]?\/)/', function ($item) {
return $item[1] === '/' ? '\/' : '\/';
}, $regex);
return $regex;
}
private static function arr2str($data){
$result='';
if(is_array($data)){
foreach ($data as $k=>$v){
if(is_array($v)){
$result.=self::arr2str($v);
}else{
$result.=$k.'='.$v.' ';
}
}
}else{
$result.=$data;
}
return $result;
}
}