-
Notifications
You must be signed in to change notification settings - Fork 0
/
Csrf.php
113 lines (97 loc) · 3.66 KB
/
Csrf.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
<?php
class Csrf
{
// Tên kiểm tra token
private $_csrf_token_name = 'cms-token-name';
// Thời gian sống của session, 3600 = 1h
private $_csrf_time_live = 3600;
private $_csrf_value = '';
// Hàm khởi tạo, có hai tham số
// - $use_token : nếu true thì có sử dụng validate token
// - $token_post: nếu true thì nếu method = post thì sẽ validate token trong form
// - $token_get : nếu true thì nếu method = get thì sẽ validate token trên url
function __construct($use_token = true, $token_post = false, $token_get = false)
{
// Nếu không muốn sử dụng token thì dừng
if (!$use_token){
return;
}
// Tạo CSRF Token
$this->__create_csrf_token();
// Nếu có validate cho phương thức POST
if ($token_post && !$this->__validate_post()){
die ('Token sai');
}
// Nếu có validate cho phương thức GET
if ($token_get && !$this->__validate_get()){
die ('Token sai');
}
}
// Mỗi người dùng sẽ có một mã token riêng biệt,
// nên trong hàm này ta sẽ tạo một quy luật riêng để tạo token nhé
private function __create_csrf_token()
{
// Khởi tạo token name
$this->_csrf_token_name = 'token'.md5($_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT'].'@#$%^+&*(-)');
// Nếu token chưa dược khởi tạo thì khởi tạo
if (!isset($_COOKIE[$this->_csrf_token_name]))
{
// Tạo token
$token = md5($_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT']);
// Lưu token trong 1h
setcookie($this->_csrf_token_name, $token, time() + $this->_csrf_time_live);
}
else{
$token = $_COOKIE[$this->_csrf_token_name];
setcookie($this->_csrf_token_name, $token, time() + $this->_csrf_time_live);
}
$this->_csrf_value = $token;
}
// Kiểm tra phương thức POST
private function __validate_post()
{
// Kiểm tra nếu phương thức hiện tại là POST
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// Kiểm tra có tồn tại token không
if (!isset($_POST[$this->_csrf_token_name]) || !isset($_COOKIE[$this->_csrf_token_name])){
return false;
}
// Nếu tokeon không phù hợp
else if ($_POST[$this->_csrf_token_name] != $_COOKIE[$this->_csrf_token_name]){
return false;
}
}
return true;
}
// Kiểm tra phương thức GET
private function __validate_get()
{
// Kiểm tra nếu phương thức hiện tại là POST
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
// Kiểm tra có tồn tại token không
if (!isset($_GET[$this->_csrf_token_name]) || !isset($_COOKIE[$this->_csrf_token_name])){
return false;
}
// Nếu tokeon không phù hợp
else if ($_GET[$this->_csrf_token_name] != $_COOKIE[$this->_csrf_token_name]){
return false;
}
}
return true;
}
// Lấy token name
function get_token_name(){
return $this->_csrf_token_name;
}
// Lấy token value
function get_token_value(){
return $this->_csrf_value;
}
// Tạo link có token
function create_link($url){
return $url.'?'.$this->_csrf_token_name.'='.$this->_csrf_value;
}
}
?>