Objects in memory

Aprenda e pratique Hacking AWS: HackTricks Treinamento AWS Red Team Expert (ARTE) Aprenda e pratique Hacking GCP: HackTricks Treinamento GCP Red Team Expert (GRTE)

Apoie o HackTricks

CFRuntimeClass

Objetos CF* vêm do CoreFoundation, que fornece mais de 50 classes de objetos como CFString, CFNumber ou CFAllocatior.

Todas essas classes são instâncias da classe CFRuntimeClass, que quando chamada retorna um índice para a __CFRuntimeClassTable. O CFRuntimeClass é definido em CFRuntime.h:

// Some comments were added to the original code

enum { // Version field constants
_kCFRuntimeScannedObject =     (1UL << 0),
_kCFRuntimeResourcefulObject = (1UL << 2),  // tells CFRuntime to make use of the reclaim field
_kCFRuntimeCustomRefCount =    (1UL << 3),  // tells CFRuntime to make use of the refcount field
_kCFRuntimeRequiresAlignment = (1UL << 4),  // tells CFRuntime to make use of the requiredAlignment field
};

typedef struct __CFRuntimeClass {
CFIndex version;  // This is made a bitwise OR with the relevant previous flags

const char *className; // must be a pure ASCII string, nul-terminated
void (*init)(CFTypeRef cf);  // Initializer function
CFTypeRef (*copy)(CFAllocatorRef allocator, CFTypeRef cf); // Copy function, taking CFAllocatorRef and CFTypeRef to copy
void (*finalize)(CFTypeRef cf); // Finalizer function
Boolean (*equal)(CFTypeRef cf1, CFTypeRef cf2); // Function to be called by CFEqual()
CFHashCode (*hash)(CFTypeRef cf); // Function to be called by CFHash()
CFStringRef (*copyFormattingDesc)(CFTypeRef cf, CFDictionaryRef formatOptions); // Provides a CFStringRef with a textual description of the object// return str with retain
CFStringRef (*copyDebugDesc)(CFTypeRef cf);	// CFStringRed with textual description of the object for CFCopyDescription

#define CF_RECLAIM_AVAILABLE 1
void (*reclaim)(CFTypeRef cf); // Or in _kCFRuntimeResourcefulObject in the .version to indicate this field should be used
// It not null, it's called when the last reference to the object is released

#define CF_REFCOUNT_AVAILABLE 1
// If not null, the following is called when incrementing or decrementing reference count
uint32_t (*refcount)(intptr_t op, CFTypeRef cf); // Or in _kCFRuntimeCustomRefCount in the .version to indicate this field should be used
// this field must be non-NULL when _kCFRuntimeCustomRefCount is in the .version field
// - if the callback is passed 1 in 'op' it should increment the 'cf's reference count and return 0
// - if the callback is passed 0 in 'op' it should return the 'cf's reference count, up to 32 bits
// - if the callback is passed -1 in 'op' it should decrement the 'cf's reference count; if it is now zero, 'cf' should be cleaned up and deallocated (the finalize callback above will NOT be called unless the process is running under GC, and CF does not deallocate the memory for you; if running under GC, finalize should do the object tear-down and free the object memory); then return 0
// remember to use saturation arithmetic logic and stop incrementing and decrementing when the ref count hits UINT32_MAX, or you will have a security bug
// remember that reference count incrementing/decrementing must be done thread-safely/atomically
// objects should be created/initialized with a custom ref-count of 1 by the class creation functions
// do not attempt to use any bits within the CFRuntimeBase for your reference count; store that in some additional field in your CF object

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#define CF_REQUIRED_ALIGNMENT_AVAILABLE 1
// If not 0, allocation of object must be on this boundary
uintptr_t requiredAlignment; // Or in _kCFRuntimeRequiresAlignment in the .version field to indicate this field should be used; the allocator to _CFRuntimeCreateInstance() will be ignored in this case; if this is less than the minimum alignment the system supports, you'll get higher alignment; if this is not an alignment the system supports (e.g., most systems will only support powers of two, or if it is too high), the result (consequences) will be up to CF or the system to decide

} CFRuntimeClass;

Objective-C

Seções de memória utilizadas

A maioria dos dados utilizados pelo tempo de execução do ObjectiveC mudará durante a execução, portanto ele utiliza algumas seções do segmento __DATA na memória:

  • __objc_msgrefs (message_ref_t): Referências de mensagem

  • __objc_ivar (ivar): Variáveis de instância

  • __objc_data (...): Dados mutáveis

  • __objc_classrefs (Class): Referências de classe

  • __objc_superrefs (Class): Referências de superclasse

  • __objc_protorefs (protocol_t *): Referências de protocolo

  • __objc_selrefs (SEL): Referências de seletor

  • __objc_const (...): Dados da classe r/o e outros dados (esperançosamente) constantes

  • __objc_imageinfo (versão, flags): Usado durante o carregamento da imagem: Versão atualmente 0; Flags especificam suporte pré-otimizado para GC, etc.

  • __objc_protolist (protocol_t *): Lista de protocolo

  • __objc_nlcatlist (category_t): Ponteiro para Categorias Não-Lazy definidas neste binário

  • __objc_catlist (category_t): Ponteiro para Categorias definidas neste binário

  • __objc_nlclslist (classref_t): Ponteiro para classes Objective-C Não-Lazy definidas neste binário

  • __objc_classlist (classref_t): Ponteiros para todas as classes Objective-C definidas neste binário

Também utiliza algumas seções no segmento __TEXT para armazenar valores constantes se não for possível escrever nesta seção:

  • __objc_methname (C-String): Nomes de método

  • __objc_classname (C-String): Nomes de classe

  • __objc_methtype (C-String): Tipos de método

Codificação de Tipo

Objective-C utiliza algumas manipulações para codificar seletores e tipos de variáveis de tipos simples e complexos:

  • Tipos primitivos usam a primeira letra do tipo i para int, c para char, l para long... e usa a letra maiúscula no caso de ser não assinado (L para unsigned Long).

  • Outros tipos de dados cujas letras são usadas ou são especiais, usam outras letras ou símbolos como q para long long, b para bitfields, B para booleans, # para classes, @ para id, * para ponteiros de char, ^ para ponteiros genéricos e ? para indefinido.

  • Arrays, estruturas e uniões usam [, { e (

Exemplo de Declaração de Método

- (NSString *)processString:(id)input withOptions:(char *)options andError:(id)error;

O seletor seria processString:withOptions:andError:

Codificação de Tipo

  • id é codificado como @

  • char * é codificado como *

A codificação de tipo completa para o método é:

@24@0:8@16*20^@24

Análise Detalhada

  1. Tipo de Retorno (NSString *): Codificado como @ com comprimento 24

  2. self (instância do objeto): Codificado como @, no deslocamento 0

  3. _cmd (seletor): Codificado como :, no deslocamento 8

  4. Primeiro argumento (char * input): Codificado como *, no deslocamento 16

  5. Segundo argumento (NSDictionary * options): Codificado como @, no deslocamento 20

  6. Terceiro argumento (NSError ** error): Codificado como ^@, no deslocamento 24

Com o seletor + a codificação, é possível reconstruir o método.

Classes

Classes em Objective-C são uma struct com propriedades, ponteiros de métodos... É possível encontrar a struct objc_class no código-fonte:

struct objc_class : objc_object {
// Class ISA;
Class superclass;
cache_t cache;             // formerly cache pointer and vtable
class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags

class_rw_t *data() {
return bits.data();
}
void setData(class_rw_t *newData) {
bits.setData(newData);
}

void setInfo(uint32_t set) {
assert(isFuture()  ||  isRealized());
data()->setFlags(set);
}
[...]

Esta classe usa alguns bits do campo isa para indicar algumas informações sobre a classe.

Em seguida, a struct tem um ponteiro para a struct class_ro_t armazenada no disco que contém atributos da classe como seu nome, métodos base, propriedades e variáveis de instância. Durante a execução, uma estrutura adicional class_rw_t é usada contendo ponteiros que podem ser alterados, como métodos, protocolos, propriedades...

Last updated