mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-27 15:12:11 +00:00
369 lines
12 KiB
Markdown
369 lines
12 KiB
Markdown
# macOS Objective-C
|
||
|
||
<details>
|
||
|
||
<summary><strong>从零到英雄学习AWS黑客技术,通过</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
||
|
||
支持HackTricks的其他方式:
|
||
|
||
* 如果您想在**HackTricks中看到您的公司广告**或**下载HackTricks的PDF**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
||
* 获取[**官方PEASS & HackTricks商品**](https://peass.creator-spring.com)
|
||
* 发现[**PEASS家族**](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/carlospolopm)**。**
|
||
* **通过向** [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。
|
||
|
||
</details>
|
||
|
||
## 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]);
|
||
```
|
||
### **实例变量**
|
||
|
||
除了使用 setter 和 getter 方法外,您还可以使用实例变量。这些变量的名称与属性相同,但以“\_”开头:
|
||
```objectivec
|
||
- (void)makeLongTruck {
|
||
_numberOfWheels = +10000;
|
||
NSLog(@"Number of wheels: %i", self.numberOfLeaves);
|
||
}
|
||
```
|
||
### 协议
|
||
|
||
协议是一组方法声明(不包含属性)。实现协议的类需要实现声明的方法。
|
||
|
||
方法有两种类型:**必须** 和 **可选**。默认情况下,方法是 **必须** 的(但你也可以使用 **`@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 <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 {
|
||
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 %}
|
||
|
||
或者您也可以使用一个**可变**字符串类:
|
||
|
||
{% 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]
|
||
```
|
||
#### 数组、集合与字典
|
||
|
||
{% 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"];
|
||
|
||
// 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"];
|
||
```
|
||
{% endcode %}
|
||
|
||
### Blocks
|
||
|
||
Blocks 是**表现为对象的函数**,因此它们可以被传递给函数或**存储**在**数组**或**字典**中。此外,如果它们被赋予值,它们可以**代表一个值**,所以它与 lambdas 相似。
|
||
|
||
{% code overflow="wrap" %}
|
||
```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));
|
||
```
|
||
```markdown
|
||
也可以**定义一个块类型用作函数中的参数**:
|
||
```
|
||
```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 %}
|
||
|
||
也可以通过使用 `NSURL` 对象**而不是 `NSString` 对象**来管理文件。方法名称类似,但是**使用 `URL` 替代 `Path`**。
|
||
```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:<path> atomically:<YES> encoding:<encoding> error:nil`,允许它们直接写入文件:
|
||
|
||
{% code overflow="wrap" %}
|
||
```objectivec
|
||
NSString* tmp = @"something temporary";
|
||
[tmp writeToFile:@"/tmp/tmp1.txt" atomically:YES encoding:NSASCIIStringEncoding error:nil];
|
||
```
|
||
<details>
|
||
|
||
<summary><strong>从零到英雄学习AWS黑客攻击</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
||
|
||
支持HackTricks的其他方式:
|
||
|
||
* 如果您想在**HackTricks中看到您的公司广告**或**下载HackTricks的PDF**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
||
* 获取[**官方PEASS & HackTricks商品**](https://peass.creator-spring.com)
|
||
* 发现[**PEASS家族**](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/carlospolopm)**。**
|
||
* **通过向** [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。
|
||
|
||
</details>
|