论如何操控ICO走向人生巅峰

Ethereum从2013年一直走到现在,已经过去了五年。在2017年,出尽了风头,然而却因为智能合约的问题就搞出了好几个大事情,其背后透漏出的就是开发者对Ethereum或者说EVM的不了解。普通大众就更不明白了。2017年,尤其ICO项目层出不穷,然而合约代码有多少是严格审计的?今天,我们来仔细审视一段代码,看看有些项目方是如何操控ICO进程的。

老规矩,先上ICO代码

这段ICO代码,有个提取限制,即owner在ICO完后的第一个week里可以最多取1个ETH,第二个week里可以最多取2个ETH,第三个week里可以最多取4TH,第四个week里可以最多取8TH,以此类推。

代码来源于这里

/**
 *  Merdetoken
 *
 *  See: https://theethereum.wiki/w/index.php/ERC20_Token_Standard
 *
 *  This ICO allows participants to send ether to this contract in exchange
 *  for MDT tokens during the presale period (4 weeks).
 *
 *  Once the presale period is over, no addition tokens may be created and the owner is
 *  able to withdraw the funds in according to a schedule which increases over time:
 *    - week 1: 1 ether
 *    - week 2: 2 ether
 *    - week 3: 4 ether
 *    - week 4: 8 ether
 *  ...and so on. This way as the project grows and further funds are required, more can
 *  be withdrawn, however, by deferring the funding, the owner cannot just cash out and
 *  skip town.
 *
 *  An entry for a sinister contract...
 *
 *  Content Info:
 *    http://u.solidity.cc/?t=1&cn=ZmxleGlibGVfcmVjc18y&iid=41e0d6e3f8114ddc86b2bd023e624f01&uid=869384457991671808&nid=244+272699400
 *
 *
 *  Licensed under both:
 *    - MIT License
 *    - Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
 *
 *  Author information will be added once the contest has ended.
 */


// Not part of the exploit... Just shutting up compiler warnings...
pragma solidity ^0.4.10;


contract Merdetoken {

    // ERC-20 constants
    string public constant name = "Merdetoken";
    string public constant symbol = "MDT";
    uint8 public constant decimals = 18;

    // The ICO owner
    address owner;

    // The token balance for each member
    mapping (address => uint256) balances;

    // Allowances for each member
    mapping(address => mapping (address => uint256)) allowed;

    // The total supply of these tokens
    uint256 _totalSupply;

    // The date that the presale ends and the token can be traded
    uint256 activationDate;

    // The last time the owner issued withdraw(uint256)
    uint256 lastWithdrawDate;

    /**
     *  constructor(uint256)
     *
     *  Create a new instance of the Merdetoken, with a given presale
     *  duration.
     */
    function Merdetoken(uint256 duration) payable {

        // Set the ICO owner
        owner = msg.sender;

        // The owner can buy their own tokens with an endowment (if desired)
        balances[msg.sender] = msg.value;
        _totalSupply = msg.value;

        // Set the ICO presale end date
        activationDate = now + duration;
    }

    /**
     *  withdraw(uint256)
     *
     *  After the presale, the ICO owner may withdraw Ether from the contract
     *  on a deferred schedule; once per week, the amount doubling each week.withdraw
     */
    function withdraw(uint256 amount) {

        // Cannot withdraw during the presale
        if (now < activationDate) { throw; }

        // Only the owner may withdraw funds
        if (msg.sender != owner) { throw; }

        // Only allow sane values to be sent
        if (amount == 0 || amount > this.balance) { throw; }

        // Can only withdraw once per week
        if (now < lastWithdrawDate + (1 weeks)) { throw; }
        lastWithdrawDate = now;

        // Deferred withdraw schedule; may only withdraw 1 more ether than has ever
        // been withdrawn. See top decription for schedule.
        uint256 maxAmount = (_totalSupply - this.balance + (1 ether));

        // Cannot withdraw more than the maximum allowed by schedule
        if (amount > maxAmount) { throw; }

        // Send the funds
        if (!owner.send(amount)) { throw; }
    }

    /**
     *  fallback function
     *
     *  Exchange Ether for MDT tokens. This may not be called once the presale has ended.
     */
    function () payable {

        // Can only buy tokens during the presale
        if (now >= activationDate) { throw; }

        // Give out the tokens
        balances[msg.sender] += msg.value;
        _totalSupply += msg.value;
    }


    /**
     *   ERC-20 token; nothing sinister below here (if there is, it wasn't intended)
     *   Mostly just copy and pasted from the de facto existing implmentation; changes marked
     */

     function totalSupply() constant returns (uint totalSupply) {
         return _totalSupply;
     }

    function balanceOf(address _owner) constant returns (uint256 balance) {
         return balances[_owner];
    }

    function transfer(address _to, uint256 _amount) returns (bool success) {

        // Cannot transfer tokens during the presale
        if (now < activationDate) { return false; }

        if (balances[msg.sender] < _amount || _amount == 0) { return false; }

        balances[msg.sender] -= _amount;
        balances[_to] += _amount;

        return true;
    }

    function transferFrom(address _from, address _to, uint256 _amount) returns (bool success) {

        // Cannot transfer tokens during the presale
        if (now < activationDate) { return false; }

        if (allowed[_from][msg.sender] < _amount || balances[_from] < _amount || _amount == 0) {
            return false;
        }

        balances[_from] -= _amount;
        allowed[_from][msg.sender] -= _amount;
        balances[_to] += _amount;

        return true;
    }

    function approve(address _spender, uint256 _amount) returns (bool success) {
        allowed[msg.sender][_spender] = _amount;
        return true;
    }
}

作弊方法

虽然明摆着有个提取限制,但是有没有可能突破这个限制,项目方直接提款跑路呢?有!

请大家看withdraw函数,最多能够提取的金额maxAmount是由_totalSupplythis.balance决定的。而这两者看似都是同时变化的(fallback函数和构造函数),即_totalSuppy>= this.balance不变式总是成立,然而事实不总是如此,有多种方法可以打破这个不变式。

方法1

通过提前计算contract的部署地址,往这个地址打一笔钱(prefund),使得this.balance>_totalSupply成立,那么计算maxAmount就会下溢出,项目方可以直接跳过限制,直接全部提款跑路。

提前计算contract的部署地址

根据黄皮书的section 8: message call中所示,一个contract的地址取决于sendersender' nonce(sender发送的tx数目).

The address of the new account is dened as being the rightmost 160 bits of the Keccak hash of the RLP encoding of the structure containing only the sender and the nonce. Thus we dene the resultant address for the new account a:

contract address的计算方式

下面是python代码:

# -*- coding: utf-8 -*-
from ethereum.utils import *
import sys

sender = sys.argv[1] #"0x003be5df5fef651ef0c59cd175c73ca1415f53ea";
nonce = encode_int(int(sys.argv[2]))
contract_address = mk_contract_address(sender, nonce)


print("sender: {}, nonce: {}, decode_addr: {}".format(sender, nonce,"0x" + decode_addr(contract_address)))

给一个不存在的ethereum地址转账会发什么?

还是根据黄皮书来找到答案。

Throughout the present work, it is assumed that if {$$\delta$$}1[r] was originally undefined, it will be created as an account with no code or state and zero balance and nonce.

(简书markdown不支持数学符号,将就着看吧)简单来说,如果接收方的recipient不存在,就创建一个。

测试

我先计算好要部署的contract的地址,通过MetaMask向其打一笔钱,然后在etherscan中可以发现,出现一笔交易,此时没有code选项卡。再通过remix部署合约,在etherscan中可以发现多了一个contract creation交易,此时code选项卡出现了。

0x5a4085C38dB8776Ff68F49BE8CDA143E0A0FCD13

方法2

其实上述方法还有不好的地方,就是可以让人看到在合约创建之前就有一笔交易。其实还要一个更隐蔽的方法,selfdestruct(address), 这个函数可以向某个地址打一笔钱,而且没有任何痕迹

结论

事实证明,类似上述代码的ICO都有操控的嫌疑。比如说, 通过计算好预期的市场价格,通过suicide向ICO地址打一笔钱,使得totalSupply不变,而筹得金额却增加,(提前)结束整个募集过程后,使得单个token的市场价格提高,达到预期的结果(疯狂割韭菜)。

最后要打赏的客官这边: 0x003be5df5fef651ef0c59cd175c73ca1415f53ea

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,634评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,951评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,427评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,770评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,835评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,799评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,768评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,544评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,979评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,271评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,427评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,121评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,756评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,375评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,579评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,410评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,315评论 2 352

推荐阅读更多精彩内容