Cryptography / Cryptocurrency
This module explores problems in two forms of "crypto": cryptography and cryptocurrency. These are different, but the name collision has put both here!
Solidity & Smart Contract Basics
Smart contracts are self-executing programs that run on a blockchain. They allow you to encode business logic and agreements directly into code that cannot be altered once deployed (immutable).
Key Concepts
Solidity:
Solidity is the primary programming language for writing smart contracts on the Ethereum Virtual Machine (EVM). It is statically typed and contract-oriented, meaning it organizes code into contracts similar to classes in object-oriented programming.Contracts:
A contract is like a blueprint for your decentralized application. It contains state variables, functions, and events that define its behavior and interactions. When you write a contract, you choose a descriptive name (e.g.,Exploit,Token,Voting) that reflects its purpose.Deployment:
Deploying a smart contract means publishing it to the blockchain, where it becomes immutable and available for interaction. The deployment process involves compiling your Solidity code and sending it to the network, usually using tools like Foundry, Truffle, or Hardhat.
Solidity File and Contract Names
When working with Solidity, it's important to understand the distinction between the Solidity file name and the contract name within that file:
Solidity File Name
- Definition:
The Solidity file name (e.g.,MyContract.sol) is the name of the file containing your Solidity code.
A file namedMyContract.solmight contain one or more contracts related to different functionalities.
Contract Name
- Definition:
The contract name is declared inside the Solidity file and defines the actual smart contract, which contains state variables, functions, and events.
InMyContract.sol, you might have a main contract declared ascontract MyContract { ... }and other contracts if needed.
Recommended Resources
Solidity Documentation:
Comprehensive official documentation for learning and mastering Solidity.
Solidity DocsEthereum Smart Contract Best Practices:
A guide to secure smart contract development and common patterns.
Smart Contract Best PracticesSolidity By Example:
A collection of practical examples to help you quickly understand Solidity concepts.
Solidity By ExampleOpenZeppelin:
Provides a library of secure and community-vetted smart contracts which you can use as building blocks.
OpenZeppelin ContractsCyfrin:
Tutorials on smart contract development and blockchain security.
CyfrinRareskills:
Practical tutorials and modern resources for blockchain and smart contract development.
Rareskills
How to interact with the blockchain
This guide explains how to use the blockchain command to interact with the challenge. Whether you're starting a blockchain, stopping it, checking your progress, or deploying a solution, this script makes it simple. Below, we’ll go through each command step-by-step.
Initialize the Environment:
blockchain init- This command initializes the blockchain environment and starts the server.
- Note: The server will start and its logs will be visible. Keep this terminal open while working on the challenge.
Start the Blockchain:
blockchain start- This command starts a new private blockchain instance. It parses the output from the server and updates the configuration file (
Config.sol) with the new values. - Note: You can only use this command once which is when you start the challenge.If by any chance, you need a new instance of the private blockchain use
blockchain restart.
- This command starts a new private blockchain instance. It parses the output from the server and updates the configuration file (
Copy Challenge Files:
cp -r /challenge /home/hacker/name_of_your_folder
Copy the content of/challengedirectory to/home/hackerto ensure you can write your exploits and save your files in a permanent way even if you restart the challenge, your progress won't be lost. (No need to change user.)Restart the Blockchain:
blockchain restart- Use this command if you need to refresh your blockchain instance and get new configuration values. This command stops the current blockchain instance and then starts a new instance. The new configuration values are parsed and updated in
/challenge/Config.solautomatically.
- Use this command if you need to refresh your blockchain instance and get new configuration values. This command stops the current blockchain instance and then starts a new instance. The new configuration values are parsed and updated in
View Blockchain Configuration:
blockchain info- This command reads the configuration from
/challenge/Config.soland displays the current settings.
- This command reads the configuration from
Retrieve the Flag:
blockchain get flag- This command retrieves the flag from the blockchain instance. It uses the Node ID and Challenge contract address from
/challenge/Config.solto send a request to get the flag.
- This command retrieves the flag from the blockchain instance. It uses the Node ID and Challenge contract address from
Deploy Your Contract/Exploit:
blockchain deploy <solidity_file_path> <contract_name> [value]- Use this command to deploy any Solidity contract (for example, your exploit).
- Replace
<solidity_file_path>with the full path to your Solidity file and<contract_name>with the name of the contract to deploy. - The optional
[value]argument can be included if your deployment requires sending a specific amount of Ether. - Example:
blockchain deploy /home/hacker/test/Test.sol Testblockchain deploy /home/hacker/test/Test.sol Test 2ether
Additional Note:
- If you are comfortable using Foundry, you can also deploy and test your contract using
forge createdirectly. - Both
blockchain start&blockchain restartcommands automatically update and save new values in the configuration file. - With each challenge, you will find a dummy Exploit contract you can use as an initial template to write your exploit. You can't get the flag using this dummy contract.
Have fun and good luck!