[红日安全]代码审计Day4 - strpos使用不当引发漏洞

\u672c\u6587\u7531\u7ea2\u65e5\u5b89\u5168\u6210\u5458\uff1a l1nk3r \u7f16\u5199\uff0c\u5982\u6709\u4e0d\u5f53\uff0c\u8fd8\u671b\u65a7\u6b63\u3002

\u524d\u8a00

\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u662f\u7ea2\u65e5\u5b89\u5168-\u4ee3\u7801\u5ba1\u8ba1\u5c0f\u7ec4\u3002\u6700\u8fd1\u6211\u4eec\u5c0f\u7ec4\u6b63\u5728\u505a\u4e00\u4e2aPHP\u4ee3\u7801\u5ba1\u8ba1\u7684\u9879\u76ee\uff0c\u4f9b\u5927\u5bb6\u5b66\u4e60\u4ea4\u6d41\uff0c\u6211\u4eec\u7ed9\u8fd9\u4e2a\u9879\u76ee\u8d77\u4e86\u4e00\u4e2a\u540d\u5b57\u53eb PHP-Audit-Labs \u3002\u73b0\u5728\u5927\u5bb6\u6240\u770b\u5230\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u5c5e\u4e8e\u9879\u76ee \u7b2c\u4e00\u9636\u6bb5 \u7684\u5185\u5bb9\uff0c\u672c\u9636\u6bb5\u7684\u5185\u5bb9\u9898\u76ee\u5747\u6765\u81ea PHP SECURITY CALENDAR 2017 \u3002\u5bf9\u4e8e\u6bcf\u4e00\u9053\u9898\u76ee\uff0c\u6211\u4eec\u5747\u7ed9\u51fa\u5bf9\u5e94\u7684\u5206\u6790\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645CMS\u8fdb\u884c\u89e3\u8bf4\u3002\u5728\u6587\u7ae0\u7684\u6700\u540e\uff0c\u6211\u4eec\u8fd8\u4f1a\u7559\u4e00\u9053CTF\u9898\u76ee\uff0c\u4f9b\u5927\u5bb6\u7ec3\u4e60\uff0c\u5e0c\u671b\u5927\u5bb6\u559c\u6b22\u3002\u4e0b\u9762\u662f \u7b2c4\u7bc7 \u4ee3\u7801\u5ba1\u8ba1\u6587\u7ae0\uff1a

Day 4 - False Beard

\u9898\u76ee\u540d\u5b57\u53eb\u5047\u80e1\u5b50\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a

\u9898\u76ee\u89e3\u6790\uff1a

\u6211\u4eec\u770b\u5230 \u7b2c11\u884c \u548c \u7b2c12\u884c \uff0c\u7a0b\u5e8f\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff0c\u4f7f\u7528 xml \u7ed3\u6784\u5b58\u50a8\u7528\u6237\u7684\u767b\u5f55\u4fe1\u606f\u3002\u5b9e\u9645\u4e0a\u8fd9\u6837\u5f88\u5bb9\u6613\u9020\u6210\u6570\u636e\u6ce8\u5165\u3002\u7136\u540e \u7b2c21\u884c \u5b9e\u4f8b\u5316 Login \u7c7b\uff0c\u5e76\u5728 \u7b2c16\u884c \u5904\u8c03\u7528 login \u65b9\u6cd5\u8fdb\u884c\u767b\u9646\u64cd\u4f5c\u3002\u5728\u8fdb\u884c\u767b\u5f55\u64cd\u4f5c\u4e4b\u524d\uff0c\u4ee3\u7801\u5728 \u7b2c8\u884c \u548c \u7b2c9\u884c \u4f7f\u7528 strpos \u51fd\u6570\u6765\u9632\u6b62\u8f93\u5165\u7684\u53c2\u6570\u542b\u6709 < \u548c > \u7b26\u53f7\uff0c\u731c\u6d4b\u5f00\u53d1\u8005\u5e94\u8be5\u662f\u8003\u8651\u5230\u975e\u6cd5\u5b57\u7b26\u6ce8\u5165\u95ee\u9898\u3002\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b strpos \u51fd\u6570\u7684\u5b9a\u4e49\uff1a

strpos \u2014 \u67e5\u627e\u5b57\u7b26\u4e32\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e

\u4f5c\u7528\uff1a\u4e3b\u8981\u662f\u7528\u6765\u67e5\u627e\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002

\u7ed3\u6784\uff1a int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

\u5728\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c strpos \u51fd\u6570\u8fd4\u56de\u67e5\u627e\u5230\u7684\u5b50\u5b57\u7b26\u4e32\u7684\u4e0b\u6807\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5f00\u5934\u5c31\u662f\u6211\u4eec\u8981\u641c\u7d22\u7684\u76ee\u6807\uff0c\u5219\u8fd4\u56de\u4e0b\u6807 0 \uff1b\u5982\u679c\u641c\u7d22\u4e0d\u5230\uff0c\u5219\u8fd4\u56de false \u3002\u5728\u8fd9\u9053\u9898\u76ee\u4e2d\uff0c\u5f00\u53d1\u8005\u53ea\u8003\u8651\u5230 strpos \u51fd\u6570\u8fd4\u56de false \u7684\u60c5\u51b5\uff0c\u5374\u5ffd\u7565\u4e86\u5339\u914d\u5230\u7684\u5b57\u7b26\u5728\u9996\u4f4d\u65f6\u4f1a\u8fd4\u56de 0 \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a false \u548c 0 \u7684\u53d6\u53cd\u5747\u4e3a true \u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u7528\u6237\u540d\u548c\u5bc6\u7801\u9996\u5b57\u7b26\u6ce8\u5165 < \u7b26\u53f7\uff0c\u4ece\u800c\u6ce8\u5165xml\u6570\u636e\u3002\u6211\u4eec\u5c1d\u8bd5\u4f7f\u7528\u4ee5\u4e0b payload \uff0c\u89c2\u5bdf strpos \u51fd\u6570\u7684\u8fd4\u56de\u7ed3\u679c\u3002

user=<"><injected-tag%20property="&pass=<injected-tag>

\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u5f88\u660e\u663e\u662f\u53ef\u4ee5\u6ce8\u5165xml\u6570\u636e\u7684\u3002

\u5b9e\u4f8b\u5206\u6790

\u5b9e\u9645\u4e0a\uff0c\u672c\u6b21\u6f0f\u6d1e\u662f\u5f00\u53d1\u8005\u5bf9 strpos \u51fd\u6570\u7406\u89e3\u4e0d\u591f\uff0c\u6216\u8005\u8bf4\u662f\u5f00\u53d1\u8005\u8003\u8651\u4e0d\u5468\uff0c\u5bfc\u81f4\u8fc7\u6ee4\u65b9\u6cd5\u53ef\u88ab\u7ed5\u8fc7\u3002\u7531\u4e8e\u6211\u4eec\u6682\u65f6\u6ca1\u6709\u5728\u4e92\u8054\u7f51\u4e0a\u627e\u5230 strpos \u4f7f\u7528\u4e0d\u5f53\u5bfc\u81f4\u6f0f\u6d1e\u7684CMS\u6848\u4f8b\uff0c\u6240\u4ee5\u8fd9\u91cc\u53ea\u80fd\u9009\u53d6\u4e00\u4e2a\u76f8\u4f3c\u7684\u6f0f\u6d1e\u8fdb\u884c\u5206\u6790\uff0c\u540c\u6837\u662f\u5f00\u53d1\u8005\u9a8c\u8bc1\u4e0d\u591f\u5468\u5168\u5bfc\u81f4\u7684\u6f0f\u6d1e\u3002

\u672c\u6b21\u6848\u4f8b\uff0c\u6211\u4eec\u9009\u53d6 DeDecms V5.7SP2\u6b63\u5f0f\u7248 \u8fdb\u884c\u5206\u6790\uff0c\u8be5CMS\u5b58\u5728\u672a\u4fee\u590d\u7684\u4efb\u610f\u7528\u6237\u5bc6\u7801\u91cd\u7f6e\u6f0f\u6d1e\u3002\u6f0f\u6d1e\u7684\u89e6\u53d1\u70b9\u5728 member/resetpassword.php \u6587\u4ef6\u4e2d\uff0c\u7531\u4e8e\u5bf9\u63a5\u6536\u7684\u53c2\u6570 safeanswer \u6ca1\u6709\u8fdb\u884c\u4e25\u683c\u7684\u7c7b\u578b\u5224\u65ad\uff0c\u5bfc\u81f4\u53ef\u4ee5\u4f7f\u7528\u5f31\u7c7b\u578b\u6bd4\u8f83\u7ed5\u8fc7\u3002\u6211\u4eec\u6765\u770b\u770b\u76f8\u5173\u4ee3\u7801\uff1a

\u9488\u5bf9\u4e0a\u9762\u7684\u4ee3\u7801\u505a\u4e2a\u5206\u6790\uff0c\u5f53 $dopost \u7b49\u4e8e safequestion \u7684\u65f6\u5019\uff0c\u901a\u8fc7\u4f20\u5165\u7684 $mid \u5bf9\u5e94\u7684 id \u503c\u6765\u67e5\u8be2\u5bf9\u5e94\u7528\u6237\u7684\u5b89\u5168\u95ee\u9898\u3001\u5b89\u5168\u7b54\u6848\u3001\u7528\u6237id\u3001\u7535\u5b50\u90ae\u4ef6\u7b49\u4fe1\u606f\u3002\u8ddf\u8fdb\u5230 \u7b2c11\u884c \uff0c\u5f53\u6211\u4eec\u4f20\u5165\u7684\u95ee\u9898\u548c\u7b54\u6848\u975e\u7a7a\uff0c\u800c\u4e14\u7b49\u4e8e\u4e4b\u524d\u8bbe\u7f6e\u7684\u95ee\u9898\u548c\u7b54\u6848\uff0c\u5219\u8fdb\u5165 sn \u51fd\u6570\u3002\u7136\u800c\u8fd9\u91cc\u4f7f\u7528\u7684\u662f == \u800c\u4e0d\u662f === \u6765\u5224\u65ad\uff0c\u6240\u4ee5\u662f\u53ef\u4ee5\u7ed5\u8fc7\u7684\u3002\u5047\u8bbe\u7528\u6237\u6ca1\u6709\u8bbe\u7f6e\u5b89\u5168\u95ee\u9898\u548c\u7b54\u6848\uff0c\u90a3\u4e48\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5b89\u5168\u95ee\u9898\u7684\u503c\u4e3a 0 \uff0c\u7b54\u6848\u7684\u503c\u4e3a null \uff08\u8fd9\u91cc\u662f\u6570\u636e\u5e93\u4e2d\u7684\u503c\uff0c\u5373 $row[\'safequestion\="0" \u3001 $row[\'safeanswer\=null \uff09\u3002\u5f53\u6ca1\u6709\u8bbe\u7f6e safequestion \u548c safeanswer \u7684\u503c\u65f6\uff0c\u5b83\u4eec\u7684\u503c\u5747\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002\u7b2c11\u884c\u7684if\u8868\u8fbe\u5f0f\u4e5f\u5c31\u53d8\u6210\u4e86 if(\'0\' == \'\' && null == \'\') \uff0c\u5373 if(false && true) \uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u8981\u8ba9\u8868\u8fbe\u5f0f $row[\'safequestion\ == $safequestion \u4e3a true \u5373\u53ef\u3002\u4e0b\u56fe\u662f null == \'\' \u7684\u5224\u65ad\u7ed3\u679c\uff1a

\u6211\u4eec\u53ef\u4ee5\u5229\u7528 php\u5f31\u7c7b\u578b \u7684\u7279\u70b9\uff0c\u6765\u7ed5\u8fc7\u8fd9\u91cc $row[\'safequestion\ == $safequestion \u7684\u5224\u65ad\uff0c\u5982\u4e0b\uff1a

\u901a\u8fc7\u6d4b\u8bd5\u627e\u5230\u4e86\u4e09\u4e2a\u7684payload\uff0c\u5206\u522b\u662f 0.0 \u3001 0. \u3001 0e1 \uff0c\u8fd9\u4e09\u79cd\u7c7b\u578bpayload\u5747\u80fd\u4f7f\u5f97 $row[\'safequestion\ == $safequestion \u4e3a true \uff0c\u5373\u6210\u529f\u8fdb\u5165 sn \u51fd\u6570\u3002\u8ddf\u8fdb sn \u51fd\u6570\uff0c\u76f8\u5173\u4ee3\u7801\u5728 member/inc/inc_pwd_functions.php \u6587\u4ef6\u4e2d\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a

\u5728 sn \u51fd\u6570\u5185\u90e8\uff0c\u4f1a\u6839\u636eid\u5230pwd_tmp\u8868\u4e2d\u5224\u65ad\u662f\u5426\u5b58\u5728\u5bf9\u5e94\u7684\u4e34\u65f6\u5bc6\u7801\u8bb0\u5f55\uff0c\u6839\u636e\u7ed3\u679c\u786e\u5b9a\u5206\u652f\uff0c\u8d70\u5411 newmail \u51fd\u6570\u3002\u5047\u8bbe\u5f53\u524d\u6211\u4eec\u7b2c\u4e00\u6b21\u8fdb\u884c\u5fd8\u8bb0\u5bc6\u7801\u64cd\u4f5c\uff0c\u90a3\u4e48\u6b64\u65f6\u7684 $row \u5e94\u8be5\u4e3a\u7a7a\uff0c\u6240\u4ee5\u8fdb\u5165\u7b2c\u4e00\u4e2a if(!is_array($row)) \u5206\u652f\uff0c\u5728 newmail \u51fd\u6570\u4e2d\u6267\u884c INSERT \u64cd\u4f5c\uff0c\u76f8\u5173\u64cd\u4f5c\u4ee3\u7801\u4f4d\u7f6e\u5728 member/inc/inc_pwd_functions.php \u6587\u4ef6\u4e2d\uff0c\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1a

\u8be5\u4ee3\u7801\u4e3b\u8981\u529f\u80fd\u662f\u53d1\u9001\u90ae\u4ef6\u81f3\u76f8\u5173\u90ae\u7bb1\uff0c\u5e76\u4e14\u63d2\u5165\u4e00\u6761\u8bb0\u5f55\u81f3 dede_pwd_tmp \u8868\u4e2d\u3002\u800c\u6070\u597d\u6f0f\u6d1e\u7684\u89e6\u53d1\u70b9\u5c31\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u770b\u770b \u7b2c13\u884c \u81f3 \u7b2c18\u884c \u7684\u4ee3\u7801\uff0c\u5982\u679c ($send == \'N\') \u8fd9\u4e2a\u6761\u4ef6\u4e3a\u771f\uff0c\u901a\u8fc7 ShowMsg \u6253\u5370\u51fa\u4fee\u6539\u5bc6\u7801\u529f\u80fd\u7684\u94fe\u63a5\u3002 \u7b2c17\u884c \u4fee\u6539\u5bc6\u7801\u94fe\u63a5\u4e2d\u7684 $mid \u53c2\u6570\u5bf9\u5e94\u7684\u503c\u662f\u7528\u6237id\uff0c\u800c $randval \u662f\u5728\u7b2c\u4e00\u6b21 insert \u64cd\u4f5c\u7684\u65f6\u5019\u5c06\u5176 md5 \u52a0\u5bc6\u4e4b\u540e\u63d2\u5165\u5230 dede_pwd_tmp \u8868\u4e2d\uff0c\u5e76\u4e14\u5728\u8fd9\u91cc\u5df2\u7ecf\u76f4\u63a5\u56de\u663e\u7ed9\u7528\u6237\u3002\u90a3\u4e48\u8fd9\u91cc\u62fc\u63a5\u7684url\u5176\u5b9e\u662f

http://127.0.0.1/member/resetpassword.php?dopost=getpasswd&id=$mid&key=$randval

\u7ee7\u7eed\u8ddf\u8fdb\u4e00\u4e0b dopost=getpasswd \u7684\u64cd\u4f5c\uff0c\u76f8\u5173\u4ee3\u7801\u4f4d\u7f6e\u5728 member/resetpassword.php \u4e2d\uff0c

\u5728\u91cd\u7f6e\u5bc6\u7801\u7684\u65f6\u5019\u5224\u65ad\u8f93\u5165\u7684\u7528\u6237id\u662f\u5426\u6267\u884c\u8fc7\u91cd\u7f6e\u5bc6\u7801\uff0c\u5982\u679cid\u4e3a\u7a7a\u5219\u9000\u51fa\uff1b\u5982\u679c $row \u4e0d\u4e3a\u7a7a\uff0c\u5219\u4f1a\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u5185\u5bb9\uff0c\u76f8\u5173\u4ee3\u7801\u5728 member/resetpassword.php \u4e2d\u3002

\u4e0a\u56fe\u4ee3\u7801\u4f1a\u5148\u5224\u65ad\u662f\u5426\u8d85\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u8d85\u65f6\uff0c\u5219\u8fdb\u5165\u5bc6\u7801\u4fee\u6539\u9875\u9762\u3002\u5728\u5bc6\u7801\u4fee\u6539\u9875\u9762\u4f1a\u5c06 $setp \u8d4b\u503c\u4e3a2\u3002

\u7531\u4e8e\u73b0\u5728\u7684\u6570\u636e\u5305\u4e2d $setp=2 \uff0c\u56e0\u6b64\u8fd9\u90e8\u5206\u529f\u80fd\u4ee3\u7801\u5b9e\u73b0\u53c8\u56de\u5230\u4e86 member/resetpassword.php \u6587\u4ef6\u4e2d\u3002

\u4e0a\u56fe\u4ee3\u7801 \u7b2c6\u884c \u5224\u65ad\u4f20\u5165\u7684 $key \u662f\u5426\u7b49\u4e8e\u6570\u636e\u5e93\u4e2d\u7684 $row[\'pwd\ \uff0c\u5982\u679c\u76f8\u7b49\u5c31\u5b8c\u6210\u91cd\u7f6e\u5bc6\u7801\u64cd\u4f5c\uff0c\u81f3\u6b64\u4e5f\u5c31\u5b8c\u6210\u4e86\u6574\u4e2a\u653b\u51fb\u7684\u5206\u6790\u8fc7\u7a0b\u3002

\u6f0f\u6d1e\u9a8c\u8bc1

\u6211\u4eec\u5206\u522b\u6ce8\u518c test1 \uff0c test2 \u4e24\u4e2a\u8d26\u53f7

\u7b2c\u4e00\u6b65\u8bbf\u95ee payload \u4e2d\u7684 url

http://127.0.0.1/dedecms/member/resetpassword.php?dopost=safequestion&safequestion=0.0&safeanswer=&id=9

\u8fd9\u91cc test2 \u7684id\u662f9

\u901a\u8fc7\u6293\u5305\u83b7\u53d6\u5230 key \u503c\u3002

\u53bb\u6389\u591a\u4f59\u7684\u5b57\u7b26\u8bbf\u95ee\u4fee\u6539\u5bc6\u7801\u94fe\u63a5

http://192.168.31.240/dedecms/member/resetpassword.php?dopost=getpasswd&id=9&key=OTyEGJtg

\u6700\u540e\u6210\u529f\u4fee\u6539\u5bc6\u7801\uff0c\u6211\u5c06\u5bc6\u7801\u4fee\u6539\u6210 123456 \uff0c\u6570\u636e\u5e93\u4e2d test2 \u7684\u5bc6\u7801\u5b57\u6bb5\u4e5f\u53d8\u6210\u4e86 123456 \u52a0\u5bc6\u4e4b\u540e\u7684\u503c\u3002

\u4fee\u590d\u5efa\u8bae

\u9488\u5bf9\u4e0a\u9762 DeDecms\u4efb\u610f\u7528\u6237\u5bc6\u7801\u91cd\u7f6e \u6f0f\u6d1e\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528 === \u6765\u4ee3\u66ff == \u5c31\u884c\u4e86\u3002\u56e0\u4e3a === \u64cd\u4f5c\u4f1a\u540c\u65f6\u5224\u65ad\u5de6\u53f3\u4e24\u8fb9\u7684\u503c\u548c\u6570\u636e\u7c7b\u578b\u662f\u5426\u76f8\u7b49\uff0c\u82e5\u6709\u4e00\u4e2a\u4e0d\u7b49\uff0c\u5373\u8fd4\u56de false \u3002\u5177\u4f53\u4fee\u590d\u4ee3\u7801\u5982\u4e0b\uff1a

\u7ed3\u8bed

\u770b\u5b8c\u4e86\u4e0a\u8ff0\u5206\u6790\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u5bf9 strpos\u4f7f\u7528\u4e0d\u5f53 \u5f15\u53d1\u7684\u6f0f\u6d1e\u6709\u4e86\u66f4\u52a0\u6df1\u5165\u7684\u7406\u89e3\uff0c\u6587\u4e2d\u7528\u5230\u7684\u4ee3\u7801\u53ef\u4ee5\u4ece \u8fd9\u91cc \u4e0b\u8f7d\uff0c\u5f53\u7136\u6587\u4e2d\u82e5\u6709\u4e0d\u5f53\u4e4b\u5904\uff0c\u8fd8\u671b\u5404\u4f4d\u65a7\u6b63\u3002\u5982\u679c\u4f60\u5bf9\u6211\u4eec\u7684\u9879\u76ee\u611f\u5174\u8da3\uff0c\u6b22\u8fce\u53d1\u9001\u90ae\u4ef6\u5230 [email protected] \u8054\u7cfb\u6211\u4eec\u3002 Day4 \u7684\u5206\u6790\u6587\u7ae0\u5c31\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u6700\u540e\u7559\u4e86\u4e00\u9053CTF\u9898\u76ee\u7ed9\u5927\u5bb6\u7ec3\u624b\uff0c\u9898\u76ee\u5982\u4e0b\uff1a\u94fe\u63a5: https://pan.baidu.com/s/1pHjOVK0Ib-tjztkgBxe3nQ \u5bc6\u7801: 59t2