Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Just enough bitcoing to go cryptojacking with J...

Just enough bitcoing to go cryptojacking with JavaScript

Whatever their price, Bitcoin and cryptocurrencies continue to provide interesting technical and security opportunities. This talk gives a comprehensive introduction of bitcoin & cryptocurrency technology, and then analyzes how cryptojacking attacks are executed with JavaScript.

luke crouch

April 12, 2019
Tweet

More Decks by luke crouch

Other Decks in Technology

Transcript

  1. The “crypto” in cryptocurrency • Cryptographic hash functions • Hash

    Pointers • Blockchain • Public Key Cryptography • Digital Signatures
  2. H(message) hash message We the People of the United States

    … until an election of Representatives shall have intervened. e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
  3. “hash” function • Maps data of arbitrary size into data

    of a fixed size • e.g., justTheFirstLetter(string) • justTheFirstLetter(“hash”) // returns h • justTheFirstLetter(“function”) // returns f • justTheFirstLetter(“returns”) // returns r • justTheFirstLetter(“fixed-size”) // returns f
  4. “hash” function • e.g., justTheFirstLetter(string) • justTheFirstLetter(“hash”) // returns h

    • justTheFirstLetter(“function”) // returns f • justTheFirstLetter(“returns”) // returns r • justTheFirstLetter(“fixed-size”) // returns f
  5. H(message) hash collision! message “The bearer of this note may

    redeem it for one dollar by presenting it to me” 51a6b3 We the People of the United States … until an election of Representatives shall have intervened. resist collisions: 2 different messages shouldn’t produce the same hash
  6. tails 3e5368 heads hiding: what if there are only 2

    possible messages? 22814c 3e5368 ? heads
  7. H(message || nonce) hash message tails dc66ec heads hiding: add

    random value: “nonce” or “salt” 0f2c72 nonce a a
  8. H() hash message tails dc66ec heads hiding: add random value:

    “nonce” 0f2c72 nonce tails 3e5368 heads 22814c a a
  9. tails dc66ec heads hiding: add random value: “nonce” 0f2c72 tails

    3e5368 heads 22814c a a tails 8d23aa heads 15db7d b b
  10. ? message hash dc66ec hiding: given the hash, not feasible

    to find the message or nonce ? ? nonce ?
  11. cryptographic hash function
 for cryptocurrency • resists “collisions” • hides

    original (plaintext) message • for cryptocurrency: “puzzle friendly”
  12. puzzle friendliness Given hash output and part of input (i.e.,

    the nonce), it’s “hard” (but not infeasible) to find the input
  13. message hash dc66ec puzzle-friendly: given the hash and nonce, hard

    to find a message lots of work nonce 123456 message lots of work
  14. openssl to hash messages echo “The bearer of this note

    may redeem it for one dollar by presenting it to me” | openssl sha256 51a6b38fd78e5e20246bd0103668056a2a8981274e9487ea3f18158e59b690e7 echo “We the People of the United States … until an election of Representatives shall have intervened.” | openssl sha256 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
  15. H( ) = e3b0c4 We the People of the United

    States … until an election of Representatives shall have intervened. hash pointer: where full plaintext data is stored, AND a cryptographic hash of the data
  16. prev: H()
 e3b0c4 block chain: linked list of hash pointers

    prev: H()
 f4c1d5 data data prev: H()
 05d2e6 data
  17. Now we can have a tamper-proof ledger (of digital dollars)!

    prev: H()
 e3b0c4 prev: H()
 f4c1d5 “The bearer of this note may redeem it for one dollar by presenting it to me” prev: H()
 05d2e6 “The bearer of this note may redeem it for one dollar by presenting it to me” “The bearer of this note may redeem it for one dollar by presenting it to me”
  18. Instead, Alice needs a
 public “lock” she can copy and

    share with anyone, and a single private “key” …
  19. … a “trap door 1-way function” is a 1-way function

    that is infeasible to reverse … unless you have a secret piece of information
  20. math math math • Phi function of large numbers is

    infeasible to calculate, *except* for prime numbers • Multiplying large prime numbers is infeasible to reverse • The “Prime Factorization” problem • Euler’s Theorem shows that mphi(n) ≅1 mod n.
  21. Most important to us: math says the public key can

    be public 
 while the secret key stays secret
  22. digital signatures: required properties • Only you can sign your

    signature, but anyone can verify that it’s valid
 
 
 • The signature is attached to a single document - it can’t be used to indicate you agree to a different document
  23. signature = sign(secret key, message) “The bearer of this note

    may redeem it for one dollar by presenting it to me” -with love, from Luke
  24. valid = verify(public key, message, signature) “The bearer of this

    note may redeem it for one dollar by presenting it to me” -with love, from Luke
  25. openssl to create secret & public keys openssl ecparam -genkey

    -name secp256k1 -rand /dev/urandom -out \ /Users/lcrouch/secret_key openssl ec -in /Users/lcrouch/ secret_key -pubout -out \ /Users/lcrouch/public_key
  26. openssl to sign & verify openssl dgst -sign secret_key message

    -out luke_dollar.signed openssl dgst -verify public_key 
 -signature luke_dollar.signed valid
  27. The “crypto” in cryptocurrency gives us … • Integrity, from

    • Blockchain, from • Hash pointers, from • Hash functions • Authenticity,
 Non-repudiation, from • Digital Signatures, from • Public Key Cryptography … not encryption for confidentiality
  28. Goofycoin 1. Designated entity - Goofy - can create new

    coins anytime and coins belong to Goofy 2. Whoever owns a coin can transfer it to someone else
  29. Goofy creates coins • Generates unique coin ID: 001 •

    Makes message: “CreateCoin 001” • Hashes and signs message as H(1) with secret key
  30. Goofy transfers coin to Alice • New message:
 “Starting at

    H(1), PayCoin 001 to Alice” • Note: Alice = Alice’s public key / address • Hashes and signs message as H(2) with secret key
  31. Alice transfers coin to Bob • New message:
 “Starting at

    H(2), PayCoin 001 to Bob” • Note: Bob = Bob’s public key / address • Hashes and signs message as H(3) with secret key
  32. Now we can have a tamper-proof ledger of transactions! CreateCoin

    001 Goofy PayCoin: 001 Alice Goofy H( ) Alice H( ) PayCoin: 001 Bob
  33. Goofycoin 1. Designated entity - Goofy - can create new

    coins anytime and coins belong to Goofy 2. Whoever owns a coin can transfer it to someone else 3. Anyone can verify coin ownership by following block-chain back to Goofy
  34. Alice can pay the same coin to Chuck CreateCoin [12345]

    Goofy PayCoin: [12345] Alice Goofy H( ) Alice H( ) PayCoin: [12345] Bob Alice H( ) PayCoin: [12345] Chuck
  35. Scroogecoin • Goofycoin + • Designated entity - Scrooge -

    publishes the canonical public ledger of all transactions • Everyone broadcasts coin transactions to Scrooge • Only accept coins in Scrooge’s public ledger
  36. Scroogecoin • Scrooge builds block chain to digitally sign •

    Each block has a transaction in it • ID • contents • hash point to previous block • Scrooge signs final hash pointer • Scrooge publishes signature and block chain
  37. PayCoins number value recipient 1 1 Bob 2 6.1 Alice

    PayCoins coins: 1(3) transID: 2 Which coins are being consumed?
  38. number value recipient 1 1 Bob 2 6.1 Alice PayCoins

    coins: 1(3) transID: 2 number value recipient 1 3.2 Scrooge 2 1.4 Goofy 3 7.1 Alice transID: 1 CreateCoins 1(3)
  39. Scrooge signs off on everything prev: H()
 e3b0c4 transID: 1

    prev: H()
 f4c1d5 transID: 2 H()
 05d2e6 Scrooge number value recipient 1 3.2 Scrooge 2 1.4 Goofy 3 7.1 Alice number value recipient 1 1 Bob 2 6.1 Alice PayCoins coins: 1(3) CreateCoins
  40. Alice tries to re-spend coin from Block 1 after Scrooge

    signs Block 2 prev: H()
 e3b0c4 transID: 1 prev: H()
 f4c1d5 transID: 2 H()
 05d2e6 Scrooge number value recipient 1 3.2 Scrooge 2 1.4 Goofy 3 7.1 Alice number value recipient 1 1 Bob 2 6.1 Alice PayCoins coins: 1(3) CreateCoins prev: H()
 f4c1d5 transID: 2 number value recipient 1 1 Chuck 2 6.1 Alice PayCoins coins: 1(3)
  41. Scrooge rejects Alice’s double-spend transaction prev: H()
 e3b0c4 transID: 1

    prev: H()
 f4c1d5 transID: 2 H()
 05d2e6 Scrooge number value recipient 1 3.2 Scrooge 2 1.4 Goofy 3 7.1 Alice number value recipient 1 1 Bob 2 6.1 Alice PayCoins coins: 1(3) CreateCoins prev: H()
 f4c1d5 transID: 2 number value recipient 1 1 Chuck 2 6.1 Alice PayCoins coins: 1(3)
  42. but Scrooge … • maintains the ledger of transactions •

    has authority over which transactions are valid • creates new coins
  43. Without Scrooge … • Who maintains the ledger of transactions?

    • Who has authority over which transactions are valid? • Who creates new coins?
  44. Coin miners • maintain the ledger of transactions • have

    authority over which transactions are valid • create new coins
  45. Skipping: Distributed Consensus Protocol tl;dr: it needs a majority of

    nodes to behave honestly, and to pick a random node from the network for each block
  46. If only there were a digital currency we could give

    them without having to know their real identities …
  47. Alice mines a block, and gets to create new coins

    to herself number value recipient CreateCoins 0 25 Alice (dc3c6e) PayCoins from 1(1) 1 1.1 Bob (e5ceb5) 2 6.1 Alice (dc3c6e) transID: 2
  48. Financial incentive to be honest: miners only include valid transactions,

    because they want their own CreateCoins transaction included in the long-term consensus block-chain
  49. A distributed consensus protocol 1. New transactions are broadcast to

    all nodes 2. Each node collects new transactions into a block 3. In each round, a random node adds its block to the end of the block-chain 4. Other nodes accept the block only if all transactions in it are valid - i.e., unspent coins with valid signatures
  50. Proof of work • Approximate random selection in proportion to

    a scarce resource • In Bitcoin, it’s computing power • Nodes compete with each other on computing power
  51. puzzle friendliness Given hash output and part of input (i.e.,

    the nonce), it’s “hard” (but not infeasible) to find the input
  52. message hash dc66ec puzzle-friendly: given the hash and nonce, hard

    to find a message lots of work nonce 123456 message lots of work
  53. Hash puzzles in bitcoin mining • To create a block,

    a node is required to find a nonce such that when you combine … • nonce • previous block’s hash • list of transactions • … the hash output should be a string that falls into a certain range
  54. So, to mine cryptocurrency you need: • Lots of efficient

    processing to complete the proof-of-work hashing • Lots of efficient networking to communicate with the distributed peer-to-peer nodes
  55. CPU mining Bitcoin ~20M h/s = several hundred thousand years

    to get a block https://99bitcoins.com/20-insane-bitcoin-mining-rigs/
  56. scrypt & Litecoin • scrypt hashing function • Fill a

    large buffer of RAM • Mutate the memory in pseudo-random order • O(N2) function • Choose N large enough to make memory faster • Litecoin • ASICs already out for Litecoin param of N=128KB • 504M h/s at 1.58 w/MH • (Remember Bitcoin was 2,100,000M h/s and 0.43 watts/Gigahashes) https://www.hashespersecond.com/asic/
  57. Memory-hard hashes • CryptoNight hashing function • Monero • 2MB

    memory buffer • Uses AES-NI found on most modern x86_64 CPUs • ASICs: 0.2M h/s @ 2.27 w/KH • Litecoin ASIC: 504M h/s @ 1.58 w/MH • Bitcoin ASIC: 2,100,000M h/s and 0.043 w/MH • Also, Monero adjusted PoW algo. so these devices don’t work for Monero anymore https://www.hashespersecond.com/asic/
  58. With Web APIs you get: • Efficient processing to complete

    the proof-of-work hashing • Web Workers & WebAssembly • Efficient networking to communicate with the distributed peer-to-peer nodes • Web Sockets
  59. CoinHive Malware Analysis • Creates WebWorker with number of threads

    == number of CPU cores available (up to 8 max) • Loads WebAssembly program into worker for mining (default throttle to 100% CPU usage!) • Creates WebSocket connection between server and browser • Server notifies browser if new blocks are found and sends new transactions to browser
  60. • threads - number of WebWorkers to use; defaults to

    using as many cores as possible • throttle - fraction of time threads should be idle; defaults to “0” https://webcache.googleusercontent.com/search?q=cache:https://coinhive.com/documentation/miner
  61. Mozilla research • Crawl Alexa Top 10k looking for <script>

    tags to one of the 212 crypto-jacking hosts in adblock-nocoin-list • Of 6M <script> calls, 945 (0.015%) use crypto-jacking script • number of crypto-jacking hosts used: 11 (5.2% of known hosts) • number of CoinHive scripts: 507 (54%) • Majority of domains detected with crypto-jacking were streaming sites https://github.com/mozilla/UCOSP-winter-2018_TrackingTechnologies/blob/master/analyses/cryptojacking/cryptojacking_analysis.ipynb
  62. Done • Go to about:config • Search for urlclassifier.trackingTable •

    add tracking-protection-base-cryptomining and tracking-protection-content-cryptomining
  63. “Towards Seamless Tracking-Free Web: Improved Detection of Trackers via One-

    class learning” • Summary:
 Training a classifier to detect JS tracking code • Conducted by:
 • Data Source:
 Web crawl of Alexa top 50
 45 random sites from Alexa 5,000-45,000 • Machine Learning Algorithms/Methods:
 Support Vector Machine, Positive & Unlabeled • Data features:
 JavaScript semantic & syntactic tokens and n-grams • Results • 99% true positives for one-class Support Vector Machine https://www.degruyter.com/downloadpdf/j/popets.2017.2017.issue-1/popets-2017-0006/popets-2017-0006.pdf
  64. Questions? • Crypto -> Blockchain • Bitcoin • Web Workers,

    Web Assembly • Web Sockets • Preventing • Luke Crouch • @groovecoder • speakerdeck.com/groovecoder • github.com/groovecoder