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.
- package/LICENSE +21 -0
- package/README.md +351 -0
- package/agents/AGENT_REGISTRY.md +150 -0
- package/agents/assumption-analyzer.json +7 -0
- package/agents/assumption-analyzer.md +37 -0
- package/agents/composition-attacker.json +7 -0
- package/agents/composition-attacker.md +46 -0
- package/agents/economic-attacker.json +7 -0
- package/agents/economic-attacker.md +43 -0
- package/agents/exploit-writer.json +7 -0
- package/agents/exploit-writer.md +48 -0
- package/agents/orchestrator.json +16 -0
- package/agents/orchestrator.md +46 -0
- package/agents/report-writer.json +7 -0
- package/agents/report-writer.md +52 -0
- package/agents/state-machine-hacker.json +7 -0
- package/agents/state-machine-hacker.md +43 -0
- package/agents/test-generator.json +7 -0
- package/agents/test-generator.md +49 -0
- package/cli.js +93 -0
- package/config.json +74 -0
- package/lib/detect-lang.js +109 -0
- package/lib/install.js +229 -0
- package/lib/utils.js +41 -0
- package/obsidian-vault/README.md +103 -0
- package/obsidian-vault/attack-patterns/state-inconsistency.md +90 -0
- package/obsidian-vault/exploits/_index.md +109 -0
- package/obsidian-vault/exploits/beanstalk-2022.md +334 -0
- package/obsidian-vault/exploits/nomad-2022.md +295 -0
- package/obsidian-vault/exploits/ronin-2022.md +251 -0
- package/obsidian-vault/exploits/wormhole-2022.md +284 -0
- package/obsidian-vault/failed-hypotheses/_template.md +77 -0
- package/obsidian-vault/hypotheses/_template.md +43 -0
- package/obsidian-vault/hypotheses/bridge-protocol-template.md +254 -0
- package/obsidian-vault/hypotheses/dex-protocol-template.md +185 -0
- package/obsidian-vault/hypotheses/governance-protocol-template.md +263 -0
- package/obsidian-vault/hypotheses/lending-protocol-template.md +218 -0
- package/obsidian-vault/hypotheses/staking-protocol-template.md +223 -0
- package/obsidian-vault/invariant-catalog/defi-invariants.md +307 -0
- package/obsidian-vault/invariant-catalog/solana-invariants.md +213 -0
- package/obsidian-vault/novel-patterns/pattern-mutation-framework.md +316 -0
- package/obsidian-vault/reports/_template.md +92 -0
- package/obsidian-vault/research/cross-protocol-analysis/.gitkeep +0 -0
- package/obsidian-vault/research/emerging-threats/.gitkeep +0 -0
- package/obsidian-vault/research/protocol-specific/.gitkeep +0 -0
- package/obsidian-vault/test-strategies/fuzzing.md +75 -0
- package/obsidian-vault/vulnerabilities/access-control.md +122 -0
- package/obsidian-vault/vulnerabilities/flash-loan-attack.md +66 -0
- package/obsidian-vault/vulnerabilities/oracle-manipulation.md +135 -0
- package/obsidian-vault/vulnerabilities/reentrancy.md +141 -0
- package/obsidian-vault/vulnerabilities/rust-unsafe-deserialization.md +128 -0
- package/obsidian-vault/vulnerabilities/solana-account-confusion.md +125 -0
- package/obsidian-vault/vulnerabilities/solana-close-account.md +141 -0
- package/obsidian-vault/vulnerabilities/solana-cpi-attacks.md +131 -0
- package/obsidian-vault/vulnerabilities/solana-signer-authorization.md +119 -0
- package/package.json +56 -0
- package/skills/audit-connect.md +385 -0
- package/skills/auditor.md +280 -0
- package/skills/exploit-generator.md +394 -0
- package/skills/novel-discovery.md +551 -0
- package/skills/test-generator.md +511 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
# Pattern Mutation Framework
|
|
2
|
+
|
|
3
|
+
tags: #novel-patterns #mutation #creativity #framework
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
Novel vulnerabilities often emerge from combining or mutating known patterns. This framework provides systematic approaches to discover new attack vectors through pattern transformation.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Mutation Strategy 1: Feature Composition
|
|
13
|
+
|
|
14
|
+
### Concept
|
|
15
|
+
Combine two existing features to create an emergent vulnerability.
|
|
16
|
+
|
|
17
|
+
### Method
|
|
18
|
+
```
|
|
19
|
+
1. List all features: F1, F2, F3, ..., Fn
|
|
20
|
+
2. For each pair (Fi, Fj):
|
|
21
|
+
- Can Fi's output become Fj's input unexpectedly?
|
|
22
|
+
- Does Fi change state that Fj depends on?
|
|
23
|
+
- Can Fi front-run or back-run Fj?
|
|
24
|
+
3. Test promising combinations
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Example: Flash Loan + Governance
|
|
28
|
+
```
|
|
29
|
+
Flash Loan (Feature A):
|
|
30
|
+
→ Provides large capital temporarily
|
|
31
|
+
→ Must be repaid in same transaction
|
|
32
|
+
|
|
33
|
+
Governance (Feature B):
|
|
34
|
+
→ Requires token threshold to propose
|
|
35
|
+
→ Voting power proportional to holdings
|
|
36
|
+
|
|
37
|
+
Composition Attack:
|
|
38
|
+
1. Flash loan governance token
|
|
39
|
+
2. Submit malicious proposal
|
|
40
|
+
3. Vote with flash-loaned power
|
|
41
|
+
4. Repay flash loan
|
|
42
|
+
5. Proposal executes after timelock
|
|
43
|
+
|
|
44
|
+
Result: Governance manipulation without holding tokens
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Composition Matrix
|
|
48
|
+
| Feature A | Feature B | Potential Attack |
|
|
49
|
+
|-----------|-----------|------------------|
|
|
50
|
+
| Flash Loan | Price Oracle | Price manipulation |
|
|
51
|
+
| Flash Loan | Reward Distribution | Reward harvesting |
|
|
52
|
+
| Reentrancy | Upgradeable | Self-destruct proxy |
|
|
53
|
+
| Staking | Transfer | Double voting power |
|
|
54
|
+
| Time Lock | Emergency Pause | Race condition |
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Mutation Strategy 2: Context Shifting
|
|
59
|
+
|
|
60
|
+
### Concept
|
|
61
|
+
Take a pattern that exists in one context and apply it to a different context.
|
|
62
|
+
|
|
63
|
+
### Method
|
|
64
|
+
```
|
|
65
|
+
1. Identify known vulnerability pattern P
|
|
66
|
+
2. Identify contexts where P is NOT expected: C1, C2, C3
|
|
67
|
+
3. Ask: "Could P work in context Cn?"
|
|
68
|
+
4. Test the shifted pattern
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Context Shift Examples
|
|
72
|
+
|
|
73
|
+
#### Reentrancy in View Functions (Read-Only Reentrancy)
|
|
74
|
+
```
|
|
75
|
+
Original Context: State-changing functions
|
|
76
|
+
Shifted Context: View functions
|
|
77
|
+
Novel Attack: Manipulate state → call view → state still stale → profit
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### Integer Overflow in Governance
|
|
81
|
+
```
|
|
82
|
+
Original Context: ERC20 balances
|
|
83
|
+
Shifted Context: Vote counting
|
|
84
|
+
Novel Attack: Overflow vote counts to manipulate proposals
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Access Control in Callbacks
|
|
88
|
+
```
|
|
89
|
+
Original Context: Admin functions
|
|
90
|
+
Shifted Context: Token callbacks (ERC777, ERC721)
|
|
91
|
+
Novel Attack: Unauthorized actions via callback hooks
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Mutation Strategy 3: Parameter Manipulation
|
|
97
|
+
|
|
98
|
+
### Concept
|
|
99
|
+
Vary the parameters of a known attack to find new vectors.
|
|
100
|
+
|
|
101
|
+
### Method
|
|
102
|
+
```
|
|
103
|
+
For each attack parameter:
|
|
104
|
+
- What if it's zero?
|
|
105
|
+
- What if it's maximum (type(uint256).max)?
|
|
106
|
+
- What if it's boundary value (1, -1)?
|
|
107
|
+
- What if it's manipulated during the attack?
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Parameter Exploration
|
|
111
|
+
|
|
112
|
+
#### Attack Timing
|
|
113
|
+
```
|
|
114
|
+
Instead of: Attack at beginning
|
|
115
|
+
Try: Attack in middle, Attack at end, Attack split across blocks
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### Attack Scale
|
|
119
|
+
```
|
|
120
|
+
Instead of: Attack with large amount
|
|
121
|
+
Try: Attack with dust amount, Attack with exact threshold
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### Attack Sequence
|
|
125
|
+
```
|
|
126
|
+
Instead of: A → B → C
|
|
127
|
+
Try: A → C → B, B → A → C, Parallel execution
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Mutation Strategy 4: Trust Boundary Violation
|
|
133
|
+
|
|
134
|
+
### Concept
|
|
135
|
+
Exploit assumptions about which contracts/users are trusted.
|
|
136
|
+
|
|
137
|
+
### Method
|
|
138
|
+
```
|
|
139
|
+
1. Identify all trust assumptions
|
|
140
|
+
2. Ask "what if this trust is misplaced?"
|
|
141
|
+
3. Test untrusted input at trust boundaries
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Trust Boundary Patterns
|
|
145
|
+
|
|
146
|
+
#### Malicious Token
|
|
147
|
+
```
|
|
148
|
+
Assumption: ERC20 tokens behave normally
|
|
149
|
+
Violation: Token with hooks, fees, or callbacks
|
|
150
|
+
Result: Reentrancy, accounting errors
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### Malicious Oracle
|
|
154
|
+
```
|
|
155
|
+
Assumption: Oracle price is accurate
|
|
156
|
+
Violation: Manipulable oracle, stale data
|
|
157
|
+
Result: Liquidation manipulation, undercollateralized loans
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### Malicious User Contract
|
|
161
|
+
```
|
|
162
|
+
Assumption: Users are EOAs or benign contracts
|
|
163
|
+
Violation: Attacker controls contract with hooks
|
|
164
|
+
Result: Reentrancy, gas griefing
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Mutation Strategy 5: State Space Exploration
|
|
170
|
+
|
|
171
|
+
### Concept
|
|
172
|
+
Explore combinations of state variables to find invalid states.
|
|
173
|
+
|
|
174
|
+
### Method
|
|
175
|
+
```
|
|
176
|
+
1. Define all state variables: S1, S2, S3, ..., Sn
|
|
177
|
+
2. Define valid ranges for each
|
|
178
|
+
3. Explore edge cases:
|
|
179
|
+
- All minimum
|
|
180
|
+
- All maximum
|
|
181
|
+
- Mixed extremes
|
|
182
|
+
- Transition states
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### State Mutation Examples
|
|
186
|
+
|
|
187
|
+
#### Empty States
|
|
188
|
+
```
|
|
189
|
+
Scenario: First user interaction
|
|
190
|
+
Test: Does protocol handle empty/zero states correctly?
|
|
191
|
+
Attack: First depositor exploit, division by zero
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### Maximum States
|
|
195
|
+
```
|
|
196
|
+
Scenario: Maximum values reached
|
|
197
|
+
Test: Does protocol handle overflow, gas limits?
|
|
198
|
+
Attack: Block protocol with max deposits
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
#### Race Conditions
|
|
202
|
+
```
|
|
203
|
+
Scenario: Multiple users simultaneously
|
|
204
|
+
Test: Is state update atomic?
|
|
205
|
+
Attack: Double withdrawal, reward stealing
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## Mutation Strategy 6: Economic Game Theory
|
|
211
|
+
|
|
212
|
+
### Concept
|
|
213
|
+
Model the system as a game and find Nash equilibria that harm the protocol.
|
|
214
|
+
|
|
215
|
+
### Method
|
|
216
|
+
```
|
|
217
|
+
1. Identify players and their strategies
|
|
218
|
+
2. Calculate payoffs for each strategy combination
|
|
219
|
+
3. Find dominant strategies for attackers
|
|
220
|
+
4. Check if honest participation is rational
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Economic Attacks
|
|
224
|
+
|
|
225
|
+
#### MEV Extraction
|
|
226
|
+
```
|
|
227
|
+
Attack: Sandwich user's trade
|
|
228
|
+
Mutation: Sandwich liquidation, governance votes, claims
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
#### Governance Attacks
|
|
232
|
+
```
|
|
233
|
+
Attack: Buy tokens, vote, sell
|
|
234
|
+
Mutation: Flash loan governance, vote delegation manipulation
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
#### Liquidity Attacks
|
|
238
|
+
```
|
|
239
|
+
Attack: Remove liquidity at critical moment
|
|
240
|
+
Mutation: Just-in-time liquidity, liquidity sniping
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
## Practical Application
|
|
246
|
+
|
|
247
|
+
### Step-by-Step Mutation Process
|
|
248
|
+
|
|
249
|
+
```
|
|
250
|
+
Step 1: Catalog Known Patterns
|
|
251
|
+
- List all known vulnerabilities for protocol type
|
|
252
|
+
- Document standard mitigations
|
|
253
|
+
|
|
254
|
+
Step 2: Generate Mutations
|
|
255
|
+
- Apply each mutation strategy
|
|
256
|
+
- Generate 3-5 variations per strategy
|
|
257
|
+
|
|
258
|
+
Step 3: Feasibility Check
|
|
259
|
+
- Which mutations are technically possible?
|
|
260
|
+
- Which have economic incentive?
|
|
261
|
+
- Which bypass existing mitigations?
|
|
262
|
+
|
|
263
|
+
Step 4: Prioritize Testing
|
|
264
|
+
- Rank by likelihood of success
|
|
265
|
+
- Rank by potential impact
|
|
266
|
+
- Test top 3-5 mutations
|
|
267
|
+
|
|
268
|
+
Step 5: Document Results
|
|
269
|
+
- Successful mutations → poc/
|
|
270
|
+
- Failed mutations → failed-hypotheses/
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Mutation Worksheet
|
|
274
|
+
|
|
275
|
+
```markdown
|
|
276
|
+
## Pattern: [Base Pattern]
|
|
277
|
+
|
|
278
|
+
### Context Shift
|
|
279
|
+
- Original: [Context A]
|
|
280
|
+
- Shifted: [Context B]
|
|
281
|
+
- Feasibility: [High/Medium/Low]
|
|
282
|
+
- Notes: [Why or why not]
|
|
283
|
+
|
|
284
|
+
### Feature Composition
|
|
285
|
+
- Feature A: [Feature]
|
|
286
|
+
- Feature B: [Feature]
|
|
287
|
+
- Combined Effect: [What happens]
|
|
288
|
+
- Feasibility: [High/Medium/Low]
|
|
289
|
+
|
|
290
|
+
### Parameter Manipulation
|
|
291
|
+
- Parameter: [Which parameter]
|
|
292
|
+
- Variation: [How varied]
|
|
293
|
+
- Result: [Effect]
|
|
294
|
+
|
|
295
|
+
### Trust Boundary
|
|
296
|
+
- Assumption: [What is assumed]
|
|
297
|
+
- Violation: [How to violate]
|
|
298
|
+
- Exploit: [Attack vector]
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
---
|
|
302
|
+
|
|
303
|
+
## Integration with Novel Discovery
|
|
304
|
+
|
|
305
|
+
Use this framework within the 6-phase discovery process:
|
|
306
|
+
- **Phase 3 (Economic):** Economic game theory mutations
|
|
307
|
+
- **Phase 4 (State Machine):** State space exploration
|
|
308
|
+
- **Phase 5 (Composition):** Feature composition
|
|
309
|
+
- **Phase 2 (Break Assumptions):** Trust boundary violation
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
## Links
|
|
314
|
+
- [[attack-patterns/state-inconsistency]] — Base pattern for mutations
|
|
315
|
+
- [[invariant-catalog/defi-invariants]] — Test invariants against mutations
|
|
316
|
+
- [[../skills/novel-discovery]] — Full discovery framework
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Audit Report: [Protocol Name]
|
|
2
|
+
|
|
3
|
+
tags: #report #audit
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Executive Summary
|
|
8
|
+
**Protocol:** [Name]
|
|
9
|
+
**Date:** [Date]
|
|
10
|
+
**Auditor:** [Name]
|
|
11
|
+
**Scope:** [Files audited]
|
|
12
|
+
**Total Findings:** CRITICAL: X | HIGH: X | MEDIUM: X | LOW: X | INFO: X
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Findings Summary
|
|
17
|
+
|
|
18
|
+
| ID | Severity | Title | Status |
|
|
19
|
+
|---|---|---|---|
|
|
20
|
+
| C-01 | CRITICAL | [Title] | Open |
|
|
21
|
+
| H-01 | HIGH | [Title] | Open |
|
|
22
|
+
| M-01 | MEDIUM | [Title] | Open |
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Detailed Findings
|
|
27
|
+
|
|
28
|
+
### [C-01] CRITICAL — [Title]
|
|
29
|
+
|
|
30
|
+
**Location:** `Contract.sol` :: `functionName()` :: line N
|
|
31
|
+
|
|
32
|
+
**Description:**
|
|
33
|
+
[Clear explanation of the vulnerability]
|
|
34
|
+
|
|
35
|
+
**Root Cause:**
|
|
36
|
+
[Technical reason why this exists]
|
|
37
|
+
|
|
38
|
+
**Impact:**
|
|
39
|
+
[What an attacker can achieve and economic damage]
|
|
40
|
+
|
|
41
|
+
**Attack Vector:**
|
|
42
|
+
```
|
|
43
|
+
1. Attacker calls X with Y
|
|
44
|
+
2. Contract state becomes inconsistent
|
|
45
|
+
3. Attacker calls Z to extract funds
|
|
46
|
+
4. Loss: [amount]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**PoC (Foundry):**
|
|
50
|
+
```solidity
|
|
51
|
+
function test_C01_exploit() public {
|
|
52
|
+
// setup
|
|
53
|
+
|
|
54
|
+
// attack
|
|
55
|
+
|
|
56
|
+
// assert
|
|
57
|
+
assertGt(attacker.balance, 0);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Recommendation:**
|
|
62
|
+
```solidity
|
|
63
|
+
// Fixed version
|
|
64
|
+
function fixedFunction() external nonReentrant {
|
|
65
|
+
// CEI pattern
|
|
66
|
+
uint256 amount = balances[msg.sender];
|
|
67
|
+
balances[msg.sender] = 0; // Effect first
|
|
68
|
+
payable(msg.sender).call{value: amount}(""); // Then interact
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Appendix
|
|
75
|
+
|
|
76
|
+
### Methodology
|
|
77
|
+
1. Manual code review
|
|
78
|
+
2. Static analysis (Slither)
|
|
79
|
+
3. Fuzz testing (Foundry)
|
|
80
|
+
4. Invariant testing
|
|
81
|
+
5. PoC development
|
|
82
|
+
|
|
83
|
+
### Tools Used
|
|
84
|
+
- Foundry
|
|
85
|
+
- Slither
|
|
86
|
+
- Echidna
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Links
|
|
91
|
+
- [[vulnerabilities/]]
|
|
92
|
+
- [[poc/]]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Fuzzing Strategy
|
|
2
|
+
|
|
3
|
+
tags: #strategy #testing #fuzzing
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
Fuzzing automatically tests functions with random inputs to find edge cases and unexpected behavior. In Foundry, it runs hundreds/thousands of times per test.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## When to Fuzz
|
|
13
|
+
|
|
14
|
+
Use fuzz tests when:
|
|
15
|
+
- Function accepts numerical inputs
|
|
16
|
+
- Function has complex branching logic
|
|
17
|
+
- You want to find edge cases you can't imagine
|
|
18
|
+
- Testing invariants across many states
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Foundry Fuzz Config (foundry.toml)
|
|
23
|
+
|
|
24
|
+
```toml
|
|
25
|
+
[fuzz]
|
|
26
|
+
runs = 10000 # More runs = better coverage
|
|
27
|
+
max_test_rejects = 65536
|
|
28
|
+
seed = 0x1234
|
|
29
|
+
|
|
30
|
+
[invariant]
|
|
31
|
+
runs = 500
|
|
32
|
+
depth = 50 # Calls per run
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Fuzz Target Priority
|
|
38
|
+
|
|
39
|
+
### Highest Value Targets
|
|
40
|
+
1. `withdraw()` / `claim()` — any function moving funds
|
|
41
|
+
2. `swap()` — exchange functions
|
|
42
|
+
3. `liquidate()` — liquidation logic
|
|
43
|
+
4. `borrow()` — lending logic
|
|
44
|
+
5. `mint()` / `burn()` — supply modification
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Bounding Inputs
|
|
49
|
+
|
|
50
|
+
```solidity
|
|
51
|
+
// Always bound to prevent wasted runs
|
|
52
|
+
amount = bound(amount, 1, type(uint128).max);
|
|
53
|
+
user = address(uint160(bound(uint256(user), 1, type(uint160).max)));
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Echidna Integration
|
|
59
|
+
|
|
60
|
+
```yaml
|
|
61
|
+
# echidna.yaml
|
|
62
|
+
testLimit: 50000
|
|
63
|
+
seqLen: 100
|
|
64
|
+
shrinkLimit: 5000
|
|
65
|
+
filterFunctions: ["setUp"]
|
|
66
|
+
|
|
67
|
+
# Run:
|
|
68
|
+
# echidna . --contract FuzzTarget --config echidna.yaml
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Links
|
|
74
|
+
- [[skills/test-generator]]
|
|
75
|
+
- [[vulnerabilities/reentrancy]]
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Access Control
|
|
2
|
+
|
|
3
|
+
tags: #vulnerability #high #critical #access-control
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Summary
|
|
8
|
+
Access control vulnerabilities occur when sensitive functions lack proper authorization checks, allowing unauthorized actors to execute privileged operations.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Pattern Recognition
|
|
13
|
+
|
|
14
|
+
### Code Signals
|
|
15
|
+
- `public` functions that should be `internal` or `onlyOwner`
|
|
16
|
+
- Missing modifiers on admin functions
|
|
17
|
+
- Modifier logic that can be bypassed
|
|
18
|
+
- `tx.origin` used instead of `msg.sender`
|
|
19
|
+
- Role checks missing or incorrectly implemented
|
|
20
|
+
|
|
21
|
+
### Detection Query for Claude
|
|
22
|
+
```
|
|
23
|
+
Which functions modify critical state or move funds?
|
|
24
|
+
Do all of them have proper access modifiers?
|
|
25
|
+
Can the modifier be bypassed (e.g., through delegatecall)?
|
|
26
|
+
Is tx.origin used anywhere?
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Variants
|
|
32
|
+
|
|
33
|
+
### Missing Modifier
|
|
34
|
+
Function performs sensitive action with no access check at all.
|
|
35
|
+
|
|
36
|
+
### Broken Modifier Logic
|
|
37
|
+
```solidity
|
|
38
|
+
modifier onlyOwner() {
|
|
39
|
+
require(msg.sender == owner || owner == address(0)); // WRONG: anyone when owner not set
|
|
40
|
+
_;
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Uninitialized Owner
|
|
45
|
+
Owner is never set in constructor, defaults to `address(0)`, anyone can become owner.
|
|
46
|
+
|
|
47
|
+
### tx.origin Bypass
|
|
48
|
+
```solidity
|
|
49
|
+
require(tx.origin == owner); // Bypassed via phishing
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Delegatecall Context Confusion
|
|
53
|
+
Proxy calls implementation — `msg.sender` changes context, breaking access checks.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Attack Strategy
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
1. Identify functions with weak or missing access control
|
|
61
|
+
2. Call function directly from attacker EOA
|
|
62
|
+
3. OR deploy attacker contract to bypass tx.origin check
|
|
63
|
+
4. Execute privileged action (drain funds, set new owner, pause protocol)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Detection Signals
|
|
69
|
+
- `function X() public` with no modifier on state-changing operation
|
|
70
|
+
- `initialize()` or `setup()` functions with no protection
|
|
71
|
+
- `owner` never assigned in constructor
|
|
72
|
+
- Access checks using `tx.origin`
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## PoC Template
|
|
77
|
+
|
|
78
|
+
```solidity
|
|
79
|
+
function test_accessControlBypass() public {
|
|
80
|
+
address attacker = makeAddr("attacker");
|
|
81
|
+
|
|
82
|
+
vm.startPrank(attacker);
|
|
83
|
+
target.sensitiveAdminFunction(); // Should revert but doesn't
|
|
84
|
+
vm.stopPrank();
|
|
85
|
+
|
|
86
|
+
// Assert unauthorized action succeeded
|
|
87
|
+
assertEq(target.owner(), attacker);
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## Fix
|
|
94
|
+
|
|
95
|
+
```solidity
|
|
96
|
+
// Use OpenZeppelin Ownable or AccessControl
|
|
97
|
+
import "@openzeppelin/contracts/access/Ownable.sol";
|
|
98
|
+
|
|
99
|
+
contract Fixed is Ownable {
|
|
100
|
+
function sensitiveFunction() external onlyOwner {
|
|
101
|
+
// safe
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// For roles:
|
|
106
|
+
import "@openzeppelin/contracts/access/AccessControl.sol";
|
|
107
|
+
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
|
|
108
|
+
// use hasRole(ADMIN_ROLE, msg.sender)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## Real World Examples
|
|
114
|
+
- Parity Multisig (2017) — $30M
|
|
115
|
+
- Compound (2021) — $80M governance
|
|
116
|
+
- Nomad Bridge (2022) — $190M
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## Links
|
|
121
|
+
- [[attack-patterns/privilege-escalation]]
|
|
122
|
+
- [[vulnerabilities/signature-replay]]
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Flash Loan Attack
|
|
2
|
+
|
|
3
|
+
tags: #vulnerability #critical #flashloan #defi
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Summary
|
|
8
|
+
Flash loans allow borrowing unlimited capital within a single transaction with zero collateral. Attackers use this to amplify economic attacks that would otherwise require enormous capital.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Pattern Recognition
|
|
13
|
+
|
|
14
|
+
### Code Signals
|
|
15
|
+
- Single-block price dependency
|
|
16
|
+
- Collateral valued at spot DEX price
|
|
17
|
+
- Governance that acts in same block as proposal
|
|
18
|
+
- Reward calculations based on snapshot that can be gamed
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Attack Strategy
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
1. Identify what asset/position can be inflated with capital
|
|
26
|
+
2. Calculate if profit > flash loan fee (0.09% Uniswap, 0.05% Aave)
|
|
27
|
+
3. Flash borrow enough to move the market
|
|
28
|
+
4. Execute attack
|
|
29
|
+
5. Repay + keep profit
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## PoC Template
|
|
35
|
+
|
|
36
|
+
```solidity
|
|
37
|
+
contract FlashLoanAttacker {
|
|
38
|
+
IUniswapV3Pool pool;
|
|
39
|
+
IVulnerable target;
|
|
40
|
+
|
|
41
|
+
function attack() external {
|
|
42
|
+
pool.flash(address(this), 1_000_000e18, 0, "");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function uniswapV3FlashCallback(uint256 fee0, uint256, bytes calldata) external {
|
|
46
|
+
// Execute attack with flash loaned funds
|
|
47
|
+
// ...
|
|
48
|
+
|
|
49
|
+
// Repay
|
|
50
|
+
IERC20(token).transfer(address(pool), 1_000_000e18 + fee0);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Fix
|
|
58
|
+
- Use TWAP oracle (30+ minute window)
|
|
59
|
+
- Add time delay between actions
|
|
60
|
+
- Use Chainlink price feeds
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Links
|
|
65
|
+
- [[vulnerabilities/oracle-manipulation]]
|
|
66
|
+
- [[attack-patterns/price-manipulation]]
|