{"id":720,"date":"2023-10-25T09:01:01","date_gmt":"2023-10-25T01:01:01","guid":{"rendered":""},"modified":"2023-10-25T09:01:01","modified_gmt":"2023-10-25T01:01:01","slug":"\u667a\u80fd\u5408\u7ea6 \u786c\u7f16\u7801\u79c1\u94a5\uff08Hardcoded Private Key\uff09","status":"publish","type":"post","link":"https:\/\/mushiming.com\/720.html","title":{"rendered":"\u667a\u80fd\u5408\u7ea6 \u786c\u7f16\u7801\u79c1\u94a5\uff08Hardcoded Private Key\uff09"},"content":{"rendered":"
\n\u786c\u7f16\u7801\u79c1\u94a5\uff08Hardcoded Private Key\uff09\uff1a\u5408\u7ea6\u4e2d\u786c\u7f16\u7801\u7684\u79c1\u94a5\u53ef\u80fd\u88ab\u653b\u51fb\u8005\u53d1\u73b0\u548c\u6ee5\u7528\uff0c\u5bfc\u81f4\u8d44\u91d1\u88ab\u7a83\u53d6\u3002\u5f00\u53d1\u8005\u5e94\u907f\u514d\u5728\u5408\u7ea6\u4e2d\u660e\u6587\u5b58\u50a8\u79c1\u94a5\u6216\u5176\u4ed6\u654f\u611f\u4fe1\u606f\u3002<\/p>\n<\/blockquote>\n
\u6f0f\u6d1e\u4ee3\u7801<\/h2>\n
pragma solidity ^0.8.0;\n\ncontract HardcodedPrivateKey {\n mapping(address => uint256) private balances;\n\n constructor() {\n balances[msg.sender] = 1000;\n }\n\n function transfer(address to, uint256 amount) public {\n require(balances[msg.sender] >= amount, \"Insufficient balance\");\n balances[msg.sender] -= amount;\n balances[to] += amount;\n }\n\n function checkBalance(address account) public view returns (uint256) {\n \/\/ \u4f7f\u7528\u786c\u7f16\u7801\u7684\u79c1\u94a5\u8bbf\u95ee\u5176\u4ed6\u8d26\u6237\u4f59\u989d\n require(msg.sender == 0x1234567890, \"Unauthorized\");\n return balances[account];\n }\n}\n<\/code><\/pre>\n
\u5408\u7ea6\u89e3\u91ca\uff1a<\/p>\n
\n\u5728\u8fd9\u4e2a\u5408\u7ea6\u4e2d\uff0c\u6709\u4e00\u4e2a\u540d\u4e3a
HardcodedPrivateKey<\/code> \u7684\u5408\u7ea6\uff0c\u5305\u542b\u4e86\u4e00\u4e2a\u5b58\u50a8\u8d26\u6237\u4f59\u989d\u7684\u6620\u5c04
balances<\/code>\u3002\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u5408\u7ea6\u5c06\u90e8\u7f72\u8005\u7684\u8d26\u6237\u4f59\u989d\u521d\u59cb\u5316\u4e3a 1000\u3002<\/p>\n
\u5408\u7ea6\u63d0\u4f9b\u4e86
transfer<\/code> \u51fd\u6570\u7528\u4e8e\u8f6c\u8d26\uff0c\u5e76\u4f7f\u7528
balances<\/code> \u6620\u5c04\u6765\u8bb0\u5f55\u8d26\u6237\u4f59\u989d\u7684\u53d8\u5316\u3002\u53e6\u5916\uff0c\u5408\u7ea6\u8fd8\u63d0\u4f9b\u4e86
checkBalance<\/code> \u51fd\u6570\uff0c\u7528\u4e8e\u67e5\u8be2\u8d26\u6237\u4f59\u989d\u3002<\/p>\n
\u7136\u800c\uff0c\u8fd9\u4e2a\u5408\u7ea6\u5b58\u5728 Hardcoded Private Key \u7684\u98ce\u9669\u3002\u5728
checkBalance<\/code> \u51fd\u6570\u4e2d\uff0c\u901a\u8fc7\u786c\u7f16\u7801\u7684\u65b9\u5f0f\uff0c\u5c06\u5408\u7ea6\u7684\u6240\u6709\u8005\u7684\u5730\u5740\uff080x1234567890\uff09\u4e0e
msg.sender<\/code> \u8fdb\u884c\u6bd4\u8f83\uff0c\u4ee5\u9650\u5236\u53ea\u6709\u5408\u7ea6\u6240\u6709\u8005\u80fd\u591f\u8bbf\u95ee\u5176\u4ed6\u8d26\u6237\u7684\u4f59\u989d\u3002<\/p>\n
\u7531\u4e8e\u79c1\u94a5\u786c\u7f16\u7801\u5728\u5408\u7ea6\u4e2d\uff0c\u653b\u51fb\u8005\u53ef\u4ee5\u8f7b\u6613\u5730\u53d1\u73b0\u5e76\u4f7f\u7528\u76f8\u540c\u7684\u79c1\u94a5\uff0c\u4ece\u800c\u7ed5\u8fc7\u8bbf\u95ee\u63a7\u5236\uff0c\u83b7\u53d6\u5176\u4ed6\u8d26\u6237\u7684\u4f59\u989d\u4fe1\u606f\u3002<\/p>\n
\u4e3a\u4e86\u4fee\u590d Hardcoded Private Key \u653b\u51fb\uff0c\u5f00\u53d1\u8005\u5e94\u907f\u514d\u5728\u5408\u7ea6\u4e2d\u660e\u6587\u5b58\u50a8\u79c1\u94a5\u6216\u5176\u4ed6\u654f\u611f\u4fe1\u606f\u3002\u5408\u7ea6\u5e94\u91c7\u7528\u66f4\u5b89\u5168\u7684\u8eab\u4efd\u9a8c\u8bc1\u673a\u5236\uff0c\u5982\u57fa\u4e8e\u6743\u9650\u63a7\u5236\u7684\u8bbf\u95ee\u63a7\u5236\u4fee\u9970\u7b26\uff08\u5982
onlyOwner<\/code>\uff09\uff0c\u6216\u4f7f\u7528\u5916\u90e8\u8d26\u6237\u7b7e\u540d\u6765\u9a8c\u8bc1\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<\/blockquote>\n
\u653b\u51fb\u65b9\u6cd5<\/h2>\n
pragma solidity ^0.8.0;\n\ncontract HardcodedPrivateKeyAttack {\n HardcodedPrivateKey public targetContract;\n \n constructor(HardcodedPrivateKey _targetContract) {\n targetContract = HardcodedPrivateKey(_targetContract);\n }\n\n function attack() public view returns (uint256) {\n \/\/ \u8c03\u7528\u76ee\u6807\u5408\u7ea6\u7684 checkBalance \u51fd\u6570\uff0c\u7ed5\u8fc7\u8bbf\u95ee\u63a7\u5236\n return targetContract.checkBalance(address(this));\n }\n}\n<\/code><\/pre>\n
\u5408\u7ea6\u89e3\u91ca\uff1a<\/p>\n
\n\u5728\u8fd9\u4e2a\u653b\u51fb\u5408\u7ea6\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u540d\u4e3a
HardcodedPrivateKeyAttack<\/code> \u7684\u5408\u7ea6\u6765\u653b\u51fb
HardcodedPrivateKey<\/code> \u5408\u7ea6\u3002\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f20\u5165\u4e86\u76ee\u6807\u5408\u7ea6\u7684\u5b9e\u4f8b\u5730\u5740
_targetContract<\/code>\u3002<\/p>\n
\u653b\u51fb\u7684\u6838\u5fc3\u662f
attack<\/code> \u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u8c03\u7528\u76ee\u6807\u5408\u7ea6\u7684
checkBalance<\/code> \u51fd\u6570\uff0c\u5e76\u4f20\u9012\u5f53\u524d\u653b\u51fb\u5408\u7ea6\u7684\u5730\u5740\u4f5c\u4e3a\u53c2\u6570\u3002\u7531\u4e8e\u79c1\u94a5\u88ab\u786c\u7f16\u7801\u5728\u76ee\u6807\u5408\u7ea6\u4e2d\uff0c\u5e76\u4e14\u653b\u51fb\u8005\u4f7f\u7528\u4e86\u76f8\u540c\u7684\u79c1\u94a5\uff0c\u6240\u4ee5\u8bbf\u95ee\u63a7\u5236\u6761\u4ef6
require(msg.sender == 0x1234567890, \"Unauthorized\")<\/code> \u5c06\u4f1a\u901a\u8fc7\u9a8c\u8bc1\u3002<\/p>\n
\u901a\u8fc7\u6267\u884c\u8fd9\u4e2a\u653b\u51fb\u5408\u7ea6\u4e2d\u7684
attack<\/code> \u51fd\u6570\uff0c\u653b\u51fb\u8005\u53ef\u4ee5\u6210\u529f\u8c03\u7528
HardcodedPrivateKey<\/code> \u5408\u7ea6\u7684
checkBalance<\/code> \u51fd\u6570\uff0c\u5e76\u83b7\u53d6\u5176\u4ed6\u8d26\u6237\u7684\u4f59\u989d\u4fe1\u606f\u3002<\/p>\n<\/blockquote>\n
\u4fee\u590d\u65b9\u6cd5<\/h2>\n
pragma solidity ^0.8.0;\n\ncontract HardcodedPrivateKeyFixed {\n mapping(address => uint256) private balances;\n address private owner;\n\n constructor() {\n owner = msg.sender;\n balances[owner] = 1000;\n }\n\n function transfer(address to, uint256 amount) public {\n require(balances[msg.sender] >= amount, \"Insufficient balance\");\n balances[msg.sender] -= amount;\n balances[to] += amount;\n }\n\n function checkBalance(address account) public view returns (uint256) {\n require(msg.sender == owner || msg.sender == account, \"Unauthorized\");\n return balances[account];\n }\n}\n<\/code><\/pre>\n
\u5408\u7ea6\u89e3\u6790<\/p>\n
\n\u5728\u4fee\u590d\u540e\u7684\u5408\u7ea6\u4e2d\uff0c\u6211\u4eec\u5bf9
checkBalance<\/code> \u51fd\u6570\u8fdb\u884c\u4e86\u6539\u8fdb\u3002\u5728\u8bbf\u95ee\u63a7\u5236\u6761\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26
||<\/code> \u6765\u540c\u65f6\u9a8c\u8bc1\u5408\u7ea6\u7684\u6240\u6709\u8005\u548c\u8bbf\u95ee\u8005\u7684\u8eab\u4efd\u3002<\/p>\n
\u901a\u8fc7
require(msg.sender == owner || msg.sender == account, \"Unauthorized\")<\/code>\uff0c\u6211\u4eec\u8981\u6c42\u53ea\u6709\u5408\u7ea6\u7684\u6240\u6709\u8005\u6216\u8d26\u6237\u6301\u6709\u8005\u624d\u80fd\u591f\u8bbf\u95ee\u8d26\u6237\u4f59\u989d\u3002\u8fd9\u6837\uff0c\u9664\u4e86\u5408\u7ea6\u6240\u6709\u8005\uff0c\u5176\u4ed6\u4eba\u65e0\u6cd5\u7ed5\u8fc7\u8bbf\u95ee\u63a7\u5236\u83b7\u53d6\u4ed6\u4eba\u7684\u4f59\u989d\u4fe1\u606f\u3002<\/p>\n
\u901a\u8fc7\u8fd9\u79cd\u4fee\u590d\uff0c\u6211\u4eec\u6709\u6548\u5730\u9632\u6b62\u4e86 Hardcoded Private Key \u653b\u51fb\uff0c\u786e\u4fdd\u53ea\u6709\u5408\u7ea6\u6240\u6709\u8005\u548c\u8d26\u6237\u6301\u6709\u8005\u624d\u80fd\u591f\u8bbf\u95ee\u76f8\u5173\u7684\u8d26\u6237\u4f59\u989d\u4fe1\u606f\u3002\u5f00\u53d1\u8005\u5e94\u907f\u514d\u5728\u5408\u7ea6\u4e2d\u660e\u6587\u5b58\u50a8\u79c1\u94a5\u6216\u5176\u4ed6\u654f\u611f\u4fe1\u606f\uff0c\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u6743\u9650\u63a7\u5236\u673a\u5236\uff0c\u4ee5\u786e\u4fdd\u5408\u7ea6\u548c\u7528\u6237\u7684\u5b89\u5168\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"\u667a\u80fd\u5408\u7ea6 \u786c\u7f16\u7801\u79c1\u94a5\uff08Hardcoded Private Key\uff09\u5408\u7ea6\u4e2d\u786c\u7f16\u7801\u7684\u79c1\u94a5\u53ef\u80fd\u88ab\u653b\u51fb\u8005\u6ee5\u7528\uff0c\u5bfc\u81f4\u8d44\u91d1\u88ab\u7a83\u53d6\u3002\u4fee\u590d\u65b9\u6cd5\uff1a\u907f\u514d\u5728\u5408\u7ea6...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/720"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=720"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/720\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}