forked from strik3/openhaystack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenHaystackPluginService.m
136 lines (105 loc) · 5.43 KB
/
OpenHaystackPluginService.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// ALTPluginService.m
// AltPlugin
//
// Created by Riley Testut on 11/14/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
// OpenHaystack – Tracking personal Bluetooth devices via Apple's Find My network
//
// Copyright © 2021 Secure Mobile Networking Lab (SEEMOO)
// Copyright © 2021 The Open Wireless Link Project
//
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "OpenHaystackPluginService.h"
#import <dlfcn.h>
#import "AppleAccountData.h"
#import <Accounts/Accounts.h>
#import <Security/Security.h>
@import AppKit;
@interface AKAppleIDSession : NSObject
- (id)appleIDHeadersForRequest:(id)arg1;
@end
@interface AKDevice
+ (AKDevice *)currentDevice;
- (NSString *)uniqueDeviceIdentifier;
- (NSString *)serialNumber;
- (NSString *)serverFriendlyDescription;
@end
@interface OpenHaystackPluginService ()
@property(nonatomic, readonly) NSISO8601DateFormatter *dateFormatter;
@end
@implementation OpenHaystackPluginService
+ (instancetype)sharedService {
static OpenHaystackPluginService *_service = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_service = [[self alloc] init];
});
return _service;
}
- (instancetype)init {
self = [super init];
if (self) {
_dateFormatter = [[NSISO8601DateFormatter alloc] init];
}
return self;
}
+ (void)initialize {
[[OpenHaystackPluginService sharedService] start];
}
- (void)start {
dlopen("/System/Library/PrivateFrameworks/AuthKit.framework/AuthKit", RTLD_NOW);
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"de.tu-darmstadt.seemoo.OpenHaystack.FetchAnisetteData"
object:nil];
}
- (void)receiveNotification:(NSNotification *)notification {
NSString *requestUUID = notification.userInfo[@"requestUUID"];
NSMutableURLRequest *req =
[[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:@"https://developerservices2.apple.com/services/QH65B2/listTeams.action?clientId=XABBG36SBA"]];
[req setHTTPMethod:@"POST"];
AKAppleIDSession *session = [[NSClassFromString(@"AKAppleIDSession") alloc] initWithIdentifier:@"com.apple.gs.xcode.auth"];
NSDictionary *headers = [session appleIDHeadersForRequest:req];
AKDevice *device = [NSClassFromString(@"AKDevice") currentDevice];
NSDate *date = [self.dateFormatter dateFromString:headers[@"X-Apple-I-Client-Time"]];
NSData *sptoken = [self fetchSearchpartyToken];
AppleAccountData *anisetteData = [[NSClassFromString(@"AppleAccountData") alloc] initWithMachineID:headers[@"X-Apple-I-MD-M"]
oneTimePassword:headers[@"X-Apple-I-MD"]
localUserID:headers[@"X-Apple-I-MD-LU"]
routingInfo:[headers[@"X-Apple-I-MD-RINFO"] longLongValue]
deviceUniqueIdentifier:device.uniqueDeviceIdentifier
deviceSerialNumber:device.serialNumber
deviceDescription:device.serverFriendlyDescription
date:date
locale:[NSLocale currentLocale]
timeZone:[NSTimeZone localTimeZone]];
if (sptoken != nil) {
anisetteData.searchPartyToken = [sptoken copy];
}
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:anisetteData requiringSecureCoding:YES error:nil];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"de.tu-darmstadt.seemoo.OpenHaystack.AnisetteDataResponse"
object:nil
userInfo:@{@"requestUUID" : requestUUID, @"anisetteData" : data}
deliverImmediately:YES];
}
- (NSData *_Nullable)fetchSearchpartyToken {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:@"com.apple.account.AppleAccount"];
NSArray *appleAccounts = [accountStore accountsWithAccountType:accountType];
if (appleAccounts == nil && appleAccounts.count > 0) {
return nil;
}
ACAccount *iCloudAccount = appleAccounts[0];
ACAccountCredential *iCloudCredentials = iCloudAccount.credential;
if ([iCloudCredentials respondsToSelector:NSSelectorFromString(@"credentialItems")]) {
NSDictionary *credentialItems = [iCloudCredentials performSelector:NSSelectorFromString(@"credentialItems")];
NSString *searchPartyToken = credentialItems[@"search-party-token"];
NSData *tokenData = [searchPartyToken dataUsingEncoding:NSASCIIStringEncoding];
return tokenData;
}
return nil;
}
@end