DeFi security testing framework for Compound V2 protocol analysis, flash loan attacks, oracle manipulation, and historical vulnerability reproduction
A comprehensive security audit framework for Compound V2 protocol analysis, designed for educational and defensive security research. Test flash loan attacks, reentrancy vulnerabilities, oracle manipulation, and recreate historical DeFi bugs.
This skill provides a complete testing environment for identifying and analyzing DeFi vulnerabilities in the Compound V2 protocol. It includes attack vector simulations, historical bug recreations, and security audit infrastructure.
First, clone or navigate to the repository and run the setup script:
```bash
./scripts/setup-env.sh
```
If the setup script is not available, manually configure:
```bash
cp .env.example .env
source .env
```
```bash
forge install
forge build
```
```bash
cast block latest --rpc-url $ETH_RPC_URL
```
All audit tests inherit from multiple base contracts:
```solidity
contract MyAuditTest is Test, AuditBase, CompoundHelpers, OracleManipulator {
// Provides: Foundry Test + audit utilities + Compound helpers + oracle manipulation
}
```
`AuditBase.sol` provides mainnet addresses:
```
test/
├── exploits/
│ ├── FlashLoanAttacks.t.sol # Flash loan attack simulations
│ ├── ReentrancyTests.t.sol # Reentrancy vulnerability tests
│ └── CompoundHistoricalBugs.t.sol # Historical bug recreations
├── unit/ # Unit tests
└── integration/ # Integration tests
```
```bash
forge test --fork-url $ETH_RPC_URL
```
```bash
forge test --match-contract FlashLoanAttacksTest --fork-url $ETH_RPC_URL -vv
forge test --match-test test_FlashLoanPriceManipulation --fork-url $ETH_RPC_URL -vvv
```
```bash
forge test --match-contract ReentrancyTestsTest --fork-url $ETH_RPC_URL -vv
forge test --match-test test_SupplyReentrancy --fork-url $ETH_RPC_URL -vvv
```
```bash
forge test --match-contract CompoundHistoricalBugsTest --fork-url $ETH_RPC_URL -vv
```
```bash
forge test --match-path test/exploits/FlashLoanAttacks.t.sol --fork-url $ETH_RPC_URL -vv
forge test --match-path test/exploits/ReentrancyTests.t.sol --fork-url $ETH_RPC_URL -vv
```
```bash
forge test --fork-url $ETH_RPC_URL -vvvv
```
```bash
forge test --gas-report --fork-url $ETH_RPC_URL
```
```bash
forge coverage --fork-url $ETH_RPC_URL --report lcov
```
```bash
forge build --sizes
```
```bash
forge test --profile ci
forge test --profile intense
```
Educational focus: Understanding how flash loans can be weaponized in DeFi attacks.
Educational focus: Identifying reentrancy vulnerabilities in DeFi protocols.
Educational focus: Learning from real-world DeFi security incidents.
Educational focus: Understanding price feed vulnerabilities.
```solidity
contract MyAuditTest is Test, AuditBase, CompoundHelpers {
function setUp() public {
setUpAudit(); // Initialize fork and test accounts
// Custom setup...
}
function test_MyVulnerability() public withLogging("Test", "Action") {
// Test implementation with automatic logging
}
}
```
```solidity
// 1. Setup victim position
setupVictimPosition();
// 2. Record initial state
uint256 initialBalance = token.balanceOf(attacker);
// 3. Execute attack
vm.startPrank(ATTACKER);
// ... attack logic ...
vm.stopPrank();
// 4. Analyze results and log vulnerability if found
if (profit > 0) {
logVulnerability("Attack Name", target, severity, "Description");
}
```
```solidity
// Compound interactions
supplyToCompound(user, ICToken(CUSDC), amount);
borrowFromCompound(user, ICToken(CDAI), borrowAmount);
liquidatePosition(liquidator, borrower, cTokenBorrowed, repayAmount, cTokenCollateral);
// Price manipulation
simulateFlashLoanPriceManipulation(targetToken, flashAmount, isPump);
```
**"Contract does not exist" errors during setUp:**
**Working tests:**
```bash
forge test --match-contract ReentrancyTestsTest --fork-url $ETH_RPC_URL
```
**RPC Rate Limiting:**
```bash
forge test --slow --fork-url $ETH_RPC_URL
```
**Fork Block Issues:**
**Gas Estimation Failures:**
**Memory Issues:**
```bash
forge test --match-test test_FlashLoanAttack -vvvv --fork-url $ETH_RPC_URL
forge test --match-contract ReentrancyTestsTest --fork-url $ETH_RPC_URL -v
cast block latest --rpc-url $ETH_RPC_URL
```
This framework is for **defensive security research and education only**:
1. **Mainnet Protection**: Tests include chain ID validation
2. **Private Key Safety**: Git hooks prevent committing real keys
3. **RPC Rate Limiting**: Configured to avoid API abuse
4. **Gas Monitoring**: Alerts for unusual consumption patterns
This project serves as a learning resource for:
The goal is to build better, more secure DeFi protocols by understanding how attacks work and developing robust defenses.
```bash
./scripts/setup-env.sh
forge build
forge test --fork-url $ETH_RPC_URL
forge test --match-test test_SupplyReentrancy --fork-url $ETH_RPC_URL -vvv
forge test --gas-report --fork-url $ETH_RPC_URL
forge coverage --fork-url $ETH_RPC_URL --report lcov
```
---
**Disclaimer**: This audit framework is for educational and defensive purposes only. Always conduct security research ethically and responsibly. Never use these techniques for malicious activities.
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/compound-v2-security-audit/raw