access-control
它能做什么
这份手册指导审查智能合约中谁可以更改重要设置或转移资金。适合希望验证权限检查的审计人员和开发人员。
适用场景
- ✓当 Solidity 合约中有函数可以更改所有权、设置或资金时。
- ✓当你想检查角色或所有者检查是否正确时。
- ✓当团队想找出那些公开可调用但本应受限的函数时。
技能链接
https://github.com/BitterSecurity/Decepticon/blob/main/packages/decepticon/decepticon/skills/standard/contracts/access-control/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我检查我们合约的管理员函数是否缺少正确的所有者检查?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体找出每一个会改变状态的函数,检查适用的权限修饰符,并标记缺少或设置错误的检查。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Access Control Playbook
Access control bugs are the most boring class — and the most common in production audits. They're cheap to find with grep + LSP. They drain millions when missed (LeetSwap, Audius, Saddle, Akropolis).
Audit steps
1. Find every state-changing function
# Functions that are NOT view/pure and NOT internal/private
grep -rE 'function [a-zA-Z_]+\(.*\)(public|external)' src/ | grep -v 'view\|pure'
# Or via slither
slither . --print function-summary
2. For each external/public state-changer, ask
- Does it modify storage that affects user funds, ownership, or configuration?
- Is there a modifier (
onlyOwner,onlyRole,onlyDAO, custom auth)? - If yes, what's the modifier checking?
- If no, should there be one?
3. Audit each modifier
Common modifier patterns + bugs:
| Pattern | Bug |
|---|---|
require(msg.sender == owner) |
owner settable by anyone? setOwner unprotected? |
require(msg.sender == tx.origin) |
Wrong — tx.origin breaks meta-transactions, AND it's phishable |
_msgSender() in OZ ERC2771Context |
Forwarder trusted but anyone can forward — does the contract validate the forwarder? |
onlyRole(MINTER) |
Who can grant MINTER? Is the admin a multisig or single key? |
require(initialized == false) |
Initializer can be called twice if initialized writeable elsewhere |
require(block.timestamp > deployTime + 24 hours) |
Time-based is often a fake delay — check if deployTime is settable |
require(approvedSigners[msg.sender]) |
Approval list managed by single key? |
4. Specific anti-patterns to grep
# Functions accidentally external (default in Solidity <0.5)
grep -rn 'function [a-zA-Z_]*[^ ]* *{' src/ | grep -v 'internal\|private\|public\|external'
# msg.sender == tx.origin (phishable)
grep -rn 'tx.origin' src/
# Reentrancy in access-control checks
grep -rn 'onlyOwner.*nonReentrant' src/ # both? often wrong order
# `delegatecall` without auth gate
grep -rn 'delegatecall' src/
# `selfdestruct` available
grep -rn 'selfdestruct\|suicide(' src/
5. Initializer bugs
// VULNERABLE
function initialize(address _owner) external {
owner = _owner;
// missing: require(!initialized); initialized = true;
}
// VULNERABLE (proxy implementation)
contract Impl {
constructor() { ... } // NEVER RUNS in proxy context
// initialize() should set proxy state but doesn't gate
}
// SAFE
function initialize(address _owner) external initializer {
// OZ's initializer modifier enforces single-call
__Ownable_init(_owner);
}
For UUPS/Transparent proxies: check _authorizeUpgrade is overridden
and gated. Default OZ override is empty (revert).
6. Function-selector collisions (proxies)
Transparent proxy admin function selectors collide w/ impl function selectors → admin functions become uncallable, OR impl functions are shadowed by admin. Check w/ slither:
slither-check-erc src/Impl.sol --erc ERC1967
7. Role grant/revoke
For AccessControl:
DEFAULT_ADMIN_ROLEis the admin of all roles by default- Anyone with DEFAULT_ADMIN_ROLE can grant any role to anyone
- If init code grants DEFAULT_ADMIN_ROLE to deployer w/ no transfer to multisig → key person risk
Check role hierarchy:
grep -rn '_setRoleAdmin\|_setupRole\|grantRole' src/
PoC template (Foundry)
function test_unauth_call() public {
address attacker = address(0xBEEF);
vm.prank(attacker);
// Call the function that should require auth
target.dangerousFunction(arg1, arg2);
// Assert state change happened
assertEq(target.criticalParam(), expectedManipulatedValue);
// No revert = vulnerable
}
function test_role_takeover() public {
address attacker = address(0xBEEF);
vm.startPrank(attacker);
// If grantRole is callable by anyone
target.grantRole(target.MINTER_ROLE(), attacker);
assertTrue(target.hasRole(target.MINTER_ROLE(), attacker));
// Now exploit MINTER_ROLE
target.mint(attacker, 1_000_000 ether);
}
function test_init_re_entry() public {
// Call initialize twice
target.initialize(address(this));
vm.expectRevert("Initializable: contract is already initialized");
target.initialize(attacker);
}
function test_upgrade_no_auth() public {
address attacker = address(0xBEEF);
address malicious = address(new MaliciousImpl());
vm.prank(attacker);
target.upgradeTo(malicious);
// Now any call goes to
常见问题
为什么这一类漏洞很常见?
该手册称它是生产审计中最常见的一类,而且用简单搜索就能低成本地找到。
常见的错误写法是什么?
用 tx.origin 做授权,该手册指出这是错误的做法,并且可能被钓鱼利用。
智能体会检查哪些关于角色的内容?
谁可以授予角色,以及管理员是多签账户还是单一密钥。
详情
作者的原始描述
Missing modifiers, wrong msg.sender checks, default-public functions, missing onlyOwner / onlyRole / onlyDAO authorization.
热度
- GitHub 星标
- 6.5k
- 排名
- #14005
- 许可证
- Apache-2.0
- 更新时间
- 2026/11/1
评论
- …