IOS witchcraft: the unkillable background & monitoring process

IOS witchcraft: the unkillable background & monitoring process

Implementation without jailbreak:

Startup: After the App is installed on an iOS device, the App will start as soon as the iOS device is restarted, regardless of whether the App has been opened before.

***Background running: The application enters the background state and can run in the background without being killed by the system;

Monitor process: can obtain the apps running on IOS devices other than the system (including those running in progress and in the background);

Configure the project plist file

Add to:

  1. <key>UIBackgroundModes</key>
  2.  
  3. <array>
  4.  
  5. <string>voip</string>
  6.  
  7. </array>


Functional class: ProccessHelper

  1. [objc] view plaincopy
  2.  
  3. # import <Foundation/Foundation.h>
  4.  
  5. @interface ProcessHelper : NSObject
  6.  
  7. + (NSArray *)runningProcesses;
  8.  
  9. @end  
  10.  
  11. [cpp] view plaincopyprint?
  12. # import   "ProccessHelper.h"  
  13. //#include<objc/runtime.h>  
  14. #include <sys/sysctl.h>
  15.  
  16. #include <stdbool.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <sys/sysctl.h>
  20.  
  21. @implementation ProccessHelper
  22.  
  23. //You can determine if your app is being run under the debugger with the following code from  
  24. static bool AmIBeingDebugged( void )
  25. // Returns true if the current process is being debugged (either  
  26. // running under the debugger or has a debugger attached post facto).  
  27. {
  28. int junk;
  29. int mib[ 4 ];
  30. struct kinfo_proc info;
  31. size_t size;
  32.  
  33. // Initialize the flags so that, if sysctl fails for some bizarre  
  34. // reason, we get a predictable result.  
  35.  
  36. info.kp_proc.p_flag = 0 ;
  37.  
  38. // Initialize mib, which tells sysctl the info we want, in this case  
  39. // we're looking for information about a specific process ID.  
  40.  
  41. mib[ 0 ] = CTL_KERN;
  42. mib[ 1 ] = KERN_PROC;
  43. mib[ 2 ] = KERN_PROC_PID;
  44. mib[ 3 ] = getpid();
  45.  
  46. // Call sysctl.  
  47.  
  48. size = sizeof(info);
  49. junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0 );
  50. assert (junk == 0 );
  51.  
  52. // We're being debugged if the P_TRACED flag is set.  
  53.  
  54. return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
  55. }
  56.  
  57. //Return the id, name, cpu usage, and running time of all running processes  
  58. //Use function int sysctl(int *, u_int, void *, size_t *, void *, size_t)  
  59. + (NSArray *)runningProcesses
  60. {
  61. //Specify the name parameter. The first element specifies which subsystem of the kernel this request is directed to, and the second and subsequent elements specify a part of the system in turn.  
  62. //CTL_KERN, KERN_PROC, KERN_PROC_ALL all running processes  
  63. int mib[ 4 ] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
  64.  
  65.  
  66. size_t miblen = 4 ;
  67. //Value-result parameter: When the function is called, the value pointed to by size specifies the size of the buffer; when the function returns, the value gives the amount of data stored in the buffer by the kernel  
  68. //If the buffer is not large enough, the function returns ENOMEM error  
  69. size_t size;
  70. //Return 0 if successful; return -1 if failed  
  71. int st = sysctl(mib, miblen, NULL, &size, NULL, 0 );
  72.  
  73. struct kinfo_proc * process = NULL;
  74. struct kinfo_proc * newprocess = NULL;
  75. do  
  76. {
  77. size += size / 10 ;
  78. newprocess = realloc(process, size);
  79. if (!newprocess)
  80. {
  81. if (process)
  82. {
  83. free(process);
  84. process = NULL;
  85. }
  86. return nil;
  87. }
  88.  
  89. process = newprocess;
  90. st = sysctl(mib, miblen, process, &size, NULL, 0 );
  91. } while (st == - 1 && errno == ENOMEM);
  92.  
  93. if (st == 0 )
  94. {
  95. if (size % sizeof(struct kinfo_proc) == 0 )
  96. {
  97. int nprocess = size / sizeof(struct kinfo_proc);
  98. if (nprocess)
  99. {
  100. NSMutableArray * array = [[NSMutableArray alloc] init];
  101. for ( int i = nprocess - 1 ; i >= 0 ; i--)
  102. {
  103. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  104. NSString * processID = [[NSString alloc] initWithFormat:@ "%d" , process[i].kp_proc.p_pid];
  105. NSString * processName = [[NSString alloc] initWithFormat:@ "%s" , process[i].kp_proc.p_comm];
  106. NSString * proc_CPU = [[NSString alloc] initWithFormat:@ "%d" , process[i].kp_proc.p_estcpu];
  107. double t = [[NSDate date] timeIntervalSince1970] - process[i].kp_proc.p_un.__p_starttime.tv_sec;
  108. NSString * proc_useTiem = [[NSString alloc] initWithFormat:@ "%f" ,t];
  109. NSString *startTime = [[NSString alloc] initWithFormat:@ "%ld" , process[i].kp_proc.p_un.__p_starttime.tv_sec];
  110. NSString * status = [[NSString alloc] initWithFormat:@ "%d" ,process[i].kp_proc.p_flag];
  111.  
  112. NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
  113. [dic setValue:processID forKey:@ "ProcessID" ];
  114. [dic setValue:processName forKey:@ "ProcessName" ];
  115. [dic setValue:proc_CPU forKey:@ "ProcessCPU" ];
  116. [dic setValue:proc_useTiem forKey:@ "ProcessUseTime" ];
  117. [dic setValue:proc_useTiem forKey:@ "ProcessUseTime" ];
  118. [dic setValue:startTime forKey:@ "startTime" ];
  119.  
  120. // 18432 is the currently running application  
  121. // 16384 is background  
  122. [dic setValue:status forKey:@ "status" ];
  123.  
  124. [processID release];
  125. [processName release];
  126. [proc_CPU release];
  127. [proc_useTiem release];
  128. [array addObject:dic];
  129. [startTime release];
  130. [status release];
  131. [dic release];
  132.  
  133. [pool release];
  134. }
  135.  
  136. free(process);
  137. process = NULL;
  138. //NSLog(@"array = %@",array);  
  139.  
  140. return array;
  141. }
  142. }
  143. }
  144.  
  145. return nil;
  146. }
  147.  
  148. @end   

Implementation code:

  1. [objc] view plaincopy
  2.  
  3. systemprocessArray = [[NSMutableArray arrayWithObjects:
  4. @ "kernel_task" ,
  5. @ "launchd" ,
  6. @ "UserEventAgent" ,
  7. @ "wifid" ,
  8. @ "syslogd" ,
  9. @ "powerd" ,
  10. @ "lockdownd" ,
  11. @ "mediaserverd" ,
  12. @ "mediaremoted" ,
  13. @ "mDNSResponder" ,
  14. @ "locationd" ,
  15. @ "imagent" ,
  16. @ "iapd" ,
  17. @ "fseventsd" ,
  18. @ "fairplayd.N81" ,
  19. @ "configd" ,
  20. @ "apsd" ,
  21. @ "aggregated" ,
  22. @ "SpringBoard" ,
  23. @ "CommCenterClassi" ,
  24. @ "BTServer" ,
  25. @ "notifyd" ,
  26. @ "MobilePhone" ,
  27. @ "ptpd" ,
  28. @ "afcd" ,
  29. @ "notification_pro" ,
  30. @ "notification_pro" ,
  31. @ "syslog_relay" ,
  32. @ "notification_pro" ,
  33. @ "springboardservi" ,
  34. @ "atc" ,
  35. @ "sandboxd" ,
  36. @ "networkd" ,
  37. @ "lsd" ,
  38. @ "securityd" ,
  39. @ "lockbot" ,
  40. @ "installd" ,
  41. @ "debugserver" ,
  42. @ "amfid" ,
  43. @ "AppleIDAuthAgent" ,
  44. @ "BootLaunch" ,
  45. @ "MobileMail" ,
  46. @ "BlueTool" ,
  47. nil nil] retain];
  48.  
  49.  
  50. [objc] view plaincopy
  51.  
  52. - ( void )applicationDidEnterBackground:(UIApplication *)application
  53. {
  54. while ( 1 ) {
  55. sleep( 5 );
  56. [self postMsg];
  57. }
  58.  
  59. [cpp] view plaincopyprint?
  60. [[UIApplication sharedApplication] setKeepAliveTimeout: 600 handler:^{
  61. NSLog(@ "KeepAlive" );
  62. }];
  63. }
  64.  
  65. - ( void )applicationWillResignActive:(UIApplication *)application
  66. {
  67. }
  68. - ( void )applicationWillEnterForeground:(UIApplication *)application
  69. {
  70. }
  71. - ( void )applicationDidBecomeActive:(UIApplication *)application
  72. {
  73. }
  74. - ( void )applicationWillTerminate:(UIApplication *)application
  75. {
  76. }
  77.  
  78. #pragma mark -
  79. #pragma mark - User Method
  80.  
  81. - ( void ) postMsg
  82. {
  83. //Upload to the server  
  84. NSURL *url = [self getURL];
  85. NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 10 ];
  86. NSError *error = nil;
  87. NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
  88.  
  89. if (error) {
  90. NSLog(@ "error:%@" , [error localizedDescription]);
  91. }
  92.  
  93. NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
  94. NSLog(@ "%@" ,str);
  95. }
  96.  
  97. - (NSURL *) getURL
  98. {
  99. UIDevice *device = [UIDevice currentDevice];
  100.  
  101. NSString* uuid = @ "TESTUUID" ;
  102. NSString* manufacturer = @ "apple" ;
  103. NSString* model = [device model];
  104. NSString* mobile = [device systemVersion];
  105.  
  106. NSString *msg = [NSString stringWithFormat:@ "Msg:%@ Time:%@" , [self processMsg], [self getTime]];
  107. CFShow(msg);
  108.  
  109. / Omit some code /
  110.  
  111. NSString *urlStr = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  112. NSURL *url = [NSURL URLWithString:urlStr];
  113.  
  114. return url;
  115. }
  116.  
  117. - (BOOL) checkSystemProccess:(NSString *) proName
  118. {
  119. if ([systemprocessArray containsObject:proName]) {
  120. return YES;
  121. }
  122. return NO;
  123. }
  124.  
  125. - (BOOL) checkFirst:(NSString *) string
  126. {
  127. NSString *str = [string substringToIndex: 1 ];
  128. NSRange r = [@ "ABCDEFGHIJKLMNOPQRSTUVWXWZ" rangeOfString:str];
  129.  
  130. if (r.length > 0 ) {
  131. return YES;
  132. }
  133. return NO;
  134. }
  135.  
  136. - (NSString *) processMsg
  137. {
  138. NSArray *proMsg = [ProccessHelper runningProcesses];
  139.  
  140. if (proMsg == nil) {
  141. return nil;
  142. }
  143.  
  144. NSMutableArray *proState = [NSMutableArray array];
  145. for (NSDictionary *dic in proMsg) {
  146.  
  147. NSString *proName = [dic objectForKey:@ "ProcessName" ];
  148. if (![self checkSystemProccess:proName] && [self checkFirst:proName]) {
  149. NSString *proID = [dic objectForKey:@ "ProcessID" ];
  150. NSString *proStartTime = [dic objectForKey:@ "startTime" ];
  151.  
  152. if ([[dic objectForKey:@ "status" ] isEqualToString:@ "18432" ]) {
  153. NSString *msg = [NSString stringWithFormat:@ "ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:YES" , proName, proID, proStartTime];
  154. [proState addObject:msg];
  155. } else {
  156. NSString *msg = [NSString stringWithFormat:@ "ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:NO" , proName, proID, proStartTime];
  157. [proState addObject:msg];
  158. }
  159. }
  160. }
  161.  
  162. NSString *msg = [proState componentsJoinedByString:@ "______" ];
  163. return msg;
  164. }
  165.  
  166. // Get the time  
  167. - (NSString *) getTime
  168. {
  169. NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
  170. formatter.dateStyle = NSDateFormatterMediumStyle;
  171. formatter.timeStyle = NSDateFormatterMediumStyle;
  172. formatter.locale = [NSLocale currentLocale];
  173.  
  174. NSDate *date = [NSDate date];
  175.  
  176. [formatter setTimeStyle:NSDateFormatterMediumStyle];
  177. NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
  178. NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
  179. NSInteger unitFlags = NSYearCalendarUnit |
  180. NSMonthCalendarUnit |
  181. NSDayCalendarUnit |
  182. NSWeekdayCalendarUnit |
  183. NSHourCalendarUnit |
  184. NSMinuteCalendarUnit |
  185. NSSecondCalendarUnit;
  186. comps = [calendar components:unitFlags fromDate:date];
  187. int year = [comps year];
  188. int month = [comps month];
  189. int day = [comps day];
  190. int hour = [comps hour];
  191. int min = [comps minute];
  192. int sec = [comps second];
  193.  
  194. NSString *time = [NSString stringWithFormat:@ "%d-%d-%d %d:%d:%d" , year, month, day, hour, min, sec];
  195.  
  196. return time;
  197. }
  198.  
  199. @end   

<<:  Task-based navigation design

>>:  Jack Ma: Alibaba and Tencent are the only Chinese companies in the first tier of the global Internet

Recommend

Tencent, Alibaba, and LeTV all use these four tricks to build cars

On July 6, 2016, the Roewe RX5, a joint effort of...

6 conversion rate analysis models to improve product conversion!

1. Why is conversion rate becoming more and more ...

Development of small programs for express logistics companies

Why do express delivery companies need to develop...

I heard that AI red envelope covers are super profitable

If ordinary people want to make money using AI, t...

Are you still using Python 2? You’re about to be eliminated!

[[132462]] Python 2.7 will end support in 2020, a...

E-commerce 618 | How to design posters for information flow promotion ads?

With the hot Internet e-commerce launching variou...

3+7 routines to teach you how to play poster fission

There is a very important way to attract fans to ...

How to create a Douyin product details page? What are the requirements?

Do Douyin store merchants know how to ensure the ...