简体   繁体   English

尽管在 php 中使用 trim() 和 striplashes() 进行输入过滤器,但仍执行 XSS 攻击

[英]Perform XSS attack despite input filter with trim() and striplashes() in php

for a university project I had to build an environment which includes XSS defencing machanisms.对于一个大学项目,我必须构建一个包含 XSS 防御机制的环境。 One such mechanism is the input filter.一种这样的机制是输入过滤器。 This is written in php and removes/strips characters that lead to the execution of an xss attack.这是写在 php 中的,删除/剥离导致执行 xss 攻击的字符。 The code for this looks like this:代码如下所示:

static function inputFilter($data) {
    $data = trim($data);
    $data = stripslashes($data);
    return htmlspecialchars($data);
}

Are there still ways to perform xss attacks that are not covered?是否还有未涵盖的xss攻击方法?

I tried bypassing the filter with unicode-encoding, javascript-encoding and null byte injection.我尝试使用 unicode 编码、javascript 编码和 null 字节注入绕过过滤器。

For the most part, htmlspecialchars will cover most scenarios, as users will not be able to use HTML exploits by injecting tags such as Hello<script>alert('XSS attack;!');</script>在大多数情况下, htmlspecialchars将涵盖大多数场景,因为用户将无法通过注入Hello<script>alert('XSS attack;!');</script>等标签来使用 HTML 漏洞利用

However, it depends on where you are using this input, if it is populating the href attribute of an <a> tag, then pseudo attacks can still occur, for example:但是,这取决于您在哪里使用此输入,如果它正在填充<a>标记的href属性,那么伪攻击仍然可能发生,例如:

<a href="javascript:console.log(localStorage....)">Click here</a>

Typically, any attempt at solving XSS attacks yourself should be avoided and instead you should consider using community-driven packages built for this kind of thing, one example of that is voku/antixss as they will find these pseudo-protocol calls and santize/strip them for you.通常,应该避免任何自行解决 XSS 攻击的尝试,而应该考虑使用为此类事情构建的社区驱动包,其中一个例子是voku/antixss ,因为它们会发现这些伪协议调用和 santize/strip他们给你。

It's helpful to think of XSS-resistance code as having two phases.将 XSS 抵抗代码视为具有两个阶段会很有帮助。

  1. Validating and sanitizing, what you call input filtering, and验证和清理,即所谓的输入过滤,以及
  2. Escaping. For example converting <script> to &le;script&ge; Escaping。例如将<script>转换为&le;script&ge; so your user's browser won't see your user-furnished data as code.因此您用户的浏览器不会将您的用户提供的数据视为代码。

Validating is the process of checking whether input from your user is correct.验证是检查用户输入是否正确的过程。 For example, if you gather a number it should not contain any letters.例如,如果您收集一个数字,它不应包含任何字母。 If it does your code should fail validation, reject the input, and ask the user to try again.如果确实如此,您的代码应该无法通过验证,拒绝输入,并要求用户重试。

Sanitizing is the process of stripping away undesired parts of your user's input before using or storing it. 清理是在使用或存储用户输入之前去除不需要的部分的过程。 For example, most web apps that accept html input from untrusted users sanitize that input by removing all except a subset of HTML tags.例如,大多数接受来自不受信任用户的 html 输入的 web 应用程序通过删除除 HTML 标签的子集之外的所有标签来清理该输入。 For example, <h1> and <p> are not removed, but <script> and <iframe> are.例如, <h1><p>没有被移除,但是<script><iframe>被移除。

Validating and sanitizing techniques you choose field-by-field.逐个验证和净化您选择的技术。 If you're gathering a date of birth, for example, your validating task is to make sure the date is valid.例如,如果您要收集出生日期,您的验证任务就是确保日期有效。 If you're gathering an email address, you ensure it is formatted correctly.如果您要收集 email 地址,请确保其格式正确。 When validation fails you reject the input.当验证失败时,您拒绝输入。

PHP offers the data filtering subsystem , with built-in features for validating and sanitizing various common data types. PHP 提供数据过滤子系统,具有用于验证和清理各种常见数据类型的内置功能。

If your input data type is HTML your validation and sanitization rules will be complex.如果您的输入数据类型是 HTML,您的验证和清理规则将很复杂。 You can sanitize with htmlspecialchars(), but doing that forces your input to be plain text, not HTML: It escapes everything.您可以使用 htmlspecialchars() 进行清理,但这样做会强制您的输入为纯文本,而不是 HTML:它会转义所有内容。 That's a good solution for simple systems.对于简单的系统来说,这是一个很好的解决方案。

If you are handling actual HTML, the first rule of security code applies: don't write your own security code.如果您正在处理实际的 HTML,则应用安全代码的第一条规则:不要编写自己的安全代码。 Instead, use proven library code.相反,使用经过验证的库代码。 HTML is just too complex to sanitize safely with unproven code. HTML 太复杂了,无法使用未经验证的代码进行安全清理。 Cybercriminals are smarter and better-motivated than you and me, and they only need to find one flaw to pwn our web apps.网络罪犯比你我更聪明、更有动力,他们只需要找到一个漏洞就可以破解我们的 web 应用程序。

The WordPress project has been maintaining a module called kses . WordPress 项目一直在维护一个名为kses的模块。 It's an acronym for "kses strips evil scripts".它是“kses strip evil scripts”的缩写。 It removes dangerous tags and attributes.它删除危险的标签和属性。 There's also Lars Moelleken's anti-xss package .还有Lars Moelleken 的反 xss package

Again, you validate and sanitize input before you store it for later use.同样,您在存储输入供以后使用之前验证和清理输入。

Then, on output you will escape your stored data.然后,在 output 上,您将转义存储的数据。

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

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