$30 off During Our Annual Pro Sale. View Details »

Blockchain security - Vulnerabilities in smart contracts

Blockchain security - Vulnerabilities in smart contracts

Vulnerabilities in smart contract

Santhosh Kumar

November 24, 2022
Tweet

More Decks by Santhosh Kumar

Other Decks in Technology

Transcript

  1. Blockchain Security – Vulnerabilities in Smart Contracts Santhosh Kumar R

    Principal Information Security Engineer- DHL Germany
  2. About Me! • Principal Security Engineer at DHL, previously with

    IBM and EY India LLP. • Been in this industry for 7 years now J Played both Attacking and Defending sides in the previous roles J • Spoke at Defcon Las vegas, OWASP Appsec USA toorcon san diego, Bsides las vegas etc…. • Patreon for Null Chennai and Owasp Chennai chapters. • Holds industry certs such as OSCP, OSCE, OSWE, Crest CRT , CRTP etc.
  3. Agenda • Blockchain Basics • What is smart contract? •

    Smart Contract real life usecases • Solidity basics • First Solidity Contract • Solidity Vulnerabilities • Smart Contract vulnerabilities • Incidents surrounding Ethereum blockchain.
  4. Blockchain basics • A blockchain is a globally shared, transactional

    database. This means that everyone can read entries in the database just by participating in the network. • If you want to change something in the database, you have to create a so-called transaction which has to be accepted by all others. • The word transaction implies that the change you want to make (assume you want to change two values at the same time) is either not done at all or completely applied. • Furthermore, while your transaction is being applied to the database, no other transaction can alter it.
  5. Blockchain basics • Blocks - One major obstacle to overcome

    is what (in Bitcoin terms) is called a “double-spend attack”. • What happens if two transactions exist in the network that both want to empty an account? • Only one of the transactions can be valid, typically the one that is accepted first. The problem is that “first” is not an objective term in a peer-to-peer network. • The transactions will be bundled into what is called a “block” and then they will be executed and distributed among all participating nodes. If two transactions contradict each other, the one that ends up being second will be rejected and not become part of the block. • As part of the “order selection mechanism” (which is called “mining”) it may happen that blocks are reverted from time to time, but only at the “tip” of the chain. The more blocks are added on top of a particular block, the less likely this block will be reverted.
  6. Ethereum Virtual Machine • The Ethereum Virtual Machine or EVM

    is the runtime environment for smart contracts in Ethereum. • It is not only sandboxed but actually completely isolated, which means that code running inside the EVM has no access to network, filesystem or other processes. • There are two kinds of accounts in Ethereum which share the same address space: External accounts that are controlled by public-private key pairs (i.e. humans) and contract accounts which are controlled by the code stored together with the account.
  7. Transactions • A transaction is a message that is sent

    from one account to another account (which might be the same or empty, see below). It can include binary data (which is called “payload”) and Ether. • If the target account contains code, that code is executed and the payload is provided as input data. • If the target account is not set (the transaction does not have a recipient or the recipient is set to null), the transaction creates a new contract. • The payload of such a contract creation transaction is taken to be EVM bytecode and executed. The output data of this execution is permanently stored as the code of the contract.
  8. Gas • Upon creation, each transaction is charged with a

    certain amount of gas that has to be paid for by the originator of the transaction • While the EVM executes the transaction, the gas is gradually depleted according to specific rules. If the gas is used up at any point (i.e. it would be negative), an out-of-gas exception is triggered, which ends execution and reverts all modifications made to the state in the current call frame. • This mechanism incentivizes economical use of EVM execution time and also compensates EVM executors (i.e. miners / stakers) for their work. Since each block has a maximum amount of gas, it also limits the amount of work needed to validate a block. • The gas price is a value set by the originator of the transaction, who has to pay gas_price * gas up front to the EVM executor. • Since EVM executors can choose to include a transaction or not, transaction senders cannot abuse the system by setting a low gas price.
  9. Ethereum Virtual Machine • The Ethereum Virtual Machine has three

    areas where it can store data: storage, memory and the stack. • Each account has a data area called storage, which is persistent between function calls and transactions. • The second data area is called memory, of which a contract obtains a freshly cleared instance for each message call. • The EVM is not a register machine but a stack machine, so all computations are performed on a data area called the stack. It has a maximum size of 1024 elements and contains words of 256 bits. • Contracts can call other contracts or send Ether to non-contract accounts by the means of message calls. Message calls are similar to transactions, in that they have a source, a target, data payload, Ether, gas and return data. In fact, every transaction consists of a top-level message call which in turn can create further message calls.
  10. Ethereum Virtual Machine • It is possible to store data

    in a specially indexed data structure that maps all the way up to the block level. • The only way to remove code from the blockchain is when a contract at that address performs the selfdestruct operation. • Removing the contract in theory sounds like a good idea, but it is potentially dangerous, as if someone sends Ether to removed contracts, the Ether is forever lost. • If you want to deactivate your contracts, you should instead disable them by changing some internal state which causes all functions to revert. This makes it impossible to use the contract, as it returns Ether immediately.
  11. Solidity basics!! • The pragma keyword is used to enable

    certain compiler features or checks. A pragma directive is always local to a source file, so you have to add the pragma to all your files if you want to enable it in your whole project. • Source files can (and should) be annotated with a version pragma to reject compilation with future compiler versions that might introduce incompatible changes. • Single-line comments (//) and multi-line comments (/*...*/) are possible.
  12. What is smart contract // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.4.16

    <0.9.0; contract SimpleStorage { uint storedData; //Static variable function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } }
  13. Solidity basics!! • State variables are variables whose values are

    permanently stored in contract storage. • Functions are the executable units of code. Functions are usually defined inside a contract, but they can also be defined outside of contracts. • Function modifiers can be used to amend the semantics of functions in a declarative way. // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.4.22 <0.9.0; contract Purchase { address public seller; modifier onlySeller() { // Modifier require( msg.sender == seller, "Only seller can call this." ); _; } function abort() public view onlySeller { // Modifier usage // ... } }
  14. What is smart contract // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.4.16

    <0.9.0; contract SimpleStorage { uint storedData; //Static variable function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } }
  15. Solidity Vulnerabilities • https://www.damnvulnerabledefi.xyz/index.html • We are going to solve

    Unstoppable challenge. • This represents one of the many scenarios that we come across in real life smart contract implementation. • https://github.com/tinchoabbate/damn-vulnerable- defi/tree/v2.2.0/contracts/unstoppable
  16. DEMO

  17. Recent Smart Contract vulnerablities • Reentrancy - In Reentrancy attack,

    it is a malicious smart contract that exploits the vulnerabilities of another smart contract, usually to drain off its funds. • Access Control - The concept of Access Control, or “who is permitted to perform a particular task,” is crucial in the context of a smart contract. • Floating Pragma - A solidity pragma is probably the first line of code in a solidity code that determines the compiler’s version of the smart contract. • Zero Address Check For Critical Functions – False account (sharing of private keys) • Divide Before Multiplying - A= (10*30*18)/ 30= 180 != A= (10/30)* 30*18= 179.99999 (division) • Frontrunner – Insider trading as similar to stock trading • Missing Withdraw Functions - A common vulnerability is when your smart contract has a payable function but misses out on the withdrawal function. • Integer Overflow and Underflow
  18. Recent Blockchain incidents https://magoo.github.io/Blockchain-Graveyard/

  19. Recent Blockchain incidents • Ronin - Ronin is an Ethereum

    sidechain that was built for a popular NFT play-to-earn game, Axie Infinity. The hackers funneled an incredible $615 million worth of ETH into several decentralized exchanges. • Wormhole - In essence, they allow tokens from various blockchains (such as Solana, Ethereum, Terra, and others) to be converted from one 'currency' to the other by functioning as a middle man. -$326 million • Mirror Protocol - Mirror Protocol is a decentralized application that allows users to create digital synthetics that track real-world assets, such as stocks or commodities - $89,706,164 breach • Qubit Finance - Qubit Finance is a DeFi lending protocol, based on the Binance Smart Chain, that allows members to borrow and lend various virtual assets. Borrowers deposit funds as collateral, and interest is automatically calculated based on the smart contract protocol - $80 million • Cashio - a Solana-based stable coin (CASH). Since the token is pegged to the US dollar, users must deposit liquidity provider (LP) tokens equivalent to USD (in this case, USDT and USDC) into a collateral account on Solana's decentralized exchange, Saber, in order to mint fresh CASH - $52 million
  20. References • https://github.com/m4xx101/blocksec-incidents/tree/main/exchange • https://github.com/ethereum/public-disclosures/ • https://www.dasp.co/ • https://docs.soliditylang.org/en/latest/security-considerations.html •

    https://github.com/crytic/not-so-smart-contracts • https://www.cnbc.com/2022/08/10/hackers-have-stolen-1point4- billion-this-year-using-crypto-bridges.html • https://bitcoinist.com/new-report-blockchain-hackers-stole-1-3- billion-in-q1-2022/
  21. Have questions? • Mail me : santhosh@nearsecurity.com • Linkedin :

    https://www.linkedin.com/in/santhoshkumar22/ • Twitter: https://twitter.com/Sh4d0wS4int