{"id":248,"date":"2023-02-24T06:06:50","date_gmt":"2023-02-23T22:06:50","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=248"},"modified":"2023-04-30T15:19:37","modified_gmt":"2023-04-30T07:19:37","slug":"mysql-and-sql-injection","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/mysql-and-sql-injection\/","title":{"rendered":"MySQL \u53ca SQL \u6ce8\u5165"},"content":{"rendered":"<p>\u5982\u679c\u901a\u8fc7\u7f51\u9875\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u5e76\u5c06\u5176\u63d2\u5165\u4e00\u4e2aMySQL\u6570\u636e\u5e93\uff0c\u90a3\u4e48\u5c31\u6709\u53ef\u80fd\u53d1\u751fSQL\u6ce8\u5165\u5b89\u5168\u7684\u95ee\u9898\u3002<\/p>\n<p>\u90a3\u4e48\u5982\u4f55\u9632\u6b62SQL\u6ce8\u5165\uff0c\u5e76\u901a\u8fc7\u811a\u672c\u6765\u8fc7\u6ee4SQL\u4e2d\u6ce8\u5165\u7684\u5b57\u7b26\u3002<\/p>\n<p>\u6240\u8c13SQL\u6ce8\u5165\uff0c\u5c31\u662f\u901a\u8fc7\u628aSQL\u547d\u4ee4\u63d2\u5165\u5230Web\u8868\u5355\u9012\u4ea4\u6216\u8f93\u5165\u57df\u540d\u6216\u9875\u9762\u8bf7\u6c42\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u6700\u7ec8\u8fbe\u5230\u6b3a\u9a97\u670d\u52a1\u5668\u6267\u884c\u6076\u610f\u7684SQL\u547d\u4ee4\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u6c38\u8fdc\u4e0d\u8981\u4fe1\u4efb\u7528\u6237\u7684\u8f93\u5165\uff0c\u6211\u4eec\u5fc5\u987b\u8ba4\u5b9a\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u90fd\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u90fd\u9700\u8981\u5bf9\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u5904\u7406\u3002<\/p>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\uff0c\u8f93\u5165\u7684\u7528\u6237\u540d\u5fc5\u987b\u4e3a\u5b57\u6bcd\u3001\u6570\u5b57\u53ca\u4e0b\u5212\u7ebf\u7684\u7ec4\u5408\uff0c\u4e14\u7528\u6237\u540d\u957f\u5ea6\u4e3a 8 \u5230 20 \u4e2a\u5b57\u7b26\u4e4b\u95f4\uff1a<\/p>\n<pre><code class=\"language-php\">if (preg_match(&quot;\/^\\w{8,20}$\/&quot;, $_GET[&#039;username&#039;], $matches))\n{\n   $result = mysqli_query($conn, &quot;SELECT * FROM users \n                          WHERE username=$matches[0]&quot;);\n}\nelse \n{\n   echo &quot;username \u8f93\u5165\u5f02\u5e38&quot;;\n}<\/code><\/pre>\n<p>\u8ba9\u6211\u4eec\u770b\u4e0b\u5728\u6ca1\u6709\u8fc7\u6ee4\u7279\u6b8a\u5b57\u7b26\u65f6\uff0c\u51fa\u73b0\u7684SQL\u60c5\u51b5\uff1a<\/p>\n<pre><code class=\"language-php\">\/\/ \u8bbe\u5b9a$name \u4e2d\u63d2\u5165\u4e86\u6211\u4eec\u4e0d\u9700\u8981\u7684SQL\u8bed\u53e5\n$name = &quot;Qadir&#039;; DELETE FROM users;&quot;;\nmysqli_query($conn, &quot;SELECT * FROM users WHERE name=&#039;{$name}&#039;&quot;);<\/code><\/pre>\n<p>\u4ee5\u4e0a\u7684\u6ce8\u5165\u8bed\u53e5\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u5bf9<code>$name<\/code>\u7684\u53d8\u91cf\u8fdb\u884c\u8fc7\u6ee4\uff0c<code>$name<\/code>\u4e2d\u63d2\u5165\u4e86\u6211\u4eec\u4e0d\u9700\u8981\u7684SQL\u8bed\u53e5\uff0c\u5c06\u5220\u9664<code>users<\/code>\u8868\u4e2d\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n<p>\u5728PHP\u4e2d\u7684<code>mysqli_query()<\/code>\u662f\u4e0d\u5141\u8bb8\u6267\u884c\u591a\u4e2a SQL \u8bed\u53e5\u7684\uff0c\u4f46\u662f\u5728<code>SQLite<\/code>\u548c<code>PostgreSQL<\/code>\u662f\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u6761SQL\u8bed\u53e5\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u5bf9\u8fd9\u4e9b\u7528\u6237\u7684\u6570\u636e\u9700\u8981\u8fdb\u884c\u4e25\u683c\u7684\u9a8c\u8bc1\u3002<\/p>\n<p>\u9632\u6b62SQL\u6ce8\u5165\uff0c\u6211\u4eec\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u4e2a\u8981\u70b9\uff1a<\/p>\n<ul>\n<li>\n<ol>\n<li>\u6c38\u8fdc\u4e0d\u8981\u4fe1\u4efb\u7528\u6237\u7684\u8f93\u5165\u3002\u5bf9\u7528\u6237\u7684\u8f93\u5165\u8fdb\u884c\u6821\u9a8c\uff0c\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6216\u9650\u5236\u957f\u5ea6\uff1b\u5bf9\u5355\u5f15\u53f7\u548c\u53cc<code>-<\/code>\u8fdb\u884c\u8f6c\u6362\u7b49<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>\u6c38\u8fdc\u4e0d\u8981\u4f7f\u7528\u52a8\u6001\u62fc\u88c5sql\uff0c\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u5316\u7684sql\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\u8fdb\u884c\u6570\u636e\u67e5\u8be2\u5b58\u53d6<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>\u6c38\u8fdc\u4e0d\u8981\u4f7f\u7528\u7ba1\u7406\u5458\u6743\u9650\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4e3a\u6bcf\u4e2a\u5e94\u7528\u4f7f\u7528\u5355\u72ec\u7684\u6743\u9650\u6709\u9650\u7684\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"4\">\n<li>\u4e0d\u8981\u628a\u673a\u5bc6\u4fe1\u606f\u76f4\u63a5\u5b58\u653e\uff0c\u52a0\u5bc6\u6216\u8005hash\u6389\u5bc6\u7801\u548c\u654f\u611f\u7684\u4fe1\u606f<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"5\">\n<li>\u5e94\u7528\u7684\u5f02\u5e38\u4fe1\u606f\u5e94\u8be5\u7ed9\u51fa\u5c3d\u53ef\u80fd\u5c11\u7684\u63d0\u793a\uff0c\u6700\u597d\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u4fe1\u606f\u5bf9\u539f\u59cb\u9519\u8bef\u4fe1\u606f\u8fdb\u884c\u5305\u88c5<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"6\">\n<li>SQL\u6ce8\u5165\u7684\u68c0\u6d4b\u65b9\u6cd5\u4e00\u822c\u91c7\u53d6\u8f85\u52a9\u8f6f\u4ef6\u6216\u7f51\u7ad9\u5e73\u53f0\u6765\u68c0\u6d4b\uff0c\u8f6f\u4ef6\u4e00\u822c\u91c7\u7528sql\u6ce8\u5165\u68c0\u6d4b\u5de5\u5177jsky\uff0c\u7f51\u7ad9\u5e73\u53f0\u5c31\u6709\u4ebf\u601d\u7f51\u7ad9\u5b89\u5168\u5e73\u53f0\u68c0\u6d4b\u5de5\u5177\u3002MDCSOFT SCAN\u7b49\u3002\u91c7\u7528MDCSOFT-IPS\u53ef\u4ee5\u6709\u6548\u7684\u9632\u5fa1SQL\u6ce8\u5165\uff0cXSS\u653b\u51fb\u7b49<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h2>\u9632\u6b62SQL\u6ce8\u5165<\/h2>\n<p>\u5728\u811a\u672c\u8bed\u8a00\uff0c\u5982PHP\u53ef\u4ee5\u5bf9\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u8fdb\u884c\u8f6c\u4e49\u4ece\u800c\u6765\u9632\u6b62SQL\u6ce8\u5165<\/p>\n<p>PHP\u7684MySQL\u6269\u5c55\u63d0\u4f9b<code>mysqli_real_escape_string()<\/code>\u51fd\u6570\u6765\u8f6c\u4e49\u7279\u6b8a\u7684\u8f93\u5165\u5b57\u7b26<\/p>\n<pre><code class=\"language-php\">if (get_magic_quotes_gpc()) \n{\n  $name = stripslashes($name);\n}\n$name = mysqli_real_escape_string($conn, $name);\nmysqli_query($conn, &quot;SELECT * FROM users WHERE name=&#039;{$name}&#039;&quot;);<\/code><\/pre>\n<h2>Like\u8bed\u53e5\u4e2d\u7684\u6ce8\u5165<\/h2>\n<p>like\u67e5\u8be2\u65f6\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u503c\u6709<code>_<\/code>\u548c<code>%<\/code>\uff0c\u5219\u4f1a\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\uff1a\u7528\u6237\u672c\u6765\u53ea\u662f\u60f3\u67e5\u8be2<code>abcd_<\/code>\uff0c\u67e5\u8be2\u7ed3\u679c\u4e2d\u5374\u6709<code>abcd_<\/code>\u3001<code>abcde<\/code>\u3001<code>abcdf<\/code>\u7b49\u7b49\uff1b\u7528\u6237\u8981\u67e5\u8be2<code>30%<\/code>\uff08\u6ce8\uff1a\u767e\u5206\u4e4b\u4e09\u5341\uff09\u65f6\u4e5f\u4f1a\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p>\u5728PHP\u811a\u672c\u4e2d\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>addcslashes()<\/code>\u51fd\u6570\u6765\u5904\u7406\u4ee5\u4e0a\u60c5\u51b5\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n<pre><code class=\"language-php\">$sub = addcslashes(mysqli_real_escape_string($conn, &quot;%something_&quot;), &quot;%_&quot;);\n\/\/ $sub == \\%something\\_\nmysqli_query($conn, &quot;SELECT * FROM messages WHERE subject LIKE &#039;{$sub}%&#039;&quot;);\naddcslashes() \u51fd\u6570\u5728\u6307\u5b9a\u7684\u5b57\u7b26\u524d\u6dfb\u52a0\u53cd\u659c\u6760\u3002<\/code><\/pre>\n<p>\u8bed\u6cd5\u683c\u5f0f:<\/p>\n<pre><code class=\"language-php\">addcslashes(string, characters)<\/code><\/pre>\n<ul>\n<li><code>string<\/code> \u5fc5\u9700\uff0c\u89c4\u5b9a\u8981\u68c0\u67e5\u7684\u5b57\u7b26\u4e32<\/li>\n<li><code>characters<\/code> \u53ef\u9009\uff0c\u89c4\u5b9a\u53d7<code>addcslashes()<\/code>\u5f71\u54cd\u7684\u5b57\u7b26\u6216\u5b57\u7b26\u8303\u56f4<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u901a\u8fc7\u7f51\u9875\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u5e76\u5c06\u5176\u63d2\u5165\u4e00\u4e2aMySQL\u6570\u636e\u5e93\uff0c\u90a3\u4e48\u5c31\u6709\u53ef\u80fd\u53d1\u751fSQL\u6ce8\u5165\u5b89\u5168\u7684\u95ee\u9898\u3002 \u90a3\u4e48\u5982\u4f55 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[86],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-sql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=248"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}