存储型 XSS
漏洞的原理及修复方法
1.常见的触发场景
2.漏洞原理
3.漏洞危害
4.一些tips
5.如何避免&修复漏洞
www-data@w:~/controller$ vim missionController.class.php
class missionController extends baseController{
public $var;
public function __construct(){
parent::__construct();
if($this->loged){
return;
}else{
header('Location: /index.php');
exit();
}
}
public function feedAction(){
$missionModel = new missionModel();
$feeds = $missionModel->get();
$url = '/index.php?c=mission&a=feed';
$username = $this->username;
require('tpl/feed.tpl');
}
public function feedApiAction(){
//permission check
$id = request('id');
$sid = request('sessionid');
$userModel = new userModel();
$userInfo = $userModel->getUserInfo($id,$sid);
$role = $userInfo['role'];
$feeds = array();
$callback = request('callback');
if($role == 1){
$missionModel = new missionModel();
$feeds = $missionModel->getMost($this->id);
$feeds = json_encode(array('email'=>$this->username,'count'=>$feeds[0]));
header("Content-Type: application/json");
echo ' '.$callback.'('.$feeds.')';
}else{
echo $callback.'('.json_encode($feeds).')';
}
}
public function missionAddAction(){
$referer = $_SERVER['HTTP_REFERER'];
$hostname = parse_url($referer,PHP_URL_HOST);
if($hostname !== 'paper.com' && $hostname !== 'csrf.com'){
die('csrf');
}
$missionModel = new missionModel();
$missionUrl = request('missionUrl');
$missionContent = request('missionContent');
$missionModel->add($this->id,$this->username,$missionUrl,$missionContent);
$count = 1;
if($count > 0){
$this->json(array('result'=>0,'message'=>'add mission ok'));
}else{
$this->json(array('result'=>1,'message'=>'add mission wrong'));
}
}
public function deleteAction(){
$missionId = request('missionId');
$missionModel = new missionModel();
$missionModel->delete($missionId,$this->id);
header('Location: /index.php?c=mission&a=feed');
}
public function testDeleteAction(){
echo 'testDeleteAction';
}
public function evalAction(){
$param = request('param');
$a = 0;
eval("\$a = $param;");
echo $a;
}
public function preg_replaceAction(){
$param = request('param');
$result = preg_replace("/(.*)/e",'$var = \'\\1\';',$param);
echo $result;
}
public function add(){
$this->id;
}
}
--------------------------------------------------------------------------------
?php
class missionModel extends baseModel{
public function __construct(){
parent::__construct();
}
public function get(){
$sql = 'select id,username,url,content,time from mission order by id desc';
$db_prepare = $this->conn->prepare($sql);
$db_prepare->execute(array());
$result = $db_prepare->fetchAll();
return $result;
}
public function getMost($uid){
$sql = 'select count(*) from mission where uid=?';
$db_prepare = $this->conn->prepare($sql);
$db_prepare->execute(array($uid));
$result = $db_prepare->fetch();
return $result;
}
public function add($uid,$username,$missionUrl,$missionContent){
$sql = 'insert into mission(uid,username,url,content,time) values(?,?,?,?,?)';
$db_prepare = $this->conn->prepare($sql);
$time = unixTime();
$db_prepare->execute(array($uid,$username,$missionUrl,$missionContent,$time));
return ;
}
public function delete($id,$uid){
$sql = 'delete from mission where id=? and uid=?';
$db_prepare = $this->conn->prepare($sql);
$db_prepare->execute(array($id,$uid));
return;
}
}
---------------------------------------------------------------------------------
用户可以提交自己的内容到web server端
其他用户可以浏览之前用户产生的内容
漏洞原理:
将A用户提交给 web server 的数据存储到数据库,其他用户访问这些数据的时候,没有做转义||编码。将数据直接输出到浏览器,导致而已用户可以插入任意的 html 代码 || javascript 代码
mysql> select * from mission
www-data@w:~# cd /tpl
www-data@w:~/tpl$ ls
dom.tpl feed.tpl index.js jsohijack.tpl stroeEdit.tpl style.css
feedAdd.tpl footer.tpl indexPDO.tpl reflectResult.tpl stroe.tpl userIndex.tpl
feeds.tpl head.tpl index.tpl search.tpl stroeView.tpl
---------------------------------------------------------------------------------
www-data@w:~/tpl$ vim feeds.tpl
---------------------------------------------------------------------------------
漏洞危害:
在任意用户的浏览器内执行任意js代码,可以伪装用户在网站发起任操作,比如发消息,转账等操作,在ugc社区还可以造成xss蠕虫,比如myspace的蠕虫,新浪微博蠕虫,百度贴蠕虫等。
浏览器将用户产生的js代码当做web server的代码而解析执行了,->就会避免xss漏洞。
htmlspecialchars
html实体编码:有些字符预留的字符串