联系表格输入过滤

时间:2012-10-22 00:34:09

标签: php forms xss contact

通过我的联系表单查看我发现了跨站点脚本漏洞,但对如何修复此漏洞知之甚少。

受影响的参数:名称 使用的矢量:“> alert(document.cookie) 找到模式:\“> alert(document.cookie) 完全攻击:/ http://###################contact.php [name = \“> alert(document.cookie)& email =& message =]

受影响的参数:电子邮件 使用的矢量:“> alert(document.cookie) 找到模式:\“> alert(document.cookie) 完全攻击:/ http://###################contact.php [name =& email = \“> alert(document.cookie)& message =]

受影响的参数:消息 使用的矢量:“> alert(document.cookie) 找到模式:\“> alert(document.cookie) 完全攻击:/ http://###################contact.php [name =& email =& message = \“> alert(document.cookie) )]

我当前的表单代码如下所示

<?php 
if (isset($_POST['submit'])) { 
    $error = ""; 

    if (!empty($_POST['name'])) { 
        $name = $_POST['name']; 
        if (!preg_match('/^[a-zA-Z0-9]+$/i', $name)){  
            $error .= "The name you entered is not valid. <br/>"; 
        } 
    } else { 
        $error .= "You didn't type in your name. <br />"; 
    } 

    if (!empty($_POST['email'])) { 
        $email = $_POST['email']; 
        if (!preg_match("/^[_a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){  
            $error .= "The e-mail address you entered is not valid. <br/>"; 
        } 
    } else { 
        $error .= "You didn't type in an e-mail address. <br />"; 
    } 

    if (!empty($_POST['messagesubject'])) { 
        $messagesubject = $_POST['messagesubject']; 
    } else { 
        $error .= "You didn't type in a subject. <br />"; 
    } 

    if (!empty($_POST['message'])) { 
        $message = $_POST['message']; 
    } else { 
        $error .= "You didn't type in a message. <br />"; 
    } 

    if(($_POST['code']) == $_SESSION['code']) {  
        $code = $_POST['code']; 
    } else {  
        $error .= "The captcha code you entered does not match. Please try again. <br />";     
    } 

    if (empty($error)) { 
        $from = 'From: ' . $name . ' <' . $email . '>'; 
        $to = "aaron@fosternetwork.co.uk"; 
        $subject = strip_tags("CWS - Contact Us - \n" . $messagesubject); 
        $content = strip_tags("Name: " . $name . "\nSubject: " . $messagesubject . "\nMessage: \n" . $message); 
        $success = "<h2>Thank you! <span>Your message has been sent!</span></h2>"; 
        mail($to,$subject,$content,$from); 
        $emailSent = true; 
    } 
} 
?> 

表格

<form action="contact.php" method="post"> 

    <label>Name:</label> 
    <input type="text" name="name" value="<?php if ($_POST['name']) { echo $_POST['name']; } ?>" /> 

    <label>Email:</label> 
    <input type="text" name="email" value="<?php if ($_POST['email']) { echo $_POST['email']; } ?>" /> 

    <label>Subject:</label> 
    <input type="text" name="messagesubject" value="<?php if ($_POST['messagesubject']) { echo $_POST['messagesubject']; } ?>" /> 

    <label>Message:</label><br /> 
    <textarea name="message" rows="20" cols="20"><?php if ($_POST['message']) { echo $_POST['message']; } ?></textarea> 

    <label>Please input the following code: <img src="captcha.php"></label> 
    <input type="text" name="code"> <br />  

    <button type="submit" class="colorButton" name="submit" value="Send message">Send Message</button> 

</form>

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

你的HTML生成有一个缺陷:

 value="<?php if ($_POST['name']) { echo $_POST['name']; } ?>"

您应该始终转义输出变量,即:

 value="<?php if ($_POST['name']) { echo htmlspecialchars($_POST['name']); } ?>"

另请参阅:htmlspecialchars()htmlentities()

答案 1 :(得分:0)

永远不信任用户输入,有些人可能会插入可能损害您的系统或类似内容的javascript代码,这就是为什么您容易受到跨站脚本攻击,清理使用此功能发布的输入:

function xss_protect($data, $strip_tags = false, $allowed_tags = "\"\'") { 
if($strip_tags) {
    $data = strip_tags($data, $allowed_tags . "<b>");
}

if(stripos($data, "script") !== false) { 
    $result = str_replace("script","scr<b></b>ipt", htmlentities($data, ENT_QUOTES,"UTF-8")); 
} else { 
    $result = htmlentities($data, ENT_QUOTES, "UTF-8"); 
} 

return $result;
}

使用该功能,例如:

$code = xss_protect($_POST['code']); 

答案 2 :(得分:0)

您应该使用一些好的编程实践,例如:

初始化你的变量,就这样,你总是有东西可以打印,调试你会得到很多简单但却很烦人的错误

$name = false;
$email = false;
$message = false;
$messagesubject = false;

如果要获取新值,请修改数据,例如,根据您的情况从$ _POST获取。如果你这样做,如果没有特定字段的数据,你的脚本就会避免一些工作,而且你可以更容易地控制脚本流和数据验证。

if ( isset( $_POST['name'] ) !== false ) {
    // validate the content
    $name = process_you_choose_to_clean( $_POST['name'] );
}
if ( isset( $_POST['email'] ) !== false ) {
    // validate the content
    $name = process_you_choose_to_clean( $_POST['email'] );
}
if ( isset( $_POST['message'] ) !== false ) {
    // validate the content
    $name = process_you_choose_to_clean( $_POST['message'] );
}
if ( isset( $_POST['messagesubject'] ) !== false ) {
    // validate the content
    $name = process_you_choose_to_clean( $_POST['messagesubject'] );
}

尽量避免在你的html部分中出现混乱的代码,如果你照顾我之前提到的事情,这样的工作就完美了

<input type="text" name="name" value="<?php { echo( $name ); } ?>" />

关于sanitazing,请考虑提到的选项,并使用PHP的filter_var选项