Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add txs from non-owners in sanity checks #45

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.25;
pragma solidity ^0.4.24;

import "../dataset/access_control/arbitrary_location_write_simple.sol";
contract WalletAttacker {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pragma solidity ^0.4.24;

import "../dataset/access_control/wallet_04_confused_sign.sol";

contract WalletAttacker {
Wallet public target;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity 0.4.25;
pragma solidity ^0.4.24;

import "../dataset/arithmetic/overflow_simple_add.sol";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.25;
pragma solidity ^0.4.24;

import "../dataset/denial_of_service/dos_simple.sol";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.25;
pragma solidity ^0.4.24;

import "../dataset/reentrancy/0x7541b76cb60f4c60af330c208b0623b7f54bf615.sol";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.25;
pragma solidity ^0.4.24;

import "../dataset/reentrancy/0x7b368c4e805c3870b6c49a3f1f49f69af8662cf3.sol";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.25;
pragma solidity ^0.4.24;

import "../dataset/reentrancy/0xcead721ef5b11f1a7b530171aab69b16c5e66b6e.sol";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.25;
pragma solidity ^0.4.24;

import "../dataset/reentrancy/0xf015c35649c82f5467c9c74b7f28ee67665aad68.sol";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity 0.4.25;
pragma solidity 0.4.24;

contract RevertContract {
// Fallback function that will fail on purpose
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity 0.4.25;
pragma solidity 0.4.24;

contract SuccessContract {
mapping(address => uint256) public balanceOf;
Expand Down
8 changes: 7 additions & 1 deletion smartbugs-curated/0.4.x/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ module.exports = {
compilers: [
{
version: "0.4.24",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
{
version: "0.4.25",
settings: {
optimizer: {
enabled: true,
runs: 1000,
runs: 200,
},
},
},
Expand Down
5 changes: 4 additions & 1 deletion smartbugs-curated/0.4.x/scripts/CustomReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ class CustomReporter extends Spec {
};

// Write to JSON file
const jsonPath = path.join(__dirname, "test-results.json");
const jsonPath = path.join(
__dirname,
"test-results" + suffix + ".json",
);
fs.writeFileSync(jsonPath, JSON.stringify(results, null, 2));
console.log(`\nTest results written to ${jsonPath}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ describe("attack access_control/FibonacciBalance.sol", function () {

it("sanity check: access_control/FibonacciBalance.sol", async function () {
const [v] = await ethers.getSigners();
const { victim } = await loadFixture(deployContracts);
const { lib, victim } = await loadFixture(deployContracts);
const fibonacciLibrary = await victim.fibonacciLibrary();
expect(fibonacciLibrary).to.not.be.empty;
// await expect(victim.withdraw()).to.not.be.reverted;
expect(fibonacciLibrary).to.equal(lib.target);
expect(await victim.start()).to.equal(3);
expect(await victim.withdrawalCounter()).to.equal(0);
expect(await victim.calculatedFibNumber()).to.equal(0);
const abi = ["function setFibonacci(uint n)"];
const iface = new ethers.Interface(abi);

Expand All @@ -65,6 +67,7 @@ describe("attack access_control/FibonacciBalance.sol", function () {
data: data,
}),
).not.be.reverted;
expect(await victim.calculatedFibNumber()).to.gt(0);
});

it("exploit access control vulnerability", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ describe("attack access_control/arbitrary_location_write_simple.sol", function (

it("sanity check: access_control/arbitrary_location_write_simple.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(victim.PushBonusCode(1)).to.not.be.reverted;
await expect(victim.PopBonusCode()).to.not.be.reverted;
const [v, a] = await ethers.getSigners();
await expect(victim.connect(a).PushBonusCode(1)).to.not.be.reverted;
await expect(victim.connect(a).PopBonusCode()).to.not.be.reverted;
});

it("exploit access control vulnerability", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ describe("attack access_control/incorrect_constructor_name1.sol", function () {

it("sanity check: access_control/incorrect_constructor_name1.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(owner.sendTransaction({ to: victim.target, value: 1 })).to.not
.be.reverted;
const [v, a] = await ethers.getSigners();
await expect(a.sendTransaction({ to: victim.target, value: 1 })).to.not.be
.reverted;
expect(await ethers.provider.getBalance(victim.target)).to.equal(1);
});

it("exploit access control vulnerability", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ describe("attack access_control/incorrect_constructor_name2.sol", function () {

it("sanity check: access_control/incorrect_constructor_name2.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(owner.sendTransaction({ to: victim.target, value: 1 })).to.not
.be.reverted;
const [v, a] = await ethers.getSigners();
await expect(a.sendTransaction({ to: victim.target, value: 1 })).to.not.be
.reverted;
expect(await ethers.provider.getBalance(victim.target)).to.equal(1);
});

it("exploit access control vulnerability", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ describe("attack access_control/incorrect_constructor_name3.sol", function () {

it("sanity check: access_control/incorrect_constructor_name3.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(owner.sendTransaction({ to: victim.target, value: 1 })).to.not
.be.reverted;
const [v, a] = await ethers.getSigners();
await expect(a.sendTransaction({ to: victim.target, value: 1 })).to.not.be
.reverted;
expect(await ethers.provider.getBalance(victim.target)).to.equal(1);
});

it("exploit access control vulnerability", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ describe("attack access_control/mapping_write.sol", function () {

it("sanity check: access_control/mapping_write.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(victim.set(1, 1)).to.not.be.reverted;
const [v, a] = await ethers.getSigners();
await expect(victim.connect(a).set(1, 1)).to.not.be.reverted;
expect(await victim.get(1)).to.equal(1);
});

it("exploit access control vulnerability", async function () {
const { victim, attacker } = await loadFixture(deployContracts);
await network.provider.send("hardhat_setBalance", [victim.target, "0x1"]);
expect(await ethers.provider.getBalance(victim.target)).to.equal(1);
await expect(attacker.withdraw_from_victim()).to.be.reverted;
await attacker.attack(attacker.getAddress());
await attacker.withdraw_from_victim();
expect(await ethers.provider.getBalance(victim.target)).to.equal(0);
expect(await ethers.provider.getBalance(attacker.target)).to.equal(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,28 @@ describe("attack access_control/multiowned_vulnerable.sol", function () {

it("sanity check: access_control/multiowned_vulnerable.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(victim.connect(owner).newOwner(owner.address)).to.not.be
const [v, a, b] = await ethers.getSigners();
await expect(victim.connect(owner).newOwner(a.address)).to.not.be.reverted;
expect(await victim.owners(a.address)).to.equal(owner.address);

await expect(victim.connect(owner).deleteOwner(a.address)).to.not.be
.reverted;
await expect(victim.connect(owner).withdrawAll()).to.not.be.reverted;
expect(Number(await victim.owners(a.address))).to.equal(0);

const amount = ethers.parseEther("1.0");
await b.sendTransaction({
to: victim.target,
value: amount,
});
expect(await ethers.provider.getBalance(victim.target)).to.equal(amount);
const balanceBefore = await ethers.provider.getBalance(owner.address);
const tx = await victim.connect(owner).withdrawAll();
const receipt = await tx.wait();
const gasFee = receipt.gasUsed * receipt.gasPrice;
expect(await ethers.provider.getBalance(victim.target)).to.equal(0);
expect(await ethers.provider.getBalance(owner.address)).to.equal(
balanceBefore - gasFee + amount,
);
});

it("exploit access control vulnerability", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,22 @@ describe("attack access_control/parity_wallet_bug_2.sol", function () {

it("sanity check: access_control/parity_wallet_bug_2.sol", async function () {
const { victim } = await loadFixture(deployContracts);
const [v, a] = await ethers.getSigners();
await expect(victim.connect(owner).initWallet([owner.address], 1, 1)).to.not
.be.reverted;
await expect(victim.connect(owner).kill(owner.address)).to.not.be.reverted;
const amount = ethers.parseEther("1");
await a.sendTransaction({
to: victim.target,
value: amount,
});
expect(await ethers.provider.getBalance(victim.target)).to.equal(amount);
const balanceBefore = await ethers.provider.getBalance(owner.address);
const tx = await victim.connect(owner).kill(owner.address);
const receipt = await tx.wait();
const gasFee = receipt.gasUsed * receipt.gasPrice;
expect(await ethers.provider.getBalance(owner.address)).to.equal(
balanceBefore - gasFee + amount,
);
});

it("exploit access control vulnerability", async function () {
Expand Down
17 changes: 15 additions & 2 deletions smartbugs-curated/0.4.x/test/access_control/phishable_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,21 @@ describe("attack access_control/phishable.sol", function () {

it("sanity check: access_control/phishable.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(victim.connect(victim_sig).withdrawAll(victim_sig.address)).to
.not.be.reverted;
const [v, a] = await ethers.getSigners();
const amount = ethers.parseEther("1.0");
await a.sendTransaction({
to: victim.target,
value: amount,
});
expect(await ethers.provider.getBalance(victim.target)).to.equal(amount);
const balanceBefore = await ethers.provider.getBalance(victim_sig.address);
const tx = await victim.withdrawAll(victim_sig.address);
const receipt = await tx.wait();
const gasFee = receipt.gasUsed * receipt.gasPrice;
expect(await ethers.provider.getBalance(victim.target)).to.equal(0);
expect(await ethers.provider.getBalance(victim_sig.address)).to.equal(
balanceBefore + amount - gasFee,
);
});

it("exploit access control vulnerability", async function () {
Expand Down
1 change: 1 addition & 0 deletions smartbugs-curated/0.4.x/test/access_control/proxy_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe("attack access_control/proxy.sol", function () {

it("sanity check: access_control/proxy.sol", async function () {
const { victim, attacker } = await loadFixture(deployContracts);

const attackerInterface = new ethers.Interface(["function benign()"]);

const data = attackerInterface.encodeFunctionData("benign");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ describe("attack access_control/unprotected0.sol", function () {

it("sanity check: access_control/unprotected0.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(victim.connect(owner).changeOwner(owner)).to.not.be.reverted;
const [v, a] = await ethers.getSigners();
await expect(victim.connect(owner).changeOwner(a)).to.not.be.reverted;
});

it("exploit access control vulnerability", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,19 @@ describe("attack access_control/wallet_02_refund_nosub.sol", function () {

it("sanity check: access_control/wallet_02_refund_nosub.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(victim.deposit({ value: 1 })).to.not.be.reverted;
await expect(victim.withdraw(1)).to.not.be.reverted;
const [v, a] = await ethers.getSigners();
const amount = ethers.parseEther("1.0");
await expect(victim.connect(a).deposit({ value: amount })).to.not.be
.reverted;

const balanceBefore = await ethers.provider.getBalance(await a.address);
const tx = await victim.connect(a).withdraw(amount);
const receipt = await tx.wait();
const gasFee = receipt.gasUsed * receipt.gasPrice;
expect(await ethers.provider.getBalance(victim.target)).to.equal(0);
expect(await ethers.provider.getBalance(await a.address)).to.equal(
balanceBefore + amount - gasFee,
);
});

it("exploit access control vulnerability", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers");
const { expect } = require("chai");
const path = require("path");
const fs = require("fs");
const { on } = require("events");

describe("attack access_control/wallet_03_wrong_constructor.sol", function () {
async function deployContracts() {
Expand All @@ -25,8 +26,20 @@ describe("attack access_control/wallet_03_wrong_constructor.sol", function () {

it("sanity check: access_control/wallet_03_wrong_constructor.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(victim.deposit({ value: 1 })).to.not.be.reverted;
await expect(victim.withdraw(1)).to.not.be.reverted;
const [v, a] = await ethers.getSigners();
const amount = ethers.parseEther("2");
await expect(victim.connect(a).deposit({ value: amount })).to.not.be
.reverted;
expect(await ethers.provider.getBalance(victim.target)).to.equal(amount);
const balanceBefore = await ethers.provider.getBalance(a.address);
const oneEther = ethers.parseEther("1");
const tx = await victim.connect(a).withdraw(oneEther);
const receipt = await tx.wait();
const gasFee = receipt.gasUsed * receipt.gasPrice;
expect(await ethers.provider.getBalance(victim.target)).to.equal(oneEther);
expect(await ethers.provider.getBalance(a.address)).to.equal(
balanceBefore - gasFee + oneEther,
);
});

it("exploit access control vulnerability", async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require("fs");

describe("attack access_control/wallet_04_confused_sign.sol", function () {
async function deployContracts() {
const [v] = await ethers.getSigners();
const codePath = path.join(
__dirname,
"../../artifacts/contracts/dataset/access_control/wallet_04_confused_sign.sol/Wallet.json",
Expand All @@ -13,7 +14,7 @@ describe("attack access_control/wallet_04_confused_sign.sol", function () {
const Wallet = await ethers.getContractFactory(
"contracts/dataset/access_control/wallet_04_confused_sign.sol:Wallet",
);
const victim = await Wallet.deploy();
const victim = await Wallet.connect(v).deploy();
await victim.waitForDeployment();
const address = await victim.getAddress();

Expand All @@ -27,8 +28,35 @@ describe("attack access_control/wallet_04_confused_sign.sol", function () {

it("sanity check: access_control/wallet_04_confused_sign.sol", async function () {
const { victim } = await loadFixture(deployContracts);
await expect(victim.deposit({ value: 1 })).to.not.be.reverted;
await expect(victim.withdraw(1)).to.not.be.reverted;
const [v, a] = await ethers.getSigners();

const amount = ethers.parseEther("2");
await expect(victim.connect(a).deposit({ value: amount })).to.not.be
.reverted;
expect(await ethers.provider.getBalance(victim.target)).to.equal(amount);
let balanceBefore = await ethers.provider.getBalance(a.address);

const oneEther = ethers.parseEther("1");
let tx = await victim.connect(a).withdraw(amount);
let receipt = await tx.wait();
let gasFee = receipt.gasUsed * receipt.gasPrice;
expect(await ethers.provider.getBalance(victim.target)).to.equal(0);
expect(await ethers.provider.getBalance(a.address)).to.equal(
balanceBefore - gasFee + amount,
);

await expect(victim.connect(a).deposit({ value: oneEther })).to.not.be
.reverted;
expect(await ethers.provider.getBalance(victim.target)).to.equal(oneEther);

balanceBefore = await ethers.provider.getBalance(v.address);
tx = await victim.connect(v).migrateTo(v.address);
receipt = await tx.wait();
gasFee = receipt.gasUsed * receipt.gasPrice;
expect(await ethers.provider.getBalance(victim.target)).to.equal(0);
expect(await ethers.provider.getBalance(v.address)).to.equal(
balanceBefore - gasFee + oneEther,
);
});

it("exploit access control vulnerability", async function () {
Expand Down
Loading
Loading