I have never really been aware of this before, but we have a client who has told us our forms are vunerable to attacks. So we have to change them. I found a function which allows us to fix this problem, but we are unsure how to implement it into our code. Here is our script:
PHP Code:
<?
//# Include the connections script to make a database connection.
include("inc/connect.inc");
//# The form should post to itself.
if ( $_POST['submit'] ) {
$valid = 1;
//# The fields all follow this patern.
//# If you do not require an error check for a field then just use the
//# post field method and not the error check method
$antispambox = $_POST['antispambox'];
if ($antispambox == '73634') {}
else
{
$valid = 0;
$antispambox_error = '<b><font face="Tahoma" color="#FF0000" size=4><span class="style57">Please Enter the Numbers as shown in bold</span></font></b>';
}
// End of error checking.
if ( $valid == 1 )
{
// In testing, if you get an Bad referer error
// comment out or remove the next three lines
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="This is the information received from the Spectroscopy Enquiry Form.:\n\n";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $key\n";
foreach($val as $v){
$v = stripslashes($v);
$msg.="***$v\n";
}
} else {
$val = stripslashes($val);
$msg.="$key: $val\n";
}
}
$recipient="*****";
$subject="Enquiry Form";
error_reporting(0);
if (mail($recipient, $subject, $msg)){
echo nl2br($input);
} else
echo "An error occurred and the message could not be sent.";
header("Location: thanks.php");
exit;
}
}
?>
And here is the function. Now can someone tell me if we have to run this function on every field in the form???
PHP Code:
function RemoveXSS($val) {
// remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
// this prevents some character re-spacing such as <java\0script>
// note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
$val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val);
// straight replacements, the user should never need these since they're normal characters
// this prevents like <IMG SRC=@avascript:alert('XSS')>
$search = 'abcdefghijklmnopqrstuvwxyz';
$search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$search .= '1234567890!@#$%^&*()';
$search .= '~`";:?+/={}[]-_|\'\\';
for ($i = 0; $i < strlen($search); $i++) {
// ;? matches the ;, which is optional
// 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
// @ @ search for the hex values
$val = preg_replace('/(&#[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
// @ @ 0{0,7} matches '0' zero to seven times
$val = preg_replace('/(�{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
}
Bookmarks