⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 security.database.sql-injection.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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">&lt;?php<br /><br />$offset&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;beware,&nbsp;no&nbsp;input&nbsp;validation!<br /></span><span style="color: #0000BB">$query&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;id,&nbsp;name&nbsp;FROM&nbsp;products&nbsp;ORDER&nbsp;BY&nbsp;name&nbsp;LIMIT&nbsp;20&nbsp;OFFSET&nbsp;$offset;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>      </div>     </div>      Normal users click on the &#039;next&#039;, &#039;prev&#039; 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>&#039;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 &#039;crack&#039;, usesysid, &#039;t&#039;,&#039;t&#039;,&#039;crack&#039;    from pg_shadow where usename=&#039;postgres&#039;;--</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">&lt;?php<br /><br />$query&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;id,&nbsp;name,&nbsp;inserted,&nbsp;size&nbsp;FROM&nbsp;products<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;size&nbsp;=&nbsp;'$size'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ORDER&nbsp;BY&nbsp;$order&nbsp;LIMIT&nbsp;$limit,&nbsp;$offset;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">odbc_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</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>&#039;union select &#039;1&#039;, concat(uname||&#039;-&#039;||passwd) as name, &#039;1971-01-01&#039;, &#039;0&#039; from usertable;--</pre></div>      </div>     </div>     If this query (playing with the <i>&#039;</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&#039;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">&lt;?php<br />$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"UPDATE&nbsp;usertable&nbsp;SET&nbsp;pwd='$pwd'&nbsp;WHERE&nbsp;uid='$uid';"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>      </div>     </div>     But a malicious user sumbits the value     <i>&#039; or uid like&#039;%admin%&#039;; --</i> to <var class="varname">$uid</var> to     change the admin&#039;s password, or simply sets <var class="varname">$pwd</var> to     <i>&quot;hehehe&#039;, admin=&#039;yes&#039;, trusted=100 &quot;</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">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;$uid&nbsp;==&nbsp;'&nbsp;or&nbsp;uid&nbsp;like'%admin%';&nbsp;--<br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"UPDATE&nbsp;usertable&nbsp;SET&nbsp;pwd='...'&nbsp;WHERE&nbsp;uid=''&nbsp;or&nbsp;uid&nbsp;like&nbsp;'%admin%';&nbsp;--"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;$pwd&nbsp;==&nbsp;"hehehe',&nbsp;admin='yes',&nbsp;trusted=100&nbsp;"<br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"UPDATE&nbsp;usertable&nbsp;SET&nbsp;pwd='hehehe',&nbsp;admin='yes',&nbsp;trusted=100&nbsp;WHERE<br />...;"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>      </div>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -