{"id":2593,"date":"2024-08-14T00:30:00","date_gmt":"2024-08-13T16:30:00","guid":{"rendered":"https:\/\/systw.net\/note\/?p=2593"},"modified":"2025-08-22T21:26:33","modified_gmt":"2025-08-22T13:26:33","slug":"sc-denial-of-service","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/2593","title":{"rendered":"SC Denial of Service"},"content":{"rendered":"\n<p>\u62d2\u7d55\u670d\u52d9\u653b\u64ca\uff08Denial of Service, DoS\uff09\u5728 Solidity \u667a\u80fd\u5408\u7d04\u4e2d\u662f\u6307\u653b\u64ca\u8005\u901a\u904e\u5229\u7528\u5408\u7d04\u6f0f\u6d1e\uff0c\u8017\u76e1\u8cc7\u6e90\uff08\u5982 gas\u3001CPU \u9031\u671f\u6216\u5132\u5b58\u7a7a\u9593\uff09\uff0c\u4f7f\u5408\u7d04\u7121\u6cd5\u6b63\u5e38\u904b\u4f5c\uff0c\u5f9e\u800c\u963b\u6b62\u7528\u6236\u8207\u5408\u7d04\u4ea4\u4e92\u3002\u5e38\u898b\u7684 DoS \u653b\u64ca\u985e\u578b\u5305\u62ec\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Gas \u8017\u76e1\u653b\u64ca<\/strong>\uff1a\u653b\u64ca\u8005\u5275\u5efa\u9700\u8981\u5927\u91cf gas \u7684\u4ea4\u6613\uff0c\u4f7f\u5408\u7d04\u7121\u6cd5\u5b8c\u6210\u57f7\u884c\u3002<\/li>\n\n\n\n<li><strong>\u91cd\u5165\u653b\u64ca<\/strong>\uff1a\u5229\u7528\u5408\u7d04\u8abf\u7528\u9806\u5e8f\u6f0f\u6d1e\uff0c\u53cd\u8986\u8abf\u7528\u51fd\u6578\u4ee5\u8017\u76e1\u8cc7\u6e90\u6216\u5b58\u53d6\u672a\u6388\u6b0a\u8cc7\u91d1\u3002<\/li>\n\n\n\n<li><strong>\u5340\u584a Gas \u9650\u5236\u653b\u64ca<\/strong>\uff1a\u901a\u904e\u63d0\u4ea4\u5927\u91cf\u9ad8 gas \u6d88\u8017\u7684\u4ea4\u6613\uff0c\u4f54\u7528\u5340\u584a\u7684 gas \u9650\u5236\uff0c\u963b\u7919\u5176\u4ed6\u5408\u6cd5\u4ea4\u6613\u88ab\u8655\u7406\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u9019\u4e9b\u653b\u64ca\u7684\u76ee\u6a19\u662f\u4f7f\u5408\u7d04\u4e0d\u53ef\u7528\uff0c\u5f71\u97ff\u5176\u529f\u80fd\u3001\u7528\u6236\u9ad4\u9a57\u53ca\u76f8\u95dc\u53bb\u4e2d\u5fc3\u5316\u61c9\u7528\uff08dApp\uff09\u7684\u904b\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7bc4\u4f8b\uff08\u5b58\u5728\u6f0f\u6d1e\u7684\u5408\u7d04\uff09<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u6613\u53d7 DoS \u653b\u64ca\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.24;\n\ncontract Solidity_DOS {\n    address public king;\n    uint256 public balance;\n\n    function claimThrone() external payable {\n        require(msg.value > balance, \"Need to pay more to become the king\");\n\n        \/\/If the current king has a malicious fallback function that reverts, it will prevent the new king from claiming the throne, causing a Denial of Service.\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">(bool sent,) = king.call{value: balance}(\"\");<\/mark>\n        require(sent, \"Failed to send Ether\");\n\n        balance = msg.value;\n        king = msg.sender;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u5408\u7d04\u529f\u80fd<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9019\u662f\u4e00\u500b\u300c\u570b\u738b\u904a\u6232\u300d\u5408\u7d04\uff0c\u5141\u8a31\u7528\u6236\u901a\u904e\u652f\u4ed8\u6bd4\u7576\u524d <code>balance<\/code> \u66f4\u9ad8\u7684\u91d1\u984d\uff08<code>msg.value<\/code>\uff09\u4f86\u6210\u70ba\u65b0\u7684\u300c\u570b\u738b\u300d\uff08<code>king<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u7576\u65b0\u7528\u6236\u6210\u529f\u652f\u4ed8\u5f8c\uff0c\u820a\u7684 <code>king<\/code> \u6703\u6536\u5230\u9000\u6b3e\uff08\u7576\u524d\u7684 <code>balance<\/code>\uff09\uff0c\u7136\u5f8c <code>king<\/code> \u548c <code>balance<\/code> \u66f4\u65b0\u70ba\u65b0\u7528\u6236\u7684\u5730\u5740\u548c\u652f\u4ed8\u91d1\u984d\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6f0f\u6d1e\u5206\u6790<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u554f\u984c<\/strong>\uff1a\u5728 <code>claimThrone<\/code> \u51fd\u6578\u4e2d\uff0c\u5408\u7d04\u4f7f\u7528 <code>king.call{value: balance}(\"\")<\/code> \u5411\u820a\u7684 <code>king<\/code> \u767c\u9001\u4ee5\u592a\u5e63\u3002\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u820a\u7684 <code>king<\/code> \u662f\u4e00\u500b\u60e1\u610f\u5408\u7d04\uff0c\u4e14\u5176\u56de\u9000\u51fd\u6578\uff08<code>receive<\/code> \u6216 <code>fallback<\/code>\uff09\u6545\u610f\u56de\u6efe\uff08\u4f8b\u5982\uff0c\u901a\u904e <code>revert<\/code> \u6216\u7121\u9650\u8ff4\u5708\u8017\u76e1 gas\uff09\uff0c\u5247 <code>call<\/code> \u6703\u5931\u6557\uff0c\u5c0e\u81f4 <code>require(sent, \"Failed to send Ether\")<\/code> \u89f8\u767c\u4ea4\u6613\u56de\u6efe\u3002<\/li>\n\n\n\n<li>\u9019\u610f\u5473\u8457\u65b0\u7528\u6236\u7121\u6cd5\u6210\u70ba <code>king<\/code>\uff0c\u56e0\u70ba\u820a <code>king<\/code> \u7684\u60e1\u610f\u884c\u70ba\u963b\u6b62\u4e86\u5408\u7d04\u72c0\u614b\u7684\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>DoS \u98a8\u96aa<\/strong>\uff1a\u60e1\u610f <code>king<\/code> \u53ef\u4ee5\u6301\u7e8c\u963b\u6b62\u4efb\u4f55\u65b0\u7528\u6236\u66f4\u65b0 <code>king<\/code>\uff0c\u4f7f\u5408\u7d04\u7121\u6cd5\u6b63\u5e38\u904b\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u653b\u64ca\u5408\u7d04<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\ncontract AttackerContract_with_Receive {\n    address public targetContractAddress;\n\n    constructor(address _targetContractAddress) {\n        targetContractAddress = _targetContractAddress;\n    }\n\n    function attack() external payable {\n        (bool success, ) = targetContractAddress.call{value: msg.value}(\"\");\n        require(success, \"Failed to call claimThrone\");\n    }\n\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">    receive() external payable {\n        revert();\n    }<\/mark>\n}<\/code><\/pre>\n\n\n\n<p>\u4e00\u65e6\u5408\u7d04\u8a66\u5716\u9000\u9084 ETH \u7d66\u9019\u500b\u60e1\u610f\u5408\u7d04\u6642\uff0c\u6703\u89f8\u767c\u9019\u500b <code>receive()<\/code> \u51fd\u6578\u3002<code>revert()<\/code> \u8a9e\u53e5\u5c07\u6703\u5c0e\u81f4\u6574\u500b\u9000\u6b3e\u64cd\u4f5c\u5931\u6557\uff0c\u9032\u800c\u56de\u6efe <code>Solidity_DOS<\/code> \u5408\u7d04\u4e2d\u7684 <code>claimThrone()<\/code> \u4ea4\u6613\uff0c\u9032\u884c\u9396\u5b9a\u76ee\u6a19\u5408\u7d04\u7684\u529f\u80fd\uff0c\u9054\u5230 DoS \u653b\u64ca\u7684\u76ee\u7684\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5f71\u97ff<\/h4>\n\n\n\n<p>DoS \u653b\u64ca\u53ef\u80fd\u5c0e\u81f4\u4ee5\u4e0b\u5f8c\u679c\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5408\u7d04\u4e0d\u53ef\u7528<\/strong>\uff1a\u7528\u6236\u7121\u6cd5\u8207\u5408\u7d04\u4ea4\u4e92\uff0c\u5f71\u97ff\u95dc\u9375\u529f\u80fd\uff08\u5982\u8cc7\u91d1\u8f49\u79fb\u3001\u72c0\u614b\u66f4\u65b0\uff09\u3002<\/li>\n\n\n\n<li><strong>\u8ca1\u52d9\u640d\u5931<\/strong>\uff1a\u5728\u7ba1\u7406\u8cc7\u91d1\u6216\u8cc7\u7522\u7684 dApp \u4e2d\uff0cDoS \u653b\u64ca\u53ef\u80fd\u5c0e\u81f4\u8cc7\u91d1\u7121\u6cd5\u63d0\u53d6\u6216\u64cd\u4f5c\u4e2d\u65b7\uff0c\u9020\u6210\u7d93\u6fdf\u640d\u5931\u3002<\/li>\n\n\n\n<li><strong>\u8072\u8b7d\u640d\u5bb3<\/strong>\uff1a\u7528\u6236\u5c0d\u5408\u7d04\u53ca\u5176\u76f8\u95dc\u5e73\u53f0\u7684\u4fe1\u4efb\u4e0b\u964d\uff0c\u53ef\u80fd\u5c0e\u81f4\u7528\u6236\u6d41\u5931\u548c\u5546\u696d\u6a5f\u6703\u6e1b\u5c11\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fee\u5fa9\u65b9\u6cd5<\/h4>\n\n\n\n<p>\u70ba\u9632\u6b62 DoS \u653b\u64ca\uff0c\u958b\u767c\u8005\u61c9\u63a1\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n\n\n\n<p><strong>\u8655\u7406\u5916\u90e8\u8abf\u7528\u5931\u6557<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u975e\u963b\u585e\u7684\u5916\u90e8\u8abf\u7528\u8655\u7406\u65b9\u5f0f\uff0c\u4f8b\u5982\u7570\u6b65\u8655\u7406\u6216\u5c07\u8cc7\u91d1\u8f49\u79fb\u5ef6\u5f8c\u5230\u7368\u7acb\u51fd\u6578\uff0c\u78ba\u4fdd\u5408\u7d04\u4e0d\u56e0\u55ae\u4e00\u5931\u6557\u800c\u7671\u7613\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u907f\u514d\u9ad8 gas \u6d88\u8017<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8b39\u614e\u4f7f\u7528 <code>call<\/code>\uff08\u56e0\u70ba\u5b83\u5141\u8a31\u63a5\u6536\u8005\u57f7\u884c\u4efb\u610f\u7a0b\u5f0f\u78bc\uff09\u4ee5\u53ca\u8ff4\u5708\u6216\u904d\u6b77\u64cd\u4f5c\uff0c\u907f\u514d gas \u8017\u76e1\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>transfer<\/code> \u6216 <code>send<\/code>\uff08\u6709\u56fa\u5b9a gas \u9650\u5236\uff09\u4ee3\u66ff <code>call<\/code>\uff0c\u6e1b\u5c11\u60e1\u610f\u56de\u9000\u51fd\u6578\u7684\u5f71\u97ff\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5206\u6563\u6b0a\u9650<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u907f\u514d\u5c07\u904e\u591a\u6b0a\u9650\u96c6\u4e2d\u65bc\u55ae\u4e00\u89d2\u8272\uff08\u5982 <code>king<\/code>\uff09\u3002\u4f7f\u7528\u591a\u91cd\u7c3d\u540d\uff08Multi-Signature\uff09\u6216\u5206\u6563\u5f0f\u6b0a\u9650\u7ba1\u7406\uff0c\u964d\u4f4e\u55ae\u9ede\u6545\u969c\u98a8\u96aa\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u72c0\u614b\u66f4\u65b0\u512a\u5148<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u57f7\u884c\u5916\u90e8\u8abf\u7528\uff08\u5982\u8f49\u5e33\uff09\u524d\u66f4\u65b0\u5408\u7d04\u72c0\u614b\uff0c\u9632\u6b62\u56e0\u5916\u90e8\u8abf\u7528\u5931\u6557\u5c0e\u81f4\u72c0\u614b\u4e0d\u4e00\u81f4\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fee\u5fa9\u5f8c\u7684\u7bc4\u4f8b\u5408\u7d04<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4fee\u5fa9\u4e86 DoS \u6f0f\u6d1e\u7684\u5408\u7d04\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\ncontract Solidity_DOS {\n    address public king;\n    uint256 public balance;\n\n    \/\/ Use a safer approach to transfer funds, like transfer, which has a fixed gas stipend.\n    \/\/ This avoids using call and prevents issues with malicious fallback functions.\n    function claimThrone() external payable {\n        require(msg.value &gt; balance, \"Need to pay more to become the king\");\n\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">address previousKing = king;\n        uint256 previousBalance = balance;<\/mark>\n\n        \/\/ Update the state before transferring Ether to prevent reentrancy issues.\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">king = msg.sender;\n        balance = msg.value;<\/mark>\n\n        \/\/ Use transfer instead of call to ensure the transaction doesn't fail due to a malicious fallback.<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\n        payable(previousKing).transfer(previousBalance);<\/mark>\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u4fee\u5fa9\u5167\u5bb9<\/strong>\uff1a<\/p>\n\n\n\n<p><strong>\u4f7f\u7528 <code>transfer<\/code> \u4ee3\u66ff <code>call<\/code><\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>transfer<\/code> \u63d0\u4f9b\u56fa\u5b9a gas \u9650\u5236\uff082300 gas\uff09\uff0c\u9632\u6b62\u63a5\u6536\u8005\u7684\u56de\u9000\u51fd\u6578\u57f7\u884c\u8907\u96dc\u908f\u8f2f\u6216\u6545\u610f\u56de\u6efe\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8f49\u5e33\u5931\u6557\uff0c\u4ea4\u6613\u6703\u81ea\u52d5\u56de\u6efe\uff0c\u78ba\u4fdd\u5408\u7d04\u72c0\u614b\u4e00\u81f4\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u72c0\u614b\u66f4\u65b0\u512a\u5148<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u57f7\u884c <code>transfer<\/code> \u524d\uff0c\u5148\u66f4\u65b0 <code>king<\/code> \u548c <code>balance<\/code>\uff0c\u907f\u514d\u56e0\u8f49\u5e33\u5931\u6557\u5c0e\u81f4\u72c0\u614b\u672a\u66f4\u65b0\uff08\u4e5f\u6e1b\u5c11\u91cd\u5165\u98a8\u96aa\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5b89\u5168\u6027\u63d0\u5347<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5373\u4f7f\u820a <code>king<\/code> \u7684\u56de\u9000\u51fd\u6578\u60e1\u610f\u56de\u6efe\uff0c<code>transfer<\/code> \u7684\u5931\u6557\u4e0d\u6703\u963b\u6b62\u65b0 <code>king<\/code> \u767b\u57fa\uff0c\u89e3\u6c7a DoS \u554f\u984c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u7e3d\u7d50<\/h4>\n\n\n\n<p>\u62d2\u7d55\u670d\u52d9\u653b\u64ca\uff08DoS\uff09\u901a\u904e\u8017\u76e1\u8cc7\u6e90\u6216\u5229\u7528\u5916\u90e8\u8abf\u7528\u5931\u6557\u4f7f\u667a\u80fd\u5408\u7d04\u7121\u6cd5\u4f7f\u7528\u3002\u7bc4\u4f8b\u4e2d\u7684\u6f0f\u6d1e\u5408\u7d04\u56e0\u4f7f\u7528 <code>call<\/code> \u4e14\u672a\u59a5\u5584\u8655\u7406\u60e1\u610f\u56de\u9000\u51fd\u6578\uff0c\u5141\u8a31\u820a <code>king<\/code> \u963b\u6b62\u65b0\u7528\u6236\u66f4\u65b0\u72c0\u614b\uff0c\u5c0e\u81f4\u5408\u7d04\u4e0d\u53ef\u7528\u3002\u4fee\u5fa9\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b89\u5168\u7684 <code>transfer<\/code>\u3001\u512a\u5148\u66f4\u65b0\u72c0\u614b\u3001\u907f\u514d\u9ad8 gas \u64cd\u4f5c\u548c\u5206\u6563\u6b0a\u9650\u3002\u4fee\u5fa9\u5f8c\u7684\u5408\u7d04\u901a\u904e <code>transfer<\/code> \u548c\u72c0\u614b\u512a\u5148\u66f4\u65b0\u89e3\u6c7a\u4e86 DoS \u98a8\u96aa\uff0c\u78ba\u4fdd\u5408\u7d04\u529f\u80fd\u6b63\u5e38\u4e14\u7528\u6236\u53ef\u9806\u5229\u4ea4\u4e92\u3002\u958b\u767c\u8005\u61c9\u8b39\u614e\u8a2d\u8a08\u5916\u90e8\u8abf\u7528\u4e26\u9032\u884c\u5168\u9762\u6e2c\u8a66\uff0c\u4ee5\u9632\u6b62 DoS \u653b\u64ca\u5f71\u97ff\u5408\u7d04\u7684\u53ef\u9760\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u62d2\u7d55\u670d\u52d9\u653b\u64ca\uff08Denial of Service, DoS\uff09 &#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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[371],"tags":[],"class_list":["post-2593","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\/2593","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=2593"}],"version-history":[{"count":3,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2593\/revisions"}],"predecessor-version":[{"id":2711,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2593\/revisions\/2711"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=2593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=2593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=2593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}