From 67bc2f6882855dfb35cefc6dda1cea162a1404b3 Mon Sep 17 00:00:00 2001 From: Nathan FLATTIN Date: Thu, 18 Jul 2024 15:26:10 +0200 Subject: [PATCH 1/2] fix: detection bot no longer raises alerts all the time --- contracts/src/attacks/DetectionBot.sol | 18 +++++++++++++++--- contracts/test/levels/DoubleEntryPoint.t.sol | 3 ++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/contracts/src/attacks/DetectionBot.sol b/contracts/src/attacks/DetectionBot.sol index 56ec907b5..26ad7381c 100644 --- a/contracts/src/attacks/DetectionBot.sol +++ b/contracts/src/attacks/DetectionBot.sol @@ -13,15 +13,27 @@ interface IForta { contract DetectionBot is IDetectionBot { IForta public fortaContract; + address public cryptoVaultContract; - constructor(address forta) { + constructor(address forta, address cryptoVault) { fortaContract = IForta(forta); + cryptoVaultContract = cryptoVault; } function handleTransaction(address user, bytes calldata msgData) public override { // Only the Forta contract can call this method require(msg.sender == address(fortaContract), "Unauthorized"); - fortaContract.raiseAlert(user); - msgData; + + // Decode the parameters of the delegateTransfer method + (, , address origSender) = abi.decode( + msgData[4:], + (address, uint256, address) + ); + + // The origSender mustn't be the CryptoVault + // because DoubleEntryPoint is an underlying token, + // if so raise an alert + if (origSender == cryptoVaultContract) + fortaContract.raiseAlert(user); } } diff --git a/contracts/test/levels/DoubleEntryPoint.t.sol b/contracts/test/levels/DoubleEntryPoint.t.sol index c5e9d99ec..dec51284b 100644 --- a/contracts/test/levels/DoubleEntryPoint.t.sol +++ b/contracts/test/levels/DoubleEntryPoint.t.sol @@ -56,7 +56,8 @@ contract TestDoubleEntryPoint is Test, Utils { vm.startPrank(player); Forta forta = instance.forta(); - DetectionBot bot = new DetectionBot(address(forta)); + address cryptoVault = instance.cryptoVault(); + DetectionBot bot = new DetectionBot(address(forta), cryptoVault); forta.setDetectionBot(address(bot)); From faf9a6527115eab81fd0d6fc46a79c2963136274 Mon Sep 17 00:00:00 2001 From: Nathan FLATTIN Date: Thu, 18 Jul 2024 15:30:50 +0200 Subject: [PATCH 2/2] update: DoubleEntryPoint validateInstance to prevent detection bots from raising alerts all the time --- contracts/src/levels/DoubleEntryPointFactory.sol | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/contracts/src/levels/DoubleEntryPointFactory.sol b/contracts/src/levels/DoubleEntryPointFactory.sol index 8136211ac..45aba0e35 100644 --- a/contracts/src/levels/DoubleEntryPointFactory.sol +++ b/contracts/src/levels/DoubleEntryPointFactory.sol @@ -47,6 +47,13 @@ contract DoubleEntryPointFactory is Level { } function __trySweep(CryptoVault cryptoVault, DoubleEntryPoint instance) external returns (bool, bytes memory) { + // emulate a lambda transfer of a user + try LegacyToken(instance.delegatedFrom()).transfer(address(cryptoVault), 0) { + } catch { + // It mustn't revert, if so return true on failure + return (true, abi.encode(false)); + } + try cryptoVault.sweepToken(IERC20(instance.delegatedFrom())) { return (true, abi.encode(false)); } catch {