الجمعة، 28 أغسطس 2020

Blockchain Exploitation Labs - Part 1 Smart Contract Re-Entrancy


Why/What Blockchain Exploitation?

In this blog series we will analyze blockchain vulnerabilities and exploit them ourselves in various lab and development environments. If you would like to stay up to date on new posts follow and subscribe to the following:
Twitter: @ficti0n
Youtube: https://www.youtube.com/c/ConsoleCowboys
URL: http://cclabs.io
          http://consolecowboys.com

As of late I have been un-naturally obsessed with blockchains and crypto currency. With that obsession comes the normal curiosity of "How do I hack this and steal all the monies?"

However, as usual I could not find any actual walk thorough or solid examples of actually exploiting real code live. Just theory and half way explained examples.

That question with labs is exactly what we are going to cover in this series, starting with the topic title above of Re-Entrancy attacks which allow an attacker to siphon out all of the money held within a smart contract, far beyond that of their own contribution to the contract.
This will be a lab based series and I will show you how to use demo the code within various test environments and local environments in order to perform and re-create each attacks for yourself.  

Note: As usual this is live ongoing research and info will be released as it is coded and exploited.

If you are bored of reading already and just want to watch videos for this info or are only here for the demos and labs check out the first set of videos in the series at the link below and skip to the relevant parts for you, otherwise lets get into it:


Background Info:

This is a bit of a harder topic to write about considering most of my audience are hackers not Ethereum developers or blockchain architects. So you may not know what a smart contract is nor how it is situated within the blockchain development model. So I am going to cover a little bit of context to help with understanding.  I will cover the bare minimum needed as an attacker.

A Standard Application Model:
  • In client server we generally have the following:
  • Front End - what the user sees (HTML Etc)
  • Server Side - code that handles business logic
  • Back End - Your database for example MySQL

A Decentralized Application Model:

Now with a Decentralized applications (DAPP) on the blockchain you have similar front end server side technology however
  • Smart contracts are your access into the blockchain.
  • Your smart contract is kind of like an API
  • Essentially DAPPs are Ethereum enabled applications using smart contracts as an API to the blockchain data ledger
  • DAPPs can be banking applications, wallets, video games etc.

A blockchain is a trust-less peer to peer decentralized database or ledger

The back-end is distributed across thousands of nodes in its entirety on each node. Meaning every single node has a Full "database" of information called a ledger.  The second difference is that this ledger is immutable, meaning once data goes in, data cannot be changed. This will come into play later in this discussion about smart contracts.

Consensus:

The blockchain of these decentralized ledgers is synchronized by a consensus mechanism you may be familiar with called "mining" or more accurately, proof of work or optionally Proof of stake.

Proof of stake is simply staking large sums of coins which are at risk of loss if one were to perform a malicious action while helping to perform consensus of data.   

Much like proof of stake, proof of work(mining) validates hashing calculations to come to a consensus but instead of loss of coins there is a loss of energy, which costs money, without reward if malicious actions were to take place.

Each block contains transactions from the transaction pool combined with a nonce that meets the difficulty requirements.  Once a block is found and accepted it places them on the blockchain in which more then half of the network must reach a consensus on. 

The point is that no central authority controls the nodes or can shut them down. Instead there is consensus from all nodes using either proof of work or proof of stake. They are spread across the whole world leaving a single centralized jurisdiction as an impossibility.

Things to Note: 

First Note: Immutability

  • So, the thing to note is that our smart contracts are located on the blockchain
  • And the blockchain is immutable
  • This means an Agile development model is not going to work once a contract is deployed.
  • This means that updates to contracts is next to impossible
  • All you can really do is createa kill-switch or fail safe functions to disable and execute some actions if something goes wrong before going permanently dormant.
  • If you don't include a kill switch the contract is open and available and you can't remove it

Second Note:  Code Is Open Source
  • Smart Contracts are generally open source
  • Which means people like ourselves are manually bug hunting smart contracts and running static analysis tools against smart contract code looking for bugs.

When issues are found the only course of action is:
  • Kill the current contract which stays on the blockchain
  • Then deploy a whole new version.
  • If there is no killSwitch the contract will be available forever.
Now I know what you're thinking, these things are ripe for exploitation.
And you would be correct based on the 3rd note


Third Note: Security in the development process is lacking
  • Many contracts and projects do not even think about and SDLC.
  • They rarely add penetration testing and vulnerability testing in the development stages if at all
  • At best there is a bug bounty before the release of their main-nets
  • Which usually get hacked to hell and delayed because of it.
  • Things are getting better but they are still behind the curve, as the technology is new and blockchain mostly developers and marketers.  Not hackers or security testers.


Forth Note:  Potential Data Exposure via Future Broken Crypto
  • If sensitive data is placed on the blockchain it is there forever
  • Which means that if a cryptographic algorithm is broken anything which is encrypted with that algorithm is now accessible
  • We all know that algorithms are eventually broken!
  • So its always advisable to keep sensitive data hashed for integrity on the blockchain but not actually stored on the blockchain directly


 Exploitation of Re-Entrancy Vulnerabilities:

With a bit of the background out of the way let's get into the first attack in this series.

Re-Entrancy attacks allow an attacker to create a re-cursive loop within a contract by having the contract call the target function rather than a single request from a  user. Instead the request comes from the attackers contract which does not let the target contracts execution complete until the tasks intended by the attacker are complete. Usually this task will be draining the money out of the contract until all of the money for every user is in the attackers account.

Example Scenario:

Let's say that you are using a bank and you have deposited 100 dollars into your bank account.  Now when you withdraw your money from your bank account the bank account first sends you 100 dollars before updating your account balance.

Well what if when you received your 100 dollars, it was sent to malicious code that called the withdraw function again not letting  the initial target deduct your balance ?

With this scenario you could then request 100 dollars, then request 100 again and you now have 200 dollars sent to you from the bank. But 50% of that money is not yours. It's from the whole collection of money that the bank is tasked to maintain for its accounts.

Ok that's pretty cool, but what if that was in a re-cursive loop that did not BREAK until all accounts at the bank were empty?  

That is Re-Entrancy in a nutshell.   So let's look at some code.

Example Target Code:


           function withdraw(uint withdrawAmount) public returns (uint) {
       
1.         require(withdrawAmount <= balances[msg.sender]);
2.         require(msg.sender.call.value(withdrawAmount)());

3.          balances[msg.sender] -= withdrawAmount;
4.          return balances[msg.sender];
        }

Line 1: Checks that you are only withdrawing the amount you have in your account or sends back an error.
Line 2: Sends your requested amount to the address the requested that withdrawal.
Line 3: Deducts the amount you withdrew from your account from your total balance.
Line 4. Simply returns your current balance.

Ok this all seems logical.. however the issue is in Line 2 - Line 3.   The balance is being sent back to you before the balance is deducted. So if you were to call this from a piece of code which just accepts anything which is sent to it, but then re-calls the withdraw function you have a problem as it never gets to Line 3 which deducts the balance from your total. This means that Line 1 will always have enough money to keep withdrawing.

Let's take a look at how we would do that:

Example Attacking Code:


          function attack() public payable {
1.           bankAddress.withdraw(amount);
         }

2.    function () public payable {
         
3.            if (address(bankAddress).balance >= amount) {
4.               bankAddress.withdraw(amount);
                }
}

Line 1: This function is calling the banks withdraw function with an amount less than the total in your account
Line 2: This second function is something called a fallback function. This function is used to accept payments that come into the contract when no function is specified. You will notice this function does not have a name but is set to payable.
Line 3:  This line is checking that the target accounts balance is greater than the amount being withdrawn.
Line 4:  Then again calling the withdraw function to continue the loop which will in turn be sent back to the fallback function and repeat lines over and over until the target contracts balance is less than the amount being requested.



Review the diagram above which shows the code paths between the target and attacking code. During this whole process the first code example from the withdraw function is only ever getting to lines 1-2 until the bank is drained of money. It never actually deducts your requested amount until the end when the full contract balance is lower then your withdraw amount. At this point it's too late and there is no money left in the contract.


Setting up a Lab Environment and coding your Attack:

Hopefully that all made sense. If you watch the videos associated with this blog you will see it all in action.  We will now analyze code of a simple smart contract banking application. We will interface with this contract via our own smart contract we code manually and turn into an exploit to take advantage of the vulnerability.

Download the target code from the following link:

Then lets open up an online ethereum development platform at the following link where we will begin analyzing and exploiting smart contracts in real time in the video below:

Coding your Exploit and Interfacing with a Contract Programmatically:

The rest of this blog will continue in the video below where we will  manually code an interface to a full smart contract and write an exploit to take advantage of a Re-Entrency Vulnerability:

 


Conclusion: 

In this smart contract exploit writing intro we showed a vulnerability that allowed for re entry to a contract in a recursive loop. We then manually created an exploit to take advantage of the vulnerability. This is just the beginning, as this series progresses you will see other types of vulnerabilities and have the ability to code and exploit them yourself.  On this journey through the decentralized world you will learn how to code and craft exploits in solidity using various development environments and test nets.

More information


  1. Hacking Tools And Software
  2. Hacker Techniques Tools And Incident Handling
  3. Top Pentest Tools
  4. Wifi Hacker Tools For Windows
  5. Hacking Tools Free Download
  6. Hacking Tools For Beginners
  7. Hacking Tools Software
  8. Top Pentest Tools
  9. Best Pentesting Tools 2018
  10. Android Hack Tools Github
  11. Tools 4 Hack
  12. Hack Apps
  13. Hacking Tools And Software
  14. How To Hack
  15. Pentest Tools Bluekeep
  16. Hacker Search Tools
  17. Hacker Tools
  18. Hackrf Tools
  19. What Are Hacking Tools
  20. Pentest Tools Github
  21. Tools 4 Hack
  22. Hacking Tools For Windows Free Download
  23. Pentest Tools
  24. Install Pentest Tools Ubuntu
  25. Pentest Tools Bluekeep
  26. Hack Tool Apk No Root
  27. Tools 4 Hack
  28. Hack Tools For Ubuntu
  29. Pentest Tools Android
  30. Bluetooth Hacking Tools Kali
  31. Hacker Tools Software
  32. Pentest Tools Nmap
  33. Pentest Tools Nmap
  34. Hacker Tools For Mac
  35. New Hack Tools
  36. What Is Hacking Tools
  37. Top Pentest Tools
  38. Growth Hacker Tools
  39. Pentest Tools Port Scanner
  40. Pentest Tools Find Subdomains
  41. Pentest Tools Free
  42. Hacker Tool Kit
  43. Hacker Hardware Tools
  44. Hack Tools For Ubuntu
  45. Nsa Hack Tools Download
  46. Hacking Tools Mac
  47. How To Make Hacking Tools
  48. Hacking Tools Software
  49. Blackhat Hacker Tools
  50. Physical Pentest Tools
  51. Kik Hack Tools
  52. Hacking App
  53. Pentest Tools Github
  54. Pentest Tools Download
  55. Hacking Tools For Kali Linux
  56. Kik Hack Tools
  57. Hacking Tools Windows
  58. Pentest Tools Find Subdomains
  59. Hacker Search Tools
  60. How To Hack
  61. Hack Tools
  62. Underground Hacker Sites
  63. Pentest Tools Github
  64. Hack Tools For Mac
  65. Hack Tools For Ubuntu
  66. How To Make Hacking Tools
  67. Hacking Tools For Kali Linux
  68. Termux Hacking Tools 2019
  69. Hack Tools Download
  70. Pentest Tools List
  71. Ethical Hacker Tools
  72. Hacking Tools Windows 10
  73. Hacker Hardware Tools
  74. Nsa Hack Tools Download
  75. Hacker Tools
  76. Pentest Tools Bluekeep
  77. Pentest Tools Apk
  78. Hacking Tools Windows 10
  79. Underground Hacker Sites
  80. Hacker Tools
  81. Hack Tools For Games
  82. Pentest Tools Online
  83. Pentest Tools Windows
  84. Pentest Tools Online
  85. Pentest Tools Apk
  86. Black Hat Hacker Tools
  87. Underground Hacker Sites
  88. Hacking Tools Kit
  89. Computer Hacker
  90. What Is Hacking Tools
  91. Hack Tools For Mac
  92. Pentest Tools For Ubuntu
  93. Pentest Tools Find Subdomains
  94. Pentest Tools Website
  95. Pentest Tools Find Subdomains
  96. Hacker Tools For Windows
  97. Hacking Tools Github
  98. Pentest Tools Subdomain
  99. Blackhat Hacker Tools
  100. Github Hacking Tools
  101. Beginner Hacker Tools
  102. Hacker Tools 2019
  103. Pentest Tools For Ubuntu
  104. Hacker Techniques Tools And Incident Handling
  105. Pentest Tools Windows
  106. Hacker Tools For Ios
  107. Android Hack Tools Github
  108. Black Hat Hacker Tools
  109. Hacker Tools Windows
  110. Hacking Tools Mac
  111. Hacker Tools Online
  112. Hacking Tools Hardware
  113. How To Hack
  114. Pentest Tools Review
  115. Pentest Tools Apk
  116. Hacking Tools Name
  117. World No 1 Hacker Software
  118. Hacking Tools For Kali Linux
  119. Hack Tools Download
  120. Pentest Recon Tools
  121. Termux Hacking Tools 2019
  122. Pentest Tools Bluekeep
  123. Growth Hacker Tools
  124. Hacks And Tools
  125. Hacking Tools For Windows Free Download
  126. Pentest Tools Port Scanner
  127. How To Make Hacking Tools
  128. Hacking Tools For Beginners
  129. Pentest Tools Android
  130. Hack Tools For Pc
  131. Free Pentest Tools For Windows
  132. How To Make Hacking Tools
  133. Hack Website Online Tool
  134. Hacking Tools 2019
  135. Hacking Tools For Games
  136. Hacks And Tools
  137. Blackhat Hacker Tools
  138. Hack Tools Download
  139. Hacker Tools List
  140. Pentest Tools Website Vulnerability
  141. Hacker Tools 2019
  142. Hack And Tools
  143. Pentest Tools Framework
  144. Hacker Tool Kit
  145. Hack Tools For Mac
  146. Pentest Recon Tools
  147. Hacking Tools Online
  148. Hacker Tools Online
  149. Hack Tools Github
  150. Pentest Tools For Android
  151. Pentest Tools Tcp Port Scanner
  152. What Is Hacking Tools
  153. Hacking Tools Windows 10
  154. Hacker Tools For Pc
  155. Hack Tools For Pc
  156. How To Make Hacking Tools
  157. Hacking Tools Pc

Learning Resources For Hacking And Pentesting


In this article, I'm going to provide you a list of resources which I have found very useful. I don't remember all of them from top of my head so I might miss some. This list will be updated on usual basis. Hope you'll find some good stuff to learn. If you have got suggestions leave them down below in the comments section.

Free Hands on Labs:

1. Hack The Box - live machines to hack your way around. Besides boxes they have awesome challenges and great labs to try out.
2. TryHackMe - great way to learn pentesting while doing it. Lots of machines to hack and lots of ground to cover.
3. Portswigger Web Security Academy - learn web application pentesting.

Free Training (Mostly Introductory stuff):

1. Tenable University - training and certification on Nessus etc.
2. Palo Alto Networks - Palo Alto Networks offers an abundance of resources to prepare for there certifications. The training is free but the exams cost.
3. Open P-TECH - has an introductory course on Cybersecurity Fundamentals.
4. IBM Security Learning Academy - has many courses but focused on IBM security services and 
products.
5. Cisco Networking Academy - not all courses are free but Introduction to Cybersecurity and Cybersecurity Essentials are free.
6. AWS Training and Certification - has some free cloud security training courses.
7. Metasploit Unleashed - Free Online Ethical Hacking Course - Offensive Security's free online course on metasploit.
8. Coursera and Edx - you already know about them.

Blogs:

1. HackTricks - This is simply an awesome blog just visit it and you'll fall in love.
2. pentestmonkey - I visit it most of the time for one-liner reverse shells they are awesome.

Writeups:

1. 0xdf

YouTube:

1. ippsec - an awesome YouTube channel with tons of information in every video. New video comes out weekly as soon as the machine on hackthebox expires. https://ippsec.rocks for video searching
2. xct - short walkthroughs on hackthebox machines.
3. Cristi Vlad - advice and content on pentesting and python.
4. LiveOverflow - reverse engineering on steroids.
5. SANS Pen Test Training - SANS institute webinars and talks.
6. VbScrub - great pentesting videos.
7. BinaryAdventure - great pentesting and reverse engineering videos.
8. GynvaelEN - great videos and talks about CTFs and pentesting.

GitHub Repos:

1. PayloadsAllTheThings - heaven of hackers.
2. Pentest Monkey - reverse shells and more.

More information


Tishna: An Automated Pentest Framework For Web Servers, Web Applications To Web Security

About Tishna:
   Tishna is complete automated pentest framework for web servers, application layer to web security.

   Tishna was tested on: Kali Linux, Parrot Security OS, Black Arch, Termux, Android Led TV.


Tishna's interface: Tishna has 62 options with full automation and can be use for web security swiss knife.

Tishna's installation: First, boot your Kali Linux or Parrot Security OS up. Then open Terminal and enter these commands

Appeared:
  • Cyber Space (Computer Security).
  • Terror Security (Computer Security).
  • National Cyber Security Services.

Brief Introduction
  • Tishna is useful in Banks, Private Organisations and Ethical hacker personnel for legal auditing.
  • It serves as a defense method to find as much as information possible for gaining unauthorised access and intrusion.
  • With the emergence of more advanced technology, cybercriminals have also found more ways to get into the system of many organizations.
  • Tishna software can audit, servers and web behaviour.
  • Tishna can perform Scanning & Enumeration as much as possible of target.
  • It's first step to stop cyber criminals by securing your Servers and Web Application Security.
  • Tishna is false positive free, when there is something it will show no matter what, if it is not, it will give blank results rather error.

Developer

Support to the coder
   You can sponsor and support via BTC.
   The bitcoin address: 3BuUYgEgsRuEra4GwqNVLKnDCTjLEDfptu
qr code

Related links


  1. Pentest Tools Free
  2. Pentest Box Tools Download
  3. Pentest Tools Website Vulnerability
  4. Hacker Security Tools
  5. Hacking Tools Hardware
  6. Hack Tools For Pc
  7. Hack And Tools
  8. Pentest Automation Tools
  9. Hack Tools For Ubuntu
  10. Hacking Tools
  11. Github Hacking Tools
  12. Computer Hacker
  13. Pentest Tools Find Subdomains
  14. Underground Hacker Sites
  15. How To Make Hacking Tools
  16. What Are Hacking Tools
  17. Hacker Tool Kit
  18. Termux Hacking Tools 2019
  19. Hack Tools For Pc
  20. Pentest Tools Bluekeep
  21. Computer Hacker
  22. Pentest Tools Framework
  23. Hacking Tools For Windows Free Download
  24. Tools For Hacker
  25. Pentest Tools Github
  26. Hacking Tools And Software
  27. Pentest Box Tools Download
  28. Nsa Hack Tools
  29. Game Hacking
  30. How To Install Pentest Tools In Ubuntu
  31. Pentest Tools Linux
  32. Pentest Tools List
  33. Pentest Tools
  34. Pentest Tools Online
  35. Hack Tools For Mac
  36. Pentest Recon Tools
  37. Pentest Tools Open Source
  38. Hacker Tools List
  39. Hacker Tools 2020
  40. Hacker Tools Apk
  41. Pentest Reporting Tools
  42. Pentest Automation Tools
  43. Termux Hacking Tools 2019
  44. Hack Website Online Tool
  45. Hack Website Online Tool
  46. Hacking Tools Download
  47. Hacker Hardware Tools
  48. Tools For Hacker
  49. Best Hacking Tools 2020
  50. Pentest Box Tools Download
  51. Hacker Tools Github
  52. Pentest Tools Android
  53. Hacker Hardware Tools
  54. Free Pentest Tools For Windows
  55. Hacker Tools Online
  56. Pentest Recon Tools
  57. Hackers Toolbox
  58. Hack Tools Online
  59. What Are Hacking Tools
  60. Hacking Tools 2019
  61. Free Pentest Tools For Windows
  62. Hacks And Tools
  63. Hacker Tools For Ios
  64. Pentest Tools Kali Linux
  65. Pentest Tools For Windows
  66. Hacker Tools List
  67. How To Hack
  68. Pentest Tools For Mac
  69. Hackers Toolbox
  70. Hacking Apps
  71. Hacks And Tools
  72. Hack App
  73. Android Hack Tools Github
  74. Tools 4 Hack
  75. Hacker Tools For Windows
  76. Hacking App
  77. Hacking Tools Kit
  78. New Hacker Tools
  79. Hacker Tools Apk
  80. Android Hack Tools Github
  81. Best Pentesting Tools 2018
  82. Tools Used For Hacking
  83. Hackrf Tools
  84. Pentest Tools Alternative
  85. How To Hack
  86. Hacking Tools Software
  87. Kik Hack Tools
  88. Hacker Tools For Mac
  89. Tools Used For Hacking
  90. Hacking Tools For Games
  91. Pentest Tools Tcp Port Scanner
  92. Hacker Tools
  93. Pentest Tools Free
  94. Hacker Tools Software
  95. Pentest Tools
  96. Hacker Tools
  97. Growth Hacker Tools
  98. Hacking Tools 2020
  99. Hacking Tools Software
  100. Hack App
  101. Hacking Tools For Beginners
  102. Github Hacking Tools
  103. World No 1 Hacker Software
  104. Hackers Toolbox
  105. Hackrf Tools
  106. Tools 4 Hack
  107. Hacker
  108. Hack Tools For Mac
  109. Hack Tools
  110. Hak5 Tools
  111. Easy Hack Tools
  112. Wifi Hacker Tools For Windows
  113. Hacking App
  114. Hacker Tools 2019
  115. Hacking Tools Free Download
  116. Hacker Hardware Tools
  117. Hacking Tools Windows
  118. How To Make Hacking Tools
  119. Hacking Tools Name
  120. Hack And Tools
  121. What Is Hacking Tools
  122. Hack Tools Mac
  123. Free Pentest Tools For Windows
  124. Pentest Tools Website
  125. Hacking Tools Software
  126. Easy Hack Tools
  127. Hacking Tools Github
  128. Nsa Hack Tools
  129. Pentest Automation Tools
  130. Hacker Hardware Tools
  131. Hack Tools Mac
  132. Pentest Tools Linux
  133. Hacking Tools For Windows 7
  134. Hacker Tools Mac
  135. Nsa Hack Tools Download