audit-system 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +351 -0
  3. package/agents/AGENT_REGISTRY.md +150 -0
  4. package/agents/assumption-analyzer.json +7 -0
  5. package/agents/assumption-analyzer.md +37 -0
  6. package/agents/composition-attacker.json +7 -0
  7. package/agents/composition-attacker.md +46 -0
  8. package/agents/economic-attacker.json +7 -0
  9. package/agents/economic-attacker.md +43 -0
  10. package/agents/exploit-writer.json +7 -0
  11. package/agents/exploit-writer.md +48 -0
  12. package/agents/orchestrator.json +16 -0
  13. package/agents/orchestrator.md +46 -0
  14. package/agents/report-writer.json +7 -0
  15. package/agents/report-writer.md +52 -0
  16. package/agents/state-machine-hacker.json +7 -0
  17. package/agents/state-machine-hacker.md +43 -0
  18. package/agents/test-generator.json +7 -0
  19. package/agents/test-generator.md +49 -0
  20. package/cli.js +93 -0
  21. package/config.json +74 -0
  22. package/lib/detect-lang.js +109 -0
  23. package/lib/install.js +229 -0
  24. package/lib/utils.js +41 -0
  25. package/obsidian-vault/README.md +103 -0
  26. package/obsidian-vault/attack-patterns/state-inconsistency.md +90 -0
  27. package/obsidian-vault/exploits/_index.md +109 -0
  28. package/obsidian-vault/exploits/beanstalk-2022.md +334 -0
  29. package/obsidian-vault/exploits/nomad-2022.md +295 -0
  30. package/obsidian-vault/exploits/ronin-2022.md +251 -0
  31. package/obsidian-vault/exploits/wormhole-2022.md +284 -0
  32. package/obsidian-vault/failed-hypotheses/_template.md +77 -0
  33. package/obsidian-vault/hypotheses/_template.md +43 -0
  34. package/obsidian-vault/hypotheses/bridge-protocol-template.md +254 -0
  35. package/obsidian-vault/hypotheses/dex-protocol-template.md +185 -0
  36. package/obsidian-vault/hypotheses/governance-protocol-template.md +263 -0
  37. package/obsidian-vault/hypotheses/lending-protocol-template.md +218 -0
  38. package/obsidian-vault/hypotheses/staking-protocol-template.md +223 -0
  39. package/obsidian-vault/invariant-catalog/defi-invariants.md +307 -0
  40. package/obsidian-vault/invariant-catalog/solana-invariants.md +213 -0
  41. package/obsidian-vault/novel-patterns/pattern-mutation-framework.md +316 -0
  42. package/obsidian-vault/reports/_template.md +92 -0
  43. package/obsidian-vault/research/cross-protocol-analysis/.gitkeep +0 -0
  44. package/obsidian-vault/research/emerging-threats/.gitkeep +0 -0
  45. package/obsidian-vault/research/protocol-specific/.gitkeep +0 -0
  46. package/obsidian-vault/test-strategies/fuzzing.md +75 -0
  47. package/obsidian-vault/vulnerabilities/access-control.md +122 -0
  48. package/obsidian-vault/vulnerabilities/flash-loan-attack.md +66 -0
  49. package/obsidian-vault/vulnerabilities/oracle-manipulation.md +135 -0
  50. package/obsidian-vault/vulnerabilities/reentrancy.md +141 -0
  51. package/obsidian-vault/vulnerabilities/rust-unsafe-deserialization.md +128 -0
  52. package/obsidian-vault/vulnerabilities/solana-account-confusion.md +125 -0
  53. package/obsidian-vault/vulnerabilities/solana-close-account.md +141 -0
  54. package/obsidian-vault/vulnerabilities/solana-cpi-attacks.md +131 -0
  55. package/obsidian-vault/vulnerabilities/solana-signer-authorization.md +119 -0
  56. package/package.json +56 -0
  57. package/skills/audit-connect.md +385 -0
  58. package/skills/auditor.md +280 -0
  59. package/skills/exploit-generator.md +394 -0
  60. package/skills/novel-discovery.md +551 -0
  61. package/skills/test-generator.md +511 -0
@@ -0,0 +1,218 @@
1
+ # Hypothesis Template: Lending Protocol
2
+
3
+ ## Protocol Context
4
+ - **Type:** Lending / Borrowing / Money Market
5
+ - **Key Components:** Collateral management, interest accrual, liquidation, price feeds
6
+ - **Critical Invariants:** Solvency, collateralization ratios, interest rate accuracy
7
+
8
+ ---
9
+
10
+ ## Assumption Mapping
11
+
12
+ ### Developer Assumptions About Collateral
13
+ ```
14
+ ASSUMPTION: Collateral value is accurately represented by oracle
15
+ REALITY: Oracle can be manipulated or stale
16
+ HYPOTHESIS: Borrow can manipulate collateral value to avoid liquidation
17
+
18
+ ASSUMPTION: Collateral cannot be rehypothecated
19
+ REALITY: LP tokens or derivative collateral can be double-used
20
+ HYPOTHESIS: Same collateral backs multiple borrows across protocols
21
+ ```
22
+
23
+ ### Developer Assumptions About Liquidation
24
+ ```
25
+ ASSUMPTION: Liquidators are rational actors who maintain solvency
26
+ REALITY: Liquidators can be malicious or MEV bots
27
+ HYPOTHESIS: Liquidation can be griefed or manipulated
28
+
29
+ ASSUMPTION: Liquidation threshold is safe
30
+ REALITY: Rapid price movements can skip liquidation window
31
+ HYPOTHESIS: Price oracle lag allows undercollateralized positions
32
+ ```
33
+
34
+ ### Developer Assumptions About Interest
35
+ ```
36
+ ASSUMPTION: Interest accrues linearly over time
37
+ REALITY: Block.timestamp can be manipulated by miners
38
+ HYPOTHESIS: Timestamp manipulation affects interest calculation
39
+
40
+ ASSUMPTION: All borrowers can be liquidated when undercollateralized
41
+ REALITY: Liquidation can fail (no liquidity, gas, or griefing)
42
+ HYPOTHESIS: Protocol becomes insolvent with bad debt
43
+ ```
44
+
45
+ ---
46
+
47
+ ## Attack Vectors Specific to Lending
48
+
49
+ ### 1. Oracle Manipulation + Over-Borrowing
50
+ ```solidity
51
+ HYPOTHESIS ID: H-LEND-001
52
+ ASSUMPTION BROKEN: "Oracle price reflects true market value"
53
+ VIOLATION METHOD: Manipulate collateral price, borrow max, dump
54
+ PRECONDITIONS:
55
+ - Collateral token has thin liquidity
56
+ - Oracle reads from manipulable source (spot price)
57
+ - No price sanity bounds
58
+ ATTACK SEQUENCE:
59
+ 1. Flash loan large amount of collateral token
60
+ 2. Buy collateral on DEX, pushing price up 2-5x
61
+ 3. Deposit collateral at inflated price
62
+ 4. Borrow maximum stablecoins against inflated collateral
63
+ 5. Sell collateral, crashing price back down
64
+ 6. Position is undercollateralized but liquidation fails
65
+ SUCCESS CONDITION: Borrowed value > collateral value at true price
66
+ ESTIMATED IMPACT: Protocol insolvency, bad debt
67
+ NOVELTY: Specific to oracle source and collateral depth
68
+ ```
69
+
70
+ ### 2. Liquidation Griefing
71
+ ```solidity
72
+ HYPOTHESIS ID: H-LEND-002
73
+ ASSUMPTION BROKEN: "Undercollateralized positions will be liquidated"
74
+ REALITY: Liquidation can be made unprofitable or fail
75
+ PRECONDITIONS:
76
+ - Liquidation requires external call
77
+ - Liquidation bonus is small
78
+ - Gas costs are significant
79
+ ATTACK SEQUENCE:
80
+ 1. Identify undercollateralized position
81
+ 2. Front-run liquidation with transaction that makes it unprofitable
82
+ 3. Or: grief liquidation by manipulating state during call
83
+ 4. Position remains, debt grows, protocol becomes insolvent
84
+ SUCCESS CONDITION: Position remains undercollateralized for extended period
85
+ ESTIMATED IMPACT: Accumulated bad debt
86
+ NOVELTY: Economic griefing pattern
87
+ ```
88
+
89
+ ### 3. Interest Rate Manipulation
90
+ ```solidity
91
+ HYPOTHESIS ID: H-LEND-003
92
+ ASSUMPTION BROKEN: "Interest rate formula is manipulation-resistant"
93
+ REALITY: Utilization ratio can be temporarily manipulated
94
+ PRECONDITIONS:
95
+ - Interest rate based on current utilization
96
+ - No time-weighted averaging
97
+ ATTACK SEQUENCE:
98
+ 1. Borrow nearly 100% of pool (spike utilization)
99
+ 2. Interest rate spikes to maximum
100
+ 3. Exploit high rate for flash loan or other mechanism
101
+ 4. Repay immediately
102
+ SUCCESS CONDITION: Profit from rate spike > gas
103
+ ESTIMATED IMPACT: Interest rate volatility, user harm
104
+ NOVELTY: Temporal manipulation of rate
105
+ ```
106
+
107
+ ### 4. Double-Counting Collateral
108
+ ```solidity
109
+ HYPOTHESIS ID: H-LEND-004
110
+ ASSUMPTION BROKEN: "Collateral is uniquely owned"
111
+ REALITY: Derivative tokens can be deposited multiple times
112
+ PRECONDITIONS:
113
+ - Protocol accepts LP tokens or wrapped tokens
114
+ - No tracking of underlying collateral
115
+ ATTACK SEQUENCE:
116
+ 1. Deposit asset, receive derivative token (e.g., aToken)
117
+ 2. Use derivative as collateral in Protocol A
118
+ 3. Borrow from A, deposit back into original protocol
119
+ 4. Repeat across multiple protocols
120
+ 5. Same underlying asset backs multiple borrows
121
+ SUCCESS CONDITION: Total borrowed > underlying asset value
122
+ ESTIMATED IMPACT: Cascading liquidations, systemic risk
123
+ NOVELTY: Cross-protocol rehypothecation
124
+ ```
125
+
126
+ ### 5. Bad Debt Through Failed Liquidation
127
+ ```solidity
128
+ HYPOTHESIS ID: H-LEND-005
129
+ ASSUMPTION BROKEN: "Liquidation always succeeds or reverts"
130
+ REALITY: Partial liquidation can leave bad debt
131
+ PRECONDITIONS:
132
+ - Liquidation has caps or limits
133
+ - Collateral illiquid
134
+ ATTACK SEQUENCE:
135
+ 1. Open large position
136
+ 2. Collateral value drops rapidly
137
+ 3. Liquidation can only sell portion (cap or liquidity)
138
+ 4. Remaining debt exceeds remaining collateral
139
+ 5. Protocol absorbs loss
140
+ SUCCESS CONDITION: Bad debt remains on protocol balance sheet
141
+ ESTIMATED IMPACT: LP loss, insolvency risk
142
+ NOVELTY: Partial liquidation failure
143
+ ```
144
+
145
+ ---
146
+
147
+ ## Invariants to Test
148
+
149
+ ```solidity
150
+ // INVARIANT 1: Total borrows <= total collateral * maxLTV
151
+ assert(totalBorrows <= sum(collateralValues) * maxLTV);
152
+
153
+ // INVARIANT 2: Protocol solvency
154
+ assert(totalCollateralValue >= totalBorrows + accruedInterest);
155
+
156
+ // INVARIANT 3: Individual position collateralization
157
+ for each position: assert(collateralValue >= borrow * liquidationThreshold);
158
+
159
+ // INVARIANT 4: Interest accrual accuracy
160
+ assert(currentInterestIndex >= previousInterestIndex);
161
+
162
+ // INVARIANT 5: No negative balances
163
+ for each user: assert(userBalance >= 0);
164
+ ```
165
+
166
+ ---
167
+
168
+ ## Foundry Test Skeleton
169
+
170
+ ```solidity
171
+ contract LendingHypothesisTest is Test {
172
+ ILendingPool pool;
173
+ IOracle oracle;
174
+ IERC20 collateral;
175
+ IERC20 stablecoin;
176
+
177
+ function test_oracleManipulationOverBorrow() public {
178
+ // Setup: Flash loan, identify thin-liquidity collateral
179
+ // Attack: Manipulate oracle, borrow max, dump
180
+ // Assert: Position undercollateralized, bad debt created
181
+ }
182
+
183
+ function test_liquidationGriefing() public {
184
+ // Setup: Create undercollateralized position
185
+ // Attack: Grief all liquidation attempts
186
+ // Assert: Position remains, debt grows
187
+ }
188
+
189
+ function test_interestRateSpike() public {
190
+ // Setup: Identify pool with low liquidity
191
+ // Attack: Borrow all, spike utilization
192
+ // Assert: Rate manipulation successful
193
+ }
194
+
195
+ function test_doubleCountingCollateral() public {
196
+ // Setup: Get derivative token (aToken, cToken, LP)
197
+ // Attack: Deposit same underlying across multiple protocols
198
+ // Assert: Total borrowed > underlying value
199
+ }
200
+ }
201
+ ```
202
+
203
+ ---
204
+
205
+ ## Related Vulnerabilities
206
+ - [[../vulnerabilities/oracle-manipulation]]
207
+ - [[../vulnerabilities/flash-loan-attack]]
208
+ - [[../vulnerabilities/access-control]]
209
+
210
+ ---
211
+
212
+ ## Validation Checklist
213
+ - [ ] Hypothesis is testable with Foundry
214
+ - [ ] Preconditions are achievable with flash loans
215
+ - [ ] Attack sequence is specific to THIS lending protocol
216
+ - [ ] Would NOT be found by standard audits
217
+ - [ ] Economic incentive exists (profit > cost)
218
+ - [ ] Considers cross-protocol interactions
@@ -0,0 +1,223 @@
1
+ # Hypothesis Template: Staking Protocol
2
+
3
+ ## Protocol Context
4
+ - **Type:** Staking / Yield Farming / Rewards Distribution
5
+ - **Key Components:** Stake/unstake logic, reward accrual, lockup periods, slashing
6
+ - **Critical Invariants:** Reward rate accuracy, total staked = sum of shares, slashing correctness
7
+
8
+ ---
9
+
10
+ ## Assumption Mapping
11
+
12
+ ### Developer Assumptions About Rewards
13
+ ```
14
+ ASSUMPTION: Rewards are distributed proportionally to stake
15
+ REALITY: Reward calculation can have edge cases at boundaries
16
+ HYPOTHESIS: First/last staker can capture disproportionate rewards
17
+
18
+ ASSUMPTION: Reward rate is constant or predictably variable
19
+ REALITY: Reward rate can be manipulated by large stakers
20
+ HYPOTHESIS: Stake/unstake timing affects reward rate
21
+ ```
22
+
23
+ ### Developer Assumptions About Lockups
24
+ ```
25
+ ASSUMPTION: Locked tokens cannot be withdrawn early
26
+ REALITY: Governance or emergency functions may bypass lockup
27
+ HYPOTHESIS: Emergency withdrawal can be abused
28
+
29
+ ASSUMPTION: Lockup duration is enforced correctly
30
+ REALITY: Timestamp manipulation or edge cases
31
+ HYPOTHESIS: Block.timestamp manipulation allows early withdrawal
32
+ ```
33
+
34
+ ### Developer Assumptions About Slashing
35
+ ```
36
+ ASSUMPTION: Slashing conditions are unambiguous
37
+ REALITY: Slashing logic may have edge cases
38
+ HYPOTHESIS: Validator can avoid slashing through technicality
39
+
40
+ ASSUMPTION: Slashed amount is correctly distributed
41
+ REALITY: Distribution logic may have bugs
42
+ HYPOTHESIS: Slashed rewards go to wrong recipient
43
+ ```
44
+
45
+ ---
46
+
47
+ ## Attack Vectors Specific to Staking
48
+
49
+ ### 1. Reward Calculation Exploit (First/Last Staker)
50
+ ```solidity
51
+ HYPOTHESIS ID: H-STAKE-001
52
+ ASSUMPTION BROKEN: "Rewards are fairly distributed regardless of timing"
53
+ VIOLATION METHOD: Exploit reward-per-share calculation at protocol boundaries
54
+ PRECONDITIONS:
55
+ - Rewards distributed based on rewardPerShare
56
+ - rewardPerShare updated on stake/unstake
57
+ - Small total stake initially
58
+ ATTACK SEQUENCE:
59
+ 1. Be first to stake with large amount
60
+ 2. rewardPerShare = totalRewards / totalStaked (small denominator)
61
+ 3. Later users stake, rewardPerShare diluted
62
+ 4. Attacker already captured disproportionate share
63
+ OR:
64
+ 1. Stake when rewardPerShare is high
65
+ 2. Unstake immediately before others stake
66
+ 3. Capture rewards without proportional risk
67
+ SUCCESS CONDITION: Rewards captured > proportional to stake time/amount
68
+ ESTIMATED IMPACT: Later stakers receive fewer rewards
69
+ NOVELTY: Timing-based exploitation of reward formula
70
+ ```
71
+
72
+ ### 2. Stake/Unstake Flash Manipulation
73
+ ```solidity
74
+ HYPOTHESIS ID: H-STAKE-002
75
+ ASSUMPTION BROKEN: "Stakers have skin in the game"
76
+ REALITY: Flash loans allow temporary stake for voting/rewards
77
+ PRECONDITIONS:
78
+ - No minimum stake duration
79
+ - Rewards/voting rights granted immediately
80
+ ATTACK SEQUENCE:
81
+ 1. Flash loan large amount
82
+ 2. Stake and immediately claim voting rights or rewards
83
+ 3. Use voting power to pass malicious proposal
84
+ 4. Or: claim rewards meant for long-term stakers
85
+ 5. Unstake and repay flash loan
86
+ SUCCESS CONDITION: Achieve governance control or claim rewards without capital
87
+ ESTIMATED IMPACT: Governance capture or unfair reward distribution
88
+ NOVELTY: Flash loan usage for temporary stake
89
+ ```
90
+
91
+ ### 3. Reward Rate Manipulation
92
+ ```solidity
93
+ HYPOTHESIS ID: H-STAKE-003
94
+ ASSUMPTION BROKEN: "Reward rate is independent of staker behavior"
95
+ REALITY: Large stake/unstake can change reward rate
96
+ PRECONDITIONS:
97
+ - Reward rate based on utilization or TVL
98
+ - No rate limits or dampening
99
+ ATTACK SEQUENCE:
100
+ 1. Stake large amount, triggering reward rate increase
101
+ 2. Other stakers attracted by high rate join
102
+ 3. Unstake suddenly, rate crashes
103
+ 4. Remaining stakers trapped with low/no rewards
104
+ OR:
105
+ 1. Manipulate rate to maximum
106
+ 2. Claim rewards at inflated rate
107
+ 3. Exit before rate normalizes
108
+ SUCCESS CONDITION: Profit from rate manipulation
109
+ ESTIMATED IMPACT: Reward system instability
110
+ NOVELTY: Rate manipulation attack
111
+ ```
112
+
113
+ ### 4. Lockup Bypass Through Reentrancy
114
+ ```solidity
115
+ HYPOTHESIS ID: H-STAKE-004
116
+ ASSUMPTION BROKEN: "Lockup period is strictly enforced"
117
+ REALITY: Reentrancy can bypass time checks
118
+ PRECONDITIONS:
119
+ - unstake() makes external call before time check completes
120
+ - Callback allows reentrant unstake
121
+ ATTACK SEQUENCE:
122
+ 1. Stake with lockup period
123
+ 2. Call unstake() immediately
124
+ 3. In external call (token callback), call unstake() again
125
+ 4. State not yet updated, lockup check passes twice
126
+ 5. Withdraw before lockup expires
127
+ SUCCESS CONDITION: Tokens withdrawn before lockup end
128
+ ESTIMATED IMPACT: Lockup mechanism bypassed
129
+ NOVELTY: Reentrancy bypass of time-based restriction
130
+ ```
131
+
132
+ ### 5. Slashing Condition Exploitation
133
+ ```solidity
134
+ HYPOTHESIS ID: H-STAKE-005
135
+ ASSUMPTION BROKEN: "Slashing conditions cover all attack vectors"
136
+ REALITY: Slashing logic may have gaps or edge cases
137
+ PRECONDITIONS:
138
+ - Slashing based on specific conditions (downtime, double-sign)
139
+ - No comprehensive coverage
140
+ ATTACK SEQUENCE:
141
+ 1. Identify gap in slashing conditions
142
+ 2. Act maliciously without triggering slash (e.g., selective downtime)
143
+ 3. Extract value while avoiding penalty
144
+ 4. Or: Frame another validator for slashing
145
+ SUCCESS CONDITION: Malicious behavior without penalty
146
+ ESTIMATED IMPACT: Protocol security degraded
147
+ NOVELTY: Logic gap in slashing mechanism
148
+ ```
149
+
150
+ ---
151
+
152
+ ## Invariants to Test
153
+
154
+ ```solidity
155
+ // INVARIANT 1: Total staked = sum of all user shares
156
+ assert(totalStaked == sum(userShares));
157
+
158
+ // INVARIANT 2: Rewards distributed = rewards accrued
159
+ assert(totalRewardsDistributed == sum(userRewards) + unclaimedRewards);
160
+
161
+ // INVARIANT 3: Lockup is enforced
162
+ for each user with locked stake:
163
+ assert(block.timestamp >= user.unlockTime || user.stakeAmount == 0);
164
+
165
+ // INVARIANT 4: Reward rate bounds
166
+ assert(rewardRate >= minRate && rewardRate <= maxRate);
167
+
168
+ // INVARIANT 5: No user has negative balance
169
+ for each user: assert(userShare >= 0 && userRewards >= 0);
170
+ ```
171
+
172
+ ---
173
+
174
+ ## Foundry Test Skeleton
175
+
176
+ ```solidity
177
+ contract StakingHypothesisTest is Test {
178
+ IStakingContract staking;
179
+ IERC20 stakeToken;
180
+ IERC20 rewardToken;
181
+
182
+ function test_firstStakerRewardExploit() public {
183
+ // Setup: Fresh protocol, no stakers
184
+ // Attack: Be first to stake large amount
185
+ // Assert: Disproportionate rewards captured
186
+ }
187
+
188
+ function test_flashLoanStakeManipulation() public {
189
+ // Setup: Governance vote or reward distribution pending
190
+ // Attack: Flash loan, stake, vote/claim, unstake, repay
191
+ // Assert: Governance captured or rewards claimed without capital
192
+ }
193
+
194
+ function test_lockupBypassReentrancy() public {
195
+ // Setup: Stake with lockup period
196
+ // Attack: Reentrant unstake calls
197
+ // Assert: Tokens withdrawn before lockup expires
198
+ }
199
+
200
+ function test_rewardRateManipulation() public {
201
+ // Setup: Identify rate formula
202
+ // Attack: Large stake/unstake to manipulate rate
203
+ // Assert: Rate spiked, profit extracted
204
+ }
205
+ }
206
+ ```
207
+
208
+ ---
209
+
210
+ ## Related Vulnerabilities
211
+ - [[../vulnerabilities/reentrancy]]
212
+ - [[../vulnerabilities/access-control]]
213
+ - [[../attack-patterns/state-inconsistency]]
214
+
215
+ ---
216
+
217
+ ## Validation Checklist
218
+ - [ ] Hypothesis is testable with Foundry
219
+ - [ ] Exploits staking-specific mechanics
220
+ - [ ] Not a generic reentrancy/access control issue
221
+ - [ ] Economic incentive exists
222
+ - [ ] Considers timing and sequencing attacks
223
+ - [ ] Accounts for flash loan capabilities