Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //--------includes-------//
  2.  
  3. #include <limits.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <stdbool.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <dlfcn.h>
  10. #include <mach-o/dyld.h>
  11. #include <sys/stat.h>
  12. #include <sys/attr.h>
  13. #include <Foundation/Foundation.h>
  14. #include <AppSupport/CPDistributedMessagingCenter.h>
  15. #include <rocketbootstrap.h>
  16. #include <substrate.h>
  17. #include <sys/syscall.h>
  18. #include <dirent.h>
  19.  
  20. int statfs(const char *path, struct statfs *buf);
  21.  
  22. //#define printf(a, ...)
  23. //#define NSLog(a, ...)
  24.  
  25. //-------pointers-------//
  26. FILE* (*orig_fopen)(const char * filename, const char * mode);
  27. int (*orig_access)(const char *pathname, int mode);
  28. int (*orig_open)(const char *path, int oflags);
  29. int (*orig_stat)(const char *pathname, struct stat *statbuf);
  30. int (*orig_lstat)(const char *pathname, struct stat *statbuf);
  31. int (*orig_statfs)(const char *path, struct statfs *buf);
  32. ssize_t (*orig_readlink)(const char *path, char *buf, size_t bufsiz);
  33. const char *(*orig_dyld_get_image_name)(uint32_t index);
  34. uint32_t (*orig_dyld_image_count)(void);
  35. void *(*orig_dlopen)(const char *filename, int flag);
  36. void *(*orig_dlsym)(void *handle, const char *name);
  37. char *(*orig_dlerror)(void);
  38. char *(*orig_getenv)(const char * name);
  39. int (*orig_getattrlist)(const char *path, struct attrlist * attr, void * info, size_t size, unsigned int smth);
  40. DIR *(*orig_opendir)(const char *dirname);
  41.  
  42. CPDistributedMessagingCenter * __strong messagingCenter; //message center
  43.  
  44. char fake_err[2000];
  45. char *ourBundle;
  46. char *ourContainer;
  47.  
  48. //-------defines-------//
  49. #define startsWith(string, what) !(strncmp(string, what, strlen(what)))
  50. #define endsWith(string, what) !(strncmp(string + strlen(string)-strlen(what), what, strlen(what)))
  51.  
  52. //----usefulness----//
  53. bool isMacho(const char* path) {
  54.    
  55.     if (!messagingCenter) {
  56.         NSLog(@"[unescaper] My dear friend, something's wrong. Expect weird things. oof!");
  57.         return true;
  58.     }
  59.    
  60.     NSDictionary *userInfoDict = [NSDictionary dictionaryWithObject:@(path) forKey:@"path"];
  61.     NSDictionary *response = [messagingCenter sendMessageAndReceiveReplyName:@"isMacho" userInfo:userInfoDict];
  62.     BOOL isMacho = [[response objectForKey:@"isMacho"] boolValue];
  63.  
  64.     return isMacho;
  65. }
  66.  
  67. char *realPath(const char* path) {
  68.    
  69.     if (!messagingCenter) {
  70.         NSLog(@"[unescaper] My dear friend, something's wrong. Expect weird things. oof!");
  71.         return NULL;
  72.     }
  73.    
  74.     NSDictionary *userInfoDict = [NSDictionary dictionaryWithObject:@(path) forKey:@"path"];
  75.     NSDictionary *response = [messagingCenter sendMessageAndReceiveReplyName:@"realPath" userInfo:userInfoDict];
  76.    
  77.     return strdup([[response objectForKey:@"realpath"] UTF8String]);
  78. }
  79.  
  80.  
  81. //-------functions-------//
  82. bool wouldAmfidLikeThis(const char* path) {
  83.     // 😏
  84.     NSLog(@"[unescaper] Validating %s", path);
  85.    
  86.     if (strstr(path, "Flex.dylib")) {
  87.         NSLog(@"[unescaper] %s", "Not valid!");
  88.         return false;
  89.     }
  90.    
  91.     if (!messagingCenter) {
  92.         NSLog(@"[unescaper] My dear friend, something's wrong. Expect weird things. oof!");
  93.         return true;
  94.     }
  95.    
  96.     NSDictionary *userInfoDict = [NSDictionary dictionaryWithObject:@(path) forKey:@"path"];
  97.     NSDictionary *response = [messagingCenter sendMessageAndReceiveReplyName:@"validate" userInfo:userInfoDict];
  98.    
  99.     BOOL isValid = [[response objectForKey:@"isValid"] boolValue];
  100.     NSLog(@"[unescaper] %s", isValid ? "VALID!" : "Not valid!");
  101.    
  102.     return isValid;
  103. }
  104.  
  105. bool isOutsideSandbox(const char* path) {
  106.  
  107.     NSLog(@"[unescaper] outside sandbox %s?", path);
  108.    
  109.     if (path == NULL) {
  110.         printf("[unescaper] Yes!\n");
  111.         return true;
  112.     }
  113.    
  114.     // legit
  115.     if (strstr(path, ourContainer)) {
  116.         printf("[unescaper] No!\n");
  117.         return false;
  118.     }
  119.     if (strstr(path, ourBundle)) {
  120.         printf("[unescaper] No!\n");
  121.         return false;
  122.     }
  123.     if (strstr(path, "/var/mobile/Library/Caches/com.apple.UIStatusBar")) {
  124.         printf("[unescaper] No!\n");
  125.         return false;
  126.     }
  127.     // smart boi haha
  128.     path = realPath(path);
  129.     if (!path) {
  130.         printf("[unescaper] Yes!\n");
  131.         return true;
  132.     }
  133.     if (!strlen(path)) {
  134.         printf("[unescaper] Yes!\n");
  135.         return true;
  136.     }
  137.    
  138.     bool rv;
  139.     rv = (
  140.           startsWith(path, "/Library") ||
  141.           startsWith(path, "/private/var/mobile/Library") ||
  142.           startsWith(path, "/private/var/mnt") ||
  143.           startsWith(path, "/private/var/db") ||
  144.           startsWith(path, "/private/var/stash")
  145.           );
  146.    
  147.     printf("[unescaper][sandbox] returned: %d\n", (int)rv);
  148.    
  149.     return rv;
  150. }
  151.  
  152. bool shallExist(const char *path) {
  153.  
  154.     NSLog(@"[unescaper] shall exist %s?", path);
  155.    
  156.     if (strstr(path, "UNESCAPER")) {
  157.         printf("[unescaper] No!\n");
  158.         return false;
  159.     }
  160.     if (path == NULL) {
  161.         printf("[unescaper] No!\n");
  162.         return false;
  163.     }
  164.    
  165.     // legit
  166.     if (strstr(path, ourContainer)) {
  167.         printf("[unescaper] Yes!\n");
  168.         return true;
  169.     }
  170.     if (strstr(path, ourBundle)) {
  171.         printf("[unescaper] Yes!\n");
  172.         return true;
  173.     }
  174.    
  175.     // smart boi haha
  176.     // bypass "//", ".", ".." etc
  177.     path = realPath(path);
  178.     if (!path) {
  179.         printf("[unescaper] No!\n");
  180.         return false;
  181.     }
  182.    
  183.     if (!strlen(path)) {
  184.         printf("[unescaper] No!\n");
  185.         return false;
  186.     }
  187.    
  188.     // nothing can decieve us
  189.     if (isMacho(path) || endsWith(path, ".app")) {
  190.         bool rv = wouldAmfidLikeThis(path);
  191.         printf("[unescaper][shall exist] returned: %d\n", rv);
  192.         return rv;
  193.     }
  194.     else {
  195.         bool rv;
  196.         rv = (
  197.               !(strstr(path, "apt") ||
  198.                 strstr(path, "dropbear") ||
  199.                 strstr(path, "ssh") ||
  200.                 strstr(path, "filza") ||
  201.                 strstr(path, "git-core") ||
  202.                 strstr(path, "gnupg") ||
  203.                 strstr(path, "cydia") ||
  204.                 strstr(path, "dpkg") ||
  205.                 strstr(path, "theos") ||
  206.                 strstr(path, "Substrate") ||
  207.                 startsWith(path, "/Library/LaunchDaemons") ||
  208.                 startsWith(path, "/Library/Themes") ||
  209.                 startsWith(path, "/private/var/stash") ||
  210.                 startsWith(path, "/var/stash")
  211.                 )
  212.               );
  213.        
  214.         printf("[unescaper][shall exist] returned: %d\n", (int)rv);
  215.         return rv;
  216.     }
  217. }
  218.  
  219. FILE* our_fopen(const char * filename, const char * mode) {
  220.    
  221.     if (!filename || !mode) return orig_fopen(filename, mode);
  222.    
  223.     NSLog(@"[unescaper] fopen %s\n", filename);
  224.  
  225.     if (!shallExist(filename)) {
  226.         errno = ENOENT;
  227.         return NULL;
  228.     }
  229.  
  230.     if (isOutsideSandbox(filename)) {
  231.         errno = EPERM;
  232.         return NULL;
  233.     }
  234.     return orig_fopen(filename, mode);
  235. }
  236.  
  237. int our_access(const char *pathname, int mode) {
  238.    
  239.     if (!pathname) return orig_access(pathname, mode);
  240.    
  241.     NSLog(@"[unescaper] access %s\n", pathname);
  242.    
  243.     if (!shallExist(pathname)) {
  244.         errno = ENOENT;
  245.         return -1;
  246.     }
  247.    
  248.     if (mode != F_OK) {
  249.         if (isOutsideSandbox(pathname)) {
  250.             errno = EPERM;
  251.             return -1;
  252.         }
  253.     }
  254.    
  255.     return orig_access(pathname, mode);
  256. }
  257.  
  258. int our_open(const char *path, int oflags) {
  259.    
  260.     if (!path) return orig_open(path, oflags);
  261.    
  262.     NSLog(@"[unescaper] open %s\n", path);
  263.    
  264.     if (!shallExist(path)) {
  265.         errno = ENOENT;
  266.         return -1;
  267.     }
  268.    
  269.     if (isOutsideSandbox(path)) {
  270.         errno = EPERM;
  271.         return -1;
  272.     }
  273.     return orig_open(path, oflags);
  274. }
  275.  
  276. DIR *our_opendir(const char *path) {
  277.    
  278.     if (!path) return orig_opendir(path);
  279.    
  280.     NSLog(@"[unescaper] opendir %s\n", path);
  281.    
  282.     if (!shallExist(path)) {
  283.         errno = ENOENT;
  284.         return NULL;
  285.     }
  286.    
  287.     if (isOutsideSandbox(path)) {
  288.         errno = EPERM;
  289.         return NULL;
  290.     }
  291.     return orig_opendir(path);
  292. }
  293.  
  294. int our_stat(const char *pathname, struct stat *statbuf) {
  295.    
  296.     if (!pathname) return orig_stat(pathname, statbuf);
  297.    
  298.     NSLog(@"[unescaper] stat %s\n", pathname);
  299.    
  300.     if (!shallExist(pathname)) {
  301.         errno = ENOENT;
  302.         return -1;
  303.     }
  304.    
  305.     if (isOutsideSandbox(pathname)) {
  306.         errno = EPERM;
  307.         return -1;
  308.     }
  309.     return orig_stat(pathname, statbuf);
  310. }
  311.  
  312. int our_lstat(const char *pathname, struct stat *statbuf) {
  313.    
  314.     if (!pathname || !statbuf) return orig_lstat(pathname, statbuf);
  315.    
  316.     NSLog(@"[unescaper] lstat %s\n", pathname);
  317.    
  318.     if (!shallExist(pathname)) {
  319.         errno = ENOENT;
  320.         return -1;
  321.     }
  322.    
  323.     if (isOutsideSandbox(pathname)) {
  324.         errno = EPERM;
  325.         return -1;
  326.     }
  327.     return orig_lstat(pathname, statbuf);
  328. }
  329.  
  330. int our_statfs(const char *path, struct statfs *buf) {
  331.    
  332.     if (!path || !buf) return orig_statfs(path, buf);
  333.    
  334.     NSLog(@"[unescaper] statfs %s\n", path);
  335.    
  336.     if (!shallExist(path)) {
  337.         errno = ENOENT;
  338.         return -1;
  339.     }
  340.    
  341.     if (isOutsideSandbox(path)) {
  342.         errno = EPERM;
  343.         return -1;
  344.     }
  345.     return orig_statfs(path, buf);
  346. }
  347.  
  348. int our_getattrlist(const char *path, struct attrlist * attr, void * info, size_t size, unsigned int smth) {
  349.    
  350.     if (!path || !attr || !info) return orig_getattrlist(path, attr, info, size, smth);
  351.    
  352.     NSLog(@"[unescaper] getattrlist %s\n", path);
  353.    
  354.     if (!shallExist(path)) {
  355.         errno = ENOENT;
  356.         return -1;
  357.     }
  358.    
  359.     if (isOutsideSandbox(path)) {
  360.         errno = EPERM;
  361.         return -1;
  362.     }
  363.     return orig_getattrlist(path, attr, info, size, smth);
  364. }
  365.  
  366. ssize_t our_readlink(const char *path, char *buf, size_t bufsiz) {
  367.    
  368.     if (!path || !buf) return orig_readlink(path, buf, bufsiz);
  369.    
  370.     NSLog(@"[unescaper] readlink %s\n", path);
  371.    
  372.     if (!shallExist(path)) {
  373.         errno = ENOENT;
  374.         return -1;
  375.     }
  376.    
  377.     if (isOutsideSandbox(path)) {
  378.         errno = EPERM;
  379.         return -1;
  380.     }
  381.     return orig_readlink(path, buf, bufsiz);
  382. }
  383.  
  384. const char* our_dyld_get_image_name(uint32_t image_index) {
  385.     printf("[unescaper] Called _dyld_get_image_name!\n");
  386.    
  387.     char *buf = (char *)malloc(strlen(ourContainer) + strlen("/tmp/UNESCAPER.txt") + 1);
  388.     strcpy(buf, ourContainer);
  389.     strcat(buf, "/tmp/UNESCAPER.txt");
  390.    
  391.     FILE *f = orig_fopen(buf, "r");
  392.    
  393.     free(buf);
  394.     buf = NULL;
  395.    
  396.     if (!f) {
  397.         NSLog(@"[unescaper] My dear friend, something's wrong. Expect weird things. KK!");
  398.         printf("[unescaper] err %s\n", strerror(errno));
  399.         return orig_dyld_get_image_name(image_index);
  400.     }
  401.    
  402.    
  403.     uint32_t index = -1;
  404.     size_t size = 0;
  405.     char ch = fgetc(f);
  406.    
  407. loop:;
  408.     while (ch != '\n') {
  409.         if (ch == EOF) {
  410.             fclose(f);
  411.             return NULL;
  412.         }
  413.         ch = fgetc(f);
  414.         size++;
  415.     }
  416.     index++;
  417.     if (index != image_index) {
  418.         ch = fgetc(f);
  419.         size = 0;
  420.         goto loop;
  421.     }
  422.    
  423.     buf = (char *)malloc(size + 1);
  424.     bzero(buf, size + 1);
  425.     fseek(f, -(size + 1), SEEK_CUR);
  426.     fread(buf, size, 1, f);
  427.     fclose(f);
  428.    
  429.     char *str = strdup(buf);
  430.     free(buf);
  431.     buf = NULL;
  432.    
  433.     printf("[unescaper] At index %u we have %s\n", image_index, str);
  434.    
  435.     return str;
  436. }
  437.  
  438. uint32_t our_dyld_image_count() {
  439.  
  440.     printf("[unescaper] Getting image count!\n");
  441.      
  442.     char *buf = (char *)malloc(strlen(ourContainer) + strlen("/tmp/UNESCAPER.txt") + 1);
  443.     strcpy(buf, ourContainer);
  444.     strcat(buf, "/tmp/UNESCAPER.txt");
  445.    
  446.     FILE *f = orig_fopen(buf, "r");
  447.    
  448.     free(buf);
  449.     buf = NULL;
  450.    
  451.     if (!f) {
  452.         NSLog(@"[unescaper] My dear friend, something's wrong. Expect weird things. KK!");
  453.         printf("[unescaper] err %s\n", strerror(errno));
  454.         return orig_dyld_image_count();
  455.     }
  456.    
  457.     uint32_t count = 0;
  458.     char ch = fgetc(f);
  459.    
  460.     while (ch != EOF) {
  461.         if (ch == '\n') count++;
  462.         ch = fgetc(f);
  463.     }
  464.     printf("[unescaper] found %u libs\n", count);
  465.     return count;
  466. }
  467.  
  468. void *our_dlopen(const char *filename, int flag) {
  469.    
  470.     if (!filename) return orig_dlopen(filename, flag);
  471.  
  472.     // inject anyway
  473.     // so kids can still use their cheats
  474.  
  475.     void *h = orig_dlopen(filename, flag);
  476.    
  477.     // in dyld shared cache
  478.     if (h && orig_access(filename, F_OK)) {
  479.        
  480.         char *buf = (char *)malloc(strlen(ourContainer) + strlen("/tmp/UNESCAPER.txt") + 1);
  481.         strcpy(buf, ourContainer);
  482.         strcat(buf, "/tmp/UNESCAPER.txt");
  483.        
  484.         FILE *f = orig_fopen(buf, "a+");
  485.        
  486.         free(buf);
  487.         buf = NULL;
  488.        
  489.         if (!f) {
  490.             NSLog(@"[unescaper] My dear friend, something's wrong. Expect weird things. Ohi!");
  491.             strcpy(fake_err, orig_dlerror());
  492.             return h;
  493.         }
  494.        
  495.         buf = (char *)malloc(strlen(filename) + 1);
  496.         bzero(buf, strlen(filename) + 1);
  497.        
  498.         int off = 0;
  499.         bool found = false;
  500.        
  501.         while (!feof(f)) {
  502.             fread(buf, strlen(filename), 1, f);
  503.             fseek(f, off, SEEK_SET);
  504.            
  505.             printf("[-] Found %s and looking for %s\n", buf, filename);
  506.             if (!strcmp(buf, filename)) {
  507.                 printf("[unescaper] %s already added!\n", filename);
  508.                 found = true;
  509.                 break;
  510.             }
  511.             fgetc(f);
  512.             off++;
  513.         }
  514.         if (!found) fprintf(f, "%s\n", filename);
  515.         fclose(f);
  516.        
  517.         strcpy(fake_err, "");
  518.         return h;
  519.     }
  520.    
  521.     // loaded & legit
  522.     if (h && wouldAmfidLikeThis(filename)) {
  523.        
  524.         char *buf = (char *)malloc(strlen(ourContainer) + strlen("/tmp/UNESCAPER.txt") + 1);
  525.         strcpy(buf, ourContainer);
  526.         strcat(buf, "/tmp/UNESCAPER.txt");
  527.        
  528.         FILE *f = orig_fopen(buf, "a");
  529.        
  530.         free(buf);
  531.         buf = NULL;
  532.        
  533.         if (!f) {
  534.             NSLog(@"[unescaper] My dear friend, something's wrong. Expect weird things. Ohi!");
  535.             strcpy(fake_err, orig_dlerror());
  536.             return h;
  537.         }
  538.         fprintf(f, "%s\n", filename);
  539.         fclose(f);
  540.         return h;
  541.     }
  542.    
  543.     // not legit
  544.     if (!shallExist(filename)) {
  545.         // poor guy
  546.         // he'll never know injection worked
  547.        
  548.         sprintf(fake_err, "dlopen(%s, %d): image not found", filename, flag);
  549.        
  550.         return NULL;
  551.     }
  552.    
  553.     // legit but not loaded
  554.     strcpy(fake_err, orig_dlerror());
  555.     return h;
  556. }
  557.  
  558. char *our_dlerror() {
  559.     if (!strcmp(fake_err, "")) return NULL;
  560.    
  561.     char *err = strdup(fake_err);
  562.     strcpy(fake_err, "");
  563.     return err;
  564. }
  565.  
  566. char *our_getenv(const char * name) {
  567.     if (!strcmp(name, "DYLD_INSERT_LIBRARIES")) {
  568.         return NULL;
  569.     }
  570.     return orig_getenv(name);
  571. }
  572. void *our_dlsym(void *handle, const char *name) {
  573.     if (!strcmp(name, "fopen")) return (void*)our_fopen;
  574.     if (!strcmp(name, "open")) return (void*)our_open;
  575.     if (!strcmp(name, "access")) return (void*)our_access;
  576.     //if (strcmp(name, "lstat")) return (void*)our_lstat;
  577.     if (!strcmp(name, "stat")) return (void*)our_stat;
  578.     if (!strcmp(name, "statfs")) return (void*)our_statfs;
  579.     if (!strcmp(name, "readlink")) return (void*)our_readlink;
  580.     if (!strcmp(name, "dlopen")) return (void*)our_dlopen;
  581.     if (!strcmp(name, "dlsym")) return (void*)our_dlsym;
  582.     if (!strcmp(name, "dlerror")) return (void*)our_dlerror;
  583.     if (!strcmp(name, "_dyld_get_image_name")) return (void*)our_dyld_get_image_name;
  584.     if (!strcmp(name, "getattrlist")) return (void*)our_getattrlist;
  585.     if (!strcmp(name, "_dyld_image_count")) return (void*)our_dyld_image_count;
  586.    
  587.     return orig_dlsym(handle, name);
  588. }
  589.  
  590. %hook NSFileManager
  591. -(BOOL)fileExistsAtPath:(NSString *)path {
  592.     if (!path) return %orig;
  593.    
  594.     NSLog(@"FileExistsAtPath %@", path);
  595.    
  596.     if (!shallExist(strdup([path UTF8String]))) {
  597.         return NO;
  598.     }
  599.     if (isOutsideSandbox(strdup([path UTF8String]))) {
  600.         return NO;
  601.     }
  602.     return %orig;
  603. }
  604.  
  605. -(BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory {
  606.     if (!path) return %orig;
  607.    
  608.     NSLog(@"FileExistsAtPath:isDir %@", path);
  609.    
  610.     if (!shallExist(strdup([path UTF8String]))) {
  611.         return NO;
  612.     }
  613.     if (isOutsideSandbox(strdup([path UTF8String]))) {
  614.         return NO;
  615.     }
  616.     return %orig;
  617. }
  618.  
  619. - (NSArray *)contentsOfDirectoryAtURL:(NSURL *)url error:(NSError **)error {
  620.    
  621.     if (strstr([[url absoluteString] UTF8String], "://")) return %orig;
  622.    
  623.     NSLog(@"contentsOfDirAtURL %@", [url absoluteString]);
  624.    
  625.     if (!shallExist(strdup([[url absoluteString] UTF8String]))) {
  626.        
  627.         NSMutableDictionary *cpy = [NSMutableDictionary dictionary];
  628.         [cpy setObject:[url absoluteString] forKey:@"NSFilePath"];
  629.         [cpy setObject:@"Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\"" forKey:@"NSUnderlyingError"];
  630.         [cpy setObject:@[@"Folder"] forKey:@"NSUserStringVariant"];
  631.        
  632.         *error = [NSError errorWithDomain:NSCocoaErrorDomain code:2 userInfo:cpy];
  633.        
  634.         return nil;
  635.     }
  636.     if (isOutsideSandbox(strdup([[url absoluteString] UTF8String]))) {
  637.        
  638.         NSMutableDictionary *cpy = [NSMutableDictionary dictionary];
  639.         [cpy setObject:[url absoluteString] forKey:@"NSFilePath"];
  640.         [cpy setObject:@"Error Domain=NSPOSIXErrorDomain Code=1 \"Operation not permitted\"" forKey:@"NSUnderlyingError"];
  641.         [cpy setObject:@[@"Folder"] forKey:@"NSUserStringVariant"];
  642.        
  643.         *error = [NSError errorWithDomain:NSCocoaErrorDomain code:1 userInfo:cpy];
  644.        
  645.         return nil;
  646.     }
  647.     return %orig;
  648. }
  649.  
  650.  
  651. - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error {
  652.  
  653.     NSLog(@"contentsOfDirAtURL %@", path);
  654.    
  655.     if (!shallExist(strdup([path UTF8String]))) {
  656.        
  657.         NSMutableDictionary *cpy = [NSMutableDictionary dictionary];
  658.         [cpy setObject:path forKey:@"NSFilePath"];
  659.         [cpy setObject:@"Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\"" forKey:@"NSUnderlyingError"];
  660.         [cpy setObject:@[@"Folder"] forKey:@"NSUserStringVariant"];
  661.        
  662.         *error = [NSError errorWithDomain:NSCocoaErrorDomain code:2 userInfo:cpy];
  663.        
  664.         return nil;
  665.     }
  666.     if (isOutsideSandbox(strdup([path UTF8String]))) {
  667.        
  668.         NSMutableDictionary *cpy = [NSMutableDictionary dictionary];
  669.         [cpy setObject:path forKey:@"NSFilePath"];
  670.         [cpy setObject:@"Error Domain=NSPOSIXErrorDomain Code=1 \"Operation not permitted\"" forKey:@"NSUnderlyingError"];
  671.         [cpy setObject:@[@"Folder"] forKey:@"NSUserStringVariant"];
  672.        
  673.         *error = [NSError errorWithDomain:NSCocoaErrorDomain code:1 userInfo:cpy];
  674.        
  675.         return nil;
  676.     }
  677.     return %orig;
  678. }
  679.  
  680.  
  681. - (NSDirectoryEnumerator<NSString *> *)enumeratorAtPath:(NSString *)path {
  682.     if (!path) return %orig;
  683.    
  684.     NSLog(@"Enumerator %@", path);
  685.    
  686.     if (!shallExist(strdup([path UTF8String]))) {
  687.         return nil;
  688.     }
  689.     if (isOutsideSandbox(strdup([path UTF8String]))) {
  690.         return nil;
  691.     }
  692.     return %orig;
  693. }
  694. %end
  695.  
  696. %hook UIApplication
  697. -(BOOL)canOpenURL:(NSURL *)arg1 {
  698.     NSLog(@"canOpenURL %@", [arg1 absoluteString]);
  699.     char *url = strdup([[arg1 absoluteString] UTF8String]);
  700.    
  701.     return !(strstr(url, "cydia") || strstr(url, "filza") || strstr(url, "activator"));
  702. }
  703. - (BOOL)openURL:(NSURL *)arg1  {
  704.     NSLog(@"openURL %@", [arg1 absoluteString]);
  705.     char *url = strdup([[arg1 absoluteString] UTF8String]);
  706.    
  707.     return !(strstr(url, "cydia") || strstr(url, "filza") || strstr(url, "activator"));
  708. }
  709. %end
  710.  
  711. %hook NSString
  712.  
  713.  
  714. - (NSString*)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error {
  715.    
  716.     NSLog(@"stringWithFile %@", path);
  717.    
  718.     if (!shallExist(strdup([path UTF8String]))) {
  719.        
  720.         NSMutableDictionary *cpy = [NSMutableDictionary dictionary];
  721.         [cpy setObject:path forKey:@"NSFilePath"];
  722.         [cpy setObject:@"Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\"" forKey:@"NSUnderlyingError"];
  723.        
  724.         *error = [NSError errorWithDomain:NSCocoaErrorDomain code:2 userInfo:cpy];
  725.        
  726.         return nil;
  727.     }
  728.     if (isOutsideSandbox(strdup([path UTF8String]))) {
  729.        
  730.         NSMutableDictionary *cpy = [NSMutableDictionary dictionary];
  731.         [cpy setObject:path forKey:@"NSFilePath"];
  732.         [cpy setObject:@"Error Domain=NSPOSIXErrorDomain Code=1 \"Operation not permitted\"" forKey:@"NSUnderlyingError"];
  733.        
  734.         *error = [NSError errorWithDomain:NSCocoaErrorDomain code:1 userInfo:cpy];
  735.        
  736.         return nil;
  737.     }
  738.     return %orig;
  739. }
  740.  
  741. - (NSString*)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error {
  742.    
  743.     if (strstr([[url absoluteString] UTF8String], "://")) return %orig;
  744.    
  745.     NSLog(@"stringWithFile %@", [url absoluteString]);
  746.    
  747.     if (!shallExist(strdup([[url absoluteString] UTF8String]))) {
  748.        
  749.         NSMutableDictionary *cpy = [NSMutableDictionary dictionary];
  750.         [cpy setObject:[url absoluteString] forKey:@"NSFilePath"];
  751.         [cpy setObject:@"Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\"" forKey:@"NSUnderlyingError"];
  752.        
  753.         *error = [NSError errorWithDomain:NSCocoaErrorDomain code:2 userInfo:cpy];
  754.        
  755.         return nil;
  756.     }
  757.     if (isOutsideSandbox(strdup([[url absoluteString] UTF8String]))) {
  758.        
  759.         NSMutableDictionary *cpy = [NSMutableDictionary dictionary];
  760.         [cpy setObject:[url absoluteString] forKey:@"NSFilePath"];
  761.         [cpy setObject:@"Error Domain=NSPOSIXErrorDomain Code=1 \"Operation not permitted\"" forKey:@"NSUnderlyingError"];
  762.        
  763.         *error = [NSError errorWithDomain:NSCocoaErrorDomain code:1 userInfo:cpy];
  764.        
  765.         return nil;
  766.     }
  767.     return %orig;
  768. }
  769.  
  770. %end
  771.  
  772. %hook AppsFlyerUtils
  773. + (id) isJailBreakon {
  774.     return nil;
  775. }
  776. %end
  777. %hook ANSMetadata
  778. - (BOOL) computeIsJailbroken {
  779.     return NO;
  780. }
  781. %end
  782. %hook QPLibraryConfiguration
  783. -(NSNumber *)runtimeConfigurationValueForKey:(NSString *)key {
  784.     if ([key isEqualToString:@"REQUIRE_SECURE_DEVICE"]) return [NSNumber numberWithBool:NO];
  785.     return %orig;
  786. }
  787. %end
  788. %hook USParametersProvider
  789. -(BOOL)isJailbroken {
  790.     return NO;
  791. }
  792. %end
  793. %hook FuseSDK
  794. -(BOOL)isJailBroken {
  795.     return NO;
  796. }
  797. %end
  798. %hook GTMSessionFetcher
  799. -(BOOL)appAllowsInsecureRequests {
  800.     return YES;
  801. }
  802. %end
  803.  
  804.  
  805. %ctor {
  806.  
  807.     // setup server
  808.     messagingCenter = [CPDistributedMessagingCenter centerNamed:@"com.jakeashacks.unescaperd"]; //setup our message center
  809.     rocketbootstrap_distributedmessagingcenter_apply(messagingCenter); //use rocketbootstrap to get around sandbox
  810.    
  811.     if (!messagingCenter) {
  812.         NSLog(@"[unescaper] My dear friend, something's wrong. Expect weird things. HHH!");
  813.         return;
  814.     }
  815.    
  816.     ourBundle = strdup([[[NSBundle mainBundle] bundlePath] UTF8String]);
  817.     ourContainer = strdup([NSHomeDirectory() UTF8String]);
  818.    
  819.     // setup a list of dylibs
  820.     uint32_t image_index;
  821.     uint32_t count = _dyld_image_count();
  822.    
  823.     printf("[unescaper] opening %s\n", [[@(ourContainer) stringByAppendingPathComponent:@"tmp/UNESCAPER.txt"] UTF8String]);
  824.    
  825.     FILE *f = fopen([[@(ourContainer) stringByAppendingPathComponent:@"tmp/UNESCAPER.txt"] UTF8String], "w");
  826.     if (!f) {
  827.         NSLog(@"[unescaper] My dear friend, something's wrong. Expect weird things. bubah!");
  828.         return;
  829.     }
  830.    
  831.     printf("[unescaper] adding %s on allowed dylibs\n", _dyld_get_image_name(0));
  832.     fprintf(f, "%s\n", _dyld_get_image_name(0));
  833.    
  834.     for (image_index = 1; image_index < count; image_index ++) {
  835.         if (wouldAmfidLikeThis(_dyld_get_image_name(image_index))) {
  836.             printf("[unescaper] adding %s on allowed dylibs\n", _dyld_get_image_name(image_index));
  837.             fprintf(f, "%s\n", _dyld_get_image_name(image_index));
  838.         }
  839.     }
  840.     fclose(f);
  841.    
  842.     //fake_err = [NSMutableString stringWithString:@""];
  843.     strcpy(fake_err, "");
  844.    
  845.     printf("[unescaper] hooking fopen\n");
  846.     MSHookFunction((void *)fopen, (void *)our_fopen, (void **)&orig_fopen);
  847.    
  848.     printf("[unescaper] hooking open\n");
  849.     MSHookFunction((void *)open, (void *)our_open, (void **)&orig_open);
  850.    
  851.     printf("[unescaper] hooking access\n");
  852.     MSHookFunction((void *)access, (void *)our_access, (void **)&orig_access);
  853.    
  854.     printf("[unescaper] hooking stat\n");
  855.     MSHookFunction((void *)stat, (void *)our_stat, (void **)&orig_stat);
  856.    
  857.     printf("[unescaper] hooking getattrlist\n");
  858.     MSHookFunction((void *)getattrlist, (void *)our_getattrlist, (void **)&orig_getattrlist);
  859.    
  860.     printf("[unescaper] hooking statfs\n");
  861.     MSHookFunction((void *)statfs, (void *)our_statfs, (void **)&orig_statfs);
  862.    
  863.     printf("[unescaper] hooking readlink\n");
  864.     MSHookFunction((void *)readlink, (void *)our_readlink,(void **) &orig_readlink);
  865.     printf("[unescaper] hooking getimgname\n");
  866.     MSHookFunction((void *)_dyld_get_image_name, (void *)our_dyld_get_image_name, (void **)&orig_dyld_get_image_name);
  867.     printf("[unescaper] hooking imgcnt\n");
  868.     MSHookFunction((void *)_dyld_image_count, (void *)our_dyld_image_count, (void **)&orig_dyld_image_count);
  869.     printf("[unescaper] hooking dlopen\n");
  870.     MSHookFunction((void *)dlopen, (void *)our_dlopen, (void **)&orig_dlopen);
  871.    
  872.     printf("[unescaper] hooking dlsym\n");
  873.     MSHookFunction((void *)dlsym, (void *)our_dlsym, (void **)&orig_dlsym);
  874.     printf("[unescaper] hooking dlerror\n");
  875.     MSHookFunction((void *)dlerror, (void *)our_dlerror, (void **)&orig_dlerror);
  876.     printf("[unescaper] hooking getenv\n");
  877.     MSHookFunction((void *)getenv, (void *)our_getenv, (void **)&orig_getenv);
  878.    
  879.     return;
  880. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement