Smart Contract Security Assessing Solidity smart contracts About Me - - PowerPoint PPT Presentation

smart contract security
SMART_READER_LITE
LIVE PREVIEW

Smart Contract Security Assessing Solidity smart contracts About Me - - PowerPoint PPT Presentation

Smart Contract Security Assessing Solidity smart contracts About Me Evangelos Deirmentzoglou Security Consultant Smart contract audits Nmap/Ncrack contributor Certs: OSCE, OSCP, OSWP Blockchain Basics Front Running


slide-1
SLIDE 1

Smart Contract Security

Assessing Solidity smart contracts

slide-2
SLIDE 2

About Me

  • Evangelos Deirmentzoglou
  • Security Consultant
  • Smart contract audits
  • Nmap/Ncrack contributor
  • Certs: OSCE, OSCP, OSWP
slide-3
SLIDE 3
  • Blockchain Basics
  • Front Running
  • Reentrancy
  • External Calls
  • Integer Over*ow
  • tx.origin
  • Gas limit
slide-4
SLIDE 4

Blockchain Basics

Ethereum Gas

  • Introduced to reduce abuse
  • transac.on cost (EVM opera.on)
  • gas price (in eth)

Miner receives es.mated gas spent* gas price Remainder of gas cost is returned to the sender

slide-5
SLIDE 5

Blockchain Basics

Blockchain Transac.ons

Transaction Pool Blockchain Miner

slide-6
SLIDE 6

Frontrunning

  • aka Timing a5acks
  • A6ec.ng: First In First Out Contracts
  • Miner frontrunning
  • User frontrunning
slide-7
SLIDE 7

Frontrunning

  • Examples: DEX, Bancor, random ICOs

Reference: h ps://hackernoon.com/front-running-bancor-in-150-lines-of-python-with-ethereum-api-d5e2bfd0d798

slide-8
SLIDE 8

Frontrunning

Reference: h5ps://twi5er.com/phildaian/status/1032679149960355840

slide-9
SLIDE 9

Frontrunning - Mi.ga.on

  • Set upper gas limit
  • Set a Dxed gas limit
  • tx.gasprice
  • gasleE()
slide-10
SLIDE 10

Frontrunning - Mi.ga.on

Commit/Reveal approach

Reference: h5ps://karl.tech/learning-solidity-part-2-vo.ng/

slide-11
SLIDE 11

Blockchain Basics - Payable

Payable func.ons

slide-12
SLIDE 12

Reentrancy

150$ mil stolen DAO & numerous other contracts

slide-13
SLIDE 13

Reentrancy - Vulnerable Contract

slide-14
SLIDE 14

Reentrancy - A5ack Contract

slide-15
SLIDE 15

Reentrancy - Mi.ga.on

  • Always use transfer()
  • Use sensi.ve opera.ons before calls to other

contracts

  • ReentrancyGuard by OpenZeppelin

Reference: h ps://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/ ReentrancyGuard.sol

slide-16
SLIDE 16

Reentrancy - Bridges

  • Reentrancy is not really obsolete
  • “A bridge to connect all bridges”
  • Connects ERC20 tokens
slide-17
SLIDE 17

Reentrancy - Bridges

Not reentrancy per se More like call to the unknown

slide-18
SLIDE 18

External Contracts

  • Trust that called func.on will succeed
  • Trust that you know what the called func.on

do

slide-19
SLIDE 19

External Contract - Mi.ga.on

  • Ensure external contract returns expected

value

  • Perform sensi.ve opera.ons before calling

external contract

  • Never assume external contract func.onality
slide-20
SLIDE 20

Integer Over*ow

  • Proof of Weak Hands Coin - 866 Eth
  • BatchOver*ow

Reference: h ps://medium.com/@peckshield/alert-new-batchover*ow-bug-in-mul+ple-erc20-smart-contracts- cve-2018-10299-511067db6536

slide-21
SLIDE 21

Integer Over*ow

  • Integer only
  • Conversions (Unsigned -> Signed)

More over*ow cases: h ps://github.com/ethereum/solidity/issues/796#issuecomment-253578925

slide-22
SLIDE 22

Integer Over*ow - Mi.ga.on

  • Perform opera.ons within boundaries
  • SafeMath library

Reference: h ps://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol

slide-23
SLIDE 23

tx.origin

  • Blockchain CSRF?
  • tx.origin vs msg.sender
slide-24
SLIDE 24

tx.origin

Vulnerable Contract User call() For Vulnerable Contract tx.origin User msg.sender User

slide-25
SLIDE 25

tx.origin

User Malicious Contract Vulnerable Contract call() For Malicious Contract For Vulnerable Contract tx.origin User User msg.sender User Malicious Contract

slide-26
SLIDE 26

tx.origin - Mi.ga.on

  • call vs delegatecall
  • Cau.on when using tx.origin
slide-27
SLIDE 27

Gas limit a5acks

  • Looping over unknown arrays
  • Leads to denial of service
slide-28
SLIDE 28

Gas limit a5acks - Mi.ga.on

  • Avoid looping over arrays of unknown length
  • Set an upper limit for the array length
  • Control the loop by checking gasleE()
slide-29
SLIDE 29

Things we didn’t cover

  • Randomness (is hard)
  • Visibility (Func.on, secret)
  • Unexpected ether (contract-suicide)
  • Delegatecall (3rd party libraries)
  • Storage
slide-30
SLIDE 30

Ques.ons?