+ Reply to Thread
Results 1 to 8 of 8

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

  1. #1
    Programmer RobotGymnast is on a distinguished road
    Join Date
    Apr 2008
    Posts
    142

    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. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,651
    Blog Entries
    57

    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Programmer RobotGymnast is on a distinguished road
    Join Date
    Apr 2008
    Posts
    142

    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++.

  4. #4
    Code Warrior dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta's Avatar
    Join Date
    Oct 2007
    Age
    19
    Posts
    2,827
    Blog Entries
    8

    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.
    dargueta@dargueta-laptop:~$ sudo rm -rf /media/windows-partition

  5. #5
    Programmer RobotGymnast is on a distinguished road
    Join Date
    Apr 2008
    Posts
    142

    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

  6. #6
    Code Warrior dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta's Avatar
    Join Date
    Oct 2007
    Age
    19
    Posts
    2,827
    Blog Entries
    8

    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.
    dargueta@dargueta-laptop:~$ sudo rm -rf /media/windows-partition

  7. #7
    Programmer RobotGymnast is on a distinguished road
    Join Date
    Apr 2008
    Posts
    142

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

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

  8. #8
    Code Warrior dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta has much to be proud of dargueta's Avatar
    Join Date
    Oct 2007
    Age
    19
    Posts
    2,827
    Blog Entries
    8

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

    Um...no idea.
    dargueta@dargueta-laptop:~$ sudo rm -rf /media/windows-partition

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Replies: 0
    Last Post: 02-22-2009, 10:40 AM
  2. Replies: 0
    Last Post: 12-21-2008, 06:20 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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