📄 security.database.sql-injection.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>SQL Injection</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="security.database.html">Database Security</a></div> <div class="next" style="text-align: right; float: right;"><a href="security.errors.html">Error Reporting</a></div> <div class="up"><a href="security.database.html">Database Security</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="security.database.sql-injection" class="sect1"> <h2 class="title">SQL Injection</h2> <p class="simpara"> Many web developers are unaware of how SQL queries can be tampered with, and assume that an SQL query is a trusted command. It means that SQL queries are able to circumvent access controls, thereby bypassing standard authentication and authorization checks, and sometimes SQL queries even may allow access to host operating system level commands. </p> <p class="simpara"> Direct SQL Command Injection is a technique where an attacker creates or alters existing SQL commands to expose hidden data, or to override valuable ones, or even to execute dangerous system level commands on the database host. This is accomplished by the application taking user input and combining it with static parameters to build a SQL query. The following examples are based on true stories, unfortunately. </p> <p class="para"> Owing to the lack of input validation and connecting to the database on behalf of a superuser or the one who can create users, the attacker may create a superuser in your database. <div class="example"> <p><b>Example #1 Splitting the result set into pages ... and making superusers (PostgreSQL) </b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$offset </span><span style="color: #007700">= </span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]; </span><span style="color: #FF8000">// beware, no input validation!<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> Normal users click on the 'next', 'prev' links where the <var class="varname">$offset</var> is encoded into the URL. The script expects that the incoming <var class="varname">$offset</var> is a decimal number. However, what if someone tries to break in by appending a <a href="function.urlencode.html" class="function">urlencode()</a>'d form of the following to the URL <div class="informalexample"> <div class="example-contents"><div class="cdata"><pre>0;insert into pg_shadow(usename,usesysid,usesuper,usecatupd,passwd) select 'crack', usesysid, 't','t','crack' from pg_shadow where usename='postgres';--</pre></div> </div> </div> If it happened, then the script would present a superuser access to him. Note that <i>0;</i> is to supply a valid offset to the original query and to terminate it. </p> <blockquote><p><b class="note">Note</b>: It is common technique to force the SQL parser to ignore the rest of the query written by the developer with <i>--</i> which is the comment sign in SQL. <br /> </p></blockquote> <p class="para"> A feasible way to gain passwords is to circumvent your search result pages. The only thing the attacker needs to do is to see if there are any submitted variables used in SQL statements which are not handled properly. These filters can be set commonly in a preceding form to customize <i>WHERE, ORDER BY, LIMIT</i> and <i>OFFSET</i> clauses in <i>SELECT</i> statements. If your database supports the <i>UNION</i> construct, the attacker may try to append an entire query to the original one to list passwords from an arbitrary table. Using encrypted password fields is strongly encouraged. <div class="example"> <p><b>Example #2 Listing out articles ... and some passwords (any database server) </b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$query </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT id, name, inserted, size FROM products<br /> WHERE size = '$size'<br /> ORDER BY $order LIMIT $limit, $offset;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">odbc_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> The static part of the query can be combined with another <i>SELECT</i> statement which reveals all passwords: <div class="informalexample"> <div class="example-contents"><div class="cdata"><pre>'union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable;--</pre></div> </div> </div> If this query (playing with the <i>'</i> and <i>--</i>) were assigned to one of the variables used in <var class="varname">$query</var>, the query beast awakened. </p> <p class="para"> SQL UPDATE's are also susceptible to attack. These queries are also threatened by chopping and appending an entirely new query to it. But the attacker might fiddle with the <i>SET</i> clause. In this case some schema information must be possessed to manipulate the query successfully. This can be acquired by examining the form variable names, or just simply brute forcing. There are not so many naming conventions for fields storing passwords or usernames. <div class="example"> <p><b>Example #3 From resetting a password ... to gaining more privileges (any database server) </b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$query </span><span style="color: #007700">= </span><span style="color: #DD0000">"UPDATE usertable SET pwd='$pwd' WHERE uid='$uid';"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> But a malicious user sumbits the value <i>' or uid like'%admin%'; --</i> to <var class="varname">$uid</var> to change the admin's password, or simply sets <var class="varname">$pwd</var> to <i>"hehehe', admin='yes', trusted=100 "</i> (with a trailing space) to gain more privileges. Then, the query will be twisted: <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// $uid == ' or uid like'%admin%'; --<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">"UPDATE usertable SET pwd='...' WHERE uid='' or uid like '%admin%'; --"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// $pwd == "hehehe', admin='yes', trusted=100 "<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">"UPDATE usertable SET pwd='hehehe', admin='yes', trusted=100 WHERE<br />...;"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -