From 06ec61e631ddb0c79cdf03e85c5c32170f65bb1e Mon Sep 17 00:00:00 2001 From: Patrick Wardle Date: Sun, 2 Aug 2015 16:19:47 -0700 Subject: [PATCH] added reachability checks ->VT now only queried if computer is connected to network --- AppDelegate.h | 7 +- AppDelegate.m | 28 +------ Queue.m | 11 ++- TaskEnumerator.m | 7 +- TaskExplorer.xcodeproj/project.pbxproj | 4 + .../xcdebugger/Breakpoints_v2.xcbkptlist | 66 +++-------------- Utilities.h | 3 + Utilities.m | 73 ++++++++++++++++++- 8 files changed, 103 insertions(+), 96 deletions(-) diff --git a/AppDelegate.h b/AppDelegate.h index 6e06e93..8e4414c 100755 --- a/AppDelegate.h +++ b/AppDelegate.h @@ -31,14 +31,13 @@ } -//Filter obj -//@property (nonatomic, retain) Filter* filter; +//connection flag +@property BOOL isConnected; //'filter task' search box // ->top pane @property (weak) IBOutlet NSSearchField *filterTasksBox; - //(current) bottom view controller @property(nonatomic, retain)TaskTableController *bottomViewController; @@ -52,7 +51,6 @@ //task table controller object @property (nonatomic, retain)TaskTableController *taskTableController; - //current task view format // ->flat or tree @property NSUInteger taskViewFormat; @@ -161,7 +159,6 @@ // ->query OS to refresh/reload all tasks -(IBAction)refreshTasks:(id)sender; - //callback when user has updated prefs // ->reload table, etc //-(void)applyPreferences; diff --git a/AppDelegate.m b/AppDelegate.m index 3fc2ed5..2cc04b1 100755 --- a/AppDelegate.m +++ b/AppDelegate.m @@ -28,6 +28,7 @@ @synthesize filterObj; @synthesize vtThreads; +@synthesize isConnected; @synthesize virusTotalObj; @synthesize taskTableController; @synthesize aboutWindowController; @@ -152,7 +153,6 @@ //init XPC if(YES != [self initXPC]) { - //bail goto bail; } @@ -771,32 +771,6 @@ bail: return; } - -/* -//kickoff a thread to query VT --(void)queryVT:(PluginBase*)plugin -{ - //virus total thread - NSThread* virusTotalThread = nil; - - //alloc thread - // ->will query virus total to get info about all detected items - virusTotalThread = [[NSThread alloc] initWithTarget:virusTotalObj selector:@selector(getInfo:) object:plugin]; - - //start thread - [virusTotalThread start]; - - //sync - @synchronized(self.vtThreads) - { - //save it into array - [self.vtThreads addObject:virusTotalThread]; - } - - return; -} -*/ - //automatically invoked when user clicks logo // ->load objective-see's html page -(IBAction)logoButtonHandler:(id)sender diff --git a/Queue.m b/Queue.m index 752aa5e..e6222a1 100644 --- a/Queue.m +++ b/Queue.m @@ -85,10 +85,15 @@ //process //->for now, just hash, etc [binary generateDetailedInfo]; - - //add item for VT processing - [vtObject addItem:binary]; + //when connected + // ->add item for VT processing + if(YES == ((AppDelegate*)[[NSApplication sharedApplication] delegate]).isConnected) + { + //add + [vtObject addItem:binary]; + } + //unlock [self.queueCondition unlock]; diff --git a/TaskEnumerator.m b/TaskEnumerator.m index a35489f..d8198cc 100644 --- a/TaskEnumerator.m +++ b/TaskEnumerator.m @@ -20,8 +20,6 @@ #import - - @implementation TaskEnumerator @@ -32,7 +30,6 @@ @synthesize executables; - //init -(id)init { @@ -61,6 +58,7 @@ //enumerate all tasks // ->calls back into app delegate to update task (top) table when pau // TODO: call every x # of seconds? +// TODO: existsing tasks w/ nil vtInfo, call [vtObject addItem:binary] ? -(void)enumerateTasks { //(new) task item @@ -72,6 +70,9 @@ //new tasks OrderedDictionary* newTasks = nil; + //set connected flag + ((AppDelegate*)[[NSApplication sharedApplication] delegate]).isConnected = isNetworkConnected(); + //get all tasks // ->pids and binary obj with just path/name newTasks = [self getAllTasks]; diff --git a/TaskExplorer.xcodeproj/project.pbxproj b/TaskExplorer.xcodeproj/project.pbxproj index 537497b..bde41de 100755 --- a/TaskExplorer.xcodeproj/project.pbxproj +++ b/TaskExplorer.xcodeproj/project.pbxproj @@ -49,6 +49,7 @@ CD7B9FA71ACCAE5E00DF3C71 /* startScanOver.png in Resources */ = {isa = PBXBuildFile; fileRef = CD7B9FA51ACCAE5E00DF3C71 /* startScanOver.png */; }; CD7B9FA81ACCAE5E00DF3C71 /* stopScanOver.png in Resources */ = {isa = PBXBuildFile; fileRef = CD7B9FA61ACCAE5E00DF3C71 /* stopScanOver.png */; }; CD7B9FAB1AD08FA100DF3C71 /* KKRow.m in Sources */ = {isa = PBXBuildFile; fileRef = CD7B9FAA1AD08FA100DF3C71 /* KKRow.m */; }; + CD7FBF251B6EDAC000578C5D /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD7FBF241B6EDAC000578C5D /* SystemConfiguration.framework */; }; CD83887F1AACCEDF000EB098 /* VirusTotal.m in Sources */ = {isa = PBXBuildFile; fileRef = CD83887E1AACCEDF000EB098 /* VirusTotal.m */; }; CDA5F6B41B16D805003CE340 /* remoteTaskService.m in Sources */ = {isa = PBXBuildFile; fileRef = CDA5F6B31B16D805003CE340 /* remoteTaskService.m */; }; CDA5F6B61B16D805003CE340 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = CDA5F6B51B16D805003CE340 /* main.m */; }; @@ -199,6 +200,7 @@ CD7B9FA61ACCAE5E00DF3C71 /* stopScanOver.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = stopScanOver.png; path = images/stopScanOver.png; sourceTree = SOURCE_ROOT; }; CD7B9FA91AD08FA100DF3C71 /* KKRow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KKRow.h; sourceTree = ""; }; CD7B9FAA1AD08FA100DF3C71 /* KKRow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KKRow.m; sourceTree = ""; }; + CD7FBF241B6EDAC000578C5D /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; CD83887D1AACCEDF000EB098 /* VirusTotal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VirusTotal.h; sourceTree = ""; }; CD83887E1AACCEDF000EB098 /* VirusTotal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VirusTotal.m; sourceTree = ""; }; CDA5F6AD1B16D805003CE340 /* remoteTaskService.xpc */ = {isa = PBXFileReference; explicitFileType = "wrapper.xpc-service"; includeInIndex = 0; path = remoteTaskService.xpc; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -290,6 +292,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + CD7FBF251B6EDAC000578C5D /* SystemConfiguration.framework in Frameworks */, CDF08CEA1AC8D97B009B3423 /* Quartz.framework in Frameworks */, CD6095731A87067D00E091CD /* Security.framework in Frameworks */, 1D21BC4F172AF43D009D1CFD /* Cocoa.framework in Frameworks */, @@ -347,6 +350,7 @@ 1D21BC4D172AF43D009D1CFD /* Frameworks */ = { isa = PBXGroup; children = ( + CD7FBF241B6EDAC000578C5D /* SystemConfiguration.framework */, CDF08CE91AC8D97B009B3423 /* Quartz.framework */, CD6095721A87067D00E091CD /* Security.framework */, 1D21BC4E172AF43D009D1CFD /* Cocoa.framework */, diff --git a/TaskExplorer.xcodeproj/xcuserdata/patrick.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/TaskExplorer.xcodeproj/xcuserdata/patrick.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index a58092b..66c9651 100644 --- a/TaskExplorer.xcodeproj/xcuserdata/patrick.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/TaskExplorer.xcodeproj/xcuserdata/patrick.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -39,22 +39,6 @@ moduleName = ""> - - - - - - - - - - - - @@ -254,11 +206,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "AppDelegate.m" - timestampString = "460247716.301871" + timestampString = "460249848.18895" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "1619" - endingLineNumber = "1619" + startingLineNumber = "1593" + endingLineNumber = "1593" landmarkName = "-constrainView:subView:" landmarkType = "5"> @@ -318,11 +270,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "AppDelegate.m" - timestampString = "460246314.23885" + timestampString = "460249848.18895" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "828" - endingLineNumber = "828" + startingLineNumber = "802" + endingLineNumber = "802" landmarkName = "-reloadTaskTable" landmarkType = "5"> diff --git a/Utilities.h b/Utilities.h index 510c142..e47c3db 100644 --- a/Utilities.h +++ b/Utilities.h @@ -68,4 +68,7 @@ NSString* path2Kernel(); //determine if process is (still) alive BOOL isAlive(pid_t targetPID); +//check if computer has network connection +BOOL isNetworkConnected(); + #endif diff --git a/Utilities.m b/Utilities.m index c9b622f..ef1edef 100644 --- a/Utilities.m +++ b/Utilities.m @@ -17,6 +17,7 @@ #import #import #import +#import //check if OS is supported // ->Lion and newer @@ -694,10 +695,80 @@ BOOL isAlive(pid_t targetPID) if( (0 != kill(targetPID, 0)) && (ESRCH == errno) ) { + //alive isAlive = NO; - } return isAlive; } +//check if computer has network connection +BOOL isNetworkConnected() +{ + //flag + BOOL isConnected = NO; + + //sock addr stuct + struct sockaddr zeroAddress = {0}; + + //reachability ref + SCNetworkReachabilityRef reachabilityRef = NULL; + + //reachability flags + SCNetworkReachabilityFlags flags = 0; + + //reachable flag + BOOL isReachable = NO; + + //connection required flag + BOOL connectionRequired = NO; + + //ensure its cleared out + bzero(&zeroAddress, sizeof(zeroAddress)); + + //set size + zeroAddress.sa_len = sizeof(zeroAddress); + + //set family + zeroAddress.sa_family = AF_INET; + + //create reachability ref + reachabilityRef = SCNetworkReachabilityCreateWithAddress(NULL, (const struct sockaddr*)&zeroAddress); + + //sanity check + if(NULL == reachabilityRef) + { + //bail + goto bail; + } + + //get flags + if(TRUE != SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) + { + //bail + goto bail; + } + + //set reachable flag + isReachable = ((flags & kSCNetworkFlagsReachable) != 0); + + //set connection required flag + connectionRequired = ((flags & kSCNetworkFlagsConnectionRequired) != 0); + + //finally + // ->determine if network is available + isConnected = (isReachable && !connectionRequired) ? YES : NO; + +//bail +bail: + + //cleanup + if(NULL != reachabilityRef) + { + //release + CFRelease(reachabilityRef); + } + + return isConnected; +} +