Detectando la arquitectura en Windows con ensamblador mediante registros de segmentación

Un viejo pero efectivo truco para detectar la arquitectura de un sistema es usar los registros de segmento CS, un método que usaba también el malware Kronos:
xor   eax,eax   
mov   ax,cs   
shr   eax,5   

En base a ésto, un cazador de bugs como Osanda Malith Jayathissa (@OsandaMalith) ha expuesto en su blog otras formas de obtener la arquitectura mediante los registros de segmento ES, GS y FS:

Usando ES
; Author : @OsandaMalith
main:
        xor eax,eax
        mov ax,es
        ror ax, 0x3
        and eax,0x1
        test eax, eax
        je thirtytwo
        invoke MessageBox,0, 'You are Running 64-bit', 'Architecture', MB_OK + MB_ICONINFORMATION
        jmp exit

thirtytwo:
        invoke MessageBox,0, 'You are Running 32-bit', 'Architecture', MB_OK + MB_ICONINFORMATION

exit:
        invoke ExitProcess, 0 

Usando GS
; Author : @OsandaMalith
main:
        xor eax, eax
        mov eax, gs
        test eax, eax
        je thirtytwo
        invoke MessageBox,0, 'You are Running 64-bit', 'Architecture', MB_OK + MB_ICONINFORMATION
        jmp exit

thirtytwo:
        invoke MessageBox,0, 'You are Running 32-bit', 'Architecture', MB_OK + MB_ICONINFORMATION

exit:
        invoke ExitProcess, 0

.end main    

Usando TEB

También podemos usar TEB (Win32 Thread Information Block) + 0xc0 la cuál es ‘WOW32Reserved’.


; Author : @OsandaMalith
main:
        xor eax, eax
        mov eax, [FS:0xc0]
        test eax, eax
        je thirtytwo
        invoke MessageBox,0, 'You are Running 64-bit', 'Architecture', MB_OK + MB_ICONINFORMATION
        jmp exit

thirtytwo:
        invoke MessageBox,0, 'You are Running 32-bit', 'Architecture', MB_OK + MB_ICONINFORMATION

exit:
        invoke ExitProcess, 0

.end main 
 

Y lo mejor, Osanda nos regala también un pequeño programa en C con todas estas técnicas para detectar la arquitectura, tremendamente útil para shellcoding:

#include <Windows.h>
#include <wchar.h>

/*
 * Author: Osanda Malith Jayathissa - @OsandaMalith
 * Website: https://osandamalith.com
 * Description: Few tricks that you can use to detect the architecture in Windows
 * Link :  http://osandamalith.com/2017/09/24/detecting-architecture-in-windows/
 */

BOOL detectArch_ES() {
#if defined(_MSC_VER)
    _asm {
        xor eax, eax
        mov ax, es
        ror ax, 0x3
        and eax, 0x1           
    }
#elif defined(__GNUC__)       
asm( 
        ".intel_syntax noprefix;"
        "xor eax, eax;"
        "mov ax, es;"
        "ror ax, 0x3;"
        "and eax, 0x1;"
           
    );
Detectando la arquitectura en Windows con ensamblador mediante registros de segmentación Detectando la arquitectura en Windows con ensamblador mediante registros de segmentación Reviewed by Zion3R on 20:01 Rating: 5