def __init__(self, hashMethod, interval): super(Apk_Detector, self).__init__() self.interval = interval self.hashMethod = None if hashMethod == 'md5': self.hashMethod = 'md5' elif hashMethod == 'sha1': self.hashMethod = 'sha1' else: self.hashMethod = 'md5' self.scanner = Apk_Detector_Scanner(self.VT_API_KEY, self.VT_HOST, self.VT_SCAN_URL) self.sender = Apk_Detector_Sender(self.VT_API_KEY, self.VT_HOST, self.VT_SEND_URL, self.VT_FILE_SIZE) self.lastTime = 0.0
class Apk_Detector(object): ''' Apk Detector ''' AD_OK = 100 AD_ERR_NOFILE = 101 AD_ERR_QUEUED = 102 AD_ERR_API = 103 AD_ERR_UNKNOWN = 104 AD_ERR_INT = 105 AD_ERR_TOOBIG = 106 VT_API_KEY = 'd127e6db159b0dfebc5fc34dfbe85456f36699e6b4d441b59b58c5e917a05c54' VT_HOST = 'www.virustotal.com' VT_SCAN_URL = 'https://www.virustotal.com/vtapi/v2/file/report' VT_SEND_URL = 'https://www.virustotal.com/vtapi/v2/file/scan' VT_FILE_SIZE = 32 def __init__(self, hashMethod, interval): super(Apk_Detector, self).__init__() self.interval = interval self.hashMethod = None if hashMethod == 'md5': self.hashMethod = 'md5' elif hashMethod == 'sha1': self.hashMethod = 'sha1' else: self.hashMethod = 'md5' self.scanner = Apk_Detector_Scanner(self.VT_API_KEY, self.VT_HOST, self.VT_SCAN_URL) self.sender = Apk_Detector_Sender(self.VT_API_KEY, self.VT_HOST, self.VT_SEND_URL, self.VT_FILE_SIZE) self.lastTime = 0.0 def scan(self, filePath): if not os.path.isfile(filePath): return self.AD_ERR_NOFILE hashResult = uh.utils_hash_file(filePath) if hashResult is None: return self.AD_ERR_NOFILE if self.lastTime == 0.0: self.lastTime = time.time() else: interval = time.time() - self.lastTime self.lastTime = time.time() if interval < 0: return self.AD_ERR_INT if interval < self.interval: time.sleep(self.interval - interval) scanResult = self.scanner.scan(hashResult) if scanResult == Apk_Detector_Scanner.ADSC_OK: return self.AD_OK elif scanResult == Apk_Detector_Scanner.ADSC_ERR_NOTINSTORE: return self.send(filePath) elif scanResult == Apk_Detector_Scanner.ADSC_ERR_API: return self.AD_ERR_API elif scanResult == Apk_Detector_Scanner.ADSC_ERR_SCANNING: return self.AD_ERR_QUEUED return self.AD_ERR_UNKNOWN def send(self, filePath): sendResult = self.sender.send(filePath) if sendResult == Apk_Detector_Sender.ADSE_OK: return self.AD_ERR_QUEUED elif sendResult == Apk_Detector_Sender.ADSE_ERR_API: return self.AD_ERR_API elif sendResult == Apk_Detector_Sender.ADSE_ERR_TOOBIG: return self.AD_ERR_TOOBIG return self.AD_ERR_UNKNOWN def get_report(self): return self.scanner.get_report()
class Apk_Detector(object): ''' Apk Detector ''' AD_OK = 100 AD_ERR_NOFILE = 101 AD_ERR_QUEUED = 102 AD_ERR_API = 103 AD_ERR_UNKNOWN = 104 AD_ERR_INT = 105 AD_ERR_TOOBIG = 106 AD_ERR_NOTINDB = 107 VT_HOST = 'www.virustotal.com' VT_SCAN_URL = 'https://www.virustotal.com/vtapi/v2/file/report' VT_SEND_URL = 'https://www.virustotal.com/vtapi/v2/file/scan' VT_FILE_SIZE = 32 def __init__(self, hashMethod, interval, apiKey): super(Apk_Detector, self).__init__() self.interval = interval self.hashMethod = None self.vtApiKey = apiKey if hashMethod == 'md5': self.hashMethod = "md5" elif hashMethod == 'sha1': self.hashMethod = 'sha1' else: self.hashMethod = 'md5' self.scanner = Apk_Detector_Scanner(self.vtApiKey, self.VT_HOST, self.VT_SCAN_URL) self.sender = Apk_Detector_Sender(self.vtApiKey, self.VT_HOST, self.VT_SEND_URL, self.VT_FILE_SIZE) self.lastTime = 0.0 def scan(self, fileHash): self.wait() scanResult = self.scanner.scan(fileHash) if scanResult == Apk_Detector_Scanner.ADSC_OK: return self.AD_OK elif scanResult == Apk_Detector_Scanner.ADSC_ERR_NOTINSTORE: return self.AD_ERR_NOTINDB elif scanResult == Apk_Detector_Scanner.ADSC_ERR_API: return self.AD_ERR_API elif scanResult == Apk_Detector_Scanner.ADSC_ERR_SCANNING: return self.AD_ERR_QUEUED return self.AD_ERR_UNKNOWN def send(self, filePath): self.wait() if not os.path.isfile(filePath): return self.AD_ERR_NOFILE hashResult = uh.utils_hash_file(filePath) if hashResult is None: return self.AD_ERR_NOFILE sendResult = self.sender.send(filePath) if sendResult == Apk_Detector_Sender.ADSE_OK: return self.AD_ERR_QUEUED elif sendResult == Apk_Detector_Sender.ADSE_ERR_API: return self.AD_ERR_API elif sendResult == Apk_Detector_Sender.ADSE_ERR_TOOBIG: return self.AD_ERR_TOOBIG return self.AD_ERR_UNKNOWN def wait(self): if self.lastTime == 0.0: self.lastTime = time.time() else: interval = time.time() - self.lastTime self.lastTime = time.time() if interval < 0: return self.AD_ERR_INT if interval < self.interval: time.sleep(self.interval - interval) def get_report(self): return self.scanner.get_report()