{"id":2576,"date":"2024-08-14T10:31:00","date_gmt":"2024-08-14T02:31:00","guid":{"rendered":"https:\/\/systw.net\/note\/?p=2576"},"modified":"2025-08-24T14:03:34","modified_gmt":"2025-08-24T06:03:34","slug":"sc-price-oracle-manipulation","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/2576","title":{"rendered":"Price Oracle Manipulation"},"content":{"rendered":"\n<p>\u5728\u53bb\u4e2d\u5fc3\u5316\u91d1\u878d\uff08DeFi\uff09\u4e2d\uff0c\u9810\u8a00\u6a5f\uff08oracles\uff09\u626e\u6f14\u8457\u91cd\u8981\u89d2\u8272\uff0c\u5b83\u5011\u662f\u9023\u63a5\u5340\u584a\u93c8\u5167\u667a\u80fd\u5408\u7d04\u548c\u5340\u584a\u93c8\u5916\u73fe\u5be6\u4e16\u754c\u8cc7\u6599\u7684\u6a4b\u6a11\u3002\u7576\u667a\u80fd\u5408\u7d04\u9700\u8981\u77e5\u9053\u67d0\u500b\u8cc7\u7522\u7684\u5e02\u5834\u50f9\u683c\u6642\uff0c\u5b83\u6703\u5411\u9810\u8a00\u6a5f\u8acb\u6c42\u8cc7\u6599\u3002\u7136\u800c\uff0c\u5982\u679c\u653b\u64ca\u8005\u80fd\u5920\u64cd\u7e31\u9810\u8a00\u6a5f\u63d0\u4f9b\u7684\u8cc7\u6599\uff0c\u5c31\u6703\u5c0e\u81f4\u5408\u7d04\u505a\u51fa\u932f\u8aa4\u7684\u5224\u65b7\uff0c\u9032\u800c\u5f15\u767c\u4e00\u7cfb\u5217\u707d\u96e3\u6027\u5f8c\u679c\u3002<\/p>\n\n\n\n<p>\u653b\u64ca\u8005\u53ef\u80fd\u6703\u901a\u904e\u4ee5\u4e0b\u65b9\u5f0f\u5229\u7528\u9019\u500b\u6f0f\u6d1e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u64cd\u7e31\u9810\u8a00\u6a5f\u63d0\u4f9b\u7684\u50f9\u683c\u8cc7\u6599\uff0c\u8b93\u667a\u80fd\u5408\u7d04\u8aa4\u4ee5\u70ba\u67d0\u500b\u8cc7\u7522\u7684\u50f9\u503c\u767c\u751f\u4e86\u6539\u8b8a\u3002<\/li>\n\n\n\n<li>\u5229\u7528\u9019\u4e9b\u88ab\u64cd\u7e31\u7684\u50f9\u683c\uff0c\u9032\u884c\u672a\u7d93\u6388\u6b0a\u7684\u501f\u8cb8\u3001\u8d85\u984d\u69d3\u687f\u4ea4\u6613\uff0c\u751a\u81f3\u8017\u76e1\u6d41\u52d5\u6027\u6c60\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u6f0f\u6d1e\u7bc4\u4f8b<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b58\u5728\u6f0f\u6d1e\u7684\u667a\u80fd\u5408\u7d04\u7bc4\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ninterface IPriceFeed {\n    function getLatestPrice() external view returns (int);\n}\n\ncontract PriceOracleManipulation {\n    address public owner;\n    IPriceFeed public priceFeed;\n\n    constructor(address _priceFeed) {\n        owner = msg.sender;\n        priceFeed = IPriceFeed(_priceFeed);\n    }\n\n    function borrow(uint256 amount) public {\n        int price = priceFeed.getLatestPrice();  \n        require(price &gt; 0, \"Price must be positive\");\n\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\/\/ Vulnerability: No validation or protection against price manipulation<\/mark>\n        uint256 collateralValue = uint256(price) * amount;\n\n        \/\/ Borrow logic based on manipulated price\n        \/\/ If an attacker manipulates the oracle, they could borrow more than they should\n    }\n    \/\/ Repayment logic\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6f0f\u6d1e\u5206\u6790<\/strong>\uff1a<\/p>\n\n\n\n<p>\u9019\u500b\u5408\u7d04\u7684 <code>borrow<\/code> \u51fd\u6578\u76f4\u63a5\u4f7f\u7528\u5f9e\u9810\u8a00\u6a5f <code>priceFeed<\/code> \u7372\u53d6\u7684\u50f9\u683c\uff0c\u4f46\u6c92\u6709\u4efb\u4f55\u6a5f\u5236\u4f86\u9a57\u8b49\u9019\u500b\u50f9\u683c\u7684\u771f\u5be6\u6027\u6216\u5408\u7406\u6027\u3002\u5982\u679c\u653b\u64ca\u8005\u80fd\u64cd\u63a7 <code>priceFeed<\/code>\uff0c\u5c07\u8cc7\u7522\u50f9\u683c\u865b\u5831\uff0c\u4ed6\u5011\u5c31\u80fd\u4ee5\u8f03\u5c11\u7684\u62b5\u62bc\u54c1\u501f\u51fa\u66f4\u591a\u7684\u8cc7\u91d1\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5f71\u97ff<\/h4>\n\n\n\n<p>\u50f9\u683c\u9810\u8a00\u6a5f\u64cd\u7e31\u53ef\u80fd\u5c0e\u81f4\u4ee5\u4e0b\u56b4\u91cd\u5f8c\u679c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8d85\u984d\u501f\u8cb8<\/strong>\uff1a\u653b\u64ca\u8005\u53ef\u4ee5\u865b\u5831\u8cc7\u7522\u50f9\u683c\uff0c\u8b93\u5408\u7d04\u8aa4\u4ee5\u70ba\u5176\u62b5\u62bc\u54c1\u50f9\u503c\u66f4\u9ad8\uff0c\u5f9e\u800c\u501f\u51fa\u9060\u8d85\u904e\u5be6\u969b\u50f9\u503c\u7684\u8cc7\u91d1\u3002<\/li>\n\n\n\n<li><strong>\u932f\u8aa4\u6e05\u7b97<\/strong>\uff1a\u7576\u88ab\u64cd\u7e31\u7684\u50f9\u683c\u5c0e\u81f4\u62b5\u62bc\u54c1\u88ab\u932f\u8aa4\u5730\u4f4e\u4f30\u6642\uff0c\u53ef\u80fd\u4f7f\u7121\u8f9c\u7684\u7528\u6236\u5728\u4e0d\u8a72\u88ab\u6e05\u7b97\u6642\uff0c\u537b\u88ab\u5f37\u5236\u6e05\u7b97\u3002<\/li>\n\n\n\n<li><strong>\u8cc7\u91d1\u6c60\u67af\u7aed<\/strong>\uff1a\u653b\u64ca\u8005\u53ef\u4ee5\u5229\u7528\u88ab\u64cd\u7e31\u7684\u50f9\u683c\uff0c\u5728\u5e02\u5834\u4e0a\u9032\u884c\u5957\u5229\uff0c\u751a\u81f3\u8017\u76e1\u5408\u7d04\u4e2d\u7684\u6d41\u52d5\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u5982\u4f55\u4fee\u5fa9\uff1f<\/h4>\n\n\n\n<p>\u70ba\u4e86\u9632\u7bc4\u9019\u985e\u653b\u64ca\uff0c\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u88dc\u6551\u63aa\u65bd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u591a\u91cd\u9810\u8a00\u6a5f\u805a\u5408<\/strong>\uff1a\u4e0d\u8981\u50c5\u4f9d\u8cf4\u55ae\u4e00\u9810\u8a00\u6a5f\u3002\u53ef\u4ee5\u5f9e\u591a\u500b\u7368\u7acb\u4e14\u53ef\u9760\u7684\u9810\u8a00\u6a5f\u7372\u53d6\u8cc7\u6599\uff0c\u7136\u5f8c\u53d6\u5e73\u5747\u503c\u6216\u4e2d\u4f4d\u6578\uff0c\u4ee5\u964d\u4f4e\u55ae\u4e00\u4f86\u6e90\u88ab\u64cd\u7e31\u7684\u98a8\u96aa\u3002<\/li>\n\n\n\n<li><strong>\u8a2d\u5b9a\u50f9\u683c\u9580\u6abb<\/strong>\uff1a\u5728\u5408\u7d04\u4e2d\u8a2d\u5b9a\u53ef\u63a5\u53d7\u7684<strong>\u6700\u4f4e\uff08minimum\uff09\u548c\u6700\u9ad8\uff08maximum\uff09\u50f9\u683c\u7bc4\u570d<\/strong>\u3002\u5982\u679c\u9810\u8a00\u6a5f\u63d0\u4f9b\u7684\u50f9\u683c\u8d85\u51fa\u9019\u500b\u7bc4\u570d\uff0c\u5247\u62d2\u7d55\u8a72\u7b46\u4ea4\u6613\u3002<\/li>\n\n\n\n<li><strong>\u6642\u9593\u9396<\/strong>\uff1a\u5728\u50f9\u683c\u66f4\u65b0\u4e4b\u9593\u8a2d\u7f6e\u4e00\u500b\u6642\u9593\u9396\uff0c\u907f\u514d\u50f9\u683c\u77ac\u9593\u767c\u751f\u5287\u70c8\u8b8a\u5316\uff0c\u7d66\u653b\u64ca\u8005\u53ef\u4e58\u4e4b\u6a5f\u3002<\/li>\n\n\n\n<li><strong>\u8cc7\u6599\u9a57\u8b49<\/strong>\uff1a\u4f7f\u7528\u52a0\u5bc6\u8b49\u660e\uff08\u5982\u4fe1\u4efb\u65b9\u7c3d\u540d\uff09\u4f86\u9a57\u8b49\u9810\u8a00\u6a5f\u8cc7\u6599\u7684\u771f\u5be6\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fee\u5fa9\u5f8c\u7684\u7bc4\u4f8b<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4fee\u5fa9\u4e86\u4e0a\u8ff0\u6f0f\u6d1e\u7684\u5408\u7d04\u7bc4\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ninterface IPriceFeed {\n    function getLatestPrice() external view returns (int);\n}\n\ncontract PriceOracleManipulation {\n    address public owner;\n    IPriceFeed public priceFeed;\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">    int public minPrice = 1000; \/\/ Set minimum acceptable price\n    int public maxPrice = 2000; \/\/ Set maximum acceptable price<\/mark>\n\n    function borrow(uint256 amount) public {\n        int price = priceFeed.getLatestPrice();\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">require(price &gt; 0 &amp;&amp; price &gt;= minPrice &amp;&amp; price &lt;= maxPrice, \"Price manipulation detected\");<\/mark>\n\n        uint256 collateralValue = uint256(price) * amount;\n\n        \/\/ Borrow logic based on valid price\n    }\n\n    function repay(uint256 amount) public {\n        \/\/ Repayment logic\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u4fee\u5fa9\u5167\u5bb9<\/strong>\uff1a<\/p>\n\n\n\n<p>\u5728\u4fee\u5fa9\u5f8c\u7684\u7248\u672c\u4e2d\uff0c<code>borrow<\/code> \u51fd\u6578\u5728\u53d6\u5f97\u50f9\u683c\u5f8c\uff0c\u6703\u5148\u4f7f\u7528 <code>require<\/code> \u8a9e\u53e5\u6aa2\u67e5\u50f9\u683c\u662f\u5426\u843d\u5728\u9810\u8a2d\u7684 <strong><code>minPrice<\/code> \u548c <code>maxPrice<\/code><\/strong> \u7bc4\u570d\u5167\u3002\u9019\u6a23\u4e00\u4f86\uff0c\u5373\u4f7f\u9810\u8a00\u6a5f\u88ab\u653b\u64ca\uff0c\u5408\u7d04\u4e5f\u6703\u56e0\u70ba\u50f9\u683c\u4e0d\u5408\u7406\u800c\u62d2\u7d55\u57f7\u884c\uff0c\u5f9e\u800c\u4fdd\u8b77\u4e86\u5408\u7d04\u7684\u8cc7\u7522\u5b89\u5168\u3002<\/p>\n\n\n\n<p>\u65b0\u589e\u7a0b\u5f0f\u78bc\uff1a<code>int public minPrice = 1000;<\/code>\/\/ \u8a2d\u5b9a\u53ef\u63a5\u53d7\u7684\u6700\u4f4e\u50f9\u683c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><br><\/mark>\u65b0\u589e\u7a0b\u5f0f\u78bc\uff1a<code>int public maxPrice = 2000;<\/code>\/\/ \u8a2d\u5b9a\u53ef\u63a5\u53d7\u7684\u6700\u9ad8\u50f9\u683c<\/p>\n\n\n\n<p>\u65b0\u589e\u7a0b\u5f0f\u78bc\uff1a<code>require(price &gt; 0 &amp;&amp; price &gt;= minPrice &amp;&amp; price &lt;= maxPrice, \"Price manipulation detected\");<\/code>\/\/ \u95dc\u9375\u4fee\u5fa9\uff1a\u65b0\u589e\u50f9\u683c\u9a57\u8b49\uff0c\u78ba\u4fdd\u50f9\u683c\u5728\u9810\u8a2d\u7684\u7bc4\u570d\u5167<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u53bb\u4e2d\u5fc3\u5316\u91d1\u878d\uff08DeFi\uff09\u4e2d\uff0c\u9810\u8a00\u6a5f\uff08oracles\uff09\u626e\u6f14\u8457 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[371],"tags":[],"class_list":["post-2576","post","type-post","status-publish","format-standard","hentry","category-web3-security"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/comments?post=2576"}],"version-history":[{"count":3,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2576\/revisions"}],"predecessor-version":[{"id":2724,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2576\/revisions\/2724"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=2576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=2576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=2576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}