Solidity Security Practices Part IX: Differential Fuzzing

By akohad Apr3,2023

[ad_1]

Why is Differential Fuzzing Effective for Solidity?

Implementing Differential Fuzzing in Solidity

Creating Two Implementations of a Contract

pragma solidity ^0.8.0;
contract EtherSenderTransfer {
function sendEther(address payable _recipient) public payable {
_recipient.transfer(msg.value);
}
}
contract EtherSenderSend {
function sendEther(address payable _recipient) public payable {
require(_recipient.send(msg.value));
}
}

Comparing Outputs with a Fuzzing Framework

npm install fuzzilli
// Import the Fuzzilli library
const Fuzzilli = require('fuzzilli');
// Import our two contract implementations
const EtherSenderTransfer = artifacts.require('EtherSenderTransfer');
const EtherSenderSend = artifacts.require('EtherSenderSend');
// Create a new Fuzzilli instance
const fuzzer = new Fuzzilli.SolidityFuzzer();
// Define the number of iterations to run
const numIterations = 1000;
// Define a function to compare the outputs of our two implementations
function compareOutputs(output1, output2) {
if (output1 != output2) {
console.log(`Outputs do not match: ${output1} vs ${output2}`);
}
}
// Define a function to run Fuzzilli on our two implementations
async function runFuzzer() {
for (let i = 0; i < numIterations; i++) {
// Generate a random input for our contract
const input = fuzzer.generateRandomInput();
// Deploy the two implementations of our contract
const contract1 = await EtherSenderTransfer.new();
const contract2 = await EtherSenderSend.new();
// Call the sendEther function on both implementations with the same input
const output1 = await contract1.sendEther(input.recipient, {value: input.amount});
const output2 = await contract2.sendEther(input.recipient, {value: input.amount});
// Compare the outputs of the two implementations
compareOutputs(output1, output2);
}
}
// Call the runFuzzer function
runFuzzer();

Final Words

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *