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

读研整活笔记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赋值并压栈
上一篇:【函数分享】每日PHP函数分享(2021-1-7)


下一篇:【小样本学习】 Few-Shot Learning 基本概念