<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Zihni &#187; sql enjeksiyon</title>
	<atom:link href="http://www.zihni.net/tag/sql-enjeksiyon/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zihni.net</link>
	<description>Zihinsel şeyler, web programlama falan filan</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:36:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Güvenli PHP Uygulamaları-Sql enjektelerine karşı önlemler</title>
		<link>http://www.zihni.net/php/guvenli-php-uygulamalari-sql-enjeksiyon-saldirilarina-karsi-onlemler/</link>
		<comments>http://www.zihni.net/php/guvenli-php-uygulamalari-sql-enjeksiyon-saldirilarina-karsi-onlemler/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 22:09:38 +0000</pubDate>
		<dc:creator>Zihni</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[güvenlik]]></category>
		<category><![CDATA[mysql_real_escape_string]]></category>
		<category><![CDATA[sql enjeksiyon]]></category>

		<guid isPermaLink="false">http://www.zihni.net/?p=282</guid>
		<description><![CDATA[


Veritabanına gönderilen her bir değişken mysql_real_escape_string()
işlevinden geçirilerek sql enjeksiyonlarına karşı önlem alınabilir.
Bu işlev  veritabanları için özel anlamı olan karakterleri escape-kurtarma işlemine tabii tutar,
bu karakterlerden bazıları:  \n, \r, \, &#8216;, &#8221;
Aşağıdaki gibi kullanıcı adı ve şifre alanlarından oluşan bir giriş formumuz olsun:

1
2
3
4
5
6
7
8
9
10
11
12
&#60;html&#62;
&#60;head&#62;
&#60;meta http-equiv=&#34;Content-Type&#34; content=&#34;text/html; charset=UTF-8&#34; /&#62;
&#60;title&#62;mysql_real_escape_string&#60;/title&#62;
&#60;/head&#62;
&#60;body&#62;
&#60;form name=&#34;form1&#34; action=&#34;&#34; method=&#34;post&#34;&#62;
Kullanıcı adı:&#60;input type=&#34;text&#34; name=&#34;kullanici_adi&#34; value=&#34;&#34; /&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Veritabanına gönderilen her bir değişken mysql_real_escape_string()<br />
işlevinden geçirilerek sql enjeksiyonlarına karşı önlem alınabilir.<br />
Bu işlev  veritabanları için özel anlamı olan karakterleri escape-kurtarma işlemine tabii tutar,<br />
bu karakterlerden bazıları:  \n, \r, \, &#8216;, &#8221;</p>
<p>Aşağıdaki gibi kullanıcı adı ve şifre alanlarından oluşan bir giriş formumuz olsun:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>mysql_real_escape_string<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>form name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;form1&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
Kullanıcı adı<span style="color: #339933;">:&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;kullanici_adi&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">/&gt;</span> <span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
Şifre<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;password&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sifre&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">/&gt;</span> <span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;gönder&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;gonder&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Kullanıcının formdan aşağıdaki bilgileri gönderdiğini varsayalım.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'kullanici_adi'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'zihni'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sifre'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;' OR ''='&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Kullanıci adi ve sifre verilerinin veritabanında olup olmadığını kontrol edelim</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$sorgu</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM kullanicilar
WHERE kullanici_adi='<span style="color: #006699; font-weight: bold;">{$_POST['kullanici_adi']}</span>' AND sifre='<span style="color: #006699; font-weight: bold;">{$_POST['sifre']}</span>'&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sonuc</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorgu</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$satir</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sonuc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #000088;">$satir</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'kullanici_adi'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>mysql&#8217;e gönderilen sorgu aşağıdaki gibi olur:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">SELECT <span style="color: #339933;">*</span> FROM kullanicilar WHERE kullanici_adi<span style="color: #339933;">=</span><span style="color: #0000ff;">'zihni'</span> AND sifre<span style="color: #339933;">=</span><span style="color: #0000ff;">''</span> OR <span style="color: #0000ff;">''</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span></pre></td></tr></table></div>

<p>Görüldüğü gibi sifre geçersiz olsa bile sorgu sonucu başarılı olur.</p>
<p>Eğer formdan gelen bilgileri  mysql_real_escape_string() işlevinden geçirdikten sonra veritabanına gönderirsek:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$sorgu</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM kullanicilar
WHERE kullanici_adi='<span style="color: #009933; font-weight: bold;">%s</span>' AND sifre='<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span>
<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'kullanici_adi'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sifre'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sonuc</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorgu</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$satir</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sonuc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #000088;">$satir</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'kullanici_adi'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #000088;">$sorgu</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>mysql&#8217;e gönderilen sorgu aşağıdaki gibi olur ve bir sonuç döndürmez.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">SELECT <span style="color: #339933;">*</span> FROM kullanicilar WHERE kullanici_adi<span style="color: #339933;">=</span><span style="color: #0000ff;">'zihni'</span> AND sifre<span style="color: #339933;">=</span><span style="color: #0000ff;">'\'</span> OR \<span style="color: #0000ff;">'\'</span><span style="color: #339933;">=</span>\<span style="color: #0000ff;">''</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.zihni.net/php/guvenli-php-uygulamalari-sql-enjeksiyon-saldirilarina-karsi-onlemler/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
