-
Notifications
You must be signed in to change notification settings - Fork 32
/
ResultsWindowController.m
executable file
·267 lines (204 loc) · 6.37 KB
/
ResultsWindowController.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
//
// ResultsWindowController.m
// KnockKnock
//
// Created by Patrick Wardle on 2/6/15.
// Copyright (c) 2015 Objective-See, LLC. All rights reserved.
//
#import "Utilities.h"
#import "AppDelegate.h"
#import "ResultsWindowController.h"
@implementation ResultsWindowController
@synthesize details;
@synthesize detailsLabel;
//automatically called when nib is loaded
// ->center window
-(void)awakeFromNib
{
//center
[self.window center];
}
//initialize window
-(void)windowDidLoad
{
//super
[super windowDidLoad];
//not in dark mode?
// make window white
if(YES != isDarkMode())
{
//make white
self.window.backgroundColor = NSColor.whiteColor;
}
//set details
self.detailsLabel.stringValue = self.details;
//set unknown items
self.vtDetailsLabel.stringValue = self.vtDetails;
//toggle 'Submit' button
self.submitToVT.hidden = !(self.unknownItems.count);
//make 'close' button active
[self.window makeFirstResponder:self.closeButton];
return;
}
//callback into app delegate to submit
-(IBAction)submitToVT:(id)sender
{
//disable submit
self.submitToVT.enabled = NO;
//disable close
self.closeButton.enabled = NO;
//start spinner
[self.submissionActivityIndicator startAnimation:nil];
//set message
self.submissionStatus.stringValue = [NSString stringWithFormat:@"Submitting %lu unknown items to VirusTotal...", self.unknownItems.count];
//show it
self.submissionStatus.hidden = NO;
//submit in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//nap for UI messages
sleep(1);
//submit
[self submit];
});
return;
}
//update UI now that submission is done
-(void)submissionComplete:(NSUInteger)successes
{
//enable close
self.closeButton.enabled = YES;
//stop spinner
[self.submissionActivityIndicator stopAnimation:nil];
//success
if(successes == self.unknownItems.count)
{
//update message
self.submissionStatus.stringValue = [NSString stringWithFormat:@"Submissions complete. (In subsequent scans item's VT detection ratios will now be displayed)."];
}
else
{
//update message
self.submissionStatus.stringValue = [NSString stringWithFormat:@"Submissions complete, though errors were encountered"];
}
return;
}
//submit unknown items to VT
// note: runs in background!
-(void)submit
{
//VT object
VirusTotal* vtObj = nil;
//result (from VT)
NSDictionary* result = nil;
//scan ids
NSMutableDictionary* scanIDs = nil;
//successful scans
NSUInteger successes = 0;
//alloc/init VT obj
vtObj = [[VirusTotal alloc] init];
//alloc
scanIDs = [NSMutableDictionary dictionary];
//submit all unknown items
for(ItemBase* item in self.unknownItems)
{
//scan id from VT
NSString* scanID = nil;
//skip non-file items
if(YES != [item isKindOfClass:[File class]])
{
//skip
continue;
}
//skip item's without hashes
// ...not sure how this could ever happen
if(nil == ((File*)item).hashes[KEY_HASH_SHA1])
{
//skip
continue;
}
//submit file to VT
result = [vtObj submit:(File*)item];
//extract scan id
scanID = result[VT_RESULTS_SCANID];
if(nil == scanID)
{
//err msg
NSLog(@"KNOCKKNOCK ERROR: failed to submit %@", item.path);
//update UI
dispatch_async(dispatch_get_main_queue(), ^{
//update
self.submissionStatus.stringValue = [NSString stringWithFormat:@"Failed to submit '%@'", ((File*)item).name];
});
//nap (for UI)
sleep(1);
continue;
}
//reset file's VT info
// as we've just submittted
((File*)item).vtInfo = nil;
//update UI
dispatch_async(dispatch_get_main_queue(), ^{
//update
self.submissionStatus.stringValue = [NSString stringWithFormat:@"Submitted '%@'", ((File*)item).path];
//set item's VT status in UI to pending (...)
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) itemProcessed:(File*)item];
});
//save
scanIDs[scanID] = item;
}
//only continue if there were submissions
if(0 != scanIDs.count)
{
//nap for VT to process
sleep(5);
//update UI
dispatch_async(dispatch_get_main_queue(), ^{
//update
self.submissionStatus.stringValue = [NSString stringWithFormat:@"Awaiting results..."];
});
//nap more VT to process
sleep(5);
//get VT results
for(NSString* scanID in scanIDs.allKeys)
{
//get results for item
// waits until recieved (and updates UI on success)
if(YES != [vtObj getInfoForItem:(File*)scanIDs[scanID] scanID:scanID])
{
//update UI
dispatch_async(dispatch_get_main_queue(), ^{
//update
self.submissionStatus.stringValue = [NSString stringWithFormat:@"Failed to get results for '%@'", ((File*)scanIDs[scanID]).name];
});
//nap for UI
sleep(1);
}
//happy
// results will show up in UI
successes++;
}
}
//tell UI all is done
dispatch_async(dispatch_get_main_queue(), ^{
//complete
[self submissionComplete:successes];
});
return;
}
//automatically invoked when user clicks 'OK'
// ->close window
-(IBAction)close:(id)sender
{
//close
[[self window] close];
return;
}
//automatically invoked when window is closing
// ->make ourselves unmodal
-(void)windowWillClose:(NSNotification *)notification
{
//make un-modal
[[NSApplication sharedApplication] stopModal];
return;
}
@end