
智能合约黑客攻击 Ethernaut: 29. Switch
Ethernaut 是一个由 OpenZeppelin 基于 Solidity 编程语言开发的对抗游戏,每个关卡都有需要被 Hack 的智能合约。教程GitHub - 攻击代码Bilibili - 视频教程YouTube - 视频教程TikTok - ….题目将合约中的 switchOn 改为 trueHack思路这道题 难度比较高,需要构建非常特殊的 CALLDATA 。const attack = '0x30c13ade0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000020606e1500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000476227e120000000000000000000...

智能合约黑客攻击 Ethernaut: 1.Fallback
Ethernaut 是一个基于 Web3/Solidity 的对抗游戏,受 overthewire.org 启发, 运行于以太坊虚拟机. 每个关卡是一个需要被 ‘hacked’ 的智能合约。题目把 Fallback 合约中的 owner改为自己,并且把合约中的余额转走。Hack思路合约本来应该是想做一个捐款合约,捐钱最多的人拥有这个合约的权限,但是在接收 Ether 的 receive() 回调方法里验证逻辑不对,可以比较容易跳过检查, 用非常少的钱就用有这个合约的 Owner 权限。// 1.检查合约owner、余额等信息 await contract.owner(); await getBalance(contract.address) // 2.发送最低金额以成为捐款人 await contract.contribute({value: toWei("0.0001")}) // 3.直接发送给合同 1 wei,这将使我们成为新的所有者 await sendTransaction({ from: player, to: contract.address , value:1 }...

智能合约黑客攻击 Ethernaut: 3. CoinFlip
Ethernaut 是一个由 OpenZeppelin 基于 Solidity 编程语言开发的对抗游戏,每个关卡都有需要被 Hack 的智能合约。教程GitHub - 攻击代码Bilibili - 视频教程YouTube - 视频教程TikTok - ….题目一个猜硬币正反面游戏,需要连续猜对10次。Hack思路所有的计算逻辑全部都写在了合约里,完全可以把逻辑复制一份计算出来。interface ICoinFlipChallenge { function flip(bool _guess) external returns (bool); } contract CoinFlipAttack { uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968; ICoinFlipChallenge public exploitInst; constructor(address _target) { exploitInst = ICoinFlipChallen...
Twitter: https://twitter.com/LeekDEV YouTube: https://www.youtube.com/@LeekDEV BILIBILI: https://space.bilibili.com/3493272831920239

智能合约黑客攻击 Ethernaut: 29. Switch
Ethernaut 是一个由 OpenZeppelin 基于 Solidity 编程语言开发的对抗游戏,每个关卡都有需要被 Hack 的智能合约。教程GitHub - 攻击代码Bilibili - 视频教程YouTube - 视频教程TikTok - ….题目将合约中的 switchOn 改为 trueHack思路这道题 难度比较高,需要构建非常特殊的 CALLDATA 。const attack = '0x30c13ade0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000020606e1500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000476227e120000000000000000000...

智能合约黑客攻击 Ethernaut: 1.Fallback
Ethernaut 是一个基于 Web3/Solidity 的对抗游戏,受 overthewire.org 启发, 运行于以太坊虚拟机. 每个关卡是一个需要被 ‘hacked’ 的智能合约。题目把 Fallback 合约中的 owner改为自己,并且把合约中的余额转走。Hack思路合约本来应该是想做一个捐款合约,捐钱最多的人拥有这个合约的权限,但是在接收 Ether 的 receive() 回调方法里验证逻辑不对,可以比较容易跳过检查, 用非常少的钱就用有这个合约的 Owner 权限。// 1.检查合约owner、余额等信息 await contract.owner(); await getBalance(contract.address) // 2.发送最低金额以成为捐款人 await contract.contribute({value: toWei("0.0001")}) // 3.直接发送给合同 1 wei,这将使我们成为新的所有者 await sendTransaction({ from: player, to: contract.address , value:1 }...

智能合约黑客攻击 Ethernaut: 3. CoinFlip
Ethernaut 是一个由 OpenZeppelin 基于 Solidity 编程语言开发的对抗游戏,每个关卡都有需要被 Hack 的智能合约。教程GitHub - 攻击代码Bilibili - 视频教程YouTube - 视频教程TikTok - ….题目一个猜硬币正反面游戏,需要连续猜对10次。Hack思路所有的计算逻辑全部都写在了合约里,完全可以把逻辑复制一份计算出来。interface ICoinFlipChallenge { function flip(bool _guess) external returns (bool); } contract CoinFlipAttack { uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968; ICoinFlipChallenge public exploitInst; constructor(address _target) { exploitInst = ICoinFlipChallen...
Twitter: https://twitter.com/LeekDEV YouTube: https://www.youtube.com/@LeekDEV BILIBILI: https://space.bilibili.com/3493272831920239

Subscribe to Leek DEV

Subscribe to Leek DEV
<100 subscribers
<100 subscribers
Share Dialog
Share Dialog


Now there are many ways to write contracts to save Gas, here is a good case found more step-by-step, you can refer to.
A method to calculate the sum of even numbers in an input array and record the result after the calculation.
The inputs are constant [12, 3, 4, 5, 3, 44, 2, 12, 3, 4, 5, 21, 46, 1, 2, 12].
uint public total
function sumIfEvenAndLessThan99(uint[] memory nums) external {
for (uint i = 0; i < nums.length; i += 1) {
bool isEven = nums[i] % 2 == 0;
bool isLessThan99 = nums[i] < 99;
if (isEven && isLessThan99) {
total += nums[i];
}
}
};
Solidity variables memory, calldata 2 represent a very similar role, are temporary variables within the function, their biggest difference is that calldata is not modifiable, in some read-only case save Gas.
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
for (uint i = 0; i < nums.length; i += 1) {
bool isEven = nums[i] % 2 == 0;
bool isLessThan99 = nums[i] < 99;
if (isEven && isLessThan99) {
total += nums[i];
}
}
}
Solidity function is also similar to the stack to structure, read and write variables inside the function to save Gas than read and write external variables, in some need high frequency read and write scenarios will be a good way to copy the function external variables to the function internal operation.
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
uint _total = total;
for (uint i = 0; i < nums.length; i += 1) {
bool isEven = nums[i] % 2 == 0;
bool isLessThan99 = nums[i] < 99;
if (isEven && isLessThan99) {
_total += nums[i];
}
}
total = _total;
}
This seems to be related to c++ language features
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
uint _total = total;
for (uint i = 0; i < nums.length; i += 1) {
if (nums[i] % 2 == 0 && nums[i] < 99) {
_total += nums[i];
}
}
total = _total;
}
The memory declared by Solidity is to be counted as Gas, and there are times when it is appropriate to reduce the number of internally declared variables.
It is true that `isEven` and `isLessThan99` variables are declared inside the loop, and these variables are only used to make a conditional judgment, so it is obvious that they can be combined to reduce the number of internally declared variables.
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
uint _total = total;
for (uint i = 0; i < nums.length; ++i) {
if (nums[i] % 2 == 0 && nums[i] < 99) {
_total += nums[i];
}
}
total = _total;
}
The array length `nums.length` is read every loop with the array loop variable `nums[i]`, and is read from the argument, which can be copied into memory to save more gas.
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
uint _total = total;
uint len = nums.length;
for (uint i = 0; i < len; ++i) {
uint num = nums[i];
if (num % 2 == 0 && num < 99) {
_total += num;
}
}
total = _total;
}
Solidity 8.0 and later will do a benefit check on the numbers by default, which will consume a certain amount of Gas by default, and can be unchecked by adding `Unchecked` to save a lot of Gas in some cases.
function sumIfEvenAndLessThan99(uint256[] calldata nums) external {
uint256 _total = total;
uint256 len = nums.length;
for (uint256 i = 0; i < len; ) {
uint256 num = nums[i];
if (num % 2 == 0 && num < 99) {
unchecked {
_total += num;
}
}
unchecked {
++i;
}
}
unchecked {
total = _total;
}
}
The gas savings after each modification compared to the beginning were calculated.
https://solidity-by-example.org/gas-golf/
Now there are many ways to write contracts to save Gas, here is a good case found more step-by-step, you can refer to.
A method to calculate the sum of even numbers in an input array and record the result after the calculation.
The inputs are constant [12, 3, 4, 5, 3, 44, 2, 12, 3, 4, 5, 21, 46, 1, 2, 12].
uint public total
function sumIfEvenAndLessThan99(uint[] memory nums) external {
for (uint i = 0; i < nums.length; i += 1) {
bool isEven = nums[i] % 2 == 0;
bool isLessThan99 = nums[i] < 99;
if (isEven && isLessThan99) {
total += nums[i];
}
}
};
Solidity variables memory, calldata 2 represent a very similar role, are temporary variables within the function, their biggest difference is that calldata is not modifiable, in some read-only case save Gas.
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
for (uint i = 0; i < nums.length; i += 1) {
bool isEven = nums[i] % 2 == 0;
bool isLessThan99 = nums[i] < 99;
if (isEven && isLessThan99) {
total += nums[i];
}
}
}
Solidity function is also similar to the stack to structure, read and write variables inside the function to save Gas than read and write external variables, in some need high frequency read and write scenarios will be a good way to copy the function external variables to the function internal operation.
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
uint _total = total;
for (uint i = 0; i < nums.length; i += 1) {
bool isEven = nums[i] % 2 == 0;
bool isLessThan99 = nums[i] < 99;
if (isEven && isLessThan99) {
_total += nums[i];
}
}
total = _total;
}
This seems to be related to c++ language features
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
uint _total = total;
for (uint i = 0; i < nums.length; i += 1) {
if (nums[i] % 2 == 0 && nums[i] < 99) {
_total += nums[i];
}
}
total = _total;
}
The memory declared by Solidity is to be counted as Gas, and there are times when it is appropriate to reduce the number of internally declared variables.
It is true that `isEven` and `isLessThan99` variables are declared inside the loop, and these variables are only used to make a conditional judgment, so it is obvious that they can be combined to reduce the number of internally declared variables.
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
uint _total = total;
for (uint i = 0; i < nums.length; ++i) {
if (nums[i] % 2 == 0 && nums[i] < 99) {
_total += nums[i];
}
}
total = _total;
}
The array length `nums.length` is read every loop with the array loop variable `nums[i]`, and is read from the argument, which can be copied into memory to save more gas.
uint public total;
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
uint _total = total;
uint len = nums.length;
for (uint i = 0; i < len; ++i) {
uint num = nums[i];
if (num % 2 == 0 && num < 99) {
_total += num;
}
}
total = _total;
}
Solidity 8.0 and later will do a benefit check on the numbers by default, which will consume a certain amount of Gas by default, and can be unchecked by adding `Unchecked` to save a lot of Gas in some cases.
function sumIfEvenAndLessThan99(uint256[] calldata nums) external {
uint256 _total = total;
uint256 len = nums.length;
for (uint256 i = 0; i < len; ) {
uint256 num = nums[i];
if (num % 2 == 0 && num < 99) {
unchecked {
_total += num;
}
}
unchecked {
++i;
}
}
unchecked {
total = _total;
}
}
The gas savings after each modification compared to the beginning were calculated.
https://solidity-by-example.org/gas-golf/
No activity yet