-
Notifications
You must be signed in to change notification settings - Fork 3
Added 10 reentrancy vulnerability type smart contracts #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
contract dumbDAO { | ||
|
||
mapping (address => uint) public balances; | ||
|
||
function withdraw(address _recipient) returns (bool) { | ||
if (balances[msg.sender] == 0){ throw; } | ||
if (_recipient.call.value(balances[msg.sender])()) { | ||
balances[msg.sender] = 0; | ||
return true; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
contract EtherStore { | ||
|
||
uint256 public withdrawalLimit = 1 ether; | ||
mapping(address => uint256) public balances; | ||
|
||
function withdrawFunds (uint256 _weiToWithdraw) public { | ||
require(balances[msg.sender] >= _weiToWithdraw); | ||
require(_weiToWithdraw <= withdrawalLimit); | ||
require(msg.sender.call.value(_weiToWithdraw)()); | ||
balances[msg.sender] -= _weiToWithdraw; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,11 @@ | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
contract Reentrance { | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add SPDX and pragma. +// SPDX-License-Identifier: MIT
+pragma solidity 0.4.26;
contract Reentrance { 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||
mapping (address => uint) userBalance; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
function withdrawBalance_fixed(){ | ||||||||||||||||||||||||
uint amount = userBalance[msg.sender]; | ||||||||||||||||||||||||
if(!(msg.sender.call.value(amount)())){ throw; } | ||||||||||||||||||||||||
userBalance[msg.sender] = 0; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+6
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 🧩 Analysis chainFunction name is misleading; it’s still vulnerable. Rename to avoid confusion or fix the logic. Given PR goal is “vulnerable samples,” prefer rename + intent comment. - function withdrawBalance_fixed(){
+ // VULNERABILITY: reentrancy (external call before state update)
+ function withdrawBalance_vulnerable() public {
uint amount = userBalance[msg.sender];
if(!(msg.sender.call.value(amount)())){ throw; }
userBalance[msg.sender] = 0;
} Renaming alters the ABI; confirm no harness depends on the old name. 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "Searching repository for function references..."
rg -n --hidden -S "withdrawBalance_fixed|withdrawBalance_vulnerable|withdrawBalance\s*\(" || true
echo
echo "Show file if present: audit_engine/smart_contracts/reentrancy/40755.sol"
if [ -f "audit_engine/smart_contracts/reentrancy/40755.sol" ]; then
sed -n '1,240p' audit_engine/smart_contracts/reentrancy/40755.sol
else
echo "File not found: audit_engine/smart_contracts/reentrancy/40755.sol"
fi
echo
echo "Search for low-level call patterns that indicate reentrancy (msg.sender.call.value / .call.value / call.value):"
rg -n --hidden -S "msg.sender.call.value|call.value\(|\\.call\\.value" || true
echo
echo "Search tests and json/abi files for the original function name (possible harnesses):"
rg -n --hidden -S "withdrawBalance_fixed" -g '!**/node_modules/**' -g '!**/.git/**' || true
rg -n --hidden -S "40755.sol|withdrawBalance_fixed" -g '!**/node_modules/**' -g '!**/.git/**' || true Length of output: 6361 Function name is misleading; it’s still vulnerable. Rename to reflect intent (or fix the logic). Repo search found no references to - function withdrawBalance_fixed(){
+ // VULNERABILITY: reentrancy (external call before state update)
+ function withdrawBalance_vulnerable() public {
uint amount = userBalance[msg.sender];
if(!(msg.sender.call.value(amount)())){ throw; }
userBalance[msg.sender] = 0;
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||||||||||
|
||||||||||||||
contract Private_Bank { | ||||||||||||||
|
||||||||||||||
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add SPDX and pragma. +// SPDX-License-Identifier: MIT
+pragma solidity 0.4.26;
contract Private_Bank { 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||
mapping (address => uint) public balances; | ||||||||||||||
|
||||||||||||||
function CashOut(uint _am) { | ||||||||||||||
if(_am <= balances[msg.sender]) { | ||||||||||||||
if(msg.sender.call.value(_am)()) { | ||||||||||||||
balances[msg.sender] -= _am; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
contract Reentrance { | ||
|
||
mapping (address => uint) userBalance; | ||
|
||
function withdrawBalance(){ | ||
if(!(msg.sender.call.value(userBalance[msg.sender])())){ throw; } | ||
userBalance[msg.sender] = 0; | ||
} | ||
Comment on lines
+2
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ Verification inconclusiveLegacy patterns: pin compiler; consider explicit visibility. This uses +// SPDX-License-Identifier: UNLICENSED
+pragma solidity 0.4.25;
+
+/// @dev Intentionally vulnerable to reentrancy for audit_engine tests. DO NOT DEPLOY.
contract Reentrance {
@@
- function withdrawBalance(){
+ function withdrawBalance() public {
if(!(msg.sender.call.value(userBalance[msg.sender])())){ throw; }
userBalance[msg.sender] = 0;
} If you want to avoid the deprecated Critical: reentrancy — external call before state update in withdrawBalance File: audit_engine/smart_contracts/reentrancy/40757.sol Lines: 2–9
🤖 Prompt for AI Agents
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
contract Reentrance { | ||
|
||
mapping(address => uint) public balances; | ||
|
||
function withdraw(uint _amount) public { | ||
if(balances[msg.sender] >= _amount) { | ||
if(msg.sender.call.value(_amount)()) { | ||
balances[msg.sender] -= _amount; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,11 @@ | ||||||||||||||
|
||||||||||||||
contract SendBalance { | ||||||||||||||
|
||||||||||||||
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainAdd SPDX and pin compiler version for deterministic builds. Missing license identifier and pragma. Without a pragma these may compile against 0.8.x and fail. Apply: +// SPDX-License-Identifier: MIT
+pragma solidity 0.4.26;
contract SendBalance { 🏁 Script executed: #!/bin/bash
# Verify all .sol files in this PR declare a pragma and count deprecated patterns.
fd -e sol audit_engine/smart_contracts/reentrancy \
| while read f; do rg -n '^\s*pragma\s+solidity' "$f" >/dev/null || echo "MISSING_PRAGMA: $f"; done
rg -nH 'throw|\.call\.value\s*\(' audit_engine/smart_contracts/reentrancy Length of output: 5129 🏁 Script executed: #!/bin/bash
set -euo pipefail
f="audit_engine/smart_contracts/reentrancy/40761.sol"
echo "=== SHOWING $f (first 120 lines) ==="
sed -n '1,120p' "$f" || echo "ERROR: failed to read $f"
echo
echo "=== SEARCH FOR SPDX / PRAGMA IN $f ==="
rg -n --hidden --no-ignore-case "^\s*//\s*SPDX-License-Identifier|^\s*pragma\s+solidity" "$f" || echo "no SPDX/pragma lines found in $f"
echo
echo "=== DIRECTORY-WIDE: report files without a pragma or SPDX ==="
for sol in $(rg -n --files --hidden "audit_engine/smart_contracts/reentrancy" | rg -n "\.sol$" -n | sed -n '1,$p' 2>/dev/null || true); do
# fallback: list all .sol under directory if above fails
:
done
# Robust listing of .sol files and check for pragma/SPDX
find audit_engine/smart_contracts/reentrancy -type f -name '*.sol' -print0 \
| xargs -0 -n1 bash -c 'f="$0"; has=$(rg -n "^\s*pragma\s+solidity|^\s*//\s*SPDX-License-Identifier" "$f" || true); if [ -z "$has" ]; then echo "MISSING_PRAGMA_OR_SPDX: $f"; else echo "OK: $f"; fi' Length of output: 3993 Add SPDX license identifier and pin compiler version for deterministic builds. audit_engine/smart_contracts/reentrancy/40761.sol (and multiple files in the same directory) are missing an SPDX header and a pragma; the code uses +// SPDX-License-Identifier: MIT
+pragma solidity 0.4.26;
contract SendBalance { 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||
mapping (address => uint) userBalances ; | ||||||||||||||
bool withdrawn = false ; | ||||||||||||||
|
||||||||||||||
function withdrawBalance(){ | ||||||||||||||
if (!(msg.sender.call.value(userBalances[msg.sender])())) { throw; } | ||||||||||||||
userBalances[msg.sender] = 0; | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
contract SimpleDAO { | ||
|
||
mapping (address => uint) public credit; | ||
|
||
function withdraw(uint amount) public { | ||
if (credit[msg.sender] >= amount) { | ||
require(msg.sender.call.value(amount)()); | ||
credit[msg.sender] -= amount; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,12 @@ | ||||||||||||||
|
||||||||||||||
contract SimpleDAO { | ||||||||||||||
|
||||||||||||||
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add SPDX and pragma. +// SPDX-License-Identifier: MIT
+pragma solidity 0.4.26;
contract SimpleDAO { 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||
mapping (address => uint) public credit; | ||||||||||||||
|
||||||||||||||
function withdraw(uint amount) { | ||||||||||||||
if (credit[msg.sender] >= amount) { | ||||||||||||||
msg.sender.call.value(amount)(); | ||||||||||||||
credit[msg.sender] -= amount; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
contract Victim { | ||
|
||
mapping(address => uint) public balances; | ||
|
||
function withdraw(uint _amount) public { | ||
if(balances[msg.sender] >= _amount) { | ||
if(msg.sender.call.value(_amount)()) { | ||
balances[msg.sender] -= _amount; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add SPDX and pragma.
📝 Committable suggestion
🤖 Prompt for AI Agents