Closed Thread
Results 1 to 8 of 8

Thread: Mutating Code? (ASM w/ C++)

  1. #1
    RobotGymnast is offline Programmer
    Join Date
    Apr 2008
    Posts
    142
    Rep Power
    14

    Mutating Code? (ASM w/ C++)

    I tried to write a very simple mutating program the other day, but it keeps failing. Because my program uses addresses that are hardcoded in (I know, bad practice), you compiling the code would likely result in runtime errors anyway.

    Therefore, my question is: does anybody have an example of an extremely simple self-modifying ASM program? I'd also like to know if there's a way that doesn't involve hardcoded addresses.

    My program was basically:

    Code:
    unsigned int testvar = 0;
    __asm
    {
    write code that made testvar=0xFFFFFFFF;
    execute code;
    }
    cout << testvar << endl;
    I keep getting "read error of memory [0xFFFFFFFF]" when I try writing to the code segment (this is in OllyDBG)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: Mutating Code? (ASM w/ C++)

    If your goal is to do this in C++, you may want to check out Boost Lambda. You could also try having a scripting language interpreter.

    I haven't played with self-modifying code, however.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    RobotGymnast is offline Programmer
    Join Date
    Apr 2008
    Posts
    142
    Rep Power
    14

    Re: Mutating Code? (ASM w/ C++)

    My goal isn't to write self-modifying C++ code. Any code that modifies itself and runs will be in ASM. However, surrounding code might be in C++.

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,496
    Blog Entries
    8
    Rep Power
    58

    Re: Mutating Code? (ASM w/ C++)

    You can get the current address and then use offsets to write code into a buffer, then jump to the buffer.

    Code:
    call    next
    next:
    pop    eax
    ;address now in EAX. for 64-bit code, pop RAX.
    sudo rm -rf /

  6. #5
    RobotGymnast is offline Programmer
    Join Date
    Apr 2008
    Posts
    142
    Rep Power
    14

    Re: Mutating Code? (ASM w/ C++)

    Yes, that gives me my current address. But now, how do I write anything? Writing to the code segment results in a read error of address 0xFFFFFFFF

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,496
    Blog Entries
    8
    Rep Power
    58

    Re: Mutating Code? (ASM w/ C++)

    Then try writing to a data segment. Try using malloc or something. Operating systems typically block this sort of behavior for security reasons, by the way.
    sudo rm -rf /

  8. #7
    RobotGymnast is offline Programmer
    Join Date
    Apr 2008
    Posts
    142
    Rep Power
    14

    Re: Mutating Code? (ASM w/ C++)

    yeah, writing to the data segment worked fine. Then how do programs create and use code caves?

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,496
    Blog Entries
    8
    Rep Power
    58

    Re: Mutating Code? (ASM w/ C++)

    Um...no idea.
    sudo rm -rf /

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem in a href location from php code to html code
    By newphpcoder in forum PHP Development
    Replies: 5
    Last Post: 05-13-2011, 02:03 PM
  2. how to make a bar code reader program in VB 2008 please send me the code
    By tontonskie in forum Visual Basic Programming
    Replies: 1
    Last Post: 11-15-2010, 12:58 AM
  3. add code for font size under php echo code
    By newphpcoder in forum PHP Development
    Replies: 2
    Last Post: 11-10-2010, 11:03 PM
  4. Code: Capture Code from USB Camera
    By MrNobody in forum Visual Basic Tutorials
    Replies: 71
    Last Post: 08-14-2009, 08:59 AM
  5. Code: Capture Code from USB Camera
    By MrNobody in forum Tutorials
    Replies: 5
    Last Post: 09-08-2007, 06:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts