JS HTTP 原生請求(番外篇)
~~~
function sendRequest(){
var xhr = new XMLHttpRequest();
var url = "/comment?comment=test";
xhr.open("GET",url,true);
xhr.onreadstatechange=function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
console.log(xhr);
}else{
console.log(error);
}
}
}
xhr.send();
}
~~~
XSS 全稱為 Cross Site Scripting,用戶在表單中有意或無意輸入一些惡意字符,從而破壞頁面的表現!
看看常見的惡意字符XSS 輸入:
1.XSS 輸入通常包含 JavaScript 腳本,如彈出惡意警告框:<script>alert("XSS");</script>
2.XSS 輸入也可能是 HTML 代碼段,譬如:
(1).網頁不停地刷新 <meta http-equiv="refresh" content="0;">
(2).嵌入其它網站的鏈接 <iframe src=http://xxxx width=250 height=250 </iframe>
對于PHP開發者來說,如何去防范XSS攻擊呢?(php防止xss攻擊的函數)
可以用如下函數:
~~~
<?PHP
/**
* @blog http://www.phpddt.com
* @param $string
* @param $low 安全別級低
*/
function clean_xss(&$string, $low = False)
{
if (! is_array ( $string ))
{
$string = trim ( $string );
$string = strip_tags ( $string );
$string = htmlspecialchars ( $string );
if ($low)
{
return True;
}
$string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string );
$no = '/%0[0-8bcef]/';
$string = preg_replace ( $no, '', $string );
$no = '/%1[0-9a-f]/';
$string = preg_replace ( $no, '', $string );
$no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
$string = preg_replace ( $no, '', $string );
return True;
}
$keys = array_keys ( $string );
foreach ( $keys as $key )
{
clean_xss ( $string [$key] );
}
}
//just a test
$str = 'phpddt.com<meta http-equiv="refresh" content="0;">';
clean_xss($str); //如果你把這個注釋掉,你就知道xss攻擊的厲害了
echo $str;
?>
~~~
<span style="color:red">如果你把這個注釋掉,你就知道xss攻擊的厲害了</span>
通過clean_xss()就過濾了惡意內容!
### XSS 過濾函數(1)
~~~
function clean_xss(&$string, $low = False){
if (! is_array ( $string )){
$string = trim ( $string );
$string = strip_tags ( $string );
$string = htmlspecialchars ( $string );
if ($low)
{
return True;
}
$string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string );
$no = '/%0[0-8bcef]/';
$string = preg_replace ( $no, '', $string );
$no = '/%1[0-9a-f]/';
$string = preg_replace ( $no, '', $string );
$no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
$string = preg_replace ( $no, '', $string );
return True;
}
$keys = array_keys ( $string );
foreach ( $keys as $key )
{
clean_xss ( $string [$key] );
}
}
~~~
### XSS過濾函數(2)
~~~
<?php
//php防注入和XSS攻擊通用過濾
$_GET && SafeFilter($_GET);
$_POST && SafeFilter($_POST);
$_COOKIE && SafeFilter($_COOKIE);
function SafeFilter (&$arr)
{
$ra=Array('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/','/script/','/javascript/','/vbscript/','/expression/','/applet/'
,'/meta/','/xml/','/blink/','/link/','/style/','/embed/','/object/','/frame/','/layer/','/title/','/bgsound/'
,'/base/','/onload/','/onunload/','/onchange/','/onsubmit/','/onreset/','/onselect/','/onblur/','/onfocus/',
'/onabort/','/onkeydown/','/onkeypress/','/onkeyup/','/onclick/','/ondblclick/','/onmousedown/','/onmousemove/'
,'/onmouseout/','/onmouseover/','/onmouseup/','/onunload/');
if (is_array($arr))
{
foreach ($arr as $key => $value)
{
if (!is_array($value))
{
if (!get_magic_quotes_gpc()) //不對magic_quotes_gpc轉義過的字符使用addslashes(),避免雙重轉義。
{
$value = addslashes($value); //給單引號(')、雙引號(")、反斜線(\)與 NUL(NULL 字符)
加上反斜線轉義
}
$value = preg_replace($ra,'',$value); //刪除非打印字符,粗暴式過濾xss可疑字符串
$arr[$key] = htmlentities(strip_tags($value)); //去除 HTML 和 PHP 標記并轉換為 HTML 實體
}
else
{
SafeFilter($arr[$key]);
}
}
}
}
?>
$str = 'www.90boke.com<meta http-equiv="refresh" content="0;">';
SafeFilter ($str); //如果你把這個注釋掉,提交之后就會無休止刷新
echo $str;
~~~
### XSS過濾函數(3)
~~~
function string_remove_xss($html) {
preg_match_all("/\<([^\<]+)\>/is", $html, $ms);
$searchs[] = '<';
$replaces[] = '<';
$searchs[] = '>';
$replaces[] = '>';
if ($ms[1]) {
$allowtags = 'img|a|font|div|table|tbody|caption|tr|td|th|br|p|b|strong|i|u|em|span|ol|ul|li|blockquote';
$ms[1] = array_unique($ms[1]);
foreach ($ms[1] as $value) {
$searchs[] = "<".$value.">";
$value = str_replace('&', '_uch_tmp_str_', $value);
$value = string_htmlspecialchars($value);
$value = str_replace('_uch_tmp_str_', '&', $value);
$value = str_replace(array('\\', '/*'), array('.', '/.'), $value);
$skipkeys = array('onabort','onactivate','onafterprint','onafterupdate','onbeforeactivate','onbeforecopy','onbeforecut','onbeforedeactivate',
'onbeforeeditfocus','onbeforepaste','onbeforeprint','onbeforeunload','onbeforeupdate','onblur','onbounce','oncellchange','onchange',
'onclick','oncontextmenu','oncontrolselect','oncopy','oncut','ondataavailable','ondatasetchanged','ondatasetcomplete','ondblclick',
'ondeactivate','ondrag','ondragend','ondragenter','ondragleave','ondragover','ondragstart','ondrop','onerror','onerrorupdate',
'onfilterchange','onfinish','onfocus','onfocusin','onfocusout','onhelp','onkeydown','onkeypress','onkeyup','onlayoutcomplete',
'onload','onlosecapture','onmousedown','onmouseenter','onmouseleave','onmousemove','onmouseout','onmouseover','onmouseup','onmousewheel',
'onmove','onmoveend','onmovestart','onpaste','onpropertychange','onreadystatechange','onreset','onresize','onresizeend','onresizestart',
'onrowenter','onrowexit','onrowsdelete','onrowsinserted','onscroll','onselect','onselectionchange','onselectstart','onstart','onstop',
'onsubmit','onunload','javascript','script','eval','behaviour','expression','style','class');
$skipstr = implode('|', $skipkeys);
$value = preg_replace(array("/($skipstr)/i"), '.', $value);
if (!preg_match("/^[\/|\s]?($allowtags)(\s+|$)/is", $value)) {
$value = '';
}
$replaces[] = empty($value) ? '' : "<" . str_replace('"', '"', $value) . ">";
}
}
$html = str_replace($searchs, $replaces, $html);
return $html;
}
//php防注入和XSS攻擊通用過濾
function string_htmlspecialchars($string, $flags = null) {
if (is_array($string)) {
foreach ($string as $key => $val) {
$string[$key] = string_htmlspecialchars($val, $flags);
}
} else {
if ($flags === null) {
$string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string);
if (strpos($string, '&#') !== false) {
$string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
}
} else {
if (PHP_VERSION < '5.4.0') {
$string = htmlspecialchars($string, $flags);
} else {
if (!defined('CHARSET') || (strtolower(CHARSET) == 'utf-8')) {
$charset = 'UTF-8';
} else {
$charset = 'ISO-8859-1';
}
$string = htmlspecialchars($string, $flags, $charset);
}
}
}
return $string;
}
~~~
### XSS過濾函數(4)
~~~
function xss_clean($data){
// Fix &entity\n;
$data=str_replace(array('&','<','>'),array('&','<','>'),$data);
$data=preg_replace('/(&#*\w+)[\x00-\x20]+;/u','$1;',$data);
$data=preg_replace('/(&#x*[0-9A-F]+);*/iu','$1;',$data);
$data=html_entity_decode($data,ENT_COMPAT,'UTF-8');
// Remove any attribute starting with "on" or xmlns
$data=preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu','$1>',$data);
// Remove javascript: and vbscript: protocols
$data=preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu','$1=$2nojavascript...',$data);
$data=preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu','$1=$2novbscript...',$data);
$data=preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u','$1=$2nomozbinding...',$data);
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i','$1>',$data);
$data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i','$1>',$data);
$data=preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu','$1>',$data);
// Remove namespaced elements (we do not need them)
$data=preg_replace('#</*\w+:\w[^>]*+>#i','',$data);
do{// Remove really unwanted tags
$old_data=$data;
$data=preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i','',$data);
}while($old_data!==$data);
// we are done...
return $data;
}
~~~
- 簡介
- Cookie
- HTML5 LocalStorage
- session
- 當瀏覽器關閉后,Session就銷毀了嗎?
- mysql數據庫保存session
- HTTP協議的由來
- fsockopen異步請求
- http防盜鏈
- Apache偽靜態知識補充
- 大并發量解決方案
- 大型網站是怎樣解決多用戶高并發訪問
- 網站高并發 大流量訪問的處理及解決方法
- 并發數與在線客戶數?注冊用戶數的關系
- 即時聊天程序
- 反向Ajax實現
- ob緩存作用
- 淺聊并發之戰
- php擴展安裝
- php安裝redis擴展
- SQLMap自動化實施SQL注入共計
- 命名空間namespace
- 集群和分布式之【session共享】
- php Redis存儲Session 【1】
- php Redis存儲Session 【2】
- php mysql存儲session【1】
- php緩存
- 文件緩存
- memcache和redis的比較
- 原生session與session in redis對比
- XSS攻擊【1】
- XSS攻擊【2】
- PHP消息隊列
- php+mysql 模擬發送郵件隊列
- php+mysql 模擬訂單處理隊列
- php+redis 模擬秒殺隊列
- RabbitMQ 消息隊列系統
- beanstalkd
- PHP構建即時通訊
- WebSocket協議
- workerman
- PHP變量的作用域
- PHP傳值和傳引用的區別
- PHP匿名函數
- PHP遞歸函數&應用
- PHP單例模式
- PHP性能優化
- RESTful
- 集群
- 增加pgsql擴展
- php.ini路徑查找
- Swoole Compiler
- mysql 主從
- 主從
- mysql-proxy
- window docker環境