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

feat: Add malicious contract interaction as an option #356

Merged
merged 6 commits into from
Aug 23, 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
7 changes: 7 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,13 @@ <h5>Transactions</h5>
>
Malicious ERC20 Approval (BUSD)
</button>
<button
class="btn btn-primary btn-lg btn-block mb-3"
id="maliciousContractInteractionButton"
disabled
>
Malicious Contract Interaction
</button>
<button
class="btn btn-primary btn-lg btn-block mb-3"
id="maliciousSetApprovalForAll"
Expand Down
63 changes: 55 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ERC20_SAMPLE_CONTRACTS,
ERC721_SAMPLE_CONTRACTS,
NETWORKS_BY_CHAIN_ID,
MALICIOUS_CONTRACT_ADDRESSES,
} from './onchain-sample-contracts';
import { getPermissionsDisplayString, stringifiableToHex } from './utils';

Expand All @@ -39,6 +40,11 @@ const {
* Page
*/

const SEPOLIA_NETWORK_ID_HEX = '0xaa36a7';
const SEPOLIA_NETWORK_ID_DEC = '11155111';
const BASE_NETWORK_ID = '8453';
const BASE_NETWORK_ID_HEX = '0x2105';

const currentUrl = new URL(window.location.href);
const forwarderOrigin =
currentUrl.hostname === 'localhost' ? 'http://localhost:9010' : undefined;
Expand Down Expand Up @@ -309,6 +315,10 @@ const mintSepoliaERC20 = document.getElementById('mintSepoliaERC20');
const maliciousApprovalButton = document.getElementById(
'maliciousApprovalButton',
);
const maliciousContractInteractionButton = document.getElementById(
'maliciousContractInteractionButton',
);

const maliciousERC20TransferButton = document.getElementById(
'maliciousERC20TransferButton',
);
Expand Down Expand Up @@ -429,6 +439,7 @@ const allConnectedButtons = [
signInvalidVerifyingContractType,
eip747WatchButton,
maliciousApprovalButton,
maliciousContractInteractionButton,
maliciousSetApprovalForAll,
maliciousERC20TransferButton,
maliciousRawEthButton,
Expand Down Expand Up @@ -482,6 +493,7 @@ const initialConnectedButtons = [
signInvalidVerifyingContractType,
eip747WatchButton,
maliciousApprovalButton,
maliciousContractInteractionButton,
maliciousSetApprovalForAll,
maliciousERC20TransferButton,
maliciousRawEthButton,
Expand Down Expand Up @@ -708,16 +720,31 @@ const handleNewChain = (chainId) => {
}
};

const handleNewNetwork = (networkId) => {
function isSepoliaNetworkId(networkId) {
return (
networkId === SEPOLIA_NETWORK_ID_DEC || networkId === SEPOLIA_NETWORK_ID_HEX
);
}

function isBaseNetworkId(networkId) {
return networkId === BASE_NETWORK_ID || networkId === BASE_NETWORK_ID_HEX;
}

function toggleSepoliaMintButton(networkId) {
mintSepoliaERC20.hidden = !isSepoliaNetworkId(networkId);
}

function toggleMaliciousContractInteractionButton(networkId) {
maliciousContractInteractionButton.hidden =
isBaseNetworkId(networkId) || isSepoliaNetworkId(networkId);
}

function handleNewNetwork(networkId) {
networkDiv.innerHTML = networkId;
const isNetworkIdSepolia = networkId === ('11155111' || '0xaa36a7');

if (isNetworkIdSepolia) {
mintSepoliaERC20.hidden = false;
} else {
mintSepoliaERC20.hidden = true;
}
};
toggleSepoliaMintButton(networkId);
toggleMaliciousContractInteractionButton(networkId);
}

const getNetworkAndChainId = async () => {
try {
Expand Down Expand Up @@ -1604,6 +1631,26 @@ const initializeFormElements = () => {
console.log(result);
};

// Malicious Contract interaction
maliciousContractInteractionButton.onclick = async () => {
const contractAddress =
MALICIOUS_CONTRACT_ADDRESSES[networkName] ||
MALICIOUS_CONTRACT_ADDRESSES.default;

const result = await provider.request({
method: 'eth_sendTransaction',
params: [
{
from: accounts[0],
to: contractAddress,
data: '0xef5cfb8c0000000000000000000000000b3e87a076ac4b0d1975f0f232444af6deb96c59',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting! this calldata is calling the function claimRewards but that doesn't exist in the contracts I've checked. However I do see the Blockaid malicious warning, so I guess it's fine, even if that would make it revert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the payload and the accounts are what blockaid are using to test it, so I guess it is ok indeed

value: '0x0',
},
],
});
console.log(result);
};

// Malicious ERC20 transfer
maliciousERC20TransferButton.onclick = async () => {
let erc20Contract;
Expand Down
5 changes: 5 additions & 0 deletions src/onchain-sample-contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ export const ERC721_SAMPLE_CONTRACTS = {
base: '0x90997fc967e75b7e69f899133aab31d197beb802',
opBnb: '0x61d7e121185b1d7902a3da7f3c8ac9faaee8863b',
};

export const MALICIOUS_CONTRACT_ADDRESSES = {
mainnet: '0x000062Accd1a9d62eF428eC86cA3dD4f45120000',
default: '0x00008F1149168C1D2fa1eBa1Ad3e9cD644510000',
};
Loading