-
Notifications
You must be signed in to change notification settings - Fork 42
/
HelperInterface.m
353 lines (267 loc) · 8.92 KB
/
HelperInterface.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//
// file: HelperInterface.m
// project: (open-source) installer
// description: interface for app installer comms
//
// created by Patrick Wardle
// copyright (c) 2018 Objective-See. All rights reserved.
//
#import "Consts.h"
#import "Logging.h"
#import "Utilities.h"
#import "HelperInterface.h"
#import <signal.h>
//script name
#define CONF_SCRIPT @"configure.sh"
//signing auth
#define SIGNING_AUTH @"Developer ID Application: Objective-See, LLC (VBG97UB4TA)"
//dispatch source for SIGTERM
dispatch_source_t dispatchSource = nil;
@implementation HelperInterface
//install
// do install logic and return result
-(void)install:(NSString*)app reply:(void (^)(NSNumber*))reply;
{
//results
NSNumber* result = nil;
//dbg msg
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"XPC-request: install (%@)", app]);
//configure
// pass in 'install' flag
result = [NSNumber numberWithInt:[self configure:app arguements:@[CMDLINE_FLAG_INSTALL]]];
//reply to client
reply(result);
return;
}
//uninstall
// do uninstall logic and return result
-(void)uninstall:(NSString*)app full:(BOOL)full reply:(void (^)(NSNumber*))reply;
{
//results
NSNumber* result = nil;
//dbg msg
logMsg(LOG_DEBUG, @"XPC-request: uninstall");
//configure
// pass in 'uninstall' flag, and partial/full
result = [NSNumber numberWithInt:[self configure:app arguements:@[CMDLINE_FLAG_UNINSTALL, [NSNumber numberWithBool:full].stringValue]]];
//reply to client
reply(result);
return;
}
//configure
// install or uninstall
-(int)configure:(NSString*)app arguements:(NSArray*)args
{
//result
int result = -1;
//valdiated (copy) of app
NSString* validatedApp = nil;
//validate app
validatedApp = [self validateApp:app];
if(nil == validatedApp)
{
//err msg
logMsg(LOG_ERR, @"failed to validate copy of app");
//bail
goto bail;
}
//dbg msg
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"validated %@", app]);
//exec script
result = [self execScript:validatedApp arguments:args];
if(noErr != result)
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to execute config script %@ (%d)", CONF_SCRIPT, result]);
//bail
goto bail;
}
//happy
result = 0;
bail:
//always try to remove validated app
if(YES != [[NSFileManager defaultManager] removeItemAtPath:validatedApp error:nil])
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to remove validated app %@", validatedApp]);
//set err
result = -1;
}
return result;
}
//remove self
// since system install/launches us as root, client can't directly remove us
-(void)remove
{
//flag
__block BOOL noErrors = YES;
//helper plist
__block NSString* helperPlist = nil;
//binary
__block NSString* helperBinary = nil;
//error
__block NSError* error = nil;
//dbg msg
logMsg(LOG_DEBUG, @"XPC-request: remove (self!)");
//ignore sigterm
// handling it via GCD dispatch
signal(SIGTERM, SIG_IGN);
//init dispatch source for SIGTERM
dispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());
//set handler
// disable kext & close logging
dispatch_source_set_event_handler(dispatchSource, ^{
//dbg msg
logMsg(LOG_DEBUG, @"XPC: got SIGTERM, deleting plist & self!");
//init path to plist
helperPlist = [@"/Library/LaunchDaemons" stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", INSTALLER_HELPER_ID]];
//delete plist
if(YES != [[NSFileManager defaultManager] removeItemAtPath:helperPlist error:&error])
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"ERROR: failed to delete %@ (%@)", helperPlist, error.description]);
//set error
noErrors = NO;
}
//init path to binary
helperBinary = [@"/Library/PrivilegedHelperTools" stringByAppendingPathComponent:INSTALLER_HELPER_ID];
//delete self
if(YES != [[NSFileManager defaultManager] removeItemAtPath:helperBinary error:&error])
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"ERROR: failed to delete %@ (%@)", helperBinary, error.description]);
//set error
noErrors = NO;
}
//no errors?
// display dbg msg
if(YES == noErrors)
{
//dbg msg
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"removed %@ and %@", helperPlist, helperBinary]);
}
//bye!
exit(SIGTERM);
});
//resume
dispatch_resume(dispatchSource);
//init path to plist
helperPlist = [@"/Library/LaunchDaemons" stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", INSTALLER_HELPER_ID]];
//unload
execTask(@"/bin/launchctl", @[@"unload", helperPlist], YES);
return;
}
//make copy of app and validate
-(NSString*)validateApp:(NSString*)app
{
//copy of app
NSString* appCopy = nil;
//file manager
NSFileManager* defaultManager = nil;
//path to (now) validated app
NSString* validatedApp = nil;
//error
NSError* error = nil;
//dbg msg
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"validating %@", app]);
//grab default file manager
defaultManager = [NSFileManager defaultManager];
//init path to app copy
// *root-owned* tmp directory
appCopy = [NSTemporaryDirectory() stringByAppendingPathComponent:app.lastPathComponent];
//delete if old copy is there
if(YES == [defaultManager fileExistsAtPath:appCopy])
{
//delete
if(YES != [defaultManager removeItemAtPath:appCopy error:&error])
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to delete %@ (error: %@)", appCopy, error.description]);
}
}
//copy app bundle to *root-owned* directory
if(YES != [defaultManager copyItemAtPath:app toPath:appCopy error:&error])
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to copy %@ to %@ (error: %@)", app, appCopy, error.description]);
//bail
goto bail;
}
//set group/owner to root/wheel
if(YES != setFileOwner(appCopy, @0, @0, YES))
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to set %@ to be owned by root", appCopy]);
//bail
goto bail;
}
//verify app
// make sure it's signed, and by our signing auth
if(noErr != verifyApp(appCopy, SIGNING_AUTH))
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to validate %@", appCopy]);
//bail
goto bail;
}
//happy
validatedApp = appCopy;
bail:
return validatedApp;
}
//execute config script
-(int)execScript:(NSString*)validatedApp arguments:(NSArray*)arguments
{
//result
int result = -1;
//results
NSDictionary* results = nil;
//script
NSString* script = nil;
//app bundle
NSBundle* appBundle = nil;
//file manager
NSFileManager* fileManager = nil;
//current working directory
NSString* currentWorkingDir = nil;
//init file manager
fileManager = [NSFileManager defaultManager];
//load app bundle
appBundle = [NSBundle bundleWithPath:validatedApp];
if(nil == appBundle)
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to load app bundle for %@", validatedApp]);
//bail
goto bail;
}
//get path to config script
script = [[appBundle resourcePath] stringByAppendingPathComponent:CONF_SCRIPT];
if(nil == script)
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to find config script %@", CONF_SCRIPT]);
//bail
goto bail;
}
//get current working directory
currentWorkingDir = [fileManager currentDirectoryPath];
//change working directory
// set it to (validated) app path's resources
[fileManager changeCurrentDirectoryPath:[NSString stringWithFormat:@"%@/Contents/Resources/", validatedApp]];
//exec script
results = execTask(script, arguments, YES);
//dbg msg
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"executed script: %@", results]);
//grab result
if(nil != results[EXIT_CODE])
{
//grab
result = [results[EXIT_CODE] intValue];
}
//(re)set current working directory
[fileManager changeCurrentDirectoryPath:currentWorkingDir];
bail:
return result;
}
@end