当前位置:网站首页>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
边栏推荐
- Sql实现Split
- 从数数开始
- 照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
- Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
- ~88 running people practice
- 世界环境日 | 周大福用心服务推动减碳环保
- Use and principle of thread pool
- Task state rollback and data blocking tasks based on check point mechanism
- Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
- Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
猜你喜欢

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

L1-072 scratch lottery

~89 deformation translation

Start by counting

智慧物流園區供應鏈管理系統解决方案:數智化供應鏈賦能物流運輸行業供應鏈新模式

智慧物流园区供应链管理系统解决方案:数智化供应链赋能物流运输行业供应链新模式

祝贺Artefact首席数据科学家张鹏飞先生荣获 Campaign Asia Tech MVP 2022

VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题

基于wifi控制的51单片机温度报警器

S2b2b solution for lighting industry: efficiently enable the industrial supply chain and improve the economic benefits of enterprises
随机推荐
程序员怎么才能提高代码编写速度?
C# 更加优质的操作MongoDB数据库
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
Difference between redis' memory obsolescence strategy and expiration deletion strategy
leetcode:421. 数组中两个数的最大异或值
同构图与异构图CYPHER-TASK设计与TASK锁机制
. Net applications consider x64 generation
[Acwing] 58周赛 4490. 染色
照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
Is it safe for Bank of China Securities to open an account online?
Research Report on market supply and demand and strategy of tetramethylpyrazine industry in China
2022PMP考试基本情况详情了解
C# 服务器日志模块
MD5加密的两种方式
go-micro教程 — 第二章 go-micro v3 使用Gin、Etcd
话里话外:流程图绘制初级:六大常见错误
Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
嵌入式软件架构设计-函数调用
Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
Rebalance operation in spark and its difference from repartition operation