P
P
polyakov_andrey2011-09-08 11:32:22
PHP
polyakov_andrey, 2011-09-08 11:32:22

[php] Sql and XSS Protection

Hello, please advise a function or class for checking and processing incoming data to protect against vulnerabilities (I do not use frameworks).

Answer the question

In order to leave comments, you need to log in

8 answer(s)
N
NiGP, 2011-09-08
@polyakov_andrey

Sorry, hit enter
http://www.php.net/manual/en/book.pdo.php
or rather, check out the
bindParam
from XSS, there are quite a few
strip_tags() functions that strip HTML and PHP tags from a string.
htmlspecialchars() - Converts only special characters ('&', '"', »', '<' and '>') to HTML entities ('&', '"'…). Used to filter user input for protection from XSS attacks
htmlentities() - converts all characters in a string (except letters) to HTML entities Used to protect against XSS, being a more flexible analogue of htmlspecialchars
stripslashes() - strips escaped characters (there is no need for them after conversion in essence by previous functions shield)

G
gro, 2011-09-08
@gro

To protect yourself from SQL and XSS injections, you need to understand at a minimum level what it is in general and how it works.
Then there will be no injections, no such meaningless questions, no such meaningless answers.

A
Alexander, 2011-09-08
@xel

for SQL, I recommend using mysqli, you specify the data type in it when preparing a request
from XSS, escape htmlspecialchars () and use double quotes everywhere in html - this is both standard and safe (you can escape of course with ENT_QUOTES, but this is an extra headache.

N
NiGP, 2011-09-08
@NiGP

SQL inj is pretty well dealt with by PDO
in more detail in the docs

U
Ura78, 2011-09-08
@Ura78

and the function of processing incoming data from Raz0rtyk

A
Alexey Firsov, 2011-09-09
@lesha_firs

I always did so, from sql attacks.

// Функция экранирования переменных
function quote_smart($value)
{
    // если magic_quotes_gpc включена - используем stripslashes
    if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }
    // Если переменная - число, то экранировать её не нужно
    // если нет - то окружем её кавычками, и экранируем
    if (!is_numeric($value)) {
        $value = mysql_real_escape_string($value);
    }
    return $value;
}


$info = $Database->prepare("SELECT money FROM users WHERE text = '".quote_smart($_POST["text"])."'");


A query written in this way will run without errors, and hacking with SQL Injection will not be possible.
Do not believe me, read here http://www.php.ru/manual/function.mysql-real-escape-string.html

U
Ura78, 2011-09-08
@Ura78

Apache ModSecurity Module Level Solution

M
MrGroovy, 2020-12-09
@MrGroovy

To protect against cross-site scripting (XSS), you can use built-in functions such as:
htmlspecialchars()
Converts some characters as HTML entities to store their meaning. Be sure to use it to filter user input.
The following functions can be used to protect against SQL injection:
mysql_real_escape_string()
Escapes special characters, taking into account the connection encoding, so that the result can be safely used in an SQL query.
You can also protect yourself using filtering on the "white" list, casting variables and using prepared queries.
To find fields that are subject to filtering, you can use special vulnerability scanners, for example, for SQL injection, you can use the SQLMAP utility
You also have a more comprehensive approach to protecting the site, you can use online vulnerability scanners that can check most of the site's vulnerabilities, and it also does not hurt to check for vulnerabilities associated with the operation of application protocols. Try metascan or https://sitecheck.sucuri.net

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question