当前位置:网站首页>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
边栏推荐
- Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
- 对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平
- How to contribute to the source code of ongdb core project
- Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness
- L1-072 scratch lottery
- Principle and general steps of SQL injection
- Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
- PyTorch深度学习快速入门教程
- C implementation defines a set of intermediate SQL statements that can be executed across libraries
- PingCode 性能测试之负载测试实践
猜你喜欢
建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力
S2b2b solution for lighting industry: efficiently enable the industrial supply chain and improve the economic benefits of enterprises
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
Learn more about the basic situation of 2022pmp examination
嵌入式软件架构设计-函数调用
基于wifi控制的51单片机温度报警器
Yanwen logistics plans to be listed on Shenzhen Stock Exchange: it is mainly engaged in international express business, and its gross profit margin is far lower than the industry level
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
智慧物流園區供應鏈管理系統解决方案:數智化供應鏈賦能物流運輸行業供應鏈新模式
对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平
随机推荐
Research Report on market supply and demand and strategy of China's four sided flat bag industry
C # realizes FFT forward and inverse transformation and frequency domain filtering
C implementation defines a set of intermediate SQL statements that can be executed across libraries
NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析
[acwing] 58 weeks 4489 Longest subsequence
PingCode 性能测试之负载测试实践
How to implicitly pass values when transferring forms
【云原生】服务网格是什么“格”?
~89 deformation translation
APOC custom functions and procedures
How to decrypt worksheet protection password in Excel file
整理混乱的头文件,我用include what you use
Go语言循环语句(第10课下)
Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
Congratulations to Mr. Zhang Pengfei, chief data scientist of artefact, for winning the campaign Asia tech MVP 2022
Two methods of MD5 encryption
. Net applications consider x64 generation
Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
.Net 应用考虑x64生成