This project demonstrates a real-world smart contract vulnerability in Solidity caused by the misuse of tx.origin. It simulates a phishing attack exploiting the vulnerable withdrawAll() function, triggered through the attacker's maliciousFunction().
- BadVault.sol – Vulnerable contract with
withdrawAll()relying ontx.origin. - Attacker.sol – Malicious contract calling
maliciousFunction()to exploit the vulnerability. - GoodVault.sol – Safe contract version using
msg.senderfor authorization.
- ✅
msg.sendervstx.origin - ✅ Ether transfer using
transfer() - ✅ Cross-contract calls
- ✅ Solidity authorization patterns with
require()
The vulnerable withdrawAll() function uses tx.origin to check ownership. When maliciousFunction() is called in the attacker contract, it triggers a call to withdrawAll(), which sees the original transaction sender (tx.origin) as the wallet owner, passing the check and sending all funds to the attacker.
- Open Remix.
- Deploy
BadVault.solwith some ETH. - Deploy
Attacker.sol, passing BadVault’s address. - Switch to the owner account.
- Call
maliciousFunction()from the attacker contract. - Observe BadVault’s balance → it’s drained.
- ❌ Never use
tx.originfor critical access control. - ✅ Always use
msg.senderfor reliable authorization.
LGPL-3.0-only License
