-
Notifications
You must be signed in to change notification settings - Fork 13
/
Filter.m
726 lines (592 loc) · 19 KB
/
Filter.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//
// Filter.m
// TaskExplorer
//
// Created by Patrick Wardle on 2/21/15.
// Copyright (c) 2015 Objective-See. All rights reserved.
//
#import "Task.h"
#import "Consts.h"
#import "Filter.h"
#import "ItemBase.h"
#import "Utilities.h"
#import "Connection.h"
#import "AppDelegate.h"
//binary filter keywords
NSString * const BINARY_KEYWORDS[] = {@"#apple", @"#nonapple", @"#signed", @"#unsigned", @"#flagged", @"#encrypted", @"#packed", @"#notfound"};
@implementation Filter
@synthesize binaryFilters;
//init
-(id)init
{
//init super
self = [super init];
if(nil != self)
{
//alloc binary filter keywords
binaryFilters = [NSMutableArray array];
//init binary filters
for(NSUInteger i=0; i < sizeof(BINARY_KEYWORDS)/sizeof(BINARY_KEYWORDS[0]); i++)
{
//add
[self.binaryFilters addObject:BINARY_KEYWORDS[i]];
}
}
return self;
}
//determine if search string is #keyword
-(BOOL)isKeyword:(NSString*)searchString
{
//for now just check in binary keywords
return [self.binaryFilters containsObject:searchString];
}
//filter all for global search
// ->tasks, dylibs, files, & connections
-(void)filterAll:(NSString*)filterText items:(NSMutableDictionary*)items results:(NSMutableArray*)results
{
//flag
BOOL isKeyword = NO;
//matching tasks
NSMutableArray* matchingTasks = nil;
//matching dylibs
NSMutableArray* matchingDylibs = nil;
//matching files
NSMutableArray* matchingFiles = nil;
//matching connections
NSMutableArray* matchingConnections = nil;
//alloc for matching tasks
matchingTasks = [NSMutableArray array];
//alloc for matching dylibs
matchingDylibs = [NSMutableArray array];
//alloc for matching files
matchingFiles = [NSMutableArray array];
//alloc for matching connections
matchingConnections = [NSMutableArray array];
//set flag
isKeyword = [self isKeyword:filterText];
//filter all tasks
[self filterTasks:filterText items:items results:matchingTasks pane:PANE_SEARCH];
//add all to cumulative results
[results addObjectsFromArray:matchingTasks];
//iterate over all tasks
// ->for each, filter their dylib, files, etc
for(Task* task in items.allValues)
{
//filter dylibs
[self filterFiles:filterText items:task.dylibs results:matchingDylibs pane:PANE_SEARCH];
//when keyword search
// ->skip files/connections
if(YES == isKeyword)
{
//skip
continue;
}
//filter files
[self filterFiles:filterText items:task.files results:matchingFiles pane:PANE_SEARCH];
//filter connections
[self filterConnections:filterText items:task.connections results:matchingConnections];
}
//remove dups dylibs
[matchingDylibs setArray:[[[NSSet setWithArray:matchingDylibs] allObjects] mutableCopy]];
//add to cumulative search results
[results addObjectsFromArray:matchingDylibs];
//remove dups files
[matchingFiles setArray:[[[NSSet setWithArray:matchingFiles] allObjects] mutableCopy]];
//add to cumulative search results
[results addObjectsFromArray:matchingFiles];
//remove dups connections
[matchingConnections setArray:[[[NSSet setWithArray:matchingConnections] allObjects] mutableCopy]];
//add to cumulative search results
[results addObjectsFromArray:matchingConnections];
//call back into search object to refresh it's UI and show results
dispatch_async(dispatch_get_main_queue(), ^{
//search done!
[((AppDelegate*)[[NSApplication sharedApplication] delegate]).searchWindowController completeSearch];
});
return;
}
//filter tasks
// ->name, path, pid
-(void)filterTasks:(NSString*)filterText items:(NSMutableDictionary*)items results:(NSMutableArray*)results pane:(NSUInteger)pane
{
//task
Task* task = nil;
//process #keyword filtering
// ->note: already checked its a full/matching keyword
if(YES == [filterText hasPrefix:@"#"])
{
//prep UI for filtering
// ->config/show overlay, etc
if(PANE_SEARCH != pane)
{
//prep UI
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) prepUIForFiltering:pane];
}
//when main thread
// ->do #keyword in background
if(YES == [NSThread isMainThread])
{
//in background filter by keyword
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//nap a 1/2second to let message show up
[NSThread sleepForTimeInterval:0.5];
//filter
// ->calls back in UI to refresh when done!
[self filterByKeyword:(NSString*)filterText items:items.allValues results:(NSMutableArray*)results pane:pane];
});
}
//already in background
// ->just run on current thread
else
{
//filter
[self filterByKeyword:(NSString*)filterText items:items.allValues results:(NSMutableArray*)results pane:pane];
}
}
//not keyword
// ->do normal search here...
else
{
//sync
@synchronized(items)
{
//iterate over all tasks
for(NSNumber* taskKey in items)
{
//extract task
task = items[taskKey];
//check path first
// ->mostly likely to match
if( (nil != task.binary.path) &&
(NSNotFound != [task.binary.path rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:task];
//next
continue;
}
//check name
if( (nil != task.binary.name) &&
(NSNotFound != [task.binary.name rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:task];
//next
continue;
}
//check pid
if(NSNotFound != [[task.pid stringValue] rangeOfString:filterText options:NSCaseInsensitiveSearch].location)
{
//save match
[results addObject:task];
//next
continue;
}
}//all tasks
}//sync
}//normal filtering
return;
}
//filter tasks
// ->name, path, pid
-(void)filterByKeyword:(NSString*)filterText items:(NSArray*)items results:(NSMutableArray*)results pane:(NSUInteger)pane
{
//sync
@synchronized(items)
{
//sanity check
if(0 == items.count)
{
//bail
goto bail;
}
//handle tasks
if(YES == [items.firstObject isKindOfClass:[Task class]])
{
//iterate over all items
for(id item in items)
{
//check if task's binary matches filter
if(YES == [self binaryFulfillsKeyword:filterText binary:((Task*)item).binary])
{
//add
[results addObject:item];
}
}
}
//handle dylibs
else if(YES == [items.firstObject isKindOfClass:[Binary class]])
{
//iterate over all items
for(id item in items)
{
//check if dylib's binary matches filter
if(YES == [self binaryFulfillsKeyword:filterText binary:((Binary*)item)])
{
//add
[results addObject:item];
}
}
}
} //sync
//tell the UI we are done
if(PANE_SEARCH != pane)
{
//on main thread, update UI
dispatch_async(dispatch_get_main_queue(), ^{
//finalize UI
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) finalizeFiltration:pane];
});
}
//bail
bail:
return;
}
//filter dylibs and files
// ->name and path
-(void)filterFiles:(NSString*)filterText items:(NSMutableArray*)items results:(NSMutableArray*)results pane:(NSUInteger)pane
{
//process #keyword filtering for dylibs
// ->note: already checked its a full/matching keyword
if( (YES == [filterText hasPrefix:@"#"]) &&
(YES == [items.firstObject isKindOfClass:[Binary class]]) )
{
//prep UI for filtering
// ->config/show overlay, etc
if(PANE_SEARCH != pane)
{
//prep UI
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) prepUIForFiltering:pane];
}
//when main thread
// ->do #keyword search in background
if(YES == [NSThread isMainThread])
{
//in background filter by keyword
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//nap a 1/2second to let message show up
[NSThread sleepForTimeInterval:0.5];
//filter
// ->calls back in UI to refresh when done!
[self filterByKeyword:(NSString*)filterText items:items results:(NSMutableArray*)results pane:pane];
});
}
//already in background
// ->just run on current thread
else
{
//filter
[self filterByKeyword:(NSString*)filterText items:items results:(NSMutableArray*)results pane:pane];
}
}
//not keyword
// ->do normal search here...
else
{
//sync
@synchronized(items)
{
//iterate over all items
for(ItemBase* item in items)
{
//check path first
// ->most likely to match
if( (nil != item.path) &&
(NSNotFound != [item.path rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
//check name
if( (nil != item.name) &&
(NSNotFound != [item.name rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
}//all items
}//sync
}//normal filtering
return;
}
//filter network connections
-(void)filterConnections:(NSString*)filterText items:(NSMutableArray*)items results:(NSMutableArray*)results
{
//sync
@synchronized(items)
{
//iterate over all tasks
for(Connection* item in items)
{
//check local ip
if( (nil != item.localIPAddr) &&
(NSNotFound != [item.localIPAddr rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
//check local port
if( (nil != item.localIPAddr) &&
(NSNotFound != [[NSString stringWithFormat:@"%d", [item.localPort unsignedShortValue]] rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
//check remote ip
if( (nil != item.remoteIPAddr) &&
(NSNotFound != [item.remoteIPAddr rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
//check remote port
if( (nil != item.remoteIPAddr) &&
(NSNotFound != [[NSString stringWithFormat:@"%d", [item.remotePort unsignedShortValue]] rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
//check family
if( (nil != item.family) &&
(NSNotFound != [item.family rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
//check protocol
if( (nil != item.proto) &&
(NSNotFound != [item.proto rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
//check state
if( (nil != item.state) &&
(NSNotFound != [item.state rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
//check DNS name
if( (nil != item.remoteName) &&
(NSNotFound != [item.remoteName rangeOfString:filterText options:NSCaseInsensitiveSearch].location) )
{
//save match
[results addObject:item];
//next
continue;
}
}//all connections
}//sync
return;
}
//check if a binary fulfills a keyword
-(BOOL)binaryFulfillsKeyword:(NSString*)keyword binary:(Binary*)binary
{
//flag
BOOL fulfills = NO;
//handle '#apple'
// ->signed by apple
if( (YES == [keyword isEqualToString:@"#apple"]) &&
( (YES == [self isApple:binary]) ||
(YES == [binary.path isEqualToString:KERNEL_YOSEMITE]) ))
{
//happy
fulfills = YES;
//bail
goto bail;
}
//handle '#nonapple'
// ->not signed by apple, and not kernel
else if( (YES == [keyword isEqualToString:@"#nonapple"]) &&
(YES != [self isApple:binary]) &&
(YES != [binary.path isEqualToString:KERNEL_YOSEMITE]) )
{
//happy
fulfills = YES;
//bail
goto bail;
}
//handle '#signed'
// ->signed
else if( (YES == [keyword isEqualToString:@"#signed"]) &&
(YES == [self isSigned:binary]) )
{
//happy
fulfills = YES;
//bail
goto bail;
}
//handle '#unsigned'
// ->not signed
else if( (YES == [keyword isEqualToString:@"#unsigned"]) &&
(YES != [self isSigned:binary]) )
{
//happy
fulfills = YES;
//bail
goto bail;
}
//handle '#flagged'
// ->flagged by VT
else if( (YES == [keyword isEqualToString:@"#flagged"]) &&
(YES == [self isFlagged:binary]) )
{
//happy
fulfills = YES;
//bail
goto bail;
}
//handle '#encrypted'
else if( (YES == [keyword isEqualToString:@"#encrypted"]) &&
(YES == [self isEncrypted:binary]) )
{
//happy
fulfills = YES;
//bail
goto bail;
}
//handle '#packed'
else if( (YES == [keyword isEqualToString:@"#packed"]) &&
(YES == [self isPacked:binary]) )
{
//happy
fulfills = YES;
//bail
goto bail;
}
//handle '#notfound'
else if( (YES == [keyword isEqualToString:@"#notfound"]) &&
(YES == [self notFound:binary]) )
{
//happy
fulfills = YES;
//bail
goto bail;
}
//bail
bail:
return fulfills;
}
//keyword filter '#apple' (and indirectly #nonapple)
// ->determine if binary is signed by apple
-(BOOL)isApple:(Binary*)item
{
//flag
BOOL isApple = NO;
//make sure signing info has been generated
// ->when no, generate it
if(nil == item.signingInfo)
{
//generate
[item generatedSigningInfo];
}
//check
// ->just look at signing info
if( (noErr == [item.signingInfo[KEY_SIGNATURE_STATUS] integerValue]) &&
(Apple == [item.signingInfo[KEY_SIGNATURE_SIGNER] intValue]) )
{
//set flag
isApple = YES;
}
return isApple;
}
//keyword filter '#signed' (and indirectly #unsigned)
// ->determine if binary is signed
-(BOOL)isSigned:(Binary*)item
{
//flag
BOOL isSigned = NO;
//make sure signing info has been generated
// ->when no, generate it
if(nil == item.signingInfo)
{
//generate
[item generatedSigningInfo];
}
//check
// ->just look at signing info
if(STATUS_SUCCESS == [item.signingInfo[KEY_SIGNATURE_STATUS] integerValue])
{
//set flag
isSigned = YES;
}
return isSigned;
}
//keyword filter '#flagged'
// ->determine if binary is flagged by VT
-(BOOL)isFlagged:(Binary*)item
{
//flag
BOOL isFlagged = NO;
//check
// ->note: assumes that VT query has already completed...
if( (nil != item.vtInfo) &&
(0 != [item.vtInfo[VT_RESULTS_POSITIVES] unsignedIntegerValue]) )
{
//set flag
isFlagged = YES;
}
return isFlagged;
}
//keyword filter '#encrypted'
// ->determine if binary is encrypted
-(BOOL)isEncrypted:(Binary *)item
{
//make sure item was parsed
if(nil == item.parser)
{
//parse
[item parse];
//save encrypted flag
item.isEncrypted = [item.parser.binaryInfo[KEY_IS_ENCRYPTED] boolValue];
}
//set flag
return item.isEncrypted;
}
//keyword filter '#packed'
// ->determine if binary is packed
-(BOOL)isPacked:(Binary *)item
{
//make sure item was parsed
if(nil == item.parser)
{
//make sure we have signing info
// ->packer checks ('parse') ingores Apple signed files
if(nil == item.signingInfo)
{
//generate
[item generatedSigningInfo];
}
//parse
[item parse];
//save packed flag
item.isPacked = [item.parser.binaryInfo[KEY_IS_PACKED] boolValue];
}
//set flag
return item.isPacked;
}
//keyword filter '#notfound'
-(BOOL)notFound:(Binary *)item
{
return item.notFound;
}
@end