2023-09-11 16:06:08 +00:00
# macOS Objective-C
2023-05-08 22:07:46 +00:00
< details >
2023-12-31 01:15:16 +00:00
< summary > < strong > AWSハッキングをゼロからヒーローまで学ぶ< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > ! < / strong > < / summary >
2023-05-08 22:07:46 +00:00
2023-12-31 01:15:16 +00:00
HackTricksをサポートする他の方法:
* **HackTricksにあなたの会社を広告したい**、または**HackTricksをPDFでダウンロードしたい**場合は、[**サブスクリプションプラン** ](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 )や[**テレグラムグループ** ](https://t.me/peass )に**参加する**か、**Twitter** 🐦 [**@carlospolopm** ](https://twitter.com/carlospolopm )で**フォローする**。
* [**HackTricks** ](https://github.com/carlospolop/hacktricks )と[**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud )のgithubリポジトリにPRを提出して、あなたのハッキングテクニックを共有する。
2023-05-08 22:07:46 +00:00
< / details >
2023-06-01 11:07:04 +00:00
## Objective-C
{% hint style="danger" %}
2023-12-31 01:15:16 +00:00
Objective-Cで書かれたプログラムは、[Mach-Oバイナリ ](macos-files-folders-and-binaries/universal-binaries-and-mach-o-format.md )に**コンパイルされた時に**、クラス宣言を**保持する**ことに注意してください。これらのクラス宣言には以下が**含まれます**:
2023-06-01 11:07:04 +00:00
{% endhint %}
2023-07-07 23:42:27 +00:00
* クラス
* クラスメソッド
2023-12-31 01:15:16 +00:00
* クラスインスタンス変数
2023-06-01 11:07:04 +00:00
2023-12-31 01:15:16 +00:00
この情報は[**class-dump** ](https://github.com/nygard/class-dump )を使用して取得できます:
2023-06-01 11:07:04 +00:00
```bash
class-dump Kindle.app
```
2023-12-31 01:15:16 +00:00
Note that this names could be obfuscated to make the reversing of the binary more difficult.
2023-07-07 23:42:27 +00:00
## クラス、メソッド、オブジェクト
2023-05-08 22:07:46 +00:00
2023-07-07 23:42:27 +00:00
### インターフェース、プロパティ、メソッド
2023-05-08 22:07:46 +00:00
```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
```
2023-07-07 23:42:27 +00:00
### **クラス**
2023-05-08 22:07:46 +00:00
```objectivec
@implementation MyVehicle : NSObject
// No need to indicate the properties, only define methods
- (void)startEngine {
2023-07-07 23:42:27 +00:00
NSLog(@"Engine started");
2023-05-08 22:07:46 +00:00
}
- (void)addWheels:(int)value {
2023-07-07 23:42:27 +00:00
self.numberOfWheels += value;
2023-05-08 22:07:46 +00:00
}
@end
```
2023-12-31 01:15:16 +00:00
### **オブジェクト & メソッドの呼び出し**
2023-05-08 22:07:46 +00:00
2023-12-31 01:15:16 +00:00
クラスのインスタンスを作成するには、**`alloc` ** メソッドが呼び出され、各**プロパティ**に対して**メモリを割り当て**、それらの割り当てを**ゼロ**にします。その後、**`init` ** が呼び出され、プロパティを**必要な値**に**初期化**します。
2023-05-08 22:07:46 +00:00
```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];
```
2023-07-07 23:42:27 +00:00
### **クラスメソッド**
2023-05-08 22:07:46 +00:00
2023-12-31 01:15:16 +00:00
クラスメソッドはインスタンスメソッドに使用されるハイフン (-) ではなく、**プラス記号** (+) で定義されます。**NSString** クラスメソッドの ** `stringWithString` ** のようにです。
2023-05-08 22:07:46 +00:00
```objectivec
+ (id)stringWithString:(NSString *)aString;
```
2023-12-31 01:15:16 +00:00
### セッター & ゲッター
2023-05-08 22:07:46 +00:00
2023-12-31 01:15:16 +00:00
プロパティを**設定**し、取得するには、**ドット記法**を使用するか、**メソッドを呼び出す**ように行うことができます:
2023-05-08 22:07:46 +00:00
```objectivec
// Set
newVehicle.numberOfWheels = 2;
[newVehicle setNumberOfWheels:3];
// Get
NSLog(@"Number of wheels: %i", newVehicle.numberOfWheels);
NSLog(@"Number of wheels: %i", [newVehicle numberOfWheels]);
```
2023-07-07 23:42:27 +00:00
### **インスタンス変数**
2023-05-08 22:07:46 +00:00
2023-12-31 01:15:16 +00:00
セッター & ゲッターメソッドの代わりにインスタンス変数を使用することができます。これらの変数はプロパティと同じ名前を持っていますが、"_"で始まります:
2023-05-08 22:07:46 +00:00
```objectivec
- (void)makeLongTruck {
2023-07-07 23:42:27 +00:00
_numberOfWheels = +10000;
NSLog(@"Number of wheels: %i", self.numberOfLeaves);
2023-05-08 22:07:46 +00:00
}
```
2023-07-07 23:42:27 +00:00
### プロトコル
2023-05-08 22:07:46 +00:00
2023-12-31 01:15:16 +00:00
プロトコルは、メソッド宣言のセットです(プロパティは含まれません)。プロトコルを実装するクラスは、宣言されたメソッドを実装します。
2023-05-09 00:02:08 +00:00
2023-12-31 01:15:16 +00:00
メソッドには2種類あります: **必須**と**オプション**。**デフォルト**ではメソッドは**必須**です(しかし、**`@required` ** タグを使って指示することもできます)。メソッドがオプションであることを示すには ** `@optional` ** を使用します。
2023-05-09 00:02:08 +00:00
```objectivec
@protocol myNewProtocol
- (void) method1; //mandatory
@required
- (void) method2; //mandatory
@optional
- (void) method3; //optional
@end
```
2023-12-31 01:15:16 +00:00
### 全体を通して
2023-05-09 00:02:08 +00:00
```objectivec
// gcc -framework Foundation test_obj.m -o test_obj
#import <Foundation/Foundation.h>
@protocol myVehicleProtocol
- (void) startEngine; //mandatory
@required
- (void) addWheels:(int)value; //mandatory
@optional
- (void) makeLongTruck; //optional
@end
@interface MyVehicle : NSObject < myVehicleProtocol >
@property int numberOfWheels;
- (void)startEngine;
- (void)addWheels:(int)value;
- (void)makeLongTruck;
@end
@implementation MyVehicle : NSObject
- (void)startEngine {
2023-07-07 23:42:27 +00:00
NSLog(@"Engine started");
2023-05-09 00:02:08 +00:00
}
- (void)addWheels:(int)value {
2023-07-07 23:42:27 +00:00
self.numberOfWheels += value;
2023-05-09 00:02:08 +00:00
}
- (void)makeLongTruck {
2023-07-07 23:42:27 +00:00
_numberOfWheels = +10000;
NSLog(@"Number of wheels: %i", self.numberOfWheels);
2023-05-09 00:02:08 +00:00
}
@end
int main() {
2023-07-07 23:42:27 +00:00
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];
2023-05-09 00:02:08 +00:00
}
```
2023-07-07 23:42:27 +00:00
### 基本クラス
2023-05-09 00:02:08 +00:00
2023-07-07 23:42:27 +00:00
#### 文字列
2023-05-09 00:02:08 +00:00
{% 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 %}
2023-12-31 01:15:16 +00:00
基本クラスは**不変**ですので、既存の文字列に文字列を追加するには、**新しいNSStringを作成する必要があります**。
2023-05-09 00:02:08 +00:00
{% code overflow="wrap" %}
```objectivec
NSString *bookDescription = [NSString stringWithFormat:@"%@ by %@ was published in %@", bookTitle, bookAuthor, bookPublicationYear];
```
{% endcode %}
2023-12-31 01:15:16 +00:00
または、**mutable**な文字列クラスを使用することもできます:
2023-05-09 00:02:08 +00:00
{% 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];
```
2023-12-31 01:15:16 +00:00
#### 数値
2023-05-09 00:02:08 +00:00
{% 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]
```
2023-12-31 01:15:16 +00:00
#### 配列、セット、およびディクショナリ
2023-05-09 00:02:08 +00:00
{% code overflow="wrap" %}
```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"];
2023-09-11 16:06:08 +00:00
// Inmutable Sets
2023-05-09 00:02:08 +00:00
NSSet *fruitsSet1 = [NSSet setWithObjects:@"apple", @"banana", @"orange", nil];
NSSet *fruitsSet2 = [NSSet setWithArray:@[@"apple", @"banana", @"orange"]];
2023-09-11 16:06:08 +00:00
// Mutable sets
2023-05-09 00:02:08 +00:00
NSMutableSet *mutFruitsSet = [NSMutableSet setWithObjects:@"apple", @"banana", @"orange", nil];
[mutFruitsSet addObject:@"grape"];
[mutFruitsSet removeObject:@"apple"];
// Dictionary
NSDictionary *fruitColorsDictionary = @{
2023-07-07 23:42:27 +00:00
@"apple" : @"red",
@"banana" : @"yellow",
@"orange" : @"orange",
@"grape" : @"purple"
2023-05-09 00:02:08 +00:00
};
// In dictionaryWithObjectsAndKeys you specify the value and then the key:
NSDictionary *fruitColorsDictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:
2023-07-07 23:42:27 +00:00
@"red", @"apple",
@"yellow", @"banana",
@"orange", @"orange",
@"purple", @"grape",
2023-05-09 00:02:08 +00:00
nil];
// Mutable dictionary
NSMutableDictionary *mutFruitColorsDictionary = [NSMutableDictionary dictionaryWithDictionary:fruitColorsDictionary];
[mutFruitColorsDictionary setObject:@"green" forKey:@"apple"];
[mutFruitColorsDictionary removeObjectForKey:@"grape"];
```
2023-09-11 16:06:08 +00:00
### ブロック
2023-05-09 13:53:58 +00:00
2023-12-31 01:15:16 +00:00
ブロックは**オブジェクトとして振る舞う関数**で、関数に渡したり、**配列**や**辞書**に**格納**することができます。また、値が与えられた場合には値を**表すことができる**ので、ラムダに似ています。
{% code overflow="wrap" %}
2023-05-09 13:53:58 +00:00
```objectivec
returnType (^blockName)(argumentType1, argumentType2, ...) = ^(argumentType1 param1, argumentType2 param2, ...){
2023-07-07 23:42:27 +00:00
//Perform operations here
2023-05-09 13:53:58 +00:00
};
// For example
2023-07-07 23:42:27 +00:00
int (^suma)(int, int) = ^(int a, int b){
return a+b;
2023-05-09 13:53:58 +00:00
};
NSLog(@"3+4 = %d", suma(3,4));
```
{% endcode %}
2023-12-31 01:15:16 +00:00
関数内でパラメータとして使用するために**ブロックタイプを定義することも可能です**:
2023-05-09 13:53:58 +00:00
```objectivec
// Define the block type
typedef void (^callbackLogger)(void);
// Create a bloack with the block type
2023-07-07 23:42:27 +00:00
callbackLogger myLogger = ^{
NSLog(@"%@", @"This is my block");
2023-05-09 13:53:58 +00:00
};
// Use it inside a function as a param
void genericLogger(callbackLogger blockParam) {
2023-07-07 23:42:27 +00:00
NSLog(@"%@", @"This is my function");
blockParam();
2023-05-09 13:53:58 +00:00
}
genericLogger(myLogger);
// Call it inline
genericLogger(^{
2023-07-07 23:42:27 +00:00
NSLog(@"%@", @"This is my second block");
2023-05-09 13:53:58 +00:00
});
```
2023-07-07 23:42:27 +00:00
### ファイル
2023-05-09 13:53:58 +00:00
{% code overflow="wrap" %}
```objectivec
// Manager to manage files
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if file exists:
if ([fileManager fileExistsAtPath:@"/path/to/file.txt" ] == YES) {
2023-07-07 23:42:27 +00:00
NSLog (@"File exists");
2023-05-09 13:53:58 +00:00
}
// copy files
if ([fileManager copyItemAtPath: @"/path/to/file1.txt" toPath: @"/path/to/file2.txt" error:nil] == YES) {
2023-07-07 23:42:27 +00:00
NSLog (@"Copy successful");
2023-05-09 13:53:58 +00:00
}
// Check if the content of 2 files match
if ([fileManager contentsEqualAtPath:@"/path/to/file1.txt" andPath:@"/path/to/file2.txt"] == YES) {
2023-07-07 23:42:27 +00:00
NSLog (@"File contents match");
2023-05-09 13:53:58 +00:00
}
// Delete file
if ([fileManager removeItemAtPath:@"/path/to/file1.txt" error:nil]) {
2023-07-07 23:42:27 +00:00
NSLog(@"Removed successfully");
2023-05-09 13:53:58 +00:00
}
```
{% endcode %}
2023-12-31 01:15:16 +00:00
ファイルの管理には、**`NSString` オブジェクトの代わりに `NSURL` オブジェクトを使用する**ことも可能です。メソッド名は似ていますが、**`Path` の代わりに `URL` ** が使われます。
2023-05-09 13:53:58 +00:00
```objectivec
NSURL *fileSrc = [NSURL fileURLWithPath:@"/path/to/file1.txt"];
NSURL *fileDst = [NSURL fileURLWithPath:@"/path/to/file2.txt"];
[fileManager moveItemAtURL:fileSrc toURL:fileDst error: nil];
```
2023-12-31 01:15:16 +00:00
ほとんどの基本クラスには、直接ファイルに書き込むことを可能にするメソッド `writeToFile:<path> atomically:<YES> encoding:<encoding> error:nil` が定義されています:
2023-05-09 13:53:58 +00:00
{% code overflow="wrap" %}
```objectivec
NSString* tmp = @"something temporary";
[tmp writeToFile:@"/tmp/tmp1.txt" atomically:YES encoding:NSASCIIStringEncoding error:nil];
```
2023-12-31 01:15:16 +00:00
```markdown
2023-05-09 13:53:58 +00:00
{% endcode %}
2023-05-08 22:07:46 +00:00
< details >
2023-12-31 01:15:16 +00:00
< summary > < strong > AWSハッキングをゼロからヒーローまで学ぶには< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > をチェックしてください!< / strong > < / summary >
2023-05-08 22:07:46 +00:00
2023-12-31 01:15:16 +00:00
HackTricksをサポートする他の方法:
* **HackTricksにあなたの会社を広告したい**、または**HackTricksをPDFでダウンロードしたい**場合は、[**サブスクリプションプラン** ](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 )や[**テレグラムグループ** ](https://t.me/peass )に**参加する**、または**Twitter** 🐦 [**@carlospolopm** ](https://twitter.com/carlospolopm )を**フォローする**。
* **HackTricks**の[**GitHubリポジトリ** ](https://github.com/carlospolop/hacktricks )や[**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud )にPRを提出して、あなたのハッキングのコツを共有する。
2023-05-08 22:07:46 +00:00
< / details >
2023-12-31 01:15:16 +00:00
```