-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathItemEnumerator.m
executable file
·150 lines (117 loc) · 3.99 KB
/
ItemEnumerator.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
//
// ItemEnumerator.m
// KnockKnock
//
// Created by Patrick Wardle on 4/24/15.
// Copyright (c) 2015 Objective-See. All rights reserved.
//
#import "Consts.h"
#import "Utilities.h"
#import "ItemEnumerator.h"
@implementation ItemEnumerator
@synthesize launchItems;
@synthesize applications;
@synthesize launchItemsEnumerator;
@synthesize applicationsEnumerator;
//plugin search directories
NSString * const LAUNCHITEM_SEARCH_DIRECTORIES[] = {@"/System/Library/LaunchDaemons/", @"/Library/LaunchDaemons/", @"/System/Library/LaunchAgents/", @"/Library/LaunchAgents/", @"~/Library/LaunchAgents/"};
//enumerate all 'shared' items
// ->that is to say, items that multiple plugins scan/process
-(void)start
{
//save self's thread
self.enumeratorThread = [NSThread currentThread];
//alloc/init thread to enumerate launch items
launchItemsEnumerator = [[NSThread alloc] initWithTarget:self selector:@selector(enumerateLaunchItems) object:nil];
//alloc/init thread to enumerate installed applications
applicationsEnumerator = [[NSThread alloc] initWithTarget:self selector:@selector(enumerateApplications) object:nil];
//start launch item enumerator thread
[self.launchItemsEnumerator start];
//start installed application enumerator thread
[self.applicationsEnumerator start];
return;
}
//cancel all enumerator threads
-(void)stop
{
//cancel launch item enumerator thread
if(YES == [self.launchItemsEnumerator isExecuting])
{
//cancel
[self.launchItemsEnumerator cancel];
}
//cancel installed application enumerator thread
if(YES == [self.applicationsEnumerator isExecuting])
{
//cancel
[self.applicationsEnumerator cancel];
}
//set launch items array to nil
self.launchItems = nil;
//set installed app array to nil
self.applications = nil;
return;
}
//generate list of all launch items (daemons & agents)
-(void)enumerateLaunchItems
{
//all launch items
NSMutableArray* allLaunchItems = nil;
//all launch item directories, expanded
NSMutableArray* launchItemDirectories = nil;
//alloc array for all launch items
allLaunchItems = [NSMutableArray array];
//expand list
launchItemDirectories = expandPaths(LAUNCHITEM_SEARCH_DIRECTORIES, sizeof(LAUNCHITEM_SEARCH_DIRECTORIES)/sizeof(LAUNCHITEM_SEARCH_DIRECTORIES[0]));
//iterate over all launch item direcoties
// grab all .plists and add to cumulative array
for(NSString* launchItemDirectory in launchItemDirectories)
{
//grab all plist
for(NSString* plist in directoryContents(launchItemDirectory, @"self ENDSWITH '.plist'"))
{
//build full path to item/plist and save
[allLaunchItems addObject:[launchItemDirectory stringByAppendingPathComponent:plist]];
}
}
//save into iVar
self.launchItems = allLaunchItems;
return;
}
//generate list of all installed applications
// ->save into iVar, 'applications'
-(void)enumerateApplications
{
//output from system profiler task
NSData* taskOutput = nil;
//serialized task output
NSArray* serializedOutput = nil;
//exec system profiler
taskOutput = execTask(SYSTEM_PROFILER, @[@"SPApplicationsDataType", @"-xml", @"-detailLevel", @"mini"], NULL);
if( (nil == taskOutput) ||
(0 == taskOutput.length) )
{
//bail
goto bail;
}
//serialize output to array
serializedOutput = [NSPropertyListSerialization propertyListWithData:taskOutput options:kNilOptions format:NULL error:NULL];
//grab list of installed apps from '_items' key
// ->save into iVar 'applications'
@try
{
//save
self.applications = serializedOutput[0][@"_items"];
}
@catch (NSException *exception)
{
//err msg
//NSLog(@"ERROR: serialized output not formatted as expected");
//bail
goto bail;
}
//bail
bail:
return;
}
@end