ineedcodes 1.6.1 → 1.7.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/package.json +2 -1
- package/skills/401-403-bypass-techniques/SKILL.md +348 -0
- package/skills/active-directory-acl-abuse/BLOODHOUND_PATHS.md +281 -0
- package/skills/active-directory-acl-abuse/SKILL.md +295 -0
- package/skills/active-directory-certificate-services/ADCS_ESC_MATRIX.md +197 -0
- package/skills/active-directory-certificate-services/SKILL.md +303 -0
- package/skills/active-directory-kerberos-attacks/KERBEROS_ATTACK_CHAINS.md +226 -0
- package/skills/active-directory-kerberos-attacks/SKILL.md +311 -0
- package/skills/ai-ml-security/SKILL.md +425 -0
- package/skills/android-pentesting-tricks/FRIDA_SCRIPTS.md +394 -0
- package/skills/android-pentesting-tricks/SKILL.md +369 -0
- package/skills/anti-debugging-techniques/ANTI_DEBUG_MATRIX.md +117 -0
- package/skills/anti-debugging-techniques/SKILL.md +407 -0
- package/skills/api-auth-and-jwt-abuse/SKILL.md +76 -0
- package/skills/api-authorization-and-bola/SKILL.md +48 -0
- package/skills/api-recon-and-docs/SKILL.md +61 -0
- package/skills/api-sec/SKILL.md +49 -0
- package/skills/arbitrary-write-to-rce/SKILL.md +361 -0
- package/skills/auth-sec/SKILL.md +41 -0
- package/skills/authbypass-authentication-flaws/SKILL.md +441 -0
- package/skills/bba-ad-attacker/SKILL.md +420 -0
- package/skills/bba-api-security/SKILL.md +95 -0
- package/skills/bba-attack-planner/SKILL.md +186 -0
- package/skills/bba-binary-exploit/SKILL.md +65 -0
- package/skills/bba-bizlogic-hunter/SKILL.md +304 -0
- package/skills/bba-bug-bounty/SKILL.md +245 -0
- package/skills/bba-cicd-redteam/SKILL.md +515 -0
- package/skills/bba-cloud-security/SKILL.md +97 -0
- package/skills/bba-container-escape/SKILL.md +161 -0
- package/skills/bba-credential-tester/SKILL.md +346 -0
- package/skills/bba-crypto-analyst/SKILL.md +54 -0
- package/skills/bba-ctf-solver/SKILL.md +166 -0
- package/skills/bba-detection-engineer/SKILL.md +93 -0
- package/skills/bba-engagement-planner/SKILL.md +68 -0
- package/skills/bba-exploit-chainer/SKILL.md +280 -0
- package/skills/bba-exploit-guide/SKILL.md +96 -0
- package/skills/bba-forensics-analyst/SKILL.md +512 -0
- package/skills/bba-graphql-hunter/SKILL.md +145 -0
- package/skills/bba-hardware-hacker/SKILL.md +57 -0
- package/skills/bba-jwt-cracker/SKILL.md +133 -0
- package/skills/bba-llm-redteam/SKILL.md +149 -0
- package/skills/bba-malware-analyst/SKILL.md +305 -0
- package/skills/bba-mobile-pentester/SKILL.md +350 -0
- package/skills/bba-osint-collector/SKILL.md +645 -0
- package/skills/bba-payload-crafter/SKILL.md +347 -0
- package/skills/bba-phishing-operator/SKILL.md +516 -0
- package/skills/bba-poc-validator/SKILL.md +249 -0
- package/skills/bba-privesc-advisor/SKILL.md +98 -0
- package/skills/bba-purple-team/SKILL.md +56 -0
- package/skills/bba-recon-advisor/SKILL.md +215 -0
- package/skills/bba-red-team-operator/SKILL.md +61 -0
- package/skills/bba-report-generator/SKILL.md +145 -0
- package/skills/bba-reverse-engineer/SKILL.md +298 -0
- package/skills/bba-social-engineer/SKILL.md +341 -0
- package/skills/bba-ssrf-hunter/SKILL.md +120 -0
- package/skills/bba-stig-analyst/SKILL.md +108 -0
- package/skills/bba-subdomain-takeover/SKILL.md +141 -0
- package/skills/bba-swarm-orchestrator/SKILL.md +351 -0
- package/skills/bba-threat-modeler/SKILL.md +574 -0
- package/skills/bba-vuln-scanner/SKILL.md +305 -0
- package/skills/bba-web-hunter/SKILL.md +285 -0
- package/skills/bba-wireless-pentester/SKILL.md +539 -0
- package/skills/binary-protection-bypass/PROTECTION_BYPASS_MATRIX.md +168 -0
- package/skills/binary-protection-bypass/SKILL.md +295 -0
- package/skills/browser-exploitation-v8/SKILL.md +334 -0
- package/skills/browser-exploitation-v8/V8_EXPLOITATION_PATTERNS.md +288 -0
- package/skills/business-logic-vuln/SKILL.md +33 -0
- package/skills/business-logic-vulnerabilities/CHECKLIST.md +375 -0
- package/skills/business-logic-vulnerabilities/METHODOLOGY.md +337 -0
- package/skills/business-logic-vulnerabilities/SCENARIOS.md +1095 -0
- package/skills/business-logic-vulnerabilities/SKILL.md +711 -0
- package/skills/classical-cipher-analysis/SKILL.md +663 -0
- package/skills/clickjacking/SKILL.md +163 -0
- package/skills/cmdi-command-injection/SKILL.md +687 -0
- package/skills/code-obfuscation-deobfuscation/SKILL.md +391 -0
- package/skills/container-escape-techniques/DOCKER_ESCAPE_CHAINS.md +296 -0
- package/skills/container-escape-techniques/SKILL.md +339 -0
- package/skills/cors-cross-origin-misconfiguration/SCENARIOS.md +152 -0
- package/skills/cors-cross-origin-misconfiguration/SKILL.md +270 -0
- package/skills/crlf-injection/SKILL.md +179 -0
- package/skills/csp-bypass-advanced/SKILL.md +259 -0
- package/skills/csrf-cross-site-request-forgery/SKILL.md +526 -0
- package/skills/csv-formula-injection/SKILL.md +144 -0
- package/skills/dangling-markup-injection/SKILL.md +321 -0
- package/skills/defi-attack-patterns/SKILL.md +355 -0
- package/skills/dependency-confusion/SKILL.md +178 -0
- package/skills/deserialization-insecure/JAVA_GADGET_CHAINS.md +511 -0
- package/skills/deserialization-insecure/SKILL.md +725 -0
- package/skills/dns-rebinding-attacks/SKILL.md +337 -0
- package/skills/email-header-injection/SKILL.md +349 -0
- package/skills/expression-language-injection/SKILL.md +243 -0
- package/skills/file-access-vuln/SKILL.md +33 -0
- package/skills/format-string-exploitation/SKILL.md +312 -0
- package/skills/ghost-bits-cast-attack/PAYLOAD_COOKBOOK.md +448 -0
- package/skills/ghost-bits-cast-attack/SKILL.md +799 -0
- package/skills/graphql-and-hidden-parameters/SKILL.md +50 -0
- package/skills/hack/SKILL.md +162 -0
- package/skills/hash-attack-techniques/SKILL.md +491 -0
- package/skills/heap-exploitation/HOUSE_OF_TECHNIQUES.md +244 -0
- package/skills/heap-exploitation/IO_FILE_EXPLOITATION.md +256 -0
- package/skills/heap-exploitation/SKILL.md +217 -0
- package/skills/http-host-header-attacks/SKILL.md +323 -0
- package/skills/http-parameter-pollution/SKILL.md +208 -0
- package/skills/http2-specific-attacks/SKILL.md +350 -0
- package/skills/idor-broken-object-authorization/SKILL.md +336 -0
- package/skills/injection-checking/EXTRA_INJECTION_TYPES.md +450 -0
- package/skills/injection-checking/SKILL.md +50 -0
- package/skills/insecure-source-code-management/SKILL.md +161 -0
- package/skills/ios-pentesting-tricks/IOS_RUNTIME_TRICKS.md +411 -0
- package/skills/ios-pentesting-tricks/SKILL.md +418 -0
- package/skills/jndi-injection/SKILL.md +265 -0
- package/skills/jwt-oauth-token-attacks/SKILL.md +301 -0
- package/skills/kernel-exploitation/KERNEL_HEAP_TECHNIQUES.md +293 -0
- package/skills/kernel-exploitation/KERNEL_MITIGATION_BYPASS.md +224 -0
- package/skills/kernel-exploitation/SKILL.md +307 -0
- package/skills/kubernetes-pentesting/SKILL.md +348 -0
- package/skills/lattice-crypto-attacks/SKILL.md +497 -0
- package/skills/linux-lateral-movement/SKILL.md +348 -0
- package/skills/linux-privilege-escalation/KERNEL_EXPLOITS_CHECKLIST.md +260 -0
- package/skills/linux-privilege-escalation/SKILL.md +346 -0
- package/skills/linux-privilege-escalation/SUID_CAPABILITIES_TRICKS.md +305 -0
- package/skills/linux-security-bypass/SKILL.md +345 -0
- package/skills/llm-prompt-injection/JAILBREAK_PATTERNS.md +306 -0
- package/skills/llm-prompt-injection/SKILL.md +357 -0
- package/skills/macos-process-injection/DYLIB_XPC_TECHNIQUES.md +373 -0
- package/skills/macos-process-injection/SKILL.md +384 -0
- package/skills/macos-security-bypass/SKILL.md +337 -0
- package/skills/macos-security-bypass/TCC_BYPASS_MATRIX.md +200 -0
- package/skills/memory-forensics-volatility/SKILL.md +323 -0
- package/skills/memory-forensics-volatility/VOLATILITY_CHEATSHEET.md +225 -0
- package/skills/mobile-ssl-pinning-bypass/SKILL.md +531 -0
- package/skills/network-protocol-attacks/NAME_RESOLUTION_POISONING.md +226 -0
- package/skills/network-protocol-attacks/SKILL.md +330 -0
- package/skills/nosql-injection/SKILL.md +341 -0
- package/skills/ntlm-relay-coercion/COERCION_METHODS.md +253 -0
- package/skills/ntlm-relay-coercion/SKILL.md +267 -0
- package/skills/oauth-oidc-misconfiguration/SKILL.md +46 -0
- package/skills/open-redirect/SKILL.md +380 -0
- package/skills/path-traversal-lfi/SKILL.md +802 -0
- package/skills/prototype-pollution/SKILL.md +190 -0
- package/skills/prototype-pollution-advanced/KNOWN_GADGETS.md +253 -0
- package/skills/prototype-pollution-advanced/SKILL.md +338 -0
- package/skills/race-condition/SKILL.md +526 -0
- package/skills/recon-and-methodology/SKILL.md +389 -0
- package/skills/recon-for-sec/SKILL.md +29 -0
- package/skills/request-smuggling/H2_SMUGGLING_VARIANTS.md +498 -0
- package/skills/request-smuggling/SKILL.md +314 -0
- package/skills/reverse-shell-techniques/SHELL_CHEATSHEET.md +196 -0
- package/skills/reverse-shell-techniques/SKILL.md +290 -0
- package/skills/rsa-attack-techniques/RSA_ATTACK_CATALOG.md +432 -0
- package/skills/rsa-attack-techniques/SKILL.md +437 -0
- package/skills/saml-sso-assertion-attacks/SKILL.md +41 -0
- package/skills/sandbox-escape-techniques/PYTHON_SANDBOX_ESCAPE.md +349 -0
- package/skills/sandbox-escape-techniques/SECCOMP_BYPASS.md +332 -0
- package/skills/sandbox-escape-techniques/SKILL.md +250 -0
- package/skills/smart-contract-vulnerabilities/SKILL.md +314 -0
- package/skills/smart-contract-vulnerabilities/SOLIDITY_VULN_PATTERNS.md +460 -0
- package/skills/sqli-sql-injection/SCENARIOS.md +575 -0
- package/skills/sqli-sql-injection/SKILL.md +489 -0
- package/skills/sqli-sql-injection/SQLMAP_ADVANCED.md +642 -0
- package/skills/ssrf-server-side-request-forgery/SCENARIOS.md +226 -0
- package/skills/ssrf-server-side-request-forgery/SKILL.md +323 -0
- package/skills/ssrf-server-side-request-forgery/URL_PARSER_TRICKS.md +650 -0
- package/skills/ssti-server-side-template-injection/ENGINE_PAYLOADS.md +456 -0
- package/skills/ssti-server-side-template-injection/SCENARIOS.md +319 -0
- package/skills/ssti-server-side-template-injection/SKILL.md +344 -0
- package/skills/stack-overflow-and-rop/ROP_ADVANCED_TECHNIQUES.md +249 -0
- package/skills/stack-overflow-and-rop/SKILL.md +304 -0
- package/skills/steganography-techniques/SKILL.md +348 -0
- package/skills/steganography-techniques/STEGO_TOOLS_GUIDE.md +335 -0
- package/skills/subdomain-takeover/SKILL.md +247 -0
- package/skills/symbolic-execution-tools/ANGR_COOKBOOK.md +437 -0
- package/skills/symbolic-execution-tools/SKILL.md +419 -0
- package/skills/symmetric-cipher-attacks/BLOCK_CIPHER_ATTACKS.md +364 -0
- package/skills/symmetric-cipher-attacks/SKILL.md +462 -0
- package/skills/traffic-analysis-pcap/SKILL.md +341 -0
- package/skills/tunneling-and-pivoting/SKILL.md +343 -0
- package/skills/type-juggling/SKILL.md +291 -0
- package/skills/unauthorized-access-common-services/PORT_SERVICE_MATRIX.md +340 -0
- package/skills/unauthorized-access-common-services/SKILL.md +388 -0
- package/skills/upload-insecure-files/SCENARIOS.md +158 -0
- package/skills/upload-insecure-files/SKILL.md +543 -0
- package/skills/vm-and-bytecode-reverse/SKILL.md +450 -0
- package/skills/vr-cheatsheet-authentication_bypass/SKILL.md +184 -0
- package/skills/vr-cheatsheet-dangerous_functions/SKILL.md +200 -0
- package/skills/vr-cheatsheet-linux_commands/SKILL.md +40 -0
- package/skills/vr-cheatsheet-rce/SKILL.md +177 -0
- package/skills/vr-cheatsheet-reverse_engineering/SKILL.md +144 -0
- package/skills/vr-cheatsheet-sqli/SKILL.md +270 -0
- package/skills/vr-cheatsheet-ssrf/SKILL.md +173 -0
- package/skills/vr-cheatsheet-xss/SKILL.md +177 -0
- package/skills/vr-methodology-mindset_and_tips/SKILL.md +51 -0
- package/skills/vr-template-vulnerability_report_template/SKILL.md +59 -0
- package/skills/vr-writeup-CVE-2026-9136/SKILL.md +171 -0
- package/skills/vr-writeup-ZVE-2026-3795/SKILL.md +350 -0
- package/skills/vr-writeup-api_key_leak/SKILL.md +103 -0
- package/skills/waf-bypass-techniques/SKILL.md +290 -0
- package/skills/waf-bypass-techniques/WAF_PRODUCT_MATRIX.md +202 -0
- package/skills/web-cache-deception/CACHE_POISONING_TECHNIQUES.md +637 -0
- package/skills/web-cache-deception/SKILL.md +222 -0
- package/skills/websocket-security/SKILL.md +454 -0
- package/skills/windows-av-evasion/AMSI_BYPASS_TECHNIQUES.md +289 -0
- package/skills/windows-av-evasion/SKILL.md +342 -0
- package/skills/windows-lateral-movement/CREDENTIAL_DUMPING.md +308 -0
- package/skills/windows-lateral-movement/SKILL.md +335 -0
- package/skills/windows-privilege-escalation/SKILL.md +328 -0
- package/skills/windows-privilege-escalation/TOKEN_POTATO_TRICKS.md +216 -0
- package/skills/windows-privilege-escalation/UAC_BYPASS_METHODS.md +225 -0
- package/skills/xslt-injection/SKILL.md +281 -0
- package/skills/xss-cross-site-scripting/ADVANCED_XSS_TRICKS.md +276 -0
- package/skills/xss-cross-site-scripting/SCENARIOS.md +278 -0
- package/skills/xss-cross-site-scripting/SKILL.md +379 -0
- package/skills/xxe-xml-external-entity/SCENARIOS.md +112 -0
- package/skills/xxe-xml-external-entity/SKILL.md +554 -0
- package/src/agent.js +1 -1
- package/src/session.js +6 -3
- package/src/skills.js +30 -6
- package/src/ui.js +1 -1
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
# Solidity Vulnerability Patterns — Code Reference
|
|
2
|
+
|
|
3
|
+
> **Load trigger**: When the agent needs side-by-side vulnerable vs fixed Solidity code patterns, gas-optimization-introduced vulnerabilities, or proxy storage slot collision calculations. Assumes the main [SKILL.md](./SKILL.md) is already loaded for conceptual understanding.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. REENTRANCY — VULNERABLE VS FIXED
|
|
8
|
+
|
|
9
|
+
### 1.1 Classic Reentrancy
|
|
10
|
+
|
|
11
|
+
**Vulnerable:**
|
|
12
|
+
```solidity
|
|
13
|
+
function withdraw() public {
|
|
14
|
+
uint bal = balances[msg.sender];
|
|
15
|
+
require(bal > 0);
|
|
16
|
+
(bool sent, ) = msg.sender.call{value: bal}("");
|
|
17
|
+
require(sent, "Failed to send");
|
|
18
|
+
balances[msg.sender] = 0; // state update AFTER external call
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Fixed (Checks-Effects-Interactions):**
|
|
23
|
+
```solidity
|
|
24
|
+
function withdraw() public {
|
|
25
|
+
uint bal = balances[msg.sender];
|
|
26
|
+
require(bal > 0);
|
|
27
|
+
balances[msg.sender] = 0; // state update BEFORE external call
|
|
28
|
+
(bool sent, ) = msg.sender.call{value: bal}("");
|
|
29
|
+
require(sent, "Failed to send");
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### 1.2 Cross-Function Reentrancy
|
|
34
|
+
|
|
35
|
+
**Vulnerable:**
|
|
36
|
+
```solidity
|
|
37
|
+
function withdraw() public {
|
|
38
|
+
uint bal = balances[msg.sender];
|
|
39
|
+
require(bal > 0);
|
|
40
|
+
(bool sent, ) = msg.sender.call{value: bal}("");
|
|
41
|
+
require(sent);
|
|
42
|
+
balances[msg.sender] = 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function transfer(address to, uint amount) public {
|
|
46
|
+
require(balances[msg.sender] >= amount);
|
|
47
|
+
balances[msg.sender] -= amount;
|
|
48
|
+
balances[to] += amount;
|
|
49
|
+
}
|
|
50
|
+
// Attacker: during withdraw callback, call transfer() with stale balance
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
**Fixed:**
|
|
54
|
+
```solidity
|
|
55
|
+
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
|
|
56
|
+
|
|
57
|
+
contract Safe is ReentrancyGuard {
|
|
58
|
+
function withdraw() public nonReentrant { ... }
|
|
59
|
+
function transfer(address to, uint amount) public nonReentrant { ... }
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 1.3 Read-Only Reentrancy
|
|
64
|
+
|
|
65
|
+
**Vulnerable third-party contract:**
|
|
66
|
+
```solidity
|
|
67
|
+
function getPrice() external view returns (uint) {
|
|
68
|
+
return pool.get_virtual_price(); // reads stale state during pool callback
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function deposit(uint amount) external {
|
|
72
|
+
uint price = getPrice(); // inflated during reentrancy window
|
|
73
|
+
uint shares = amount * 1e18 / price;
|
|
74
|
+
_mint(msg.sender, shares); // mints too few shares (or too many)
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Attacker contract:**
|
|
79
|
+
```solidity
|
|
80
|
+
function attack() external {
|
|
81
|
+
pool.remove_liquidity(...); // triggers callback
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
receive() external payable {
|
|
85
|
+
// During callback: pool state is intermediate
|
|
86
|
+
// get_virtual_price() returns inflated value
|
|
87
|
+
vulnerableProtocol.deposit{value: 1 ether}(1 ether);
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 2. INTEGER OVERFLOW / UNDERFLOW
|
|
94
|
+
|
|
95
|
+
### 2.1 Pre-0.8 Balance Underflow
|
|
96
|
+
|
|
97
|
+
**Vulnerable (Solidity < 0.8):**
|
|
98
|
+
```solidity
|
|
99
|
+
pragma solidity ^0.7.0;
|
|
100
|
+
|
|
101
|
+
function transfer(address to, uint256 amount) public {
|
|
102
|
+
require(balances[msg.sender] - amount >= 0); // always true for uint!
|
|
103
|
+
balances[msg.sender] -= amount; // underflows to ~2^256
|
|
104
|
+
balances[to] += amount;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**Fixed:**
|
|
109
|
+
```solidity
|
|
110
|
+
pragma solidity ^0.7.0;
|
|
111
|
+
import "@openzeppelin/contracts/math/SafeMath.sol";
|
|
112
|
+
|
|
113
|
+
function transfer(address to, uint256 amount) public {
|
|
114
|
+
balances[msg.sender] = balances[msg.sender].sub(amount); // reverts on underflow
|
|
115
|
+
balances[to] = balances[to].add(amount);
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 2.2 Timelock Bypass via Overflow
|
|
120
|
+
|
|
121
|
+
**Vulnerable:**
|
|
122
|
+
```solidity
|
|
123
|
+
function increaseLockTime(uint _seconds) public {
|
|
124
|
+
lockTime[msg.sender] += _seconds; // overflow wraps to small value
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function withdraw() public {
|
|
128
|
+
require(block.timestamp > lockTime[msg.sender]);
|
|
129
|
+
// ...
|
|
130
|
+
}
|
|
131
|
+
// Attack: increaseLockTime(type(uint256).max - lockTime + 1) → wraps to 0
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 2.3 Unsafe Casting (Post-0.8 Risk)
|
|
135
|
+
|
|
136
|
+
```solidity
|
|
137
|
+
function processAmount(uint256 amount) external {
|
|
138
|
+
uint128 truncated = uint128(amount); // 0.8 does NOT revert on downcast!
|
|
139
|
+
// amount = 2^128 + 1 → truncated = 1
|
|
140
|
+
_transfer(msg.sender, truncated);
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
**Fixed (Solidity ≥ 0.8.0):**
|
|
145
|
+
```solidity
|
|
146
|
+
function processAmount(uint256 amount) external {
|
|
147
|
+
require(amount <= type(uint128).max, "overflow");
|
|
148
|
+
uint128 safe = uint128(amount);
|
|
149
|
+
_transfer(msg.sender, safe);
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## 3. ACCESS CONTROL
|
|
156
|
+
|
|
157
|
+
### 3.1 tx.origin Phishing
|
|
158
|
+
|
|
159
|
+
**Vulnerable:**
|
|
160
|
+
```solidity
|
|
161
|
+
function transferOwnership(address newOwner) public {
|
|
162
|
+
require(tx.origin == owner); // tx.origin = EOA, not immediate caller
|
|
163
|
+
owner = newOwner;
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**Attack contract:**
|
|
168
|
+
```solidity
|
|
169
|
+
contract PhishingAttack {
|
|
170
|
+
VulnerableContract target;
|
|
171
|
+
|
|
172
|
+
function attack() external {
|
|
173
|
+
// If the owner calls this function (tricked via phishing link),
|
|
174
|
+
// tx.origin == owner → passes the check
|
|
175
|
+
target.transferOwnership(address(this));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
**Fixed:**
|
|
181
|
+
```solidity
|
|
182
|
+
function transferOwnership(address newOwner) public {
|
|
183
|
+
require(msg.sender == owner); // msg.sender = immediate caller
|
|
184
|
+
owner = newOwner;
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### 3.2 Unprotected Selfdestruct
|
|
189
|
+
|
|
190
|
+
**Vulnerable:**
|
|
191
|
+
```solidity
|
|
192
|
+
function destroy() external {
|
|
193
|
+
selfdestruct(payable(msg.sender)); // no access control
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### 3.3 Unprotected Initializer (Proxy Pattern)
|
|
198
|
+
|
|
199
|
+
**Vulnerable:**
|
|
200
|
+
```solidity
|
|
201
|
+
function initialize(address _owner) public {
|
|
202
|
+
owner = _owner; // can be called by anyone, multiple times
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
**Fixed:**
|
|
207
|
+
```solidity
|
|
208
|
+
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
|
|
209
|
+
|
|
210
|
+
function initialize(address _owner) public initializer {
|
|
211
|
+
owner = _owner;
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
## 4. DELEGATECALL STORAGE COLLISION
|
|
218
|
+
|
|
219
|
+
### 4.1 Proxy-Implementation Slot Mismatch
|
|
220
|
+
|
|
221
|
+
```solidity
|
|
222
|
+
// Proxy contract
|
|
223
|
+
contract Proxy {
|
|
224
|
+
address public implementation; // slot 0
|
|
225
|
+
address public owner; // slot 1
|
|
226
|
+
|
|
227
|
+
fallback() external payable {
|
|
228
|
+
(bool s, ) = implementation.delegatecall(msg.data);
|
|
229
|
+
require(s);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Implementation contract
|
|
234
|
+
contract Implementation {
|
|
235
|
+
uint public someValue; // slot 0 — COLLIDES with Proxy.implementation!
|
|
236
|
+
address public admin; // slot 1 — COLLIDES with Proxy.owner!
|
|
237
|
+
|
|
238
|
+
function setSomeValue(uint _val) public {
|
|
239
|
+
someValue = _val; // overwrites Proxy.implementation address!
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
**Fixed (EIP-1967 storage slots):**
|
|
245
|
+
```solidity
|
|
246
|
+
contract SafeProxy {
|
|
247
|
+
// Implementation stored at keccak256("eip1967.proxy.implementation") - 1
|
|
248
|
+
bytes32 private constant IMPL_SLOT =
|
|
249
|
+
0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
|
|
250
|
+
|
|
251
|
+
function _implementation() internal view returns (address impl) {
|
|
252
|
+
assembly { impl := sload(IMPL_SLOT) }
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### 4.2 Function Selector Collision in Transparent Proxy
|
|
258
|
+
|
|
259
|
+
```
|
|
260
|
+
admin() selector: 0xf851a440
|
|
261
|
+
collide_func() selector: 0xf851a440 ← same 4 bytes by coincidence
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Tool to check: `cast sig "functionName(argTypes)"` computes selector.
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## 5. RANDOMNESS MANIPULATION
|
|
269
|
+
|
|
270
|
+
**Vulnerable:**
|
|
271
|
+
```solidity
|
|
272
|
+
function roll() external payable {
|
|
273
|
+
uint random = uint(keccak256(abi.encodePacked(
|
|
274
|
+
block.timestamp,
|
|
275
|
+
block.difficulty,
|
|
276
|
+
msg.sender
|
|
277
|
+
))) % 6;
|
|
278
|
+
if (random == 0) {
|
|
279
|
+
payable(msg.sender).transfer(address(this).balance);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
**Attack contract:**
|
|
285
|
+
```solidity
|
|
286
|
+
contract AttackRoll {
|
|
287
|
+
function attack(VulnerableRoll target) external payable {
|
|
288
|
+
uint random = uint(keccak256(abi.encodePacked(
|
|
289
|
+
block.timestamp,
|
|
290
|
+
block.difficulty,
|
|
291
|
+
address(this)
|
|
292
|
+
))) % 6;
|
|
293
|
+
require(random == 0, "not winning, skip");
|
|
294
|
+
target.roll{value: msg.value}();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
## 6. SIGNATURE REPLAY
|
|
302
|
+
|
|
303
|
+
**Vulnerable:**
|
|
304
|
+
```solidity
|
|
305
|
+
function executeWithSig(address to, uint amount, bytes memory sig) external {
|
|
306
|
+
bytes32 hash = keccak256(abi.encodePacked(to, amount));
|
|
307
|
+
address signer = ECDSA.recover(hash, sig);
|
|
308
|
+
require(signer == owner, "invalid sig");
|
|
309
|
+
// no nonce → same signature can be replayed
|
|
310
|
+
payable(to).transfer(amount);
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
**Fixed:**
|
|
315
|
+
```solidity
|
|
316
|
+
mapping(uint256 => bool) public usedNonces;
|
|
317
|
+
|
|
318
|
+
function executeWithSig(
|
|
319
|
+
address to, uint amount, uint256 nonce, bytes memory sig
|
|
320
|
+
) external {
|
|
321
|
+
require(!usedNonces[nonce], "nonce used");
|
|
322
|
+
bytes32 hash = keccak256(abi.encodePacked(to, amount, nonce, block.chainid, address(this)));
|
|
323
|
+
address signer = ECDSA.recover(hash, sig);
|
|
324
|
+
require(signer == owner, "invalid sig");
|
|
325
|
+
usedNonces[nonce] = true;
|
|
326
|
+
payable(to).transfer(amount);
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
---
|
|
331
|
+
|
|
332
|
+
## 7. SELF-DESTRUCT FORCE-SEND ETH
|
|
333
|
+
|
|
334
|
+
**Vulnerable (balance-dependent logic):**
|
|
335
|
+
```solidity
|
|
336
|
+
function isGameComplete() public view returns (bool) {
|
|
337
|
+
return address(this).balance == 10 ether; // exact balance check
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
**Attack:**
|
|
342
|
+
```solidity
|
|
343
|
+
contract ForceEth {
|
|
344
|
+
function attack(address target) external payable {
|
|
345
|
+
selfdestruct(payable(target));
|
|
346
|
+
// forces ETH into target, bypassing receive/fallback
|
|
347
|
+
// target.balance now != 10 ether → game logic broken
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**Fixed:**
|
|
353
|
+
```solidity
|
|
354
|
+
uint public deposits; // track deposits explicitly, don't rely on balance
|
|
355
|
+
|
|
356
|
+
function isGameComplete() public view returns (bool) {
|
|
357
|
+
return deposits == 10 ether;
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
---
|
|
362
|
+
|
|
363
|
+
## 8. FLASH LOAN ORACLE MANIPULATION
|
|
364
|
+
|
|
365
|
+
**Vulnerable price oracle:**
|
|
366
|
+
```solidity
|
|
367
|
+
function getPrice(address token) public view returns (uint) {
|
|
368
|
+
(uint reserve0, uint reserve1, ) = pair.getReserves();
|
|
369
|
+
return reserve0 * 1e18 / reserve1; // spot price — manipulable in same tx
|
|
370
|
+
}
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
**Attack flow:**
|
|
374
|
+
```
|
|
375
|
+
1. Flash borrow 10,000 ETH
|
|
376
|
+
2. Swap ETH → Token on AMM (crashes token spot price)
|
|
377
|
+
3. Call lending protocol that uses spot price → borrow token at deflated price
|
|
378
|
+
4. Swap token back → ETH (restore price)
|
|
379
|
+
5. Repay flash loan + fee
|
|
380
|
+
6. Profit = borrowed tokens at deflated price - flash loan fee
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
**Fixed (TWAP oracle):**
|
|
384
|
+
```solidity
|
|
385
|
+
function getPrice(address token) public view returns (uint) {
|
|
386
|
+
// Use Uniswap V3 TWAP or Chainlink aggregator
|
|
387
|
+
(, int256 price, , uint256 updatedAt, ) = chainlinkFeed.latestRoundData();
|
|
388
|
+
require(block.timestamp - updatedAt < 3600, "stale price");
|
|
389
|
+
return uint256(price);
|
|
390
|
+
}
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
---
|
|
394
|
+
|
|
395
|
+
## 9. UNCHECKED RETURN VALUE
|
|
396
|
+
|
|
397
|
+
**Vulnerable:**
|
|
398
|
+
```solidity
|
|
399
|
+
function withdraw(uint amount) external {
|
|
400
|
+
payable(msg.sender).send(amount); // send() returns bool, not checked!
|
|
401
|
+
balances[msg.sender] -= amount; // balance decremented even if send failed
|
|
402
|
+
}
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
**Fixed:**
|
|
406
|
+
```solidity
|
|
407
|
+
function withdraw(uint amount) external {
|
|
408
|
+
(bool success, ) = payable(msg.sender).call{value: amount}("");
|
|
409
|
+
require(success, "transfer failed");
|
|
410
|
+
balances[msg.sender] -= amount;
|
|
411
|
+
}
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
---
|
|
415
|
+
|
|
416
|
+
## 10. DENIAL OF SERVICE — UNEXPECTED REVERT
|
|
417
|
+
|
|
418
|
+
**Vulnerable (push pattern):**
|
|
419
|
+
```solidity
|
|
420
|
+
address[] public recipients;
|
|
421
|
+
|
|
422
|
+
function distribute() external {
|
|
423
|
+
for (uint i = 0; i < recipients.length; i++) {
|
|
424
|
+
// If one recipient is a contract that reverts, entire distribution fails
|
|
425
|
+
payable(recipients[i]).transfer(1 ether);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
**Fixed (pull pattern):**
|
|
431
|
+
```solidity
|
|
432
|
+
mapping(address => uint) public pendingWithdrawals;
|
|
433
|
+
|
|
434
|
+
function distribute() external {
|
|
435
|
+
for (uint i = 0; i < recipients.length; i++) {
|
|
436
|
+
pendingWithdrawals[recipients[i]] += 1 ether;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function withdraw() external {
|
|
441
|
+
uint amount = pendingWithdrawals[msg.sender];
|
|
442
|
+
pendingWithdrawals[msg.sender] = 0;
|
|
443
|
+
payable(msg.sender).transfer(amount);
|
|
444
|
+
}
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
---
|
|
448
|
+
|
|
449
|
+
## 11. GAS OPTIMIZATION TRAPS
|
|
450
|
+
|
|
451
|
+
Optimizations that accidentally introduce vulnerabilities:
|
|
452
|
+
|
|
453
|
+
| Optimization | Vulnerability Introduced |
|
|
454
|
+
|---|---|
|
|
455
|
+
| `unchecked{}` loop counter | User-controlled bounds → overflow |
|
|
456
|
+
| Assembly `sstore` for gas savings | Bypasses Solidity's overflow checks and visibility |
|
|
457
|
+
| Packed storage (`uint128, uint128` in one slot) | Incorrect bit masking → value corruption |
|
|
458
|
+
| `immutable` used for mutable config | Cannot update → frozen misconfiguration |
|
|
459
|
+
| `selfdestruct` for gas refund | Contract destruction as attack vector (pre-Dencun) |
|
|
460
|
+
| Skipping zero-address checks | Gas saved but ownership can be burned |
|