<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Development%3ASecurity%3ASQL_injection</id>
	<title>Development:Security:SQL injection - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Development%3ASecurity%3ASQL_injection"/>
	<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:Security:SQL_injection&amp;action=history"/>
	<updated>2026-05-06T20:50:07Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Development:Security:SQL_injection&amp;diff=11860&amp;oldid=prev</id>
		<title>Олег Давидович: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:Security:SQL_injection&amp;diff=11860&amp;oldid=prev"/>
		<updated>2024-10-21T08:53:11Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 08:53, 21 октября 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Олег Давидович</name></author>
	</entry>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Development:Security:SQL_injection&amp;diff=11859&amp;oldid=prev</id>
		<title>1&gt;Mits: ja link</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:Security:SQL_injection&amp;diff=11859&amp;oldid=prev"/>
		<updated>2010-02-06T05:34:37Z</updated>

		<summary type="html">&lt;p&gt;ja link&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This page forms part of the [[Development:Security|Moodle security guidelines]].&lt;br /&gt;
&lt;br /&gt;
==What is the danger?==&lt;br /&gt;
&lt;br /&gt;
Suppose your code in .../course/view.php?id=123 does something like&lt;br /&gt;
&amp;lt;code sql&amp;gt;&lt;br /&gt;
SELECT FROM mdl_course WHERE id = $id;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
where the $id = 123 has come from the URL. Suppose that your code does not bother to clean that parameter properly.&lt;br /&gt;
&lt;br /&gt;
Along comes Evil Hacker, and edits the URL to be&lt;br /&gt;
: .../course/view.php?id=123;DELETE+FROM+mdl_user&lt;br /&gt;
I will let you work out why that is a very, very bad thing.&lt;br /&gt;
&lt;br /&gt;
Of course, depending on exactly what the database query is, the malicious input needs to be constructed appropriately, but that is just a matter of trial and error for Evil Hacker.&lt;br /&gt;
&lt;br /&gt;
==How Moodle avoids this problem==&lt;br /&gt;
&lt;br /&gt;
Once again, it is a case of being very suspicious of any input that came from outside Moodle. In the example above, $id should clearly have been cleaned by passing PARAM_INT to required_param.&lt;br /&gt;
&lt;br /&gt;
It is more tricky with a query like&lt;br /&gt;
&amp;lt;code sql&amp;gt;&lt;br /&gt;
UPDATE mdl_user SET lastname = &amp;#039;$lastname&amp;#039; WHERE id = $id;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
What happens when $lastname is &amp;quot;O&amp;#039;Brian&amp;quot;? Well, you have to escape the &amp;#039; like this: &amp;quot;O\&amp;#039;Brian&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
In Moodle 1.9, addslashes is applied automatically to all input you get via required_param or optional_param.&lt;br /&gt;
&lt;br /&gt;
In Moodle 2.0 we completely avoid the dangerous process of building SQL by concatenating strings. In Moodle 2.0 the SQL would look like&lt;br /&gt;
&amp;lt;code sql&amp;gt;&lt;br /&gt;
UPDATE mdl_user SET lastname = ? WHERE id = ?;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
and then we would pass an array of values array($lastname, $id) to the database along with the SQL.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What you need to do in your code==&lt;br /&gt;
&lt;br /&gt;
In Moodle 2.0&lt;br /&gt;
* Use higher level dmllib methods, like get_record, whenever possible, so you do not have to create SQL yourself.&lt;br /&gt;
* When you have to insert values into SQL statements, use place-holders to insert the values safely.&lt;br /&gt;
&lt;br /&gt;
In Moodle 1.9&lt;br /&gt;
* Use higher level dmllib methods, like get_record, whenever possible, so you do not have to create SQL yourself.&lt;br /&gt;
* Data from required_param and optional_param have already had addslashes applied, ready to be used in database queries, but make sure you put single quotes round each value.&lt;br /&gt;
* If you have loaded some data from the database, and then want to re-insert it, then apply addslashes or addslashes_object to it first.&lt;br /&gt;
&lt;br /&gt;
* Test your code by using a tool like [http://sqlmap.sourceforge.net/ sqlmap], or by manually trying tricky inputs like&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&amp;lt; &amp;gt; &amp;amp; &amp;amp;amp;lt; &amp;amp;amp;gt; &amp;amp;amp;amp; &amp;#039; \&amp;#039; 碁 \ \\&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What you need to do as an administrator==&lt;br /&gt;
&lt;br /&gt;
* This is not something that administrators can do anything about (other than keeping your Moodle up-to-date).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* http://sqlmap.sourceforge.net/ - a tool for automatically finding SQL injection vulnerabilities.&lt;br /&gt;
* [[Development:Security]]&lt;br /&gt;
* [[Development:Coding]]&lt;br /&gt;
&lt;br /&gt;
{{CategoryDeveloper}}&lt;br /&gt;
[[Category:Security]]&lt;br /&gt;
&lt;br /&gt;
[[ja:開発:セキュリティ:SQLインジェクション]]&lt;/div&gt;</summary>
		<author><name>1&gt;Mits</name></author>
	</entry>
</feed>