Sunday, August 30, 2020

Smart Contract Hacking Chapter 1 - Solidity For Penetration Testers Part 1 (Hello World)

 

Note: We will start off our Smart Contract Hacking journey with some basic solidity programming in the first two weeks. After that we will ramp things up and get a little crazy deploying blockchains and liquidating funds from accounts. But since the purpose of this series is to share the information I have learned over the last two years.  I do not want to alienate those new to Smart Contracts and programming so we will take these first few weeks a bit slow. 

Also note the text was taken from a book I was / am writing, I retrofitted it for this blog, and placed videos where screenshots may otherwise exist. If something seems off.. Just DM me on twitter and I will update it anything I might have missed during editing, but I tried to edit it as best as possible to meet this format rather then a book. 

Cheers  @Fiction 

http://cclabs.io

Thanks to @GarrGhar for helping me edit/sanity check info for each of the chapters. 


About Solidity

The solidity programming language is the language used to write smart contracts on the Ethereum blockchain. As of my initial writing of this chapter the current compiler version was 0.6.6. However, the versions change rapidly. For example, when I started coding in solidity 2 years ago, solidity was in version 4 and now its version 7 with major library and coding stylistic requirement updates in version 5. 

So, note that when compiling your code for labs its best to use the version sited in that particular example. This is easily achieved in the online compilers, by selecting the compiler version from the dropdown menu. If you would like to give yourself a small challenge, use the latest compiler version and try to modify the code to work with it. Usually this just requires a few minor modifications and can be a good learning experience under the hood of how Solidity works and what has changed.

Solidity is very similar to writing JavaScript and is fully object oriented. In the intro chapters we will attempt to provide a quick overview of solidity understanding needed for a penetration tester. This will not be full guide to programming, as programming is considered to be a pre-requisite to application hacking. Instead this chapter will be a gentle introduction of needed concepts you will use throughout this book. Solidity is also a needed pre-requisite for understanding the rest of the information and its associated exploitation course. 

However, as long as you understand general programming concepts then you should have no trouble understanding solidity. It is a relatively easy language to get up and running with quickly in comparison to more mature languages like C++ and Java which may take a more significant amount of time to learn.

The most important thing to understand with solidity is that unlike traditional languages, solidity handles transactions of monetary value by default. Meaning you don't need to attach to a payment API to add transactions to your applications. Payment functionality is baked into the language as its primary purpose and for usage with the Ethereum blockchain.  All that's needed for financial transactions in solidity is a standard library transfer function, and you can easily send value to anyone's public address. 

For example, the following simple function will transfer a specified amount of Ether to the user calling the function provided they have a large enough balance to allow the transfer. But lets not dive into that just yet. 

 

1.  function withdraw (uint amount) {
2.     require (amount <= balances[msg.sender]);
3.     msg.sender.transfer(amount);
4.  }

 

Structure of a Smart Contract

Rather than discuss payments at this point, let's not jump to far ahead of ourselves. We need to understand the structure of a smart contract. Let's take a look at a Hello World example. We will analyze all of the key aspects that make solidity different then other languages you may currently understand.

You can easily follow along with this on http://remix.ethereum.org which is a free online IDE and compiler for coding in solidity. A full video walk through of Remix is included later on in this chapter.  Remix contains in-browser compilers and virtual environments that emulate block creation and allow you to send and receive transactions.  This is a powerful development tool and absolutely free to use. 

Below is the simple code example we will analyze before moving on to a live walk through. 

1.  pragma solidity 0.6.6; 
2.   
3.  contract HelloWorld {
4.           
5.     constructor () public payable {
6.           //This is a comment
7.           //You can put your configuration information here
8.     }
9.   
10.   function hello () public pure returns (string memory) {
11.                  return "Hello World";
12.         }
13.}

 

There is a lot going on in this small program so I will try to break it down as simple as possible. In the first line, we have the pragma statement which is required at the top of each program to let the compiler know which version of solidity this code was written for.  As I said earlier, these versions change rapidly due to the evolving technology and many changes are implemented into each new version. So, the compiler needs to know which version you intended this to run on.

On line 3 is the word "contract" followed by whatever name you wish to call your contract. The contract's functionality is then enclosed in curly braces. This is similar to creating a class in any other language. It's a block of associated code that can be inherited, or interfaced with and contains its own variables and methods.

On line 5 contained within the contract curly braces we have a constructor denoted by the word "constructor".  The constructor is run one time at contract creation and used to setup any variables or details of the smart contract. This is often used for creating an administrator of the contract or other items that are needed prior to contract usage. 

Functions and variables within Solidity also have various types and visibility set with their creation.  In this case also on line 5 you will see the words "public" and "payable" used to describe the constructor. 

Public you may be familiar with as it's a common visibility keyword used in other languages denoting that anyone can call this function. There are other visibility types in Solidity listed below, we will cover each of these in more detail as we use them to our advantage when hacking smart contracts:

 

Public

This allows anyone to call and use this function

 

Private

This allows only the current contract and its functions to call it directly.

 

Internal

This is similar to private except it also allows derived contracts to use its functionality

 

External

External can only be called externally by other contracts unless the "this" keyword is used with the function call.

 

The second keyword in the constructor definition "payable" you may not be familiar with unless you have worked on blockchain projects. The word payable within solidity is needed on any item that can receive Ether. So, by setting the constructor as payable we can send a base amount of Ether to the contract when its deployed. This will add an initial monetary liquidity for whatever functionality the contract is providing. For example, if this were a gambling game, we would need some initial Ethereum to payout our winners before our revenues catch up with our payouts and we start collecting large sums of failed gambling revenue. 

Within the constructor is an example of how comments are handled in solidity, the simple double forward slash is used like in most languages. Comments function in the same way as any other language in that they are not processed and they are ignored by the compiler but are useful for understanding the code you wrote later after you have taking time apart from reading your code.

Finally, we have our simple hello function starting on line 10. Again, there is a lot going on here. First is the name of the function with parentheses that can contain arguments like in any other language. However, this function does not take arguments.

You will notice two more keywords in the function definition "pure" and "returns". Returns is simply the way the function denotes that it will return a value to the user, which it then states directly after it what type of variable it returns. In this case, it returns a string in memory.  We will talk about memory and storage later on and the security implications of them.

Next is the word "Pure" there are a couple types of functions in Solidity which will list below with a brief description.


View

This type of function does not modify or change the state of the contract but may return values and use global variables.

Pure

A pure function is a function which is completely self-contained in that it only uses local variables and it does not change the state of the smart contract.


Finally, in line 11 we return our string to the user who called the function. In the context of a user, this could be a physical user using an application or smart contract functionality or it could actually be another smart contract calling the function.

 

Hands on Lab – Remix HelloWorld

Now that we talked over in detail all the new concepts to solidity programs using a small example, lets compile and run this code on remix.ethereum.org.

Action Steps:

ü Browse to remix.etherum.org
ü Type out the the code from above (Do not copy Paste it)
ü Compile and deploy the code
ü Review the transaction in the log window

 

Intro to the Remix Development Environment Video


In Remix create a new file and type out the example helloworld code.  I would suggest that you actually type out all of the examples in this book. They will not be exhaustive or long and will provide you great value and make you comfortable when it comes to writing your own exploits and using the compilers and tools. These are all essential tools to your understanding.

Within your remix environment, you will want to select the compiler version 0.6.6 to ensure that this code runs correctly. If you typed out the code correctly you should not receive any errors and you will be able to deploy and interact with it. In the following video we will walk you through that process and explain some nuances of solidity. 


Explaining and Compiling HelloWorld Video: 




     

    Lets now quickly review a few key points about the transaction that you saw within the video when compiling your code. This transaction is shown below. 

    __________________________________________________________________________________

    call to HelloWorld.hello

    CALL

    from      0xBF8B5A94eD4dFB45089b455B1A0e296D6669c625

     to           HelloWorld.hello() 0xADe285e11e0B9eE35167d1E25C3605Eba1778C86

     transaction cost               21863 gas (Cost only applies when called by a contract)

                                             execution cost 591 gas (Cost only applies when called by a contract)

     hash     0x14557f9552d454ca865deb422ebb50a853735b57efaebcfc9c9abe57ba1836ed

     input    0x19f...f1d21

     decoded input {}

     decoded output               {

                    "0": "string: Hello World"

    }

     logs       []

    _________________________________________________________________________________

     

    The output above is a hello transaction which contains the relevant data retrieved when you executed the hello function in the video. The first important thing to notice is the word "CALL". In solidity there are call and send transactions. The difference between the two is whether they change the state of the blockchain or not. In this case we did not change the state, we only retrieved information so a CALL was issued.  If we were changing variables and sending values then a SEND transaction would have been issued instead.

    Next you will see the "From" address which should correspond with the address you used to call the transaction.  The "To" field should be the address the smart contract was given when you deployed the smart contract. You can view this on your deployment screen next to the deployed contract name by hitting the copy button and pasting it somewhere to see the full value.

    You will then see the costs and gas associated with the transaction. Costs change based on the size of the contracts and the assembly code created by the compiler. Each instruction has a cost. We will cover that later when we do a bit of debugging and decompiling. 

    Finally take note of the Decoded Output which contains the return string of "Hello World".

     

    Summary

    If you are new to solidity or new to programming in general this might have been a lot of information.  In the next chapter we cover a few more key solidity concepts before moving on to exploiting vulnerabilities where a much more in depth understanding of how solidity works and its security implications will be explored. For more solidity resources and full-length free tutorials check out the following references

      

    Homework:

    https://cryptozombies.io/en/course/

    Related news


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

    Rootkit Umbreon / Umreon - X86, ARM Samples



    Pokémon-themed Umbreon Linux Rootkit Hits x86, ARM Systems
    Research: Trend Micro


    There are two packages
    one is 'found in the wild' full and a set of hashes from Trend Micro (all but one file are already in the full package)






    Download

    Download Email me if you need the password  



    File information

    Part one (full package)

    #File NameHash ValueFile Size (on Disk)Duplicate?
    1.umbreon-ascii0B880E0F447CD5B6A8D295EFE40AFA376085 bytes (5.94 KiB)
    2autoroot1C5FAEEC3D8C50FAC589CD0ADD0765C7281 bytes (281 bytes)
    3CHANGELOGA1502129706BA19667F128B44D19DC3C11 bytes (11 bytes)
    4cli.shC846143BDA087783B3DC6C244C2707DC5682 bytes (5.55 KiB)
    5hideportsD41D8CD98F00B204E9800998ECF8427E0 bytes ( bytes)Yes, of file promptlog
    6install.sh9DE30162E7A8F0279E19C2C30280FFF85634 bytes (5.5 KiB)
    7Makefile0F5B1E70ADC867DD3A22CA62644007E5797 bytes (797 bytes)
    8portchecker006D162A0D0AA294C85214963A3D3145113 bytes (113 bytes)
    9promptlogD41D8CD98F00B204E9800998ECF8427E0 bytes ( bytes)
    10readlink.c42FC7D7E2F9147AB3C18B0C4316AD3D81357 bytes (1.33 KiB)
    11ReadMe.txtB7172B364BF5FB8B5C30FF528F6C51252244 bytes (2.19 KiB)
    12setup694FFF4D2623CA7BB8270F5124493F37332 bytes (332 bytes)
    13spytty.sh0AB776FA8A0FBED2EF26C9933C32E97C1011 bytes (1011 bytes)Yes, of file spytty.sh
    14umbreon.c91706EF9717176DBB59A0F77FE95241C1007 bytes (1007 bytes)
    15access.c7C0A86A27B322E63C3C29121788998B8713 bytes (713 bytes)
    16audit.cA2B2812C80C93C9375BFB0D7BFCEFD5B1434 bytes (1.4 KiB)
    17chown.cFF9B679C7AB3F57CFBBB852A13A350B22870 bytes (2.8 KiB)
    18config.h980DEE60956A916AFC9D2997043D4887967 bytes (967 bytes)
    19config.h.dist980DEE60956A916AFC9D2997043D4887967 bytes (967 bytes)Yes, of file config.h
    20dirs.c46B20CC7DA2BDB9ECE65E36A4F987ABC3639 bytes (3.55 KiB)
    21dlsym.c796DA079CC7E4BD7F6293136604DC07B4088 bytes (3.99 KiB)
    22exec.c1935ED453FB83A0A538224AFAAC71B214033 bytes (3.94 KiB)
    23getpath.h588603EF387EB617668B00EAFDAEA393183 bytes (183 bytes)
    24getprocname.hF5781A9E267ED849FD4D2F5F3DFB8077805 bytes (805 bytes)
    25includes.hF4797AE4B2D5B3B252E0456020F58E59629 bytes (629 bytes)
    26kill.cC4BD132FC2FFBC84EA5103ABE6DC023D555 bytes (555 bytes)
    27links.c898D73E1AC14DE657316F084AADA58A02274 bytes (2.22 KiB)
    28local-door.c76FC3E9E2758BAF48E1E9B442DB98BF8501 bytes (501 bytes)
    29lpcap.hEA6822B23FE02041BE506ED1A182E5CB1690 bytes (1.65 KiB)
    30maps.c9BCD90BEA8D9F9F6270CF2017F9974E21100 bytes (1.07 KiB)
    31misc.h1F9FCC5D84633931CDD77B32DB1D50D02728 bytes (2.66 KiB)
    32netstat.c00CF3F7E7EA92E7A954282021DD72DC41113 bytes (1.09 KiB)
    33open.cF7EE88A523AD2477FF8EC17C9DCD7C028594 bytes (8.39 KiB)
    34pam.c7A947FDC0264947B2D293E1F4D69684A2010 bytes (1.96 KiB)
    35pam_private.h2C60F925842CEB42FFD639E7C763C7B012480 bytes (12.19 KiB)
    36pam_vprompt.c017FB0F736A0BC65431A25E1A9D393FE3826 bytes (3.74 KiB)
    37passwd.cA0D183BBE86D05E3782B5B24E2C964132364 bytes (2.31 KiB)
    38pcap.cFF911CA192B111BD0D9368AFACA03C461295 bytes (1.26 KiB)
    39procstat.c7B14E97649CD767C256D4CD6E4F8D452398 bytes (398 bytes)
    40procstatus.c72ED74C03F4FAB0C1B801687BE200F063303 bytes (3.23 KiB)
    41readwrite.cC068ED372DEAF8E87D0133EAC0A274A82710 bytes (2.65 KiB)
    42rename.cC36BE9C01FEADE2EF4D5EA03BD2B3C05535 bytes (535 bytes)
    43setgid.c5C023259F2C244193BDA394E2C0B8313667 bytes (667 bytes)
    44sha256.h003D805D919B4EC621B800C6C239BAE0545 bytes (545 bytes)
    45socket.c348AEF06AFA259BFC4E943715DB5A00B579 bytes (579 bytes)
    46stat.cE510EE1F78BD349E02F47A7EB001B0E37627 bytes (7.45 KiB)
    47syslog.c7CD3273E09A6C08451DD598A0F18B5701497 bytes (1.46 KiB)
    48umbreon.hF76CAC6D564DEACFC6319FA167375BA54316 bytes (4.21 KiB)
    49unhide-funcs.c1A9F62B04319DA84EF71A1B091434C644729 bytes (4.62 KiB)
    50cryptpass.py2EA92D6EC59D85474ED7A91C8518E7EC192 bytes (192 bytes)
    51environment.sh70F467FE218E128258D7356B7CE328F11086 bytes (1.06 KiB)
    52espeon-connect.shA574C885C450FCA048E79AD6937FED2E247 bytes (247 bytes)
    53espeon-shell9EEF7E7E3C1BEE2F8591A088244BE0CB2167 bytes (2.12 KiB)
    54espeon.c499FF5CF81C2624B0C3B0B7E9C6D980D14899 bytes (14.55 KiB)
    55listen.sh69DA525AEA227BE9E4B8D59ACFF4D717209 bytes (209 bytes)
    56spytty.sh0AB776FA8A0FBED2EF26C9933C32E97C1011 bytes (1011 bytes)
    57ssh-hidden.shAE54F343FE974302F0D31776B72D0987127 bytes (127 bytes)
    58unfuck.c457B6E90C7FA42A7C46D464FBF1D68E2384 bytes (384 bytes)
    59unhide-self.pyB982597CEB7274617F286CA80864F499986 bytes (986 bytes)
    60listen.shF5BD197F34E3D0BD8EA28B182CCE7270233 bytes (233 bytes)

    part 2 (those listed in the Trend Micro article)
    #File NameHash ValueFile Size (on Disk)
    1015a84eb1d18beb310e7aeeceab8b84776078935c45924b3a10aa884a93e28acA47E38464754289C0F4A55ED7BB556489375 bytes (9.16 KiB)
    20751cf716ea9bc18e78eb2a82cc9ea0cac73d70a7a74c91740c95312c8a9d53aF9BA2429EAE5471ACDE820102C5B81597512 bytes (7.34 KiB)
    30a4d5ffb1407d409a55f1aed5c5286d4f31fe17bc99eabff64aa1498c5482a5f0AB776FA8A0FBED2EF26C9933C32E97C1011 bytes (1011 bytes)
    40ce8c09bb6ce433fb8b388c369d7491953cf9bb5426a7bee752150118616d8ffB982597CEB7274617F286CA80864F499986 bytes (986 bytes)
    5122417853c1eb1868e429cacc499ef75cfc018b87da87b1f61bff53e9b8e86709EEF7E7E3C1BEE2F8591A088244BE0CB2167 bytes (2.12 KiB)
    6409c90ecd56e9abcb9f290063ec7783ecbe125c321af3f8ba5dcbde6e15ac64aB4746BB5E697F23A5842ABCAED36C9146149 bytes (6 KiB)
    74fc4b5dab105e03f03ba3ec301bab9e2d37f17a431dee7f2e5a8dfadcca4c234D0D97899131C29B3EC9AE89A6D49A23E65160 bytes (63.63 KiB)
    88752d16e32a611763eee97da6528734751153ac1699c4693c84b6e9e4fb08784E7E82D29DFB1FC484ED277C70218781855564 bytes (54.26 KiB)
    9991179b6ba7d4aeabdf463118e4a2984276401368f4ab842ad8a5b8b730885222B1863ACDC0068ED5D50590CF792DF057664 bytes (7.48 KiB)
    10a378b85f8f41de164832d27ebf7006370c1fb8eda23bb09a3586ed29b5dbdddfA977F68C59040E40A822C384D1CEDEB6176 bytes (176 bytes)
    11aa24deb830a2b1aa694e580c5efb24f979d6c5d861b56354a6acb1ad0cf9809bDF320ED7EE6CCF9F979AEFE451877FFC26 bytes (26 bytes)
    12acfb014304b6f2cff00c668a9a2a3a9cbb6f24db6d074a8914dd69b43afa452584D552B5D22E40BDA23E6587B1BC532D6852 bytes (6.69 KiB)
    13c80d19f6f3372f4cc6e75ae1af54e8727b54b51aaf2794fedd3a1aa463140480087DD79515D37F7ADA78FF5793A42B7B11184 bytes (10.92 KiB)
    14e9bce46584acbf59a779d1565687964991d7033d63c06bddabcfc4375c5f1853BBEB18C0C3E038747C78FCAB3E0444E371940 bytes (70.25 KiB)

    More info