当前位置:网站首页>Summary of tx.origin security issues
Summary of tx.origin security issues
2022-07-04 17:13:00 【Foal】
In the contract code , The most common is to use msg.sender To check the authorization , But sometimes because some programmers are unfamiliar tx.origin and msg.sender The difference between , If used tx.origin May lead to contract safety problems . The most typical attack scenario for hackers is to use tx.origin Code problems are often combined with phishing attacks The way to attack .
tx.origin yes Solidity A global variable in , It returns the account address where the transaction was sent .
By calling tx.origin To check the authorization may lead to contract attack , because tx.origin Return the original sender of the transaction , Because the call chain of the attack may be the original sender -> Attack contracts -> Attacked contract . In the attacked contract ,tx.origin Is the original sender .
Pre knowledge
EOA Accounts and contract accounts
There are two kinds of Ethereum accounts , External accounts (EOA) And the contract account (SCA).
- The external account is managed by a pair of public and private keys , The account contains Ether Balance of .
- Contract accounts can contain Ether Outside the balance , It also has a specific piece of code , The preset code logic is called and processed when an external account or other contract sends a message to its contract address or a transaction occurs .
External accounts EOA
- Controlled by public and private key pairs
- Have ether balance
- Can send transaction (transactions)
- It does not contain relevant execution code
Contract account
- Have ether balance
- Contains execution code
- The code will only be executed when a transaction occurs at this contract address or when information sent by other contracts is received
- Have your own independent storage state , And other contracts can be called
msg.sender and tx.origin The difference between
tx.origin: Indicates the original caller , What we usually get is EOA The address of .
msg.sender: Indicates the most recent caller , Usually get the address of the superior caller , It can be EOA Address , It can also be the contract address .
If EOA user A Call contract B, contract B Call contract C. that
- stay C In contract ,msg.sender Namely B The address of the contract ,tx.origin by A Address .
- stay B In contract ,msg.sender yes A Address ,tx.origin Also for the A Address .
By judgment tx.origin==msg.sender To determine whether the caller is a contract or EOA Account .
reflection : Can an account be distinguished by judging whether it contains execution code EOA still SCA?
Can not be . Because of a contract address
CODESIZEIs greater than zero , But when the addressCODESIZEWhen it's zero , There is no guarantee that it is non contractual , Because the contract is in the construction stageCODESIZEAlso zero .
<aside>
</aside>
Vulnerability demonstration
The following vulnerability contract code , stay transfer The method is checked , The original intention is only owner Can be done transfer operation . What is used here is tx.origin==owner Inspection . We assume that Wallet The deployer of the contract is Alice.
contract Wallet { address public owner; constructor() payable { owner = msg.sender; } function transfer(address payable _to, uint _amount) public { require(tx.origin == owner, "Not owner"); (bool sent, ) = _to.call{value: _amount}(""); require(sent, "Failed to send Ether"); }}hackers ( hypothesis Eve For hackers ) You can exploit vulnerabilities in this way .
- Hackers write a Attack The contract of , And deploy .
- Hackers induce by fishing and other means Wallet Deployer call of contract Attack The contract attack Method .
- Hackers steal Wallet The contract ETH.
Attack Contract code
contract Attack { address payable public owner; Wallet wallet; constructor(Wallet _wallet) { wallet = Wallet(_wallet); owner = payable(msg.sender); } function attack() public { wallet.transfer(owner, address(wallet).balance); }}In the process ,Alice Called Attack The contract attack Method ,attack Method is called wallet The contract transfer Method , stay transfer In the method tx.origin yes alice( stay transfer In the method tx.sender yes attack contract ), because alice Namely Wallet The contract owner, So by testing , take ETH To hackers Eve.
There's another question ,Alice Would be stupid enough to call Eve The contract ?
This depends on hackers Eve Of go fishing The technique , If it's like the one above attack Method Alice Generally, I won't be fooled , But if the method name pretends to be free mint NFT Function of freemint, And a lot of other normal code is called in the code , And called other contracts C, stay C Call in the contract wallet.transfer, It may be difficult to recognize that there is a problem with this method . and Alice Use in normal life DAPP when ( If you use uniswap,stepn Isochronous ), The back end also adopts the form of calling contract methods , Compared with sending phishing emails by sending fake links directly ,Alice The vigilance to this kind of fishing will be lower .
therefore , Hackers are more likely to succeed in fishing , It can be enhanced from the following aspects
Multiple contract connections. contract A Call contract B, contract B Call contract C, contract C Call contract D,…………, Finally, the contract calls wallet.transfer.- Hackers' contracts can be used
social engineeringcamouflage , Take advantage of the greed for cheap , Low price or free mint Banner , Or the temptation of high interest rates . - Hackers can hide exploits in
receive functionin , Trigger vulnerability exploitation by inducing users to transfer money to the specified contract . Such as pretending to exchange money with users , Give customers great discounts, inducements, etc .
Safety suggestion
In this case , Use msg.sender Instead of tx.origin. Make sure that the caller owner.
function transfer(address payable _to, uint256 _amount) public { require(msg.sender == owner, "Not owner"); (bool sent, ) = _to.call{value: _amount}(""); require(sent, "Failed to send Ether");}Reference resources
SWC-115 describe https://swcregistry.io/docs/SWC-115
In code tx.origin==msg.sender What's the role ?https://ethereum.stackexchange.com/questions/113962/what-does-msg-sender-tx-origin-actually-do-why
Use tx-origin go fishing https://solidity-by-example.org/hacks/phishing-with-tx-origin
边栏推荐
- PingCode 性能测试之负载测试实践
- Rebalance operation in spark and its difference from repartition operation
- [acwing] 58 weeks 4490 dyeing
- 安信证券属于什么档次 开户安全吗
- System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
- Redis 的内存淘汰策略和过期删除策略的区别
- 多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
- The test experience "tortured" by the PMP test is worth your review
- 高度剩余法
- VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
猜你喜欢

建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力

2022年国内云管平台厂商哪家好?为什么?

整理混乱的头文件,我用include what you use

嵌入式软件架构设计-函数调用

Which domestic cloud management platform manufacturer is good in 2022? Why?

第十八届IET交直流输电国际会议(ACDC2022)于线上成功举办

Understand asp Net core - Authentication Based on jwtbearer

The test experience "tortured" by the PMP test is worth your review

新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业

Solution of commercial supply chain coordination system in the mineral industry: build a digital intelligent supply chain platform to ensure the safe supply of mineral resources
随机推荐
中信证券网上开户安全吗 开户收费吗
手里10万元存款买什么理财产品收益最高?
安信证券手机版下载 网上开户安全吗
51 single chip microcomputer temperature alarm based on WiFi control
tp配置多数据库
【Go ~ 0到1 】 第六天 文件的读写与创建
Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
Research Report on plastic recycling machine industry - market status analysis and development prospect forecast
电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例
Maximum subarray and matrix multiplication
"Cannot initialize Photoshop because the temporary storage disk is full" graphic solution
话里话外:流程图绘制初级:六大常见错误
C # realizes FFT forward and inverse transformation and frequency domain filtering
Can you really use MySQL explain?
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
PingCode 性能测试之负载测试实践
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
被PMP考试“折磨”出来的考试心得,值得你一览
Solution of commercial supply chain coordination system in the mineral industry: build a digital intelligent supply chain platform to ensure the safe supply of mineral resources
Research Report on market supply and demand and strategy of tetramethylpyrazine industry in China