Answer the question
In order to leave comments, you need to log in
How to make universal protection against xss attacks and sql injections?
I do not provide technical support for sites, but it so happened that I am often asked for help. On the one hand, it’s inconvenient to refuse, and it’s not profitable from a commercial point of view, on the other hand, you won’t pay for a big thank you in the store either. Therefore, I decided to write a universal solution, but I ran into some problems.
The essence of the solution is to catch POST, GET, COOKIE data and process them before the site performs any actions with them.
Here is the actual code
$jsxss="onabort,oncanplay,oncanplaythrough,ondurationchange,onemptied,onended,onerror,onloadeddata,onloadedmetadata,onloadstart,onpause,onplay,onplaying,onprogress,onratechange,onseeked,onseeking,onstalled,onsuspend,ontimeupdate,onvolumechange,onwaiting,oncopy,oncut,onpaste,ondrag,ondragend,ondragenter,ondragleave,ondragover,ondragstart,ondrop,onblur,onfocus,onfocusin,onfocusout,onchange,oninput,oninvalid,onreset,onsearch,onselect,onsubmit,onabort,onbeforeunload,onerror,onhashchange,onload,onpageshow,onpagehide,onresize,onscroll,onunload,onkeydown,onkeypress,onkeyup,altKey,ctrlKey,shiftKey,metaKey,key,keyCode,which,charCode,location,onclick,ondblclick,oncontextmenu,onmouseover,onmouseenter,onmouseout,onmouseleave,onmouseup,onmousemove,onwheel,altKey,ctrlKey,shiftKey,metaKey,button,buttons,which,clientX,clientY,detail,relatedTarget,screenX,screenY,deltaX,deltaY,deltaZ,deltaMode,animationstart,animationend,animationiteration,animationName,elapsedTime,propertyName,elapsedTime,transitionend,onerror,onmessage,onopen,ononline,onoffline,onstorage,onshow,ontoggle,onpopstate,ontouchstart,ontouchmove,ontouchend,ontouchcancel,persisted,javascript";
$jsxss = explode(",",$jsxss);
foreach($_POST as $k=>$v)
{
if(is_array($v))
{
foreach($v as $Kk=>$Vv)
{
$Vv = preg_replace ( "'<script[^>]*?>.*?</script>'si", "", $Vv );
$Vv = str_replace($jsxss,"",$Vv);
$Vv = str_replace (array("*","\\"), "", $Vv );
$Vv = strip_tags($Vv);
$Vv = htmlentities($Vv, ENT_QUOTES, "UTF-8");
$Vv = htmlspecialchars($Vv, ENT_QUOTES);
$_POST[$k][$Kk] = $Vv;
}
}
ELSE
{
//Сначала удаляем любые скрипты для защиты от xss-атак
$v = preg_replace ( "'<script[^>]*?>.*?</script>'si", "", $v );
//Вырезаем все известные javascript события для защиты от xss-атак
$v = str_replace($jsxss,"",$v);
//Удаляем экранированание для защиты от SQL-иньекций
$v = str_replace (array("*","\\"), "", $v );
//Экранируем специальные символы в строках для использования в выражениях SQL
$v = mysql_real_escape_string( $v );
//Удаляем другие лишние теги.
$v = strip_tags($v);
//Преобразуеv все возможные символы в соответствующие HTML-сущности
$v = htmlentities($v, ENT_QUOTES, "UTF-8");
$v = htmlspecialchars($v, ENT_QUOTES);
//Перезаписываем GET массив
$_POST[$k] = $v;
}
}
Answer the question
In order to leave comments, you need to log in
Ничего не нужно фильтровать. Просто перед тем, как выводить контент, заменяете html-сущности (для предотвращения xss) на соотв. символы и используете PDO (для предотвращения инъекций).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question