📚 OpenRCE is preserved as a read-only archive. Launched at RECon Montreal in 2005. Registration and posting are disabled.








Flag: Tornado! Hurricane!

Blogs >> JasonGeffner's Blog

Created: Friday, February 10 2006 08:45.00 CST Modified: Friday, February 10 2006 08:45.00 CST
This is an imported entry. View original. Printer Friendly ...
Using assembly buffers in C++ without using hex-strings
Author: JasonGeffner # Views: 1034

I find myself often using hex-strings of assembly instructions in C++ programs, for example, "xebx1fx5ex89x76x08x31xc0x88x46x07x89x46x0cxb0x0b" (snippet from http://www.phrack.org/phrack/49/P49-14, as a canonical example of shellcode). Such hex-strings can often be found in penetration-testing tools, as well as in code-injection tools.

 

I was working on creating a code-injection tool in C++ last night to help with my malware analysis work. Since the code that I needed to inject was a buffer of x86 assembly instructions, I used RTA to type up the assembly code, saved the file, opened it in my hex editor, copied the instructions as a hex-string, and pasted it into my injector project. I could have used HIEW or OllyDbg or something else instead of RTA; I could have even written the assembly code in an __asm{...} block in C++ and compiled it to get the instructions. However, all of these solutions required copying a hex-string back into my injector program. This gets even more annoying if I want to <gasp> update my assembly code!

I thought, "wouldnt it be nice if I could write the assembly code directly into my C++ program and be able to make use of that buffer without using any hex-strings?"

Well, I decided to implement a solution:

typedef struct _ASSEMBLY_BUFFER
{
    void* pBuffer;
    unsigned long ulSize;
} ASSEMBLY_BUFFER, *PASSEMBLY_BUFFER;

//
// Gets a pointer to the x86 assembly code buffer starting at the buffer_begin
// label. Also gets the size of the buffer.
//
void __fastcall GetAssemblyBuffer(PASSEMBLY_BUFFER)
{
    __asm
    {
        mov eax, offset buffer_begin ; Get address of first instruction in assembly
        mov [ecx], eax               ;  buffer and save it to .lpBuffer
        mov edx, offset buffer_end
        sub edx, eax                 ; Determine difference between beginning and end
        mov [ecx+4], edx             ;  of assembly buffer, and save it to .dwSize
    }
    return;

    __asm
    {
buffer_begin:

        <assembly code>       ; Our assembly code buffer

buffer_end:
    }
}

Figure 1. GetAssemblyBuffer(...) function and typedef.


We simply put our assembly code between the
buffer_begin and buffer_end labels, and can then use GetAssemblyBuffer(...) to access it.

Take the following program for example:

#include <stdio.h>

typedef struct _ASSEMBLY_BUFFER
{
    void* pBuffer;
    unsigned long ulSize;
} ASSEMBLY_BUFFER, *PASSEMBLY_BUFFER;

//
// Gets a pointer to the x86 assembly code buffer starting at the buffer_begin
// label. Also gets the size of the buffer.
//
void __fastcall GetAssemblyBuffer(PASSEMBLY_BUFFER)
{
    __asm
    {
        mov eax, offset buffer_begin ; Get address of first instruction in assembly
        mov [ecx], eax               ;  buffer and save it to .lpBuffer
        mov edx, offset buffer_end
        sub edx, eax                 ; Determine difference between beginning and end
        mov [ecx+4], edx             ;  of assembly buffer, and save it to .dwSize
    }
    return;

    __asm
    {
buffer_begin:

        mov eax, 15DBh        ; Our assembly code buffer
        rol eax, 13h
        xor eax, 0DEADBEEFh
        shr eax, 10h
        mov ebx, eax
        shl eax, 2
        add eax, ebx
        add eax, ebx
        add eax, ebx
        add eax, 4


buffer_end:
    }
}

int main(int argc, char** argv)
{
    ASSEMBLY_BUFFER asmbuf = {0};
    GetAssemblyBuffer(&asmbuf);

    printf("Assembly code buffer: ");

    for (unsigned long i = 0; i < asmbuf.ulSize; i++)
    {
        printf("\x%02x", ((
unsigned char*)asmbuf.pBuffer)[i]);
    }

    return 0;
}

Figure 2. Sample program that uses GetAssemblyBuffer(...).


The program above would output:

Assembly code buffer:
xb8xdbx15x00x00xc1xc0x13x35xefxbexadxdexc1xe8x10x8bxd8xc1xe0
x02x03xc3x03xc3x03xc3x83xc0x04

Figure 3. Output of sample program above.


With this functionality, we can now do things like
WriteProcessMemory(hProcess, lpBaseAddress, asmbuf.pBuffer, asmbuf.ulSize, lpNumberOfBytesWritten) or send(s, asmbuf.pBuffer, asmbuf.ulSize, flags) without having to paste any hex-strings into our C++ code.



If you wish to comment on this blog entry, please do so on the original site it was imported from.

There are 31,328 total registered users.


Recently Created Topics
[help] Unpacking VMP...
Mar/12
Reverse Engineering ...
Jul/06
let 'IDAPython' impo...
Sep/24
set 'IDAPython' as t...
Sep/24
GuessType return une...
Sep/20
About retrieving the...
Sep/07
How to find specific...
Aug/15
How to get data depe...
Jul/07
Identify RVA data in...
May/06
Question about memor...
Dec/12


Recent Forum Posts
Finding the procedur...
rolEYder
Question about debbu...
rolEYder
Identify RVA data in...
sohlow
let 'IDAPython' impo...
sohlow
How to find specific...
hackgreti
Problem with ollydbg
sh3dow
How can I write olly...
sh3dow
New LoadMAP plugin v...
mefisto...
Intel pin in loaded ...
djnemo
OOP_RE tool available?
Bl4ckm4n


Recent Blog Entries
halsten
Mar/14
Breaking IonCUBE VM

oleavr
Oct/24
Anatomy of a code tracer

hasherezade
Sep/24
IAT Patcher - new tool for ...

oleavr
Aug/27
CryptoShark: code tracer ba...

oleavr
Jun/25
Build a debugger in 5 minutes

More ...


Recent Blog Comments
nieo on:
Mar/22
IAT Patcher - new tool for ...

djnemo on:
Nov/17
Kernel debugger vs user mod...

acel on:
Nov/14
Kernel debugger vs user mod...

pedram on:
Dec/21
frida.github.io: scriptable...

capadleman on:
Jun/19
Using NtCreateThreadEx for ...

More ...


Imagery
SoySauce Blueprint
Jun 6, 2008

[+] expand

View Gallery (11) / Submit