{"id":1804,"date":"2025-02-18T23:03:21","date_gmt":"2025-02-18T15:03:21","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1804"},"modified":"2025-02-18T23:03:21","modified_gmt":"2025-02-18T15:03:21","slug":"mysql-%e5%8f%8a-sql-%e6%b3%a8%e5%85%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/18\/mysql-%e5%8f%8a-sql-%e6%b3%a8%e5%85%a5\/","title":{"rendered":"MySQL \u53ca SQL \u6ce8\u5165"},"content":{"rendered":"\n<p>SQL \u6ce8\u5165\uff08SQL Injection\uff09\u662f\u6307\u653b\u51fb\u8005\u901a\u8fc7\u5c06\u6076\u610f\u7684 SQL \u4ee3\u7801\u63d2\u5165\u5230\u8f93\u5165\u6846\u6216 URL \u53c2\u6570\u4e2d\uff0c\u4ece\u800c\u7834\u574f\u5e94\u7528\u7a0b\u5e8f\u4e0e\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u6b63\u5e38\u4ea4\u4e92\u3002\u5b83\u662f Web \u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u5b89\u5168\u6f0f\u6d1e\u4e4b\u4e00\u3002\u5982\u679c\u4e0d\u52a0\u4ee5\u9632\u8303\uff0c\u653b\u51fb\u8005\u80fd\u591f\u83b7\u53d6\u3001\u7be1\u6539\u6216\u5220\u9664\u6570\u636e\u5e93\u4e2d\u7684\u654f\u611f\u6570\u636e\uff0c\u751a\u81f3\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u6709\u6548\u9632\u6b62 SQL \u6ce8\u5165\uff0c\u4e86\u89e3 SQL \u6ce8\u5165\u7684\u539f\u7406\u53ca\u5176\u9632\u62a4\u63aa\u65bd\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e MySQL \u53ca SQL \u6ce8\u5165\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5305\u62ec\u5b9e\u4f8b\u4e0e\u9632\u62a4\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>SQL \u6ce8\u5165\u7684\u539f\u7406<\/strong><\/h3>\n\n\n\n<p>SQL \u6ce8\u5165\u53d1\u751f\u7684\u6839\u672c\u539f\u56e0\u662f\u5e94\u7528\u7a0b\u5e8f\u76f4\u63a5\u5c06\u7528\u6237\u8f93\u5165\u7684\u503c\u62fc\u63a5\u5230 SQL \u67e5\u8be2\u4e2d\uff0c\u800c\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u8fc7\u6ee4\u6216\u5904\u7406\u3002\u5f53\u653b\u51fb\u8005\u5728\u8f93\u5165\u6846\u4e2d\u8f93\u5165 SQL \u4ee3\u7801\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u4f1a\u5c06\u8fd9\u4e9b\u4ee3\u7801\u5f53\u4f5c\u67e5\u8be2\u7684\u4e00\u90e8\u5206\u6267\u884c\uff0c\u9020\u6210\u9884\u671f\u4e4b\u5916\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4e0d\u5b89\u5168\u7684 SQL \u67e5\u8be2<\/h4>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u8868\u5355\uff0c\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM users WHERE username = 'user_input' AND password = 'password_input';<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u4efb\u4f55\u9a8c\u8bc1\u6216\u6e05\u7406\uff0c\u653b\u51fb\u8005\u53ef\u4ee5\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\u6765\u7ed5\u8fc7\u767b\u5f55\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>user_input = ' OR '1' = '1<\/code><\/li>\n\n\n\n<li><code>password_input = ' OR '1' = '1<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u6837\uff0c\u6784\u9020\u51fa\u6765\u7684 SQL \u67e5\u8be2\u5c06\u53d8\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM users WHERE username = '' OR '1' = '1' AND password = '' OR '1' = '1';<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8e <code>'1' = '1'<\/code> \u603b\u662f\u6210\u7acb\uff0c\u653b\u51fb\u8005\u5c06\u6210\u529f\u7ed5\u8fc7\u8eab\u4efd\u9a8c\u8bc1\uff0c\u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u9632\u6b62 SQL \u6ce8\u5165<\/strong><\/h3>\n\n\n\n<p>\u4e3a\u4e86\u9632\u6b62 SQL \u6ce8\u5165\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u51e0\u79cd\u9632\u62a4\u63aa\u65bd\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">a. <strong>\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\u548c\u53c2\u6570\u5316\u67e5\u8be2<\/strong><\/h4>\n\n\n\n<p>\u6700\u6709\u6548\u7684\u9632\u6b62 SQL \u6ce8\u5165\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\uff08Prepared Statements\uff09\u548c\u53c2\u6570\u5316\u67e5\u8be2\u3002\u9884\u5904\u7406\u8bed\u53e5\u5141\u8bb8\u5728 SQL \u67e5\u8be2\u4e2d\u4f7f\u7528\u5360\u4f4d\u7b26\uff0c\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u503c\u4e0d\u4f1a\u76f4\u63a5\u62fc\u63a5\u5230\u67e5\u8be2\u5b57\u7b26\u4e32\u4e2d\uff0c\u4ece\u800c\u9632\u6b62\u6076\u610f SQL \u4ee3\u7801\u7684\u6ce8\u5165\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\uff08PHP + MySQLi\uff09<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"testdb\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\n$user_input = $_POST&#91;'username'];\n$password_input = $_POST&#91;'password'];\n\n\/\/ \u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\u9632\u6b62SQL\u6ce8\u5165\n$stmt = $conn-&gt;prepare(\"SELECT * FROM users WHERE username = ? AND password = ?\");\n$stmt-&gt;bind_param(\"ss\", $user_input, $password_input);  \/\/ \"ss\" \u8868\u793a\u4e24\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u53c2\u6570\n$stmt-&gt;execute();\n$result = $stmt-&gt;get_result();\n\n\/\/ \u5982\u679c\u627e\u5230\u4e86\u7ed3\u679c\nif ($result-&gt;num_rows &gt; 0) {\n    echo \"\u767b\u5f55\u6210\u529f\";\n} else {\n    echo \"\u65e0\u6548\u7684\u7528\u6237\u540d\u6216\u5bc6\u7801\";\n}\n\n$stmt-&gt;close();\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\uff0c<code>$user_input<\/code> \u548c <code>$password_input<\/code> \u5c06\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 SQL \u67e5\u8be2\uff0c\u6570\u636e\u5e93\u5c06\u6b63\u786e\u5904\u7406\u5b83\u4eec\uff0c\u800c\u4e0d\u4f1a\u6267\u884c\u7528\u6237\u8f93\u5165\u4e2d\u7684 SQL \u4ee3\u7801\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">b. <strong>\u4f7f\u7528 ORM \u6846\u67b6<\/strong><\/h4>\n\n\n\n<p>ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u6846\u67b6\uff08\u5982 Django ORM\u3001Laravel Eloquent \u7b49\uff09\u901a\u5e38\u5df2\u7ecf\u5185\u7f6e\u4e86\u9632\u6b62 SQL \u6ce8\u5165\u7684\u673a\u5236\u3002ORM \u81ea\u52a8\u5c06\u7528\u6237\u8f93\u5165\u7684\u503c\u8f6c\u4e49\u6216\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\uff0c\u4ece\u800c\u51cf\u5c11\u4e86 SQL \u6ce8\u5165\u7684\u98ce\u9669\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">c. <strong>\u8fc7\u6ee4\u548c\u8f6c\u4e49\u7528\u6237\u8f93\u5165<\/strong><\/h4>\n\n\n\n<p>\u867d\u7136\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\u662f\u6700\u4f73\u5b9e\u8df5\uff0c\u4f46\u6709\u65f6\u4e5f\u53ef\u4ee5\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u8fc7\u6ee4\u6216\u8f6c\u4e49\uff0c\u907f\u514d\u7279\u6b8a\u5b57\u7b26\uff08\u5982\u5355\u5f15\u53f7\u3001\u53cc\u5f15\u53f7\u3001\u5206\u53f7\u7b49\uff09\u88ab\u6267\u884c\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u8f6c\u4e49\u7528\u6237\u8f93\u5165\uff08PHP\uff09<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$user_input = mysqli_real_escape_string($conn, $_POST&#91;'username']);\n$password_input = mysqli_real_escape_string($conn, $_POST&#91;'password']);\n\n$query = \"SELECT * FROM users WHERE username = '$user_input' AND password = '$password_input'\";<\/code><\/pre>\n\n\n\n<p><code>mysqli_real_escape_string<\/code> \u51fd\u6570\u4f1a\u5c06\u7528\u6237\u8f93\u5165\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u8f6c\u4e49\uff0c\u4ece\u800c\u907f\u514d SQL \u6ce8\u5165\u3002\u4f46\u662f\uff0c\u6700\u597d\u662f\u7ed3\u5408\u4f7f\u7528\u9884\u5904\u7406\u8bed\u53e5\uff0c\u800c\u4e0d\u662f\u5355\u7eaf\u4f9d\u8d56\u4e8e\u8f6c\u4e49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">d. <strong>\u9650\u5236\u6570\u636e\u5e93\u6743\u9650<\/strong><\/h4>\n\n\n\n<p>\u786e\u4fdd\u6570\u636e\u5e93\u8d26\u6237\u7684\u6743\u9650\u6700\u4f4e\u5316\uff0c\u4ec5\u6388\u4e88\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u5fc5\u8981\u8868\u548c\u6570\u636e\u7684\u6743\u9650\u3002\u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u4e0d\u9700\u8981\u5220\u9664\u6570\u636e\uff0c\u90a3\u4e48\u5c31\u4e0d\u8981\u8d4b\u4e88\u5220\u9664\u6743\u9650\u3002\u901a\u8fc7\u9650\u5236\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u6743\u9650\uff0c\u53ef\u4ee5\u51cf\u5c11 SQL \u6ce8\u5165\u653b\u51fb\u5e26\u6765\u7684\u635f\u5bb3\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">e. <strong>\u9519\u8bef\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h4>\n\n\n\n<p>\u4e0d\u8981\u5c06\u6570\u636e\u5e93\u9519\u8bef\u76f4\u63a5\u66b4\u9732\u7ed9\u7528\u6237\u3002\u653b\u51fb\u8005\u53ef\u4ee5\u901a\u8fc7\u9519\u8bef\u6d88\u606f\u83b7\u77e5\u6570\u636e\u5e93\u7ed3\u6784\uff0c\u4ece\u800c\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u653b\u51fb\u3002\u76f8\u53cd\uff0c\u5e94\u6355\u83b7\u548c\u8bb0\u5f55\u9519\u8bef\uff0c\u63d0\u4f9b\u901a\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u9690\u85cf\u9519\u8bef\u4fe1\u606f<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u7981\u6b62\u663e\u793a\u6570\u636e\u5e93\u9519\u8bef\nini_set('display_errors', 0); \nerror_reporting(E_ALL);\n\n\/\/ \u5728\u9519\u8bef\u65e5\u5fd7\u4e2d\u8bb0\u5f55\u9519\u8bef\nlog_error(\"SQL Error: \" . $conn-&gt;error);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u68c0\u6d4b SQL \u6ce8\u5165<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u68c0\u6d4b SQL \u6ce8\u5165\u6f0f\u6d1e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u6f0f\u6d1e\u626b\u63cf\u5de5\u5177\uff08\u5982 SQLmap\uff09\u6765\u68c0\u6d4b Web \u5e94\u7528\u662f\u5426\u6613\u53d7 SQL \u6ce8\u5165\u653b\u51fb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>SQL \u6ce8\u5165<\/strong>\uff1a\u653b\u51fb\u8005\u901a\u8fc7\u8f93\u5165\u6076\u610f SQL \u4ee3\u7801\uff0c\u64cd\u63a7\u6570\u636e\u5e93\u6267\u884c\u672a\u6388\u6743\u7684\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u9632\u62a4\u63aa\u65bd<\/strong>\uff1a\u6700\u6709\u6548\u7684\u9632\u62a4\u65b9\u6cd5\u662f\u4f7f\u7528 <strong>\u9884\u5904\u7406\u8bed\u53e5\u548c\u53c2\u6570\u5316\u67e5\u8be2<\/strong>\uff0c\u5176\u4ed6\u65b9\u6cd5\u5982\u8fc7\u6ee4\u548c\u8f6c\u4e49\u7528\u6237\u8f93\u5165\u4e5f\u53ef\u4ee5\u63d0\u4f9b\u4e00\u5b9a\u7a0b\u5ea6\u7684\u4fdd\u62a4\u3002<\/li>\n\n\n\n<li><strong>\u5de5\u5177\u68c0\u6d4b<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\uff08\u5982 SQLmap\uff09\u8fdb\u884c SQL \u6ce8\u5165\u6f0f\u6d1e\u68c0\u6d4b\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u7f16\u7a0b\u4e60\u60ef<\/strong>\uff1a\u59cb\u7ec8\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u3001\u6e05\u7406\u548c\u8fc7\u6ee4\uff0c\u9650\u5236\u6570\u636e\u5e93\u6743\u9650\uff0c\u907f\u514d\u66b4\u9732\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u91c7\u53d6\u5408\u9002\u7684\u9632\u62a4\u63aa\u65bd\uff0c\u53ef\u4ee5\u6709\u6548\u964d\u4f4e SQL \u6ce8\u5165\u653b\u51fb\u7684\u98ce\u9669\uff0c\u4fdd\u969c\u6570\u636e\u5e93\u5b89\u5168\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL \u6ce8\u5165\uff08SQL Injection\uff09\u662f\u6307\u653b\u51fb\u8005\u901a\u8fc7\u5c06\u6076\u610f\u7684 SQL \u4ee3\u7801\u63d2\u5165\u5230\u8f93\u5165\u6846\u6216 URL \u53c2\u6570\u4e2d\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1804","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1804"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1804\/revisions"}],"predecessor-version":[{"id":1805,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1804\/revisions\/1805"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}