メモリ内のオブジェクト
Reading time: 9 minutes
tip
AWSハッキングを学び、実践する:HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
CFRuntimeClass
CF* オブジェクトは CoreFoundation から来ており、CFString
、CFNumber
、または CFAllocator
のような 50 以上のクラスのオブジェクトを提供します。
これらのすべてのクラスは CFRuntimeClass
のインスタンスであり、呼び出されると __CFRuntimeClassTable
へのインデックスを返します。CFRuntimeClass は 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
メモリセクションの使用
ObjectiveCランタイムによって使用されるデータのほとんどは、実行中に変更されるため、メモリ内の**__DATA**セグメントからいくつかのセクションを使用します:
__objc_msgrefs
(message_ref_t
): メッセージ参照__objc_ivar
(ivar
): インスタンス変数__objc_data
(...
): 可変データ__objc_classrefs
(Class
): クラス参照__objc_superrefs
(Class
): スーパークラス参照__objc_protorefs
(protocol_t *
): プロトコル参照__objc_selrefs
(SEL
): セレクタ参照__objc_const
(...
): クラスのr/o
データおよびその他の(おそらく)定数データ__objc_imageinfo
(version, flags
): 画像のロード中に使用されます:現在のバージョンは0
;フラグは最適化されたGCサポートなどを指定します。__objc_protolist
(protocol_t *
): プロトコルリスト__objc_nlcatlist
(category_t
): このバイナリで定義された非遅延カテゴリへのポインタ__objc_catlist
(category_t
): このバイナリで定義されたカテゴリへのポインタ__objc_nlclslist
(classref_t
): このバイナリで定義された非遅延Objective-Cクラスへのポインタ__objc_classlist
(classref_t
): このバイナリで定義されたすべてのObjective-Cクラスへのポインタ
また、定数値を格納するために**__TEXT
**セグメントのいくつかのセクションも使用します。このセクションに書き込むことができない場合:
__objc_methname
(C-String): メソッド名__objc_classname
(C-String): クラス名__objc_methtype
(C-String): メソッドタイプ
タイプエンコーディング
Objective-Cは、セレクタと変数の型を単純および複雑な型でエンコードするためにいくつかのマングリングを使用します:
- プリミティブ型は、型の最初の文字を使用します。
i
はint
、c
はchar
、l
はlong
... そして符号なしの場合は大文字を使用します(L
はunsigned Long
)。 - 他のデータ型の文字が使用されているか特別な場合は、
q
はlong long
、b
はbitfields
、B
はbooleans
、#
はclasses
、@
はid
、*
はchar pointers
、^
は一般的なpointers
、?
はundefined
のように他の文字や記号を使用します。 - 配列、構造体、共用体は
[
、{
、(
を使用します。
例 メソッド宣言
- (NSString *)processString:(id)input withOptions:(char *)options andError:(id)error;
セレクタは processString:withOptions:andError:
です。
タイプエンコーディング
id
は@
としてエンコードされます。char *
は*
としてエンコードされます。
メソッドの完全なタイプエンコーディングは:
@24@0:8@16*20^@24
詳細な内訳
- 戻り値の型 (
NSString *
):@
でエンコードされ、長さは 24 self
(オブジェクトインスタンス):@
でエンコードされ、オフセット 0_cmd
(セレクタ)::
でエンコードされ、オフセット 8- 最初の引数 (
char * input
):*
でエンコードされ、オフセット 16 - 2 番目の引数 (
NSDictionary * options
):@
でエンコードされ、オフセット 20 - 3 番目の引数 (
NSError ** error
):^@
でエンコードされ、オフセット 24
セレクタとエンコーディングを使ってメソッドを再構築できます。
クラス
Objective-C のクラスはプロパティ、メソッドポインタを持つ構造体です... 構造体 objc_class
を ソースコード で見つけることができます:
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);
}
[...]
このクラスは、クラスに関する情報を示すためにisaフィールドのいくつかのビットを使用します。
次に、構造体はディスクに保存されたclass_ro_t
構造体へのポインタを持ち、クラスの名前、基本メソッド、プロパティ、およびインスタンス変数などの属性を含みます。
実行時には、メソッド、プロトコル、プロパティなどの変更可能なポインタを含む追加の構造体class_rw_t
が使用されます。
tip
AWSハッキングを学び、実践する:HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。