{"id":21936104,"url":"https://github.com/learnweb3dao/malicious-external-contract","last_synced_at":"2026-01-28T19:01:25.215Z","repository":{"id":39610848,"uuid":"481661799","full_name":"LearnWeb3DAO/Malicious-External-Contract","owner":"LearnWeb3DAO","description":null,"archived":false,"fork":false,"pushed_at":"2022-07-13T06:43:41.000Z","size":156,"stargazers_count":0,"open_issues_count":0,"forks_count":4,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-09T19:31:57.850Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LearnWeb3DAO.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-14T15:48:41.000Z","updated_at":"2022-04-14T17:06:15.000Z","dependencies_parsed_at":"2022-07-13T09:10:52.279Z","dependency_job_id":null,"html_url":"https://github.com/LearnWeb3DAO/Malicious-External-Contract","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LearnWeb3DAO/Malicious-External-Contract","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LearnWeb3DAO%2FMalicious-External-Contract","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LearnWeb3DAO%2FMalicious-External-Contract/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LearnWeb3DAO%2FMalicious-External-Contract/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LearnWeb3DAO%2FMalicious-External-Contract/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LearnWeb3DAO","download_url":"https://codeload.github.com/LearnWeb3DAO/Malicious-External-Contract/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LearnWeb3DAO%2FMalicious-External-Contract/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28849359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-28T15:15:36.453Z","status":"ssl_error","status_checked_at":"2026-01-28T15:15:13.020Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-29T01:13:13.206Z","updated_at":"2026-01-28T19:01:25.199Z","avatar_url":"https://github.com/LearnWeb3DAO.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Malicious External Contracts\n\nIn crypto world, you will often hear about how contracts which looked legitimate were the reason behind a big scam. How are hackers able to execute malicious code from a legitimate looking contract?\n\nWe will learn one method today 👀\n\n## What will happen?\n\nThere will be three contracts - `Attack.sol`, `Helper.sol` and `Good.sol`. User will be able to enter an eligibility list using `Good.sol` which will further call `Helper.sol` to keep track of all the users which are eligible.\n\n`Attack.sol` will be designed in such a way that eligibility list can be manipulated, lets see how 👀\n\n## Build\n\nLets build an example where you can experience how the attack happens.\n\n- To setup a Hardhat project, Open up a terminal and execute these commands\n\n  ```bash\n  npm init --yes\n  npm install --save-dev hardhat\n  ```\n- If you are on Windows, please do this extra step and install these libraries as well :)\n\n  ```bash\n  npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers\n  ```\n\n- In the same directory where you installed Hardhat run:\n\n  ```bash\n  npx hardhat\n  ```\n\n  - Select `Create a basic sample project`\n  - Press enter for the already specified `Hardhat Project root`\n  - Press enter for the question on if you want to add a `.gitignore`\n  - Press enter for `Do you want to install this sample project's dependencies with npm (@nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers)?`\n\nNow you have a Hardhat project ready to go!\n\nStart by creating a new file inside the `contracts` directory called `Good.sol`\n\n```solidity\n//SPDX-License-Identifier: MIT\npragma solidity ^0.8.4;\nimport \"./Helper.sol\";\n\ncontract Good {\n    Helper helper;\n    constructor(address _helper) payable {\n        helper = Helper(_helper);\n    }\n\n    function isUserEligible() public view returns(bool) {\n        return helper.isUserEligible(msg.sender);\n    }\n\n    function addUserToList() public  {\n        helper.setUserEligible(msg.sender);\n    }\n\n    fallback() external {}\n    \n}\n```\n\nAfter creating `Good.sol`, create a new file inside the `contracts` directory named `Helper.sol`\n\n```solidity\n//SPDX-License-Identifier: MIT\npragma solidity ^0.8.4;\n\ncontract Helper {\n    mapping(address =\u003e bool) userEligible;\n\n    function isUserEligible(address user) public view returns(bool) {\n        return userEligible[user];\n    }\n\n    function setUserEligible(address user) public {\n        userEligible[user] = true;\n    }\n\n    fallback() external {}\n}\n```\n\nThe last contract that we will create inside the `contracts` directory is `Attack.sol`\n\n```solidity\n//SPDX-License-Identifier: MIT\npragma solidity ^0.8.4;\n\ncontract Attack {\n    address owner;\n    mapping(address =\u003e bool) userEligible;\n\n    constructor() {\n        owner = msg.sender;\n    }\n\n    function isUserEligible(address user) public view returns(bool) {\n        if(user == owner) {\n            return true;\n        }\n        return false;\n    }\n\n    function setUserEligible(address user) public {\n        userEligible[user] = true;\n    }\n    \n    fallback() external {}\n}\n```\n\n\nYou will notice that the fact about `Attack.sol` is that it will generate the same ABI as `Helper.sol` eventhough it has different code within it. This is because ABI only contains function definitions for public variables, functions and events. So `Attack.sol` can be typecasted as `Helper.sol`.\n\nNow because `Attack.sol` can be typecasted as `Helper.sol`, a malicious owner can deploy `Good.sol` with the address of `Attack.sol` instead of `Helper.sol` and users will believe that he is indeed using `Helper.sol` to create the eligibility list.\n\nIn our case, the scam will happen as follows. The scammer will first deploy `Good.sol` with the address of `Attack.sol`. Then when the user will enter the eligibility list using `addUserToList` function which will work fine because the code for this function is same within `Helper.sol` and `Attack.sol`.\n\nThe true colours will be observed when the user will try to call `isUserEligible` with his address because now this function will always return `false`  because it calls `Attack.sol`'s `isUserEligible` function which always returns `false` except when its the owner itself, which was not supposed to happen.\n\n\nLets try to write a test and see if this scam actually works, create a new file inside the `test` folder named `attack.js`\n\n```javascript\nconst { expect } = require(\"chai\");\nconst { BigNumber } = require(\"ethers\");\nconst { ethers, waffle } = require(\"hardhat\");\n\ndescribe(\"Attack\", function () {\n  it(\"Should change the owner of the Good contract\", async function () {\n    // Deploy the Attack contract\n    const attackContract = await ethers.getContractFactory(\"Attack\");\n    const _attackContract = await attackContract.deploy();\n    await _attackContract.deployed();\n    console.log(\"Attack Contract's Address\", _attackContract.address);\n\n    // Deploy the good contract\n    const goodContract = await ethers.getContractFactory(\"Good\");\n    const _goodContract = await goodContract.deploy(_attackContract.address, {\n      value: ethers.utils.parseEther(\"3\"),\n    });\n    await _goodContract.deployed();\n    console.log(\"Good Contract's Address:\", _goodContract.address);\n\n    const [_, addr1] = await ethers.getSigners();\n    // Now lets add an address to the eligibility list\n    let tx = await _goodContract.connect(addr1).addUserToList();\n    await tx.wait();\n\n    // check if the user is eligible\n    const eligible = await _goodContract.connect(addr1).isUserEligible();\n    expect(eligible).to.equal(false);\n  });\n});\n\n```\n\n\nTo run this test, open up your terminal pointing to the root of the directory for this level and execute this command:\n\n```bash\nnpx hardhat test\n```\n\nIf all your tests passed, this means that the scam was successful and that the user will never be determined eligible \n\n\n## Prevention\n\nMake the address of the external contract public and also get your external contract verified so that all users can view the code\n\nCreate a new contract, instead of typecasting an address into a contract inside the constructor. So instead of doing `Helper(_helper)`  where you are typecasting `_helper` address into a contract which may or may not be the `Helper` contract, create an explicit new helper contract instance using `new Helper()`.\n\nExample\n```solidity\ncontract Good {\n    Helper public helper;\n    constructor() {\n        helper = new Helper();\n    }\n```\n\n\u003cQuiz questionId=\"4e4b7aa2-7d27-4f2d-b66f-d2a3a04adb27\" /\u003e\n\u003cQuiz questionId=\"d7194a5e-fe17-420f-8dc8-1789665d0fe5\" /\u003e\n\nWow, lots of learning right? 🤯 \n\nBeaware of scammers, you might need to double check the code of a new dApp you want to put money in.\n\n\u003cSubmitQuiz /\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearnweb3dao%2Fmalicious-external-contract","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flearnweb3dao%2Fmalicious-external-contract","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearnweb3dao%2Fmalicious-external-contract/lists"}