Thank you for the comments. Here's a detailed step-through, along with the code that I used. It's not an exploit, but simply a message box.
I have a laptop with 32-bit XP Pro SP3 (not a VM). The software is OllyDbg v2.00.01, nasm and mingw.
1) Put the following code through nasm with <nasm msgbox.asm -o msgbox.bin>.
;msgbox.asm
;The addresses are for XP SP3
[SECTION .text]
BITS 32
global _start
_start:
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
jmp short GetLibrary
GetLibraryReturn:
pop ecx
mov [ecx + 10], dl ;insert the NULL
mov ebx, 0x7c801d7b ;LoadLibraryA address in kernel32.dll
push ecx
call ebx
jmp short FunctionName
FunctionReturn:
pop ecx
xor edx, edx
mov [ecx + 11],dl ;insert the NULL
push ecx
push eax
mov ebx, 0x7c80ae40 ;GetProcAddress address in kernel32.dll
call ebx
jmp short Message
MessageReturn:
pop ecx
xor edx, edx
mov [ecx+3],dl ;insert the NULL
push edx
push ecx
push ecx
push edx
call eax
ender:
xor edx, edx
push eax
mov eax, 0x7c81cb12 ;ExitProcess address in kernel32.dll
call eax
GetLibrary:
call GetLibraryReturn
db 'user32.dllN'
FunctionName:
call FunctionReturn
db 'MessageBoxAN'
Message:
call MessageReturn
db 'HeyN'
2) Extract the 105 bytes of NULL-free shellcode from msgbox.bin and put it into a C frame:
//msgbox.c
char code[]=
"\x31\xc0\x31\xdb\x31\xc9\x31\xd2"
"\xeb\x35\x59\x88\x51\x0a\xbb\x7b"
"\x1d\x80\x7c\x51\xff\xd3\xeb\x37"
"\x59\x31\xd2\x88\x51\x0b\x51\x50"
"\xbb\x40\xae\x80\x7c\xff\xd3\xeb"
"\x37\x59\x31\xd2\x88\x51\x03\x52"
"\x51\x51\x52\xff\xd0\x31\xd2\x50"
"\xb8\x12\xcb\x81\x7c\xff\xd0\xe8"
"\xc6\xff\xff\xff\x75\x73\x65\x72"
"\x33\x32\x2e\x64\x6c\x6c\x4e\xe8"
"\xc4\xff\xff\xff\x4d\x65\x73\x73"
"\x61\x67\x65\x42\x6f\x78\x41\x4e"
"\xe8\xc4\xff\xff\xff\x48\x65\x79"
"\x4e";
int main(int argc, char **argv)
{
int (*func)();
func = (int (*)()) code;
(int)(*func)();
}
3) Compile the C code with <gcc msgbox.c -o msgbox.exe>.
4) Run msgbox.exe from the command line and it pops up the message box, so everything's working as it should.
5) Open OllyDbg and load msgbox.exe. It show the Entry Point of the executable (PUSH EBP; MOV EBP, ESP; SUB ESP, 18 etc.).
AIMFind my code in OllyDbg (XOR EAX, EAX; XOR EBX, EBX; XOR ECX, ECX; XOR EDX, EDX etc.) then step through it whilst watching the registers, stack and memory.
I've tried some ideas:
a) put several int 3 immediately before the initial XOR EAX, EAX instruction
b) put "\xcc\xcc\xcc\xcc" at the top of my shellcode before compiling
c) put an instruction such as MOV EAX, 0ABAABA0 before the initial XOR EAX, EAX in the hope that I could search for that particular command (via ctrl-T in OllyDbg)
unfortunately, none of these has allowed me to find my code in OllyDbg. I'm starting to think that it's not possible so, if it's not, how do I access code that I want to debug? If it's very complicated, I'll NEED to be able to see exactly what's happening to the registers, stack and memory so I can correct any errors.
I hope that I've provided sufficient information and I apologise for such a lengthy post!