# macOS Objective-C
ゼロからヒーローまでのAWSハッキングを学ぶ htARTE(HackTricks AWS Red Team Expert) HackTricksをサポートする他の方法: * **HackTricksで企業を宣伝したい**または**HackTricksをPDFでダウンロードしたい**場合は、[**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)をチェックしてください! * [**公式PEASS&HackTricksグッズ**](https://peass.creator-spring.com)を入手する * [**The PEASS Family**](https://opensea.io/collection/the-peass-family)を発見し、独占的な[**NFTs**](https://opensea.io/collection/the-peass-family)コレクションを見つける * **💬 [Discordグループ](https://discord.gg/hRep4RUj7f)**に参加するか、[telegramグループ](https://t.me/peass)に参加するか、**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)をフォローする * **HackTricks**と[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks)のGitHubリポジトリにPRを提出して、あなたのハッキングテクニックを共有する
## Objective-C {% hint style="danger" %} Objective-Cで書かれたプログラムは、[Mach-Oバイナリ](macos-files-folders-and-binaries/universal-binaries-and-mach-o-format.md)にコンパイルされるときに、クラス宣言を**保持**します。このようなクラス宣言には、以下が含まれます: * クラス * クラスメソッド * クラスのインスタンス変数 {% endhint %} [class-dump](https://github.com/nygard/class-dump)を使用して、この情報を取得できます: ```bash class-dump Kindle.app ``` ## クラス、メソッド&オブジェクト ### インターフェース、プロパティ&メソッド ```objectivec // Declare the interface of the class @interface MyVehicle : NSObject // Declare the properties @property NSString *vehicleType; @property int numberOfWheels; // Declare the methods - (void)startEngine; - (void)addWheels:(int)value; @end ``` ### **クラス** ```objectivec @implementation MyVehicle : NSObject // No need to indicate the properties, only define methods - (void)startEngine { NSLog(@"Engine started"); } - (void)addWheels:(int)value { self.numberOfWheels += value; } @end ``` ### **オブジェクトと呼び出しメソッド** クラスのインスタンスを作成するには、**`alloc`** メソッドが呼び出され、それぞれの**プロパティにメモリを割り当て**、その割り当てを**ゼロ**にします。その後、**`init`** が呼び出され、プロパティを**必要な値**で**初期化**します。 ```objectivec // Something like this: MyVehicle *newVehicle = [[MyVehicle alloc] init]; // Which is usually expressed as: MyVehicle *newVehicle = [MyVehicle new]; // To call a method // [myClassInstance nameOfTheMethodFirstParam:param1 secondParam:param2] [newVehicle addWheels:4]; ``` ### **クラスメソッド** クラスメソッドは、インスタンスメソッドで使用されるハイフン(-)ではなく、**プラス記号(+)**で定義されます。例えば、**NSString** クラスの **`stringWithString`** メソッド: ```objectivec + (id)stringWithString:(NSString *)aString; ``` ### Setter & Getter プロパティを**設定**&**取得**するには、**ドット表記**または**メソッドを呼び出す**かのように行うことができます: ```objectivec // Set newVehicle.numberOfWheels = 2; [newVehicle setNumberOfWheels:3]; // Get NSLog(@"Number of wheels: %i", newVehicle.numberOfWheels); NSLog(@"Number of wheels: %i", [newVehicle numberOfWheels]); ``` ### **インスタンス変数** セッターとゲッターメソッドの代わりに、インスタンス変数を使用することもできます。これらの変数は、プロパティと同じ名前を持ちますが、"\_"から始まります: ```objectivec - (void)makeLongTruck { _numberOfWheels = +10000; NSLog(@"Number of wheels: %i", self.numberOfLeaves); } ``` ### プロトコル プロトコルはメソッド宣言のセットです(プロパティなし)。プロトコルを実装するクラスは、宣言されたメソッドを実装します。 メソッドには**必須**と**オプション**の2種類があります。**デフォルト**ではメソッドは**必須**です(`@required`タグで示すこともできます)。メソッドがオプションであることを示すには、**`@optional`**を使用します。 ```objectivec @protocol myNewProtocol - (void) method1; //mandatory @required - (void) method2; //mandatory @optional - (void) method3; //optional @end ``` ### すべて一緒 ```objectivec // gcc -framework Foundation test_obj.m -o test_obj #import @protocol myVehicleProtocol - (void) startEngine; //mandatory @required - (void) addWheels:(int)value; //mandatory @optional - (void) makeLongTruck; //optional @end @interface MyVehicle : NSObject @property int numberOfWheels; - (void)startEngine; - (void)addWheels:(int)value; - (void)makeLongTruck; @end @implementation MyVehicle : NSObject - (void)startEngine { NSLog(@"Engine started"); } - (void)addWheels:(int)value { self.numberOfWheels += value; } - (void)makeLongTruck { _numberOfWheels = +10000; NSLog(@"Number of wheels: %i", self.numberOfWheels); } @end int main() { MyVehicle* mySuperCar = [MyVehicle new]; [mySuperCar startEngine]; mySuperCar.numberOfWheels = 4; NSLog(@"Number of wheels: %i", mySuperCar.numberOfWheels); [mySuperCar setNumberOfWheels:3]; NSLog(@"Number of wheels: %i", mySuperCar.numberOfWheels); [mySuperCar makeLongTruck]; } ``` ### 基本クラス #### 文字列 {% code overflow="wrap" %} ```objectivec // NSString NSString *bookTitle = @"The Catcher in the Rye"; NSString *bookAuthor = [[NSString alloc] initWithCString:"J.D. Salinger" encoding:NSUTF8StringEncoding]; NSString *bookPublicationYear = [NSString stringWithCString:"1951" encoding:NSUTF8StringEncoding]; ``` {% endcode %} 基本的なクラスは**不変**なので、既存の文字列に文字列を追加するには**新しい NSString を作成する必要があります**。 {% code overflow="wrap" %} ```objectivec NSString *bookDescription = [NSString stringWithFormat:@"%@ by %@ was published in %@", bookTitle, bookAuthor, bookPublicationYear]; ``` {% endcode %} または、**mutable**文字列クラスを使用することもできます: {% code overflow="wrap" %} ```objectivec NSMutableString *mutableString = [NSMutableString stringWithString:@"The book "]; [mutableString appendString:bookTitle]; [mutableString appendString:@" was written by "]; [mutableString appendString:bookAuthor]; [mutableString appendString:@" and published in "]; [mutableString appendString:bookPublicationYear]; ``` #### 数値 {% code overflow="wrap" %} ```objectivec // character literals. NSNumber *theLetterZ = @'Z'; // equivalent to [NSNumber numberWithChar:'Z'] // integral literals. NSNumber *fortyTwo = @42; // equivalent to [NSNumber numberWithInt:42] NSNumber *fortyTwoUnsigned = @42U; // equivalent to [NSNumber numberWithUnsignedInt:42U] NSNumber *fortyTwoLong = @42L; // equivalent to [NSNumber numberWithLong:42L] NSNumber *fortyTwoLongLong = @42LL; // equivalent to [NSNumber numberWithLongLong:42LL] // floating point literals. NSNumber *piFloat = @3.141592654F; // equivalent to [NSNumber numberWithFloat:3.141592654F] NSNumber *piDouble = @3.1415926535; // equivalent to [NSNumber numberWithDouble:3.1415926535] // BOOL literals. NSNumber *yesNumber = @YES; // equivalent to [NSNumber numberWithBool:YES] NSNumber *noNumber = @NO; // equivalent to [NSNumber numberWithBool:NO] ``` #### 配列、セット、およびディクショナリ {% endcode %} ```objectivec // Inmutable arrays NSArray *colorsArray1 = [NSArray arrayWithObjects:@"red", @"green", @"blue", nil]; NSArray *colorsArray2 = @[@"yellow", @"cyan", @"magenta"]; NSArray *colorsArray3 = @[firstColor, secondColor, thirdColor]; // Mutable arrays NSMutableArray *mutColorsArray = [NSMutableArray array]; [mutColorsArray addObject:@"red"]; [mutColorsArray addObject:@"green"]; [mutColorsArray addObject:@"blue"]; [mutColorsArray addObject:@"yellow"]; [mutColorsArray replaceObjectAtIndex:0 withObject:@"purple"]; // Inmutable Sets NSSet *fruitsSet1 = [NSSet setWithObjects:@"apple", @"banana", @"orange", nil]; NSSet *fruitsSet2 = [NSSet setWithArray:@[@"apple", @"banana", @"orange"]]; // Mutable sets NSMutableSet *mutFruitsSet = [NSMutableSet setWithObjects:@"apple", @"banana", @"orange", nil]; [mutFruitsSet addObject:@"grape"]; [mutFruitsSet removeObject:@"apple"]; // Dictionary NSDictionary *fruitColorsDictionary = @{ @"apple" : @"red", @"banana" : @"yellow", @"orange" : @"orange", @"grape" : @"purple" }; // In dictionaryWithObjectsAndKeys you specify the value and then the key: NSDictionary *fruitColorsDictionary2 = [NSDictionary dictionaryWithObjectsAndKeys: @"red", @"apple", @"yellow", @"banana", @"orange", @"orange", @"purple", @"grape", nil]; // Mutable dictionary NSMutableDictionary *mutFruitColorsDictionary = [NSMutableDictionary dictionaryWithDictionary:fruitColorsDictionary]; [mutFruitColorsDictionary setObject:@"green" forKey:@"apple"]; [mutFruitColorsDictionary removeObjectForKey:@"grape"]; ``` ### ブロック ブロックはオブジェクトとして振る舞う関数のことで、関数に渡したり、配列や辞書に格納したりできます。また、値が与えられた場合には値を表すことができるため、ラムダに似ています。 ```objectivec returnType (^blockName)(argumentType1, argumentType2, ...) = ^(argumentType1 param1, argumentType2 param2, ...){ //Perform operations here }; // For example int (^suma)(int, int) = ^(int a, int b){ return a+b; }; NSLog(@"3+4 = %d", suma(3,4)); ``` {% endcode %} **関数で使用するためにブロックタイプを定義することも可能です:** ```objectivec // Define the block type typedef void (^callbackLogger)(void); // Create a bloack with the block type callbackLogger myLogger = ^{ NSLog(@"%@", @"This is my block"); }; // Use it inside a function as a param void genericLogger(callbackLogger blockParam) { NSLog(@"%@", @"This is my function"); blockParam(); } genericLogger(myLogger); // Call it inline genericLogger(^{ NSLog(@"%@", @"This is my second block"); }); ``` ### ファイル {% code overflow="wrap" %} ```objectivec // Manager to manage files NSFileManager *fileManager = [NSFileManager defaultManager]; // Check if file exists: if ([fileManager fileExistsAtPath:@"/path/to/file.txt" ] == YES) { NSLog (@"File exists"); } // copy files if ([fileManager copyItemAtPath: @"/path/to/file1.txt" toPath: @"/path/to/file2.txt" error:nil] == YES) { NSLog (@"Copy successful"); } // Check if the content of 2 files match if ([fileManager contentsEqualAtPath:@"/path/to/file1.txt" andPath:@"/path/to/file2.txt"] == YES) { NSLog (@"File contents match"); } // Delete file if ([fileManager removeItemAtPath:@"/path/to/file1.txt" error:nil]) { NSLog(@"Removed successfully"); } ``` {% endcode %} **`NSString`** オブジェクトの代わりに **`NSURL`** オブジェクトを使用してファイルを管理することも可能です。メソッド名は類似していますが、**`Path`** の代わりに **`URL`** が使われています。 ```objectivec NSURL *fileSrc = [NSURL fileURLWithPath:@"/path/to/file1.txt"]; NSURL *fileDst = [NSURL fileURLWithPath:@"/path/to/file2.txt"]; [fileManager moveItemAtURL:fileSrc toURL:fileDst error: nil]; ``` ほとんどの基本クラスには、直接ファイルに書き込むことができる`writeToFile: atomically: encoding: error:nil`メソッドが定義されています: {% code overflow="wrap" %} ```objectivec NSString* tmp = @"something temporary"; [tmp writeToFile:@"/tmp/tmp1.txt" atomically:YES encoding:NSASCIIStringEncoding error:nil]; ``` {% endcode %}
htARTE(HackTricks AWS Red Team Expert)でAWSハッキングをゼロからヒーローまで学ぶ HackTricksをサポートする他の方法: * **HackTricksで企業を宣伝したい**または**HackTricksをPDFでダウンロードしたい**場合は、[**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)をチェックしてください! * [**公式PEASS&HackTricksスワッグ**](https://peass.creator-spring.com)を入手する * [**The PEASS Family**](https://opensea.io/collection/the-peass-family)を発見し、独占的な[**NFTs**](https://opensea.io/collection/the-peass-family)コレクションを見つける * **💬 [**Discordグループ**](https://discord.gg/hRep4RUj7f)または[**telegramグループ**](https://t.me/peass)に参加するか、**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)で**フォロー**する。 * **ハッキングトリックを共有するために、[**HackTricks**](https://github.com/carlospolop/hacktricks)と[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud)のGitHubリポジトリにPRを提出する。**