Bellekteki Nesneler
Reading time: 6 minutes
tip
AWS Hacking'i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking'i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE)
HackTricks'i Destekleyin
- abonelik planlarını kontrol edin!
- Bize katılın 💬 Discord grubuna veya telegram grubuna veya bizi takip edin Twitter'da 🐦 @hacktricks_live.
- Hacking ipuçlarını paylaşın, HackTricks ve HackTricks Cloud github reposuna PR göndererek.
CFRuntimeClass
CF* nesneleri CoreFoundation'dan gelir ve CFString
, CFNumber
veya CFAllocator
gibi 50'den fazla nesne sınıfı sağlar.
Tüm bu sınıflar, çağrıldığında __CFRuntimeClassTable
'a bir indeks döndüren CFRuntimeClass
sınıfının örnekleridir. CFRuntimeClass, CFRuntime.h dosyasında tanımlanmıştır:
// 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
Bellek bölümleri
ObjectiveC çalışma zamanı tarafından kullanılan verilerin çoğu yürütme sırasında değişecektir, bu nedenle bellekteki __DATA segmentinden bazı bölümleri kullanır:
__objc_msgrefs
(message_ref_t
): Mesaj referansları__objc_ivar
(ivar
): Örnek değişkenleri__objc_data
(...
): Değişken veri__objc_classrefs
(Class
): Sınıf referansları__objc_superrefs
(Class
): Üst sınıf referansları__objc_protorefs
(protocol_t *
): Protokol referansları__objc_selrefs
(SEL
): Seçici referansları__objc_const
(...
): Sınıfr/o
verisi ve diğer (umarım) sabit veriler__objc_imageinfo
(version, flags
): Görüntü yükleme sırasında kullanılır: Mevcut sürüm0
; Bayraklar önceden optimize edilmiş GC desteğini belirtir, vb.__objc_protolist
(protocol_t *
): Protokol listesi__objc_nlcatlist
(category_t
): Bu ikili dosyada tanımlanan Tembel Olmayan Kategorilere işaretçi__objc_catlist
(category_t
): Bu ikili dosyada tanımlanan Kategorilere işaretçi__objc_nlclslist
(classref_t
): Bu ikili dosyada tanımlanan Tembel Olmayan Objective-C sınıflarına işaretçi__objc_classlist
(classref_t
): Bu ikili dosyada tanımlanan tüm Objective-C sınıflarına işaretçiler
Ayrıca, bu bölümde yazmanın mümkün olmadığı sabit değerleri depolamak için __TEXT
segmentinde birkaç bölüm kullanır:
__objc_methname
(C-String): Metot adları__objc_classname
(C-String): Sınıf adları__objc_methtype
(C-String): Metot türleri
Tür Kodlaması
Objective-C, basit ve karmaşık türlerin seçici ve değişken türlerini kodlamak için bazı karıştırmalar kullanır:
- Temel türler, türün ilk harfini kullanır
i
içinint
,c
içinchar
,l
içinlong
... ve işareti varsa büyük harf kullanır (L
içinunsigned Long
). - Harfleri kullanılan veya özel olan diğer veri türleri,
q
içinlong long
,b
içinbitfields
,B
içinbooleans
,#
içinclasses
,@
içinid
,*
içinchar pointers
,^
için genelpointers
ve?
içinundefined
gibi diğer harfler veya semboller kullanır. - Diziler, yapılar ve birleşimler
[
,{
ve(
kullanır.
Örnek Metot Bildirimi
- (NSString *)processString:(id)input withOptions:(char *)options andError:(id)error;
Seçici processString:withOptions:andError:
olacaktır.
Tür Kodlaması
id
@
olarak kodlanırchar *
*
olarak kodlanır
Yöntem için tam tür kodlaması:
@24@0:8@16*20^@24
Detaylı Analiz
- Dönüş Tipi (
NSString *
):@
ile kodlanmış, uzunluk 24 self
(nesne örneği):@
ile kodlanmış, ofset 0_cmd
(seçici)::
ile kodlanmış, ofset 8- İlk argüman (
char * input
):*
ile kodlanmış, ofset 16 - İkinci argüman (
NSDictionary * options
):@
ile kodlanmış, ofset 20 - Üçüncü argüman (
NSError ** error
):^@
ile kodlanmış, ofset 24
Seçici + kodlama ile metodu yeniden oluşturabilirsiniz.
Sınıflar
Objective-C'deki sınıflar, özellikler, metod işaretçileri ile bir yapıdadır... objc_class
yapısını kaynak kodda bulmak mümkündür:
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);
}
[...]
Bu sınıf, sınıf hakkında bazı bilgileri belirtmek için isa alanının bazı bitlerini kullanır.
Daha sonra, yapı, sınıfın adı, temel yöntemleri, özellikleri ve örnek değişkenleri gibi sınıfın niteliklerini içeren disk üzerinde saklanan class_ro_t
yapısına bir işaretçi içerir.
Çalışma zamanında, yöntemler, protokoller, özellikler gibi değiştirilebilen işaretçileri içeren ek bir yapı class_rw_t
kullanılır.
tip
AWS Hacking'i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking'i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE)
HackTricks'i Destekleyin
- abonelik planlarını kontrol edin!
- Bize katılın 💬 Discord grubuna veya telegram grubuna veya bizi takip edin Twitter'da 🐦 @hacktricks_live.
- Hacking ipuçlarını paylaşın, HackTricks ve HackTricks Cloud github reposuna PR göndererek.