当前位置:网站首页>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
CODESIZE
Is greater than zero , But when the addressCODESIZE
When it's zero , There is no guarantee that it is non contractual , Because the contract is in the construction stageCODESIZE
Also 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 engineering
camouflage , 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 function
in , 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
边栏推荐
- [Chongqing Guangdong education] National Open University spring 2019 1248 public sector human resource management reference questions
- NoSQL之readis配置与优化(终章)
- Research Report on market supply and demand and strategy of China's plastics and polymer industry
- Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
- Oracle监听器Server端与Client端配置实例
- Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
- 被PMP考试“折磨”出来的考试心得,值得你一览
- 电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例
- Median and order statistics
- Why do you say that the maximum single table of MySQL database is 20million? Based on what?
猜你喜欢
一加10 Pro和iPhone 13怎么选?
GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
Principle and general steps of SQL injection
~89 deformation translation
Understand asp Net core - Authentication Based on jwtbearer
DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
PingCode 性能测试之负载测试实践
~88 running people practice
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
随机推荐
基于check-point实现图数据构建任务
FIREBIRD使用经验总结
Years of training, towards Kata 3.0! Enter the safe container experience out of the box | dragon lizard Technology
Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
散列表
Principle and general steps of SQL injection
Sequence diagram data modeling and industrial chain analysis
Smart Logistics Park supply chain management system solution: digital intelligent supply chain enables a new supply chain model for the logistics transportation industry
网页游戏引擎
建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力
Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
表单传递时,如何隐式将值传过去
整理混乱的头文件,我用include what you use
Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
ECCV 2022放榜了:1629篇论文中选,录用率不到20%
[acwing] 58 weeks 4490 dyeing
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
矿产行业商业供应链协同系统解决方案:构建数智化供应链平台,保障矿产资源安全供应
Task state rollback and data blocking tasks based on check point mechanism
Lv166 turned over