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

📄 security.database.sql-injection.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
     </div>    </p>    <p class="para">     A frightening example how operating system level commands can be accessed     on some database hosts.     <div class="example">     <p><b>Example #4 Attacking the database hosts operating system (MSSQL 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;*&nbsp;FROM&nbsp;products&nbsp;WHERE&nbsp;id&nbsp;LIKE&nbsp;'%$prod%'"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mssql_query</span><span style="color: #007700">(</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>     If attacker submits the value     <i>a%&#039; exec master..xp_cmdshell &#039;net user test testpass /ADD&#039; --</i>     to <var class="varname">$prod</var>, then the <var class="varname">$query</var> will be:     <div class="informalexample">      <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;*&nbsp;FROM&nbsp;products<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;id&nbsp;LIKE&nbsp;'%a%'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exec&nbsp;master..xp_cmdshell&nbsp;'net&nbsp;user&nbsp;test&nbsp;testpass&nbsp;/ADD'--"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mssql_query</span><span style="color: #007700">(</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>     MSSQL Server executes the SQL statements in the batch including a command     to add a new user to the local accounts database. If this application     were running as <i>sa</i> and the MSSQLSERVER service is     running with sufficient privileges, the attacker would now have an     account with which to access this machine.    </p>    <blockquote><p><b class="note">Note</b>:            Some of the examples above is tied to a specific database server. This      does not mean that a similar attack is impossible against other products.      Your database server may be similarly vulnerable in another manner.     <br />    </p></blockquote>    <div id="security.database.avoiding" class="sect2">     <h3 class="title">Avoiding techniques</h3>     <p class="simpara">      You may plead that the attacker must possess a piece of information      about the database schema in most examples. You are right, but you      never know when and how it can be taken out, and if it happens,      your database may be exposed. If you are using an open source, or      publicly available database handling package, which may belong to a      content management system or forum, the intruders easily produce      a copy of a piece of your code. It may be also a security risk if it      is a poorly designed one.     </p>     <p class="simpara">      These attacks are mainly based on exploiting the code not being written      with security in mind. Never trust any kind of input, especially that      which comes from the client side, even though it comes from a select box,      a hidden input field or a cookie. The first example shows that such a      blameless query can cause disasters.     </p>     <ul class="itemizedlist">      <li class="listitem">       <span class="simpara">        Never connect to the database as a superuser or as the database owner.        Use always customized users with very limited privileges.       </span>      </li>      <li class="listitem">       <span class="simpara">        Check if the given input has the expected data type. PHP has        a wide range of input validating functions, from the simplest ones        found in <a href="ref.var.html" class="link">Variable Functions</a> and        in <a href="ref.ctype.html" class="link">Character Type Functions</a>        (e.g. <a href="function.is-numeric.html" class="function">is_numeric()</a>, <a href="function.ctype-digit.html" class="function">ctype_digit()</a>        respectively) and onwards to the        <a href="ref.pcre.html" class="link">Perl compatible Regular Expressions</a>        support.       </span>      </li>      <li class="listitem">       <p class="para">        If the application waits for numerical input, consider verifying data        with <a href="function.is-numeric.html" class="function">is_numeric()</a>, or silently change its type        using <a href="function.settype.html" class="function">settype()</a>, or use its numeric representation        by <a href="function.sprintf.html" class="function">sprintf()</a>.        <div class="example">         <p><b>Example #5 A more secure way to compose a query for paging</b></p>         <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />settype</span><span style="color: #007700">(</span><span style="color: #0000BB">$offset</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'integer'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$query&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 /><br /></span><span style="color: #FF8000">//&nbsp;please&nbsp;note&nbsp;%d&nbsp;in&nbsp;the&nbsp;format&nbsp;string,&nbsp;using&nbsp;%s&nbsp;would&nbsp;be&nbsp;meaningless<br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</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;%d;"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$offset</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>         </div>        </div>       </p>      </li>      <li class="listitem">       <span class="simpara">        Quote each non numeric user supplied value that is passed to the        database with the database-specific string escape function (e.g.        <a href="function.mysql-real-escape-string.html" class="function">mysql_real_escape_string()</a>,        <b>sql_escape_string()</b>, etc.). If a database-specific        string escape mechanism is not available, the        <a href="function.addslashes.html" class="function">addslashes()</a> and <a href="function.str-replace.html" class="function">str_replace()</a>        functions may be useful (depending on database type).        See <a href="security.database.storage.html" class="link">the first example</a>.        As the example shows, adding quotes to the static part of the query        is not enough, making this query easily crackable.       </span>      </li>      <li class="listitem">       <span class="simpara">        Do not print out any database specific information, especially        about the schema, by fair means or foul. See also <a href="security.errors.html" class="link">Error Reporting</a> and <a href="ref.errorfunc.html" class="link">Error Handling and Logging Functions</a>.       </span>      </li>      <li class="listitem">       <span class="simpara">        You may use stored procedures and previously defined cursors to abstract        data access so that users do not directly access tables or views, but        this solution has another impacts.       </span>      </li>     </ul>     <p class="simpara">      Besides these, you benefit from logging queries either within your script      or by the database itself, if it supports logging. Obviously, the logging is unable      to prevent any harmful attempt, but it can be helpful to trace back which      application has been circumvented. The log is not useful by itself, but      through the information it contains. More detail is generally better than less.     </p>    </div>   </div><hr /><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></body></html>

⌨️ 快捷键说明

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