Objects in memory
Last updated
Last updated
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
CF* objects come from CoreFOundation, which provides more than 50 classes of objects like CFString
, CFNumber
or CFAllocatior
.
All these clases are instances of the class CFRuntimeClass
, which when called it returns an index to the __CFRuntimeClassTable
. The CFRuntimeClass is defined in CFRuntime.h:
Most of the data used by ObjectiveC runtime will change during the execution, therefore it uses some sections from the __DATA segment in memory:
__objc_msgrefs
(message_ref_t
): Message references
__objc_ivar
(ivar
): Instance variables
__objc_data
(...
): Mutable data
__objc_classrefs
(Class
): Class references
__objc_superrefs
(Class
): Superclass references
__objc_protorefs
(protocol_t *
): Protocol references
__objc_selrefs
(SEL
): Selector references
__objc_const
(...
): Class r/o
data and other (hopefully) constant data
__objc_imageinfo
(version, flags
): Used during image load: Version currently 0
; Flags specify preoptimized GC support, etc.
__objc_protolist
(protocol_t *
): Protocol list
__objc_nlcatlist
(category_t
): Pointer to Non-Lazy Categories defined in this binary
__objc_catlist
(category_t
): Pointer to Categories defined in this binary
__objc_nlclslist
(classref_t
): Pointer to Non-Lazy Objective-C classes defined in this binary
__objc_classlist
(classref_t
): Pointers to all Objective-C classes defined in this binary
It also uses a few sections in the __TEXT
segment to store constan values of it's not possible to write in this section:
__objc_methname
(C-String): Method names
__objc_classname
(C-String): Class names
__objc_methtype
(C-String): Method types
Objective-c uses some mangling to encode selector and variable types of simple and complex types:
Primitive types use their first letter of the type i
for int
, c
for char
, l
for long
... and uses the capital letter in case it's unsigned (L
for unsigned Long
).
Other data types whose letters are used or are special, use other letters or symbols like q
for long long
, b
for bitfields
, B
for booleans
, #
for classes
, @
for id
, *
for char pointers
, ^
for generic pointers
and ?
for undefined
.
Arrays, structures and unions use [
, {
and (
The selector would be processString:withOptions:andError:
id
is encoded as @
char *
is encoded as *
The complete type encoding for the method is:
Return Type (NSString *
): Encoded as @
with length 24
self
(object instance): Encoded as @
, at offset 0
_cmd
(selector): Encoded as :
, at offset 8
First argument (char * input
): Encoded as *
, at offset 16
Second argument (NSDictionary * options
): Encoded as @
, at offset 20
Third argument (NSError ** error
): Encoded as ^@
, at offset 24
With the selector + the encoding you can reconstruct the method.
Clases in Objective-C is a struct with properties, method pointers... It's possible to find the struct objc_class
in the source code:
This class use some bits of the isa field to indicate some information about the class.
Then, the struct has a pointer to the struct class_ro_t
stored on disk which contains attributes of the class like its name, base methods, properties and instance variables.
During runtime and additional structure class_rw_t
is used containing pointers which can be altered such as methods, protocols, properties...
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)