{"id":22259,"date":"2024-11-21T10:21:51","date_gmt":"2024-11-21T02:21:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22259\/"},"modified":"2024-11-21T10:21:51","modified_gmt":"2024-11-21T02:21:51","slug":"thinkphp%e4%b8%ad%e5%8d%95%e5%bc%95%e5%8f%b7%e8%bd%ac%e4%b9%89%e7%9a%84%e7%bb%95%e8%bf%87%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22259\/","title":{"rendered":"ThinkPHP\u4e2d\u5355\u5f15\u53f7\u8f6c\u4e49\u7684\u7ed5\u8fc7\u65b9\u6cd5"},"content":{"rendered":"<p>thinkphp \u662f\u4e00\u4e2a\u6d41\u884c\u7684 php \u6846\u67b6\uff0c\u6211\u4eec\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u800c sql \u6ce8\u5165\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5b89\u5168\u5a01\u80c1\u3002\u4e3a\u4e86\u9632\u6b62 sql \u6ce8\u5165\u653b\u51fb\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\u3002\u5728\u4f7f\u7528\u6846\u67b6\u81ea\u8eab\u7684\u6570\u636e\u64cd\u4f5c\u51fd\u6570\u65f6\uff0c\u6846\u67b6\u5df2\u7ecf\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u4e86\u8f6c\u4e49\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u539f\u751f sql \u65f6\uff0c\u9700\u8981\u81ea\u884c\u5904\u7406\u8f6c\u4e49\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u4f1a\u4ecb\u7ecd thinkphp \u4e2d\u5355\u5f15\u53f7\u8f6c\u4e49\u7684\u7ed5\u8fc7\u65b9\u6cd5\u3002<\/p>\n<p>\u5728\u4f7f\u7528\u539f\u751f SQL \u7684\u65f6\u5019\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528 PDO \u9884\u5904\u7406\u8bed\u53e5\u6765\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>$sql = 'SELECT * FROM users WHERE username = :username';\n$sth = $dbh-&gt;prepare($sql);\n$sth-&gt;bindParam(':username', $username);\n$sth-&gt;execute();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u7684\u907f\u514d SQL \u6ce8\u5165\u653b\u51fb\uff0c\u56e0\u4e3a PDO \u4f1a\u81ea\u52a8\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u4f7f\u7528\u539f\u751f SQL\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u81ea\u5df1\u5904\u7406 SQL \u7684\u8f6c\u4e49\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>$username = $_GET['username'];\n$sql = \"SELECT * FROM users WHERE username = '\".addslashes($username).\"'\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u662f\u5e38\u89c1\u7684\u5904\u7406 SQL \u8f6c\u4e49\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7 addslashes \u51fd\u6570\u5c06\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\u3002\u4f46\u662f\u8fd9\u79cd\u65b9\u6cd5\u5e76\u4e0d\u5b89\u5168\uff0c\u56e0\u4e3a\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ed5\u8fc7 addslashes \u51fd\u6570\u6765\u8fdb\u884c SQL \u6ce8\u5165\u653b\u51fb\u3002\u5047\u8bbe\u6211\u4eec\u4f7f\u7528\u5355\u5f15\u53f7\u5c06\u7279\u6b8a\u5b57\u7b26\u5305\u88f9\u8d77\u6765\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>$username = \"123' OR '1'='1\";\n$sql = \"SELECT * FROM users WHERE username = '\".addslashes($username).\"'\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u53e5 SQL \u8bed\u53e5\u7684\u67e5\u8be2\u7ed3\u679c\u5c06\u4f1a\u8fd4\u56de\u6240\u6709\u7684\u7528\u6237\u4fe1\u606f\uff0c\u56e0\u4e3a\u6b64\u65f6 SQL \u8bed\u53e5\u7684\u903b\u8f91\u53d8\u6210\u4e86\uff1a<\/p>\n<pre>SELECT * FROM users WHERE username = '123' OR '1'='1'<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7531\u4e8e &#8216;1&#8217;=&#8217;1&#8242; \u603b\u662f\u6210\u7acb\uff0c\u6240\u4ee5\u8fd9\u6761 SQL \u8bed\u53e5\u67e5\u8be2\u7ed3\u679c\u7684\u662f\u6240\u6709\u7684\u7528\u6237\u4fe1\u606f\u3002\u8fd9\u5c31\u662f SQL \u6ce8\u5165\u7684\u539f\u7406\u3002\u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u65b9\u6cd5\u6765\u7ed5\u8fc7\u5355\u5f15\u53f7\u8f6c\u4e49\uff0c\u4f7f\u5f97\u5373\u4fbf\u4f7f\u7528\u4e86 &#8216; \u8fdb\u884c\u6ce8\u5165\u653b\u51fb\uff0c\u4e5f\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u5371\u5bb3\u3002<\/p>\n<p>\u7ed5\u8fc7\u5355\u5f15\u53f7\u8f6c\u4e49\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528\u53cc\u5f15\u53f7<\/li>\n<\/ol>\n<p>\u53cc\u5f15\u53f7\u5728 SQL \u4e2d\u662f\u4e00\u4e2a\u5408\u6cd5\u7684\u5b57\u7b26\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u7ed5\u8fc7\u5355\u5f15\u53f7\u8f6c\u4e49\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>$username = '123\" OR \"1\"=\"1';\n$sql = 'SELECT * FROM users WHERE username = \"'.$username.'\"';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6761 SQL \u8bed\u53e5\u7684\u67e5\u8be2\u7ed3\u679c\u5c06\u4f1a\u8fd4\u56de\u6240\u6709\u7684\u7528\u6237\u4fe1\u606f\uff0c\u56e0\u4e3a\u6b64\u65f6 SQL \u8bed\u53e5\u7684\u903b\u8f91\u53d8\u6210\u4e86\uff1a<\/p>\n<pre>SELECT * FROM users WHERE username = '123\" OR \"1\"=\"1'<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65f6\uff0c\u53cc\u5f15\u53f7\u4e2d\u7684\u5185\u5bb9\u4f1a\u88ab\u5f53\u505a\u4e00\u4e2a\u6574\u4f53\u800c\u88ab\u6267\u884c\uff0c\u4e0d\u4f1a\u53d7\u5230\u5355\u5f15\u53f7\u8f6c\u4e49\u7684\u5f71\u54cd\u3002\u56e0\u6b64\u4f7f\u7528\u53cc\u5f15\u53f7\u53ef\u4ee5\u6709\u6548\u7ed5\u8fc7\u5355\u5f15\u53f7\u8f6c\u4e49\uff0c\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528\u53cc\u5f15\u53f7\u53ef\u80fd\u4f1a\u9047\u5230\u8f6c\u4e49\u7684\u95ee\u9898\uff0c\u4f8b\u5982\uff1a\u53cc\u5f15\u53f7\u672c\u8eab\u5c31\u9700\u8981\u4f7f\u7528 &#8221; \u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528\u53cd\u659c\u6760<\/li>\n<\/ol>\n<p>\u53cd\u659c\u6760 &#8221; \u662f SQL \u4e2d\u7684\u8f6c\u4e49\u7b26\uff0c\u5728 SQL \u4e2d\u4f7f\u7528\u53cd\u659c\u6760\u6765\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>$username = '123' OR '1'='1';\n$sql = 'SELECT * FROM users WHERE username = \"'.$username.'\"';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65f6\uff0c\u8f6c\u4e49\u540e\u7684 SQL \u8bed\u53e5\u7684\u903b\u8f91\u53d8\u6210\u4e86\uff1a<\/p>\n<pre>SELECT * FROM users WHERE username = '123' OR '1'='1'<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u56e0\u4e3a &#8221; \u53ef\u4ee5\u5728 SQL \u7684\u8bed\u6cd5\u4e2d\u6b63\u5e38\u8bc6\u522b\uff0c\u6240\u4ee5\u4f7f\u7528 &#8221; \u6765\u8fdb\u884c\u8f6c\u4e49\u662f\u53ef\u884c\u7684\u3002\u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e &#8221; \u672c\u8eab\u5728 PHP \u4e2d\u4e5f\u662f\u8f6c\u4e49\u7b26\uff0c\u56e0\u6b64\u5728 PHP \u4e2d\u9700\u8981\u4f7f\u7528\u53cc\u91cd\u8f6c\u4e49\u7b26 &#8216;\\&#8221; \u6765\u8868\u793a &#8221;\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528 CHR \u51fd\u6570<\/li>\n<\/ol>\n<p>CHR \u51fd\u6570\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u6210\u5bf9\u5e94\u7684 ASCII \u7801\u5b57\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 CHR \u51fd\u6570\u6765\u5c06\u5355\u5f15\u53f7\u8f6c\u6362\u6210 ASCII \u7801\uff0c\u4ece\u800c\u7ed5\u8fc7\u5355\u5f15\u53f7\u8f6c\u4e49\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>$username = '123'.chr(39).' OR 1=1';\n$sql = 'SELECT * FROM users WHERE username = \"'.$username.'\"';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65f6\uff0c\u8f6c\u4e49\u540e\u7684 SQL \u8bed\u53e5\u7684\u903b\u8f91\u53d8\u6210\u4e86\uff1a<\/p>\n<pre>SELECT * FROM users WHERE username = '123' OR 1=1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u56e0\u4e3a chr(39) \u53ef\u4ee5\u5f97\u5230\u5355\u5f15\u53f7\u7684 ASCII \u7801\uff0c\u6240\u4ee5\u4f7f\u7528 CHR \u51fd\u6570\u4e5f\u80fd\u591f\u6709\u6548\u7ed5\u8fc7\u5355\u5f15\u53f7\u8f6c\u4e49\u3002<\/p>\n<p>\u7ed5\u8fc7\u5355\u5f15\u53f7\u8f6c\u4e49\u662f SQL \u6ce8\u5165\u653b\u51fb\u4e2d\u7684\u4e00\u4e2a\u5e38\u89c1\u6280\u5de7\uff0c\u8981\u60f3\u9632\u5fa1\u6b64\u7c7b\u653b\u51fb\uff0c\u9700\u8981\u6ce8\u610f\u5728\u4f7f\u7528\u539f\u751f SQL \u65f6\uff0c\u4e00\u5b9a\u8981\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\uff0c\u540c\u65f6\u9700\u8981\u6ce8\u610f\u4f7f\u7528\u8f6c\u4e49\u7684\u65b9\u5f0f\u3002\u5728\u4f7f\u7528\u6846\u67b6\u81ea\u8eab\u7684\u6570\u636e\u64cd\u4f5c\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u6709\u6548\u964d\u4f4e SQL \u6ce8\u5165\u653b\u51fb\u7684\u98ce\u9669\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP\u4e2d\u5355\u5f15\u53f7\u8f6c\u4e49\u7684\u7ed5\u8fc7\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>thinkphp \u662f\u4e00\u4e2a\u6d41\u884c\u7684 php \u6846\u67b6\uff0c\u6211\u4eec\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u800c sql \u6ce8\u5165\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5b89\u5168\u5a01\u80c1\u3002\u4e3a\u4e86\u9632\u6b62 sql \u6ce8\u5165\u653b\u51fb\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\u3002\u5728\u4f7f\u7528\u6846\u67b6\u81ea\u8eab\u7684\u6570\u636e\u64cd\u4f5c\u51fd\u6570\u65f6\uff0c\u6846\u67b6\u5df2\u7ecf\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u4e86\u8f6c\u4e49\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u539f\u751f sql \u65f6\uff0c\u9700\u8981\u81ea\u884c\u5904\u7406\u8f6c\u4e49\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u4f1a\u4ecb\u7ecd thinkphp \u4e2d\u5355\u5f15\u53f7\u8f6c\u4e49\u7684\u7ed5\u8fc7\u65b9\u6cd5\u3002 \u5728\u4f7f\u7528\u539f\u751f SQL \u7684\u65f6\u5019\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528 PDO \u9884\u5904\u7406\u8bed\u53e5\u6765\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\uff0c\u4f8b\u5982\uff1a $sql = &#8216;SELECT * FROM users WHERE username = :username&#8217;; $sth = $dbh-&gt;prepare($sql); $sth-&gt;bindParam(&#8216;:username&#8217;, $username); $sth-&gt;execute(); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u7684\u907f\u514d SQL \u6ce8\u5165\u653b\u51fb\uff0c\u56e0\u4e3a PDO \u4f1a\u81ea\u52a8\u5bf9\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002 \u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u4f7f\u7528\u539f\u751f SQL\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u81ea\u5df1\u5904\u7406 SQL \u7684\u8f6c\u4e49\u3002\u4f8b\u5982\uff1a $username = $_GET[&#8216;username&#8217;]; $sql = &#8220;SELECT * FROM users WHERE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22259","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22259"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22259\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}