PDA

View Full Version : این کد برای جلوگیری از هرگونه SQL Injection نوشته شده.



idocsidocs
پنج شنبه 11 آذر 1389, 21:27 عصر
لطفا بگید چطور می تونیم این کد رو بهینه تر کنیم؟

سوال اولی که برای خودم پیش اومد این بود که آیا این کد می تونه باعث کاهش سرعت اجرای اسکریپت بشه؟





$string = strtolower($_POST[textarea]);

/*
در اين قسمت از اسکريپت، تمام کاراکترهايي که براي مای اس کیو ال معني خاصي دارن به معادل اچ تی ام ال خودشون تبديل مي شن تا نتونن به پايگاه داده آسيب برسونن.
*/
//$old[]=NULL; $new[]=oo;
$old[]='!'; $new[]='&#33&#59;';
$old[]='"'; $new[]='&#34&#59;';
//$old[]="#"; $new[]='&#35&#59;';
$old[]='$'; $new[]='&#36&#59;';
$old[]='%'; $new[]='&#37&#59;';
//$old[]='&'; $new[]='&#38&#59;';
$old[]='\''; $new[]='&#39&#59;';
$old[]='('; $new[]='&#40&#59;';
$old[]=')'; $new[]='&#41&#59;';
$old[]='*'; $new[]='&#42&#59;';
$old[]='+'; $new[]='&#43&#59;';
$old[]=','; $new[]='&#44&#59;';
$old[]='-'; $new[]='&#45&#59;';
$old[]='.'; $new[]='&#46&#59;';
$old[]='/'; $new[]='&#47&#59;';
$old[]=':'; $new[]='&#58&#59;';
//$old[]=';'; $new[]='&#59&#59;';
$old[]='<'; $new[]='&#38;&#35;60&#59;';
$old[]='='; $new[]='&#38;&#35;61&#59;';
$old[]='>'; $new[]='&#38;&#35;62&#59;';
$old[]='?'; $new[]='&#38;&#35;63&#59;';
$old[]='@'; $new[]='&#38;&#35;64&#59;';
$old[]='['; $new[]='&#38;&#35;91&#59;';
$old[]='\\'; $new[]='&#38;&#35;92&#59;';
$old[]=']'; $new[]='&#38;&#35;93&#59;';
$old[]='^'; $new[]='&#38;&#35;94&#59;';
$old[]='`'; $new[]='&#38;&#35;96&#59;';
$old[]='{'; $new[]='&#38;&#35;123&#59;';
$old[]='|'; $new[]='&#38;&#35;124&#59;';
$old[]='}'; $new[]='&#38;&#35;125&#59;';
$old[]='~'; $new[]='&#38;&#35;126&#59;';
/*
در اين قسمت از اسکريپت، حروف اول کلمات رزرو شده (پر استفاده) که براي مای اس کیو ال معني خاصي دارن به معادل اچ تی ام ال خودشون تبديل مي شن تا اين کلمات برای مای اس کیو ال بی معنی بشن و نتونن به پايگاه داده آسيب برسونن.
*/
$old[]='alter'; $new[]='&#38;&#35;97&#59;lter';
$old[]='and'; $new[]='&#38;&#35;97&#59;nd';
$old[]='change'; $new[]='&#38;&#35;99&#59;hange';
$old[]='create'; $new[]='&#38;&#35;99&#59;reate';
$old[]='delete'; $new[]='&#38;&#35;100&#59;elete';
$old[]='drop'; $new[]='&#38;&#35;100&#59;rop';
$old[]='grant'; $new[]='&#38;&#35;103&#59;rant';
$old[]='group'; $new[]='&#38;&#35;103&#59;roup';
$old[]='index'; $new[]='&#38;&#35;105&#59;ndex';
$old[]='insert'; $new[]='&#38;&#35;105&#59;nsert';
$old[]='key'; $new[]='&#38;&#35;107&#59;ey';
$old[]='like'; $new[]='&#38;&#35;108&#59;ike';
$old[]='limit'; $new[]='&#38;&#35;108&#59;imit';
$old[]='order'; $new[]='&#38;&#35;111&#59;rder';
$old[]='or'; $new[]='&#38;&#35;111&#59;r';
$old[]='rename'; $new[]='&#38;&#35;114&#59;ename';
$old[]='revoke'; $new[]='&#38;&#35;114&#59;evoke';
$old[]='schema'; $new[]='&#38;&#35;115&#59;chema';
$old[]='select'; $new[]='&#38;&#35;115&#59;elect';
$old[]='set'; $new[]='&#38;&#35;115&#59;et';
$old[]='show'; $new[]='&#38;&#35;115&#59;how';
$old[]='sql'; $new[]='&#38;&#35;115&#59;ql';
$old[]='table'; $new[]='&#38;&#35;116&#59;able';
$old[]='then'; $new[]='&#38;&#35;116&#59;hen';
$old[]='to'; $new[]='&#38;&#35;116&#59;o';
$old[]='union'; $new[]='&#38;&#35;117&#59;nion';
$old[]='update'; $new[]='&#38;&#35;117&#59;pdate';
$old[]='usage'; $new[]='&#38;&#35;117&#59;sage';
$old[]='values'; $new[]='&#38;&#35;118&#59;alues';
$old[]='write'; $new[]='&#38;&#35;119&#59;rite';
$old[]='xor'; $new[]='&#38;&#35;120&#59;or';

$newstring = str_replace($old,$new,$string);

idocsidocs
پنج شنبه 11 آذر 1389, 23:30 عصر
خوشحال می شم که هر جا لازم بود، این کد رو اصلاح کنید.

UnnamE
جمعه 12 آذر 1389, 00:28 صبح
تا الان كه من php كار كردم هيچوقت مجبوذ به استفاده از همچين توابعي نشدم
بيبنيد شما با اين كد داريد بيان ميكنيد كه چه چيزهايي نمي خواهيد وارد بشه
در اصل بايد اينجور بيان كني كه چه چيزي ميخواي وارد بشه و بقيه رو رد كني همين!

Keramatifar
جمعه 12 آذر 1389, 00:44 صبح
نیازی به اینکار نیست، میتونید از توابع خود php استفاده کنید


function sanitizeString($var)
{
$var = strip_tags($var);
$var = htmlentities($var);
$var = stripslashes($var);
$var = mysql_entities_fix_string($var);
return mysql_real_escape_string($var);
}
function mysql_entities_fix_string($string)
{
return htmlentities(mysql_fix_string($string));
}

function mysql_fix_string($string)
{
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return mysql_real_escape_string($string);
}

UnnamE
جمعه 12 آذر 1389, 00:54 صبح
درود آقاي كرامتي
به نظر شما با يه preg_match ساده همه اين كارها انجام داده نميشه؟
اينكه فقط A-Z.a-z, 0-9 رو قبول كرد؟
البته من هميشه به همين روش كه گفتم انجام دادم و مشكلي نداشتم و با اكثر تست ها هم اين روش جون سالم به در برده!

shahriyar3
جمعه 12 آذر 1389, 01:02 صبح
تابع تبدیل html to ascii بود که یه سرچ میکردید



function html2ascii($s){
// convert links
$s = preg_replace('/<a\s+.*?href="?([^\" >]*)"?[^>]*>(.*?)<\/a>/i','$2 ($1)',$s);

// convert p, br and hr tags
$s = preg_replace('@<(b|h)r[^>]*>@i',"\n",$s);
$s = preg_replace('@<p[^>]*>@i',"\n\n",$s);
$s = preg_replace('@<div[^>]*>(.*)</div>@i',"\n".'$1'."\n",$s);

// convert bold and italic tags
$s = preg_replace('@<b[^>]*>(.*?)</b>@i','*$1*',$s);
$s = preg_replace('@<strong[^>]*>(.*?)</strong>@i','*$1*',$s);
$s = preg_replace('@<i[^>]*>(.*?)</i>@i','_$1_',$s);
$s = preg_replace('@<em[^>]*>(.*?)</em>@i','_$1_',$s);

// decode any entities
$s = strtr($s,array_flip(get_html_translation_table(HTM L_ENTITIES)));

// decode numbered entities
$s = preg_replace('/&#(\d+);/e','chr(str_replace(";","",str_replace("&#","","$0")))',$s);

// strip any remaining HTML tags
$s = strip_tags($s);

// return the string
return $s;
}




function ascii2html($text) {
// Encode any HTML tags
$text = htmlentities($text);

// Split the text into an array and treat each element as a paragraph
$textarray = split("\n", $text);

for ($i = 0, $j = count($textarray); $i < $j; $i++) {
// Turn HTTP and FTP URLs into links
$textarray[$i] = preg_replace("/((ht|f)tp:\/\/[^\s&]+)/", "<a href=\"$1\">$1</a>", $textarray[$i]);

// Turn email addresses into links
$textarray[$i] = preg_replace("/(\\S+@\\S+\\.\\w+)/", "<a href=\"mailto:$1\">$1</a>", $textarray[$i]);

// Add paragraphs
$textarray[$i] = "<p>" . $textarray[$i] . "</p>";
}
return join("\n", $textarray);
}

idocsidocs
جمعه 12 آذر 1389, 13:30 عصر
نیازی به اینکار نیست، میتونید از توابع خود php استفاده کنید


function sanitizeString($var)
{
$var = strip_tags($var);
$var = htmlentities($var);
$var = stripslashes($var);
$var = mysql_entities_fix_string($var);
return mysql_real_escape_string($var);
}
function mysql_entities_fix_string($string)
{
return htmlentities(mysql_fix_string($string));
}

function mysql_fix_string($string)
{
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return mysql_real_escape_string($string);
}



مطلب شما درسته ولی باید بگم که توابع پی اچ پی فقط کاراکترهای ' " > < \ - وی یکی دوتا کدیگه رو فیلتر می کنن و امنیت کامل رو مهیا نمی کنن.

این کد رو باید بعد از توابعی مثل preg_match استفاده کرد و فقط کاراکترهایمد نظر رو فیلتر کرد نه همه کاراکترها رو.

idocsidocs
جمعه 12 آذر 1389, 13:33 عصر
تابع تبدیل html to ascii بود که یه سرچ میکردید



function html2ascii($s){
// convert links
$s = preg_replace('/<a\s+.*?href="?([^\" >]*)"?[^>]*>(.*?)<\/a>/i','$2 ($1)',$s);

// convert p, br and hr tags
$s = preg_replace('@<(b|h)r[^>]*>@i',"\n",$s);
$s = preg_replace('@<p[^>]*>@i',"\n\n",$s);
$s = preg_replace('@<div[^>]*>(.*)</div>@i',"\n".'$1'."\n",$s);

// convert bold and italic tags
$s = preg_replace('@<b[^>]*>(.*?)</b>@i','*$1*',$s);
$s = preg_replace('@<strong[^>]*>(.*?)</strong>@i','*$1*',$s);
$s = preg_replace('@<i[^>]*>(.*?)</i>@i','_$1_',$s);
$s = preg_replace('@<em[^>]*>(.*?)</em>@i','_$1_',$s);

// decode any entities
$s = strtr($s,array_flip(get_html_translation_table(HTM L_ENTITIES)));

// decode numbered entities
$s = preg_replace('/&#(\d+);/e','chr(str_replace(";","",str_replace("&#","","$0")))',$s);

// strip any remaining HTML tags
$s = strip_tags($s);

// return the string
return $s;
}


function ascii2html($text) {
// Encode any HTML tags
$text = htmlentities($text);

// Split the text into an array and treat each element as a paragraph
$textarray = split("\n", $text);

for ($i = 0, $j = count($textarray); $i < $j; $i++) {
// Turn HTTP and FTP URLs into links
$textarray[$i] = preg_replace("/((ht|f)tp:\/\/[^\s&]+)/", "<a href=\"$1\">$1</a>", $textarray[$i]);

// Turn email addresses into links
$textarray[$i] = preg_replace("/(\\S+@\\S+\\.\\w+)/", "<a href=\"mailto:$1\">$1</a>", $textarray[$i]);

// Add paragraphs
$textarray[$i] = "<p>" . $textarray[$i] . "</p>";
}
return join("\n", $textarray);
}


مطلب شما درسته ولی تابع html2ascii کلمات رزور شده درMySQL رو به معادل اچ تی ام ال تبدیل نمی کنن.

UnnamE
جمعه 12 آذر 1389, 17:12 عصر
و هنوز هم ميگم preg_match زنده باد!
با اين كدها فقط داريد اعلام ميكنيد چه چيزي نميخواين
بايد وارد كنيد چه ورودي مجاز هست دو بقيه رو رد بايد كرد
اينجوري فقط كد رو فقط ميپيچونيد و همچنين فرآيند رو طولاني تر كردند!

idocsidocs
جمعه 12 آذر 1389, 21:39 عصر
و هنوز هم ميگم preg_match زنده باد!
با اين كدها فقط داريد اعلام ميكنيد چه چيزي نميخواين
بايد وارد كنيد چه ورودي مجاز هست دو بقيه رو رد بايد كرد
اينجوري فقط كد رو فقط ميپيچونيد و همچنين فرآيند رو طولاني تر كردند!

تابع preg_quote به هر کدوم از کاراکترهای زیر بک اسلش اضافه می کنه.

. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

سوالی که دارم اینه که آیا این تابع یه تابع معادل داره که بتونه بک اسلشهای اضافه شده رو حذف کنه؟

UnnamE
شنبه 13 آذر 1389, 02:05 صبح
خوبه گفتم preg_match ها :لبخند:


سوال اولی که برای خودم پیش اومد این بود که آیا این کد می تونه باعث کاهش سرعت اجرای اسکریپت بشه؟
نميشه گفت آنچنان ها ولي در درخواست هاي بالا خودش رو نشون ميده
فرض كن در دقيقه 500-1000 تا درخواست داشته باشي كه خيلي هم دست پايين گرفتيم حدس بزن چه واويلاي ميشه