<?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"
	>

<channel>
	<title>Suspekt... &#187; MySQL</title>
	<atom:link href="http://www.suspekt.org/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.suspekt.org</link>
	<description>A Blog About Code, Information Security, PHP And More</description>
	<pubDate>Wed, 05 Nov 2008 15:11:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Webinar &#8220;Bau sicherer LAMP Anwendungen&#8221;</title>
		<link>http://www.suspekt.org/2008/08/21/webinar-bau-sicherer-lamp-anwendungen/</link>
		<comments>http://www.suspekt.org/2008/08/21/webinar-bau-sicherer-lamp-anwendungen/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 08:48:52 +0000</pubDate>
		<dc:creator>Stefan Esser</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Security]]></category>

		<category><![CDATA[recording]]></category>

		<category><![CDATA[talk]]></category>

		<category><![CDATA[webinar]]></category>

		<guid isPermaLink="false">http://www.suspekt.org/?p=128</guid>
		<description><![CDATA[Last week I gave my first webinar for MySQL titled &#8220;Bau sicherer LAMP Anwendungen&#8221;. The webinar, which was a cooperation between MySQL and my company SektionEins, was held in german, covered SQL-Malware, SQL-Injection, safe programming and some tools to detect and block SQL-Injection attacks.
The recording of this webinar is now available on the MySQL site.
For [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I gave my first webinar for <a href="http://www.mysql.com">MySQL</a> titled &#8220;Bau sicherer LAMP Anwendungen&#8221;. The webinar, which was a cooperation between MySQL and my company <a href="http://www.sektioneins.com">SektionEins</a>, was held in german, covered SQL-Malware, SQL-Injection, safe programming and some tools to detect and block SQL-Injection attacks.</p>
<p>The recording of this webinar is <a href="http://www.mysql.de/news-and-events/on-demand-webinars/display-od-171.html">now available</a> on the MySQL site.</p>
<p>For those that only want to see my slides they are available on the MySQL site after registration or <a href="http://www.suspekt.org/downloads/BauSichererLampAnwendungen.pdf">here</a>.</p>
<p>Because it was a german webinar the recording and slides are in german, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.suspekt.org/2008/08/21/webinar-bau-sicherer-lamp-anwendungen/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MySQL and SQL Column Truncation Vulnerabilities</title>
		<link>http://www.suspekt.org/2008/08/18/mysql-and-sql-column-truncation-vulnerabilities/</link>
		<comments>http://www.suspekt.org/2008/08/18/mysql-and-sql-column-truncation-vulnerabilities/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 09:17:16 +0000</pubDate>
		<dc:creator>Stefan Esser</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.suspekt.org/?p=116</guid>
		<description><![CDATA[While SQL-Injection is one of the most discussed security problems in web applications other possible problems for SQL queries like overlong input are usually ignored although they can lead to all kinds of security problems.
This might be caused by the fact that security problems that are the result of overlong input are often buffer overflows [...]]]></description>
			<content:encoded><![CDATA[<p>While SQL-Injection is one of the most discussed security problems in web applications other possible problems for SQL queries like overlong input are usually ignored although they can lead to all kinds of security problems.</p>
<p>This might be caused by the fact that security problems that are the result of overlong input are often buffer overflows and buffer overflows are something many web application security experts know nothing about and choose to ignore.</p>
<p>There are however several security problems for SQL queries that are caused by overlong input and no one talks about.</p>
<h3>max_packet_size</h3>
<p>In MySQL there exists a configuration option called max_packet_size which is set to one megabyte by default and controls the maximum size of a packet sent between the SQL client and server. When queries or result rows do not fit into a single packet a error is raised. This means an overlong SQL query is never sent to the server and therefore never executed.</p>
<p>This can lead to security problems when an attacker is able to supply long data elements that are then used in SQL queries. A good example are logging queries that combine information like the HTTP User-Agent, session ids and log messages into a large query that then does not fit into the packet anymore.</p>
<p>Another example from a real world application is a session table cleanup process that first selects all sessions matching certain parameters into a PHP array, then performs a multiple level cleanup and in the end all selected session ids are put into single delete query. It should be obvious that when there are many session identifiers in the table that need deletion the query gets too long. The result of this is that flooding the application with new sessions in a short time will result in no unused session being deleted later anymore.</p>
<p>Therefore web application developers should always ensure that they do not sent overlong data to the server. And it doesn&#8217;t matter if they use prepared statements or not.</p>
<h3>SQL Column Truncation Vulnerabilities</h3>
<p>When user input is not checked for its length SQL Column Truncation Vulnerabilities can arise. &#8220;SQL Column Truncation Vulnerability&#8221; is the name I use to describe security problems arising from overlong input that is truncated during insertion in the database. By default MySQL will truncate strings longer than the defined maximum column width and only emit a warning. Those warnings are usually not seen by web applications and therefore not handled at all. In MySQL the sql_mode STRICT_ALL_TABLES can be activated to turn these warnings into errors but applications will run most of the time on servers that run in the default mode and even if an application uses the stricter sql_mode it should not produce this error in the first place. Therefore a length check is required.</p>
<p>To understand why the truncation on insert can lead to security problems imagine the following application.</p>
<ul>
<li>The application is a forum where new users can register</li>
<li>The administrator&#8217;s name is known e.g. &#8216;admin&#8217;</li>
<li>MySQL is used in the default mode</li>
<li>There is no application restriction on the length of new user names</li>
<li>The database column username is limited to 16 characters</li>
</ul>
<p>A potential attacker might now try to register the name &#8216;admin &#8216;, which will fail because the &#8216;isAlreadyRegistered&#8217; check will result in the SQL query.</p>

<div class="wp_syntax"><div class="code"><pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> user <span style="color: #993333; font-weight: bold;">WHERE</span> username<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'admin '</span></pre></div></div>

<p>Because MySQL does not compare strings in binary mode by default more relaxed comparison rules are used. One of these relaxations is that trailing space characters are ignored during the comparison. This means the string &#8216;admin    &#8216; is still equal to the string &#8216;admin&#8217; in the database. And therefore the application will refuse to accept the new user.</p>
<p>If the attacker however tries the username &#8216;admin           x&#8217; the application will search for it in the database and will not find it, because it is impossible to find a username with a length of 17 in a database field that has a 16 character limit. The application will accept the new username and insert it into the database. However the username column is to short for the full name and therefore it is truncated and &#8216;admin           &#8216; is inserted into the database.</p>
<p>The result of this is that the user table now contains two users that due to trailing spaces both will be returned when the SELECT query above is executed. At this point a potential security problem arises because now it depends on how the username is treated throughout the application. The following pseudocode for example is vulnerable.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$userdata</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>isPasswordCorrect<span style="color: #009900;">&#40;</span><span style="color: #000033;">$username</span><span style="color: #339933;">,</span> <span style="color: #000033;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000033;">$userdata</span> <span style="color: #339933;">=</span> getUserDataByLogin<span style="color: #009900;">&#40;</span><span style="color: #000033;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When the previous piece of code uses the SQL query</p>

<div class="wp_syntax"><div class="code"><pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> username <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">WHERE</span> username <span style="color: #66cc66;">=</span> ? <span style="color: #993333; font-weight: bold;">AND</span> passhash <span style="color: #66cc66;">=</span> ?</pre></div></div>

<p>to detect if the user password is correct and then does a lookup of the user data by name a security problem manifests.</p>

<div class="wp_syntax"><div class="code"><pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">WHERE</span> username <span style="color: #66cc66;">=</span> ?</pre></div></div>

<p>Because the attacker created the newly created admin user he knows the correct password to pass this check. And because the real admin user is first in the table it will be returned first when the user data lookup by name is executed later.</p>
<h3>Conclusion</h3>
<p>Both problems described here are two new things web applications needs to be audited for because both can lead to real security problems. And because no one searches for these kind of vulnerabilities, now that it is public most probably the next weeks will bring several advisories about open source software suffering from these problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.suspekt.org/2008/08/18/mysql-and-sql-column-truncation-vulnerabilities/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MySQL-Proxy learning to block SQL-Injection</title>
		<link>http://www.suspekt.org/2008/08/15/mysql-proxy-learning-to-block-sql-injection/</link>
		<comments>http://www.suspekt.org/2008/08/15/mysql-proxy-learning-to-block-sql-injection/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 09:32:41 +0000</pubDate>
		<dc:creator>Stefan Esser</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Projects]]></category>

		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.suspekt.org/?p=94</guid>
		<description><![CDATA[I previously reported about my joy with MySQL-Proxy and a simple SQL-Injection detection based on a simple heuristic.
Today I present the more interesting approach that I promised to publish after my webinar yesterday. This approach is based on the idea that SQL queries issued by an application always have a certain structure. This structure can [...]]]></description>
			<content:encoded><![CDATA[<p>I previously reported about <a href="http://www.suspekt.org/2008/08/05/mysql-proxy-heuristic-sql-injection-detection/">my joy with MySQL-Proxy</a> and a simple SQL-Injection detection based on a simple heuristic.</p>
<p>Today I present the more interesting approach that I promised to publish after my webinar yesterday. This approach is based on the idea that SQL queries issued by an application always have a certain structure. This structure can be learned and remembered by <a href="http://forge.mysql.com/wiki/MySQL_Proxy">MySQL-Proxy</a>. Any SQL query that has a different structure can then be considered an attack.</p>
<h3>Training Mode</h3>
<p>The first Lua script <a href="http://www.suspekt.org/downloads/learn_sql_queries.lua.gz">learn_sql_queries.lua</a> uses MySQL-Proxy&#8217;s read_query hook to catch COM_INIT_DB and COM_QUERY packets. COM_INIT_DB packets are issued when the database is changed and COM_QUERY packets contain normal queries.</p>
<p>When a change of database is detected a CREATE TABLE is injected into the communication to create a table called &#8216;allowed_queries&#8217; in the newly selected database. This table consist of only on column called &#8216;query&#8217;. Within this column normalized queries are collected.</p>
<p>When a normal query is received it is first tokenized by MySQL-Proxy&#8217;s tokenizer. The tokens are then used to recreate a normalized version of the query where all data values are replaced by the &#8216;?&#8217; placeholder. Additionally IN ( ?, ?, ?, &#8230;) statements are compressed to IN ( ? ) to allow arbitrary length IN value lists without having to learn all possibilities. The normalized query is then learned by inserting it into the table.</p>
<p>When all queries have been learned (maybe during development) the blocking mode can be started.</p>
<h3>Blocking Mode</h3>
<p>The second Lua script <a href="http://www.suspekt.org/downloads/block_unknown_queries.lua.gz">block_unknown_queries.lua</a> also uses MySQL-Proxy&#8217;s read_query hook to catch COM_INIT_DB and COM_QUERY packets.</p>
<p>When a change of database is detected a SELECT statement is injected into the communication that loads the table &#8216;allowed_queries&#8217; into a Lua-Table. The queries become the indices so that they can be found fast.</p>
<p>When a normal query is received it is first tokenized and normalized. The normalized Query is then searched in the Lua-Table which is just a key lookup. If the query is found in the table it is one of the known query structures that are allowed. The query is then executed as normally.</p>
<p>If the query is not found in the table it is either a query that was not learned by mistake or it is an SQL-Injection attack. The query is not executed and a database error &#8220;Possible SQL Injection&#8221; is returned.</p>
<p>Both proof of concept examples are released as GPL. Therefore feel free to modify them for your needs. You might prefer to just log SQL-Injection attempts instead of blocking them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.suspekt.org/2008/08/15/mysql-proxy-learning-to-block-sql-injection/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Mysql-Proxy Heuristic SQL Injection Detection</title>
		<link>http://www.suspekt.org/2008/08/05/mysql-proxy-heuristic-sql-injection-detection/</link>
		<comments>http://www.suspekt.org/2008/08/05/mysql-proxy-heuristic-sql-injection-detection/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 14:48:04 +0000</pubDate>
		<dc:creator>Stefan Esser</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Projects]]></category>

		<category><![CDATA[Security]]></category>

		<category><![CDATA[sql injection]]></category>

		<category><![CDATA[webinar]]></category>

		<guid isPermaLink="false">http://www.suspekt.org/?p=83</guid>
		<description><![CDATA[“MySQL Proxy is a simple program that sits between your client and MySQL server(s) that can monitor, analyze or transform their communication. Its flexibility allows for unlimited uses; common ones include: load balancing; failover; query analysis; query filtering and modification; and many more.”
The flexibility of MySQL Proxy is based on the fact that every aspect [...]]]></description>
			<content:encoded><![CDATA[<p>“<a href="http://forge.mysql.com/wiki/MySQL_Proxy">MySQL Proxy</a> is a simple program that sits between your client and MySQL server(s) that can monitor, analyze or transform their communication. Its flexibility allows for unlimited uses; common ones include: load balancing; failover; query analysis; query filtering and modification; and many more.”</p>
<p>The flexibility of MySQL Proxy is based on the fact that every aspect is scriptable with <a href="http://www.lua.org">Lua</a>. Because I am new to MySQL Proxy and the <a href="http://www.lua.org">Lua</a> language I tried to implement a very simple script that waits for incoming SQL queries, tokenizes them and tries to detect SQL Injection heuristically by searching for certain disallowed SQL functions, databases, tables, statements or comments. When an SQL query is believed to contain an SQL injection is it not executed and a <em>&#8220;Possible SQL injection&#8221;</em> error is returned.</p>
<p>You can grab the detect_sql_injection.lua script at</p>
<p><a href="http://www.suspekt.org/downloads/detect_sql_injection.lua.gz">http://www.suspekt.org/downloads/detect_sql_injection.lua.gz</a></p>
<p>If you are interested in this and german speaking you might also be interested in next week&#8217;s MySQL webinar <a href="http://www.mysql.de/news-and-events/web-seminars/display-171.html">&#8220;Bau sicherer LAMP Anwendungen&#8221;</a> where I will not only discuss this little Lua script but also another one that implements SQL injection detection by query structure learning.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.suspekt.org/2008/08/05/mysql-proxy-heuristic-sql-injection-detection/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
