-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.php
More file actions
199 lines (163 loc) · 5.81 KB
/
security.php
File metadata and controls
199 lines (163 loc) · 5.81 KB
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
<?php
/*
.---------------------------------------------------------------------------.
| Version: 4.0 RC1 |
| Author: Hany alsamman (project administrator && original founder) |
| Copyright (c) 2013, CODEX.COM. All Rights Reserved. |
| ------------------------------------------------------------------------- |
| License: DISTRIBUTED UNDER CODEX.COM TEAM (PRIVETE ACCESS ONLY) |
'---------------------------------------------------------------------------'
*/
/**
* PROPERTY_CPANEL.php
*
* @author Hany alsamman (Original founder <hany.alsamman@gmail.com>)
* @copyright CODEXC.COM
* @version $Id$
* @pattern private
* @access private
*/
class SECURITY{
var $get_magic_quotes = 0;
var $allow_unicode = 1;
/*-------------------------------------------------------------------------*/
// txt_stripslashes
// ------------------
// Make Big5 safe - only strip if not already...
/*-------------------------------------------------------------------------*/
/**
* SECURITY::txt_stripslashes()
*
* @param mixed $t
* @return
*/
function txt_stripslashes($t)
{
if ( $this->get_magic_quotes )
{
$t = stripslashes($t);
$t = preg_replace( "/\\\(?!&#|\?#)/", "\", $t );
}
return $t;
}
/**
* SECURITY::parse_clean_value()
* Performs basic cleaning
* Null characters, etc
* @param mixed $val
* @return
*/
function parse_clean_value($val)
{
if ( $val == "" )
{
return "";
}
//$sBadChars = array("select", "drop", ";", "--", "insert", "delete", "xp_","#", "%", "&", "'", "(", ")", "/",":", ";", "<", ">", "=", "[", "]", "?", "`", "|");
$val = preg_replace( "/select/i", "", $val );
$val = preg_replace( "/insert/i", "", $val );
$val = preg_replace( "/drop/i", "", $val );
$val = preg_replace( "/delete/i", "", $val );
$val = str_replace( ";", "", $val);
$val = str_replace( " ", " ", $this->txt_stripslashes($val) );
// As cool as this entity is...
$val = str_replace( "‮" , '' , $val );
$val = str_replace( "&" , "&" , $val );
$val = str_replace( '"' , """ , $val );
$val = str_replace( "\n" , "<br />" , $val ); // Convert literal newlines
$val = str_replace( "$" , "$" , $val );
$val = str_replace( "\r" , "" , $val ); // Remove literal carriage returns
$val = str_replace( "!" , "!" , $val );
$val = str_replace( "'" , "'" , $val ); // IMPORTANT: It helps to increase sql query safety.
// Ensure unicode chars are OK
if ( $this->allow_unicode )
{
$val = preg_replace("/&#([0-9]+);/s", "&#\\1;", $val );
//-----------------------------------------
// Try and fix up HTML entities with missing ;
//-----------------------------------------
$val = preg_replace( "/&#(\d+?)([^\d;])/i", "&#\\1;\\2", $val );
}
return $val;
}
/*-------------------------------------------------------------------------*/
// Key Cleaner - ensures no funny business with form elements
/*-------------------------------------------------------------------------*/
/**
* SECURITY::parse_clean_key()
*
* @param mixed $key
* @return
*/
function parse_clean_key($key)
{
if ($key == "")
{
return "";
}
$key = htmlspecialchars(urldecode($key));
$key = str_replace( ".." , "" , $key );
$key = preg_replace( "/\_\_(.+?)\_\_/" , "" , $key );
$key = preg_replace( "/^([\w\.\-\_]+)$/", "$1", $key );
return $key;
}
/**
* SECURITY::clean_globals()
*
* @param mixed $data
* @param integer $iteration
* @return
*/
function clean_globals( &$data, $iteration = 0 )
{
// Crafty hacker could send something like &foo[][][][][][]....to kill Apache process
// We should never have an globals array deeper than 10..
if( $iteration >= 10 )
{
return $data;
}
if( count( $data ) )
{
foreach( $data as $k => $v )
{
if ( is_array( $v ) )
{
$this->clean_globals( $data[ $k ], $iteration+1 );
}
else
{
# Null byte characters
$v = preg_replace( '/\\\0/' , '\0', $v );
$v = preg_replace( '/\\x00/', '\x00', $v );
$v = str_replace( '%00' , '%00', $v );
# File traversal
$v = str_replace( '../' , '../', $v );
$k = $this->parse_clean_key( $k );
$v = $this->parse_clean_value( $v );
$data[ $k ] = $v;
}
}
}
}
/**
* SECURITY::parse_incoming()
*
* @return
*/
function parse_incoming()
{
//-----------------------------------------
// Attempt to switch off magic quotes
//-----------------------------------------
//@set_magic_quotes_runtime(0);
//$this->get_magic_quotes = @get_magic_quotes_gpc();
//-----------------------------------------
// Clean globals, first.
//-----------------------------------------
$this->clean_globals( $_GET );
$this->clean_globals( $_POST );
$this->clean_globals( $_COOKIE );
$this->clean_globals( $_REQUEST );
}
}
?>