|
15 | 15 | #import <Cocoa/Cocoa.h>
|
16 | 16 | #include <stdio.h>
|
17 | 17 |
|
| 18 | +#include <dlfcn.h> |
| 19 | + |
| 20 | +/* |
| 21 | + |
| 22 | + Added some special code to undo App Translocation so we can setuid the helper. |
| 23 | + |
| 24 | + all praise and credit to these websites and their authors for the solution below. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + http://lapcatsoftware.com/articles/detect-app-translocation.html |
| 29 | + https://objective-see.com/blog/blog_0x15.html |
| 30 | + |
| 31 | + */ |
| 32 | + |
18 | 33 |
|
19 |
| -int main(int argc, char *argv[]) |
20 |
| -{ |
21 |
| - //FIXME: COMMENT BACK IN BEFORE RELEASE!!!! |
22 |
| - |
23 | 34 |
|
| 35 | +Boolean (*mySecTranslocateIsTranslocatedURL)(CFURLRef path, bool *isTranslocated, CFErrorRef * __nullable error); |
| 36 | +CFURLRef __nullable (*mySecTranslocateCreateOriginalPathForURL)(CFURLRef translocatedPath, CFErrorRef * __nullable error); |
| 37 | + |
| 38 | +bool IsTranslocatedURL(CFURLRef currentURL, CFURLRef *originalURL) |
| 39 | +{ |
| 40 | + if (currentURL == NULL) |
| 41 | + { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + // #define NSAppKitVersionNumber10_11 1404 |
| 46 | + if (floor(NSAppKitVersionNumber) <= 1404) |
| 47 | + { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + void *handle = dlopen("/System/Library/Frameworks/Security.framework/Security", RTLD_LAZY); |
| 52 | + if (handle == NULL) |
| 53 | + { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + bool isTranslocated = false; |
24 | 58 |
|
25 |
| - id pool = [NSAutoreleasePool new]; |
| 59 | + Boolean (*mySecTranslocateIsTranslocatedURL)(CFURLRef path, bool *isTranslocated, CFErrorRef * __nullable error); |
| 60 | + mySecTranslocateIsTranslocatedURL = dlsym(handle, "SecTranslocateIsTranslocatedURL"); |
| 61 | + if (mySecTranslocateIsTranslocatedURL != NULL) |
| 62 | + { |
| 63 | + if (mySecTranslocateIsTranslocatedURL(currentURL, &isTranslocated, NULL)) |
| 64 | + { |
| 65 | + if (isTranslocated) |
| 66 | + { |
| 67 | + if (originalURL != NULL) |
| 68 | + { |
| 69 | + CFURLRef __nullable (*mySecTranslocateCreateOriginalPathForURL)(CFURLRef translocatedPath, CFErrorRef * __nullable error); |
| 70 | + mySecTranslocateCreateOriginalPathForURL = dlsym(handle, "SecTranslocateCreateOriginalPathForURL"); |
| 71 | + if (mySecTranslocateCreateOriginalPathForURL != NULL) |
| 72 | + { |
| 73 | + *originalURL = mySecTranslocateCreateOriginalPathForURL((CFURLRef)currentURL, NULL); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + *originalURL = NULL; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + dlclose(handle); |
| 85 | + |
| 86 | + return isTranslocated; |
| 87 | +} |
| 88 | + |
| 89 | + |
26 | 90 |
|
| 91 | + |
| 92 | +int main(int argc, char *argv[]) |
| 93 | +{ |
| 94 | + id pool = [NSAutoreleasePool new]; |
| 95 | + NSURL *appPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
| 96 | + |
| 97 | + //get original URL |
| 98 | + NSURL *newPath = nil; |
| 99 | + |
| 100 | + if (IsTranslocatedURL((CFURLRef) appPath, &newPath) == true) |
| 101 | + { |
| 102 | + |
| 103 | + //remove quarantine attributes of original |
| 104 | + |
| 105 | + [NSTask launchedTaskWithLaunchPath:@"/usr/bin/xattr" arguments:@[@"-cr", (NSURL*)newPath.path]]; |
| 106 | + |
| 107 | + //relaunch original |
| 108 | + |
| 109 | + // ->use 'open' as allows two instances of app (this instance is exiting) |
| 110 | + [NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:@[@"-n", @"-a", newPath.path]]; |
| 111 | + //this instance is done |
| 112 | + return 0; |
| 113 | + } |
| 114 | + |
27 | 115 |
|
28 | 116 | NSString *logPath = [NSHomeDirectory() stringByAppendingPathComponent:LOG_PATH];
|
29 | 117 | freopen([logPath fileSystemRepresentation], "a", stderr);
|
|
0 commit comments