简体   繁体   English

有关XSS攻击和SQL注入的问题

[英]Issue about XSS Attack and SQL Injection

I'm new to web security .After spending time reading some blogs and community sites like SO ,I have found some techniques to be safe from XSS Attack and SQL Injection .But the problem is,most of that security related questions are very old.So,my question is 我是web security新手。在花了一些时间阅读诸如SO类的博客和社区网站之后,我发现有些技术可以XSS AttackSQL Injection 。但是问题是,大多数与安全性相关的问题都非常古老。所以,我的问题是

does my following code has any major security holes that can be bypassed by amateur or mid-level attacker(hacker) . does my following code has any major security holes that can be bypassed by amateur or mid-level attacker(hacker)

Is there anything else I can do to be safe from attack?By the way,I'm using HTMLPurifier to be safe from XSS Attack 为了防止攻击我还能做些其他事情吗?顺便说一句,我正在使用HTMLPurifier来防止XSS Attack

PHP 的PHP

require_once '/path/to/HTMLPurifier.auto.php';

$connect_dude=mysqli_connect('localhost','root','','user');

$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);

if(isset($_POST["sub"])){
  $name=$_POST["namex"];
  $email=$_POST["email"];
  $ques=$_POST["ques"];
  $clean_name = $purifier->purify($name);
  $clean_email = $purifier->purify($email);
  $clean_ques = $purifier->purify($ques);

  $stmt = mysqli_stmt_init($connect_dude);
     if(mysqli_stmt_prepare($stmt, 'INSERT INTO question (name,email,question) VALUES(?,?,?)')) {

        mysqli_stmt_bind_param($stmt, "sss", $clean_name, $clean_email, $clean_ques);
        mysqli_stmt_execute($stmt);
      }
}

HTML FORM HTML格式

<div id="form">
  <form id="sub_form" action="ask.php" method="post" enctype="multipart/form-data">
     <p id="nam" class="indi">Name</p><input type="text" id="namex" name="namex" placeholder="Your Name" required></br>
     <p id="ema" class="indi">Email</p><input type="text" id="email" name="email" placeholder="Email" required></br>
     <p id="que" class="indi">Question</p><textarea id="ques" name="ques" placeholder="Question" required></textarea></br>
     <input type="submit" id="sub" name="sub" value="Send">
  </form>
</div>

The SQL stuff is fine, parameterised queries are the best-practice approach to prevent SQL injection. SQL东西很好,参数化查询是防止SQL注入的最佳实践方法。

The approach to XSS is... a bit weird. XSS的方法有点奇怪。

HTMLPurifier is of use where you want to allow the user to input limited HTML markup. HTMLPurifier用于您要允许用户输入有限的HTML标记的地方。 That can be reasonable for formattable freetext fields (like I'm guessing ques is) if you can't be bothered to go as far as providing your own custom mini-markup language like Markdown. 如果您不介意提供自己的自定义迷你标记语言(例如Markdown),那么对于可格式化的自由文本字段(例如,我猜是ques ),这可能是合理的。

But do you really want the user to be able to input markup for all fields, including their name and e-mail address? 但是,您是否真的希望用户能够为所有字段输入标记,包括其名称和电子邮件地址? Should I be able to have the name “Edw ard B oi ng J r”? 我应该能够具有名称“EDW ARD OI NGĴR”? Are users going to have to enter &amp; 用户是否必须输入&amp; every time they want to use an ampersand? 每次他们要使用&符号时?

A better approach is usually to accept plain text as it is, and then HTML-escape it at the point you insert it into an HTML page (eg with <?php echo htmlspecialchars($value); ?> ) so that the exact string entered by the user appears in the page. 更好的方法通常是按原样接受纯文本,然后在将其插入HTML页面时对HTML进行转义(例如,使用<?php echo htmlspecialchars($value); ?> ),以便获得准确的字符串用户输入的内容将显示在页面中。 The fields where you deliberately allow markup (and so use HTMLPurifier instead of htmlspecialchars) are typically very much exceptional cases. 您特意允许标记的字段(因此使用HTMLPurifier而不是htmlspecialchars)通常是非常例外的情况。

Note that if you are injecting into other contexts you need different escaping functions: for example if you are injecting into a JavaScript string in a <script> block then you need JS-string escaping and neither htmlspecialchars nor HTMLPurifier will help you with that. 请注意,如果要注入其他上下文,则需要不同的转义功能:例如,如果要注入<script>块中的JavaScript字符串,则需要JS字符串转义,并且htmlspecialchars和HTMLPurifier都无法帮助您。

Perhaps testing if each of the post variables are set and not just the sub one. 也许测试是否设置了每个post变量,而不仅仅是sub变量。 Also, checking them in javascript as well before sending to php to rid the post of any unwanted characters. 另外,在发送到php之前,请在javascript中检查它们,以消除帖子中所有不需要的字符。 You can regex and test for empty strings in javascript for example. 例如,您可以正则表达式并测试javascript中的空字符串。

The most checks - the better. 检查最多-更好。

You can also use names that are not so common (abrv_page_name_email instead of simply "email"). 您也可以使用不太常见的名称(abrv_page_name_email而不是简单的“ email”)。

Lots of stuff. 很多东西。

Regex and mysqli_real_escape_string are also common practice. 正则表达式和mysqli_real_escape_string也是常见的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM