ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

读研整活笔记n+1:分析greedy并更新

2020-12-13 13:03:29  阅读:236  来源: 互联网

标签:function 整活 读研 代码 global vm greedy payable


读研整活笔记n+1:分析greedy并更新

需求理解

上回,我们提取了greedy特征的新pattern,这回将其落实于代码之中。
具体思路如下:

  • 分析原有代码
  • 观察何时检测greedy漏洞
  • 将新pattern落实于代码

话不多说,让我们开始行动吧!

1.分析原有代码

1.1 代码何时调用?

什么时候调用了greedy的分析函数呢?

main() -> execution_and_analyze(contract_path) -> before_sym_exec(vm,name) -> function_analysis(vm)

在main()函数中,如果对合约进行分析(参数为 -e 或者分析整个文件夹内的合约),那么会调用execution_and_analyze。

在execution_and_analyze函数中:

  • 1.调用 load() 加载合约至 vm 中。
  • 2.分别调用 before_sym_exec、detect_fake_eos、after_sym_exec

在before_sym_exec函数中:

  • 1.调用locate_transfer
  • 2.调用function_analysis进行greedy检测

综上所述,在每次检测合约的时候,在动态执行之后(?存疑,load之中进行动态执行),检测greedy。

1.2 获取原有代码

分析部分代码如下:

def function_analysis(vm) -> None:
    """Analysis function, it read the opcode and arguments of function
    and detect vulnerability of smart contract. The analysis result will
    be store in global varibles.

    Args:
        vm: the virtual include env and structure.
    """

    funcs = vm.module.funcs
    # if the analyzed contract is ethereum
    if global_vars.contract_type == 'ethereum':
        # ...判断delegateCall的代码

        # 1. Count the non payable functions, finally get the number of payable functions.
        # 2. If there are payable functions in the contract but no *ethereum.call*, greedy exists.
        non_payable_count = 0
        offset = len(vm.module.imports)
        main_index = global_vars.main_function_address - len(vm.store.funcs) + len(funcs)
        for index, func in enumerate(funcs):
            print(type(func))
            if index == main_index:
                continue
            expr = func.expr
            is_payable = True
            for i, instr in enumerate(expr.data):
                if (instr.code == bin_format.call and vm.module_instance.funcaddrs[
                    instr.immediate_arguments] in global_vars.get_call_value_addr
                        and _is_non_payable_function(expr, i)):
                    non_payable_count += 1
                    is_payable = False
                    break
            if is_payable:
                global_vars.ETH_payable_function_address_set.add(index + offset)
        if non_payable_count <= len(funcs) - 2 and not global_vars.send_token_function_addr:
            global_vars.cannot_send_ETH = True

1.3 分析代码

temp

Y N Y N Y N 弹栈取位置 这个位置是实数吗 取值压栈 位置在global_state里吗 取值压栈 化简并取出变量且找个新名字 新名字在约束中吗 更新约束中的对应变量 global_state赋值并压栈

标签:function,整活,读研,代码,global,vm,greedy,payable
来源: https://blog.csdn.net/weixin_43295763/article/details/111102156

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有