Submit a file to be scanned by VirusTotal

        :param this_file: File to be scanned (32MB file size limit)
        :return: JSON response that contains scan_id and permalink.
337569
def scan_file(self, this_file):
        
        params = {'apikey': self.api_key}
        try:
            if type(this_file) == str and os.path.isfile(this_file):
                files = {'file': (this_file, open(this_file, 'rb'))}
            elif isinstance(this_file, StringIO.StringIO):
                files = {'file': this_file.read()}
            else:
                files = {'file': this_file}
        except TypeError as e:
            return dict(error=e.message)

        try:
            response = requests.post(self.base + 'file/scan', files=files, params=params, proxies=self.proxies)
        except requests.RequestException as e:
            return dict(error=e.message)

        return _return_response_and_status_code(response)
216772
def malware(self, malware, password, file_name):
        
        if not self.can_update():
            self._tcex.handle_error(910, [self.type])

        self._data['malware'] = malware
        self._data['password'] = password
        self._data['fileName'] = file_name
        request = {'malware': malware, 'password': password, 'fileName': file_name}
        return self.tc_requests.update(self.api_type, self.api_sub_type, self.unique_id, request)
260146
def file_request(self):
        
        response = requests.get(
            self.__base_url,
            headers=self.__headers,
            stream=True)

        return response.raw.read(), response.headers
1011710
def ajax_upload(self, request, folder_id=None):
        
        mimetype = "application/json" if request.is_ajax() else "text/html"
        content_type_key = 'content_type'
        response_params = {content_type_key: mimetype}
        folder = None
        if folder_id:
            try:
                # Get folder
                folder = Folder.objects.get(pk=folder_id)
            except Folder.DoesNotExist:
                return HttpResponse(json.dumps({'error': NO_FOLDER_ERROR}),
                                    **response_params)

        # check permissions
        if folder and not folder.has_add_children_permission(request):
            return HttpResponse(
                json.dumps({'error': NO_PERMISSIONS_FOR_FOLDER}),
                **response_params)
        try:
            if len(request.FILES) == 1:
                # dont check if request is ajax or not, just grab the file
                upload, filename, is_raw = handle_request_files_upload(request)
            else:
                # else process the request as usual
                upload, filename, is_raw = handle_upload(request)

            # Get clipboad
            # TODO: Deprecated/refactor
            # clipboard = Clipboard.objects.get_or_create(user=request.user)[0]

            # find the file type
            for filer_class in media_settings.MEDIA_FILE_MODELS:
                FileSubClass = load_object(filer_class)
                # TODO: What if there are more than one that qualify?
                if FileSubClass.matches_file_type(filename, upload, request):
                    FileForm = modelform_factory(
                        model=FileSubClass,
                        fields=('original_filename', 'owner', 'file')
                    )
                    break
            uploadform = FileForm({'original_filename': filename,
                                   'owner': request.user.pk},
                                  {'file': upload})
            if uploadform.is_valid():
                file_obj = uploadform.save(commit=False)
                # Enforce the FILER_IS_PUBLIC_DEFAULT
                file_obj.is_public = settings.MEDIA_IS_PUBLIC_DEFAULT
                file_obj.folder = folder
                file_obj.save()
                # TODO: Deprecated/refactor
                # clipboard_item = ClipboardItem(
                #    clipboard=clipboard, file=file_obj)
                # clipboard_item.save()
                json_response = {
                    'thumbnail': file_obj.icons['32'],
                    'alt_text': '',
                    'label': str(file_obj),
                    'file_id': file_obj.pk,
                }
                return HttpResponse(json.dumps(json_response),
                                    **response_params)
            else:
                form_errors = '; '.join(['%s: %s' % (
                    field,
                    ', '.join(errors)) for field, errors in list(uploadform.errors.items())
                ])
                raise UploadException(
                    "AJAX request not valid: form invalid '%s'" % (form_errors,))
        except UploadException as e:
            return HttpResponse(json.dumps({'error': str(e)}),
                                **response_params)
682225
def dl_pb_file(inputs):
    

    basefile, subdir, db, dl_dir, keep_subdirs, overwrite = inputs

    # Full url of file
    url = posixpath.join(config.db_index_url, db, subdir, basefile)

    # Supposed size of the file
    remote_file_size = _remote_file_size(url)

    # Figure out where the file should be locally
    if keep_subdirs:
        dldir = os.path.join(dl_dir, subdir)
    else:
        dldir = dl_dir

    local_file = os.path.join(dldir, basefile)

    # The file exists locally.
    if os.path.isfile(local_file):
        # Redownload regardless
        if overwrite:
            dl_full_file(url, local_file)
        # Process accordingly.
        else:
            local_file_size = os.path.getsize(local_file)
            # Local file is smaller than it should be. Append it.
            if local_file_size < remote_file_size:
                print('Detected partially downloaded file: %s Appending file...' % local_file)
                headers = {"Range": "bytes="+str(local_file_size)+"-", 'Accept-Encoding': '*'}
                r = requests.get(url, headers=headers, stream=True)
                print('headers: ', headers)
                print('r content length: ', len(r.content))
                with open(local_file, 'ba') as writefile:
                    writefile.write(r.content)
                print('Done appending.')
            # Local file is larger than it should be. Redownload.
            elif local_file_size > remote_file_size:
                dl_full_file(url, local_file)
            # If they're the same size, do nothing.

    # The file doesn't exist. Download it.
    else:
        dl_full_file(url, local_file)

    return
1090661
def _get_first_64k_content(file_path):
    

    if not isfile(file_path):
        raise PathDoesNotExistException(file_path)

    file_size = getsize(file_path)

    content_size = min(file_size, 0x10000)

    content = bytearray(content_size)
    with open(file_path, "rb") as file_object:
        content_read = file_object.readinto(content)

        if content_read is None or content_read < content_size:
            raise FileContentReadException(content_size, content_read)

    return content
1256228
protected function dumpJsonFile()
    {
        $jsonPath = $this->config->getJsonPath();
        $this->logger->info(sprintf('Dump submitting json file: %s', $jsonPath));

        $this->api->dumpJsonFile();

        $filesize = number_format(filesize($jsonPath) / 1024, 2); // kB
        $this->logger->info(sprintf('File size: <info>%s</info> kB', $filesize));

        return $this;
    }
1256691
def cover(self, pageid):
        
        r = requests.get(self.api,
                         params={'action': 'query', 'prop': 'pageimages', 'pageids': pageid, 'format': 'json'},
                         headers=self.header)
        jsd = r.json()
        image = "File:" + jsd['query']['pages'][str(pageid)]['pageimage']
        r = requests.get(self.api,
                         params={'action': 'query', 'prop': 'imageinfo', 'iiprop': 'url', 'titles': image,
                                 'format': 'json'},
                         headers=self.header)
        jsd = r.json()
        return jsd['query']['pages'][list(jsd['query']['pages'].keys())[0]]['imageinfo'][0]['url']
44432
def _query(self, filename):
        """"""
        #  - Query metadata of one file
        #  - Return a dict with a 'size' key, and a file size value (-1 for not found)
        #  - Retried if an exception is thrown
        log.Info('Querying size of %s' % filename)
        from jottalib.JFS import JFSNotFoundError, JFSIncompleteFile
        remote_path = posixpath.join(self.folder.path, filename)
        try:
            remote_file = self.client.getObject(remote_path)
        except JFSNotFoundError:
            return {'size': -1}
        return {
            'size': remote_file.size,
        }
354651
def get_remote_file_size(self, url):
        
        headers = requests.head(url).headers
        return int(headers['content-length'])
1198295
def scan_file(self, src_file):
        """"""
        self.logger.info('start scanning file {} for proxy list...'
                         .format(src_file))
        with open(src_file, 'r') as fin:
            proxies = json.load(fin)
        for protocol in proxies.keys():
            for proxy in proxies[protocol]:
                self.proxy_queue.put({
                    'addr': proxy['addr'],
                    'protocol': protocol
                })
501052
def verify_uploaded_file(
            self,
            destination_folder_id,
            source_path,
            verbose = True,
    ):
        '''
        
        '''
        source_file_size = os.stat(source_path).st_size

        total_part_size = 0
        file_position = 0
        uploaded_box_file_ids = self.find_file( destination_folder_id, os.path.basename( source_path ) )
        total_sha1 = hashlib.sha1()
        for i, file_id in enumerate(uploaded_box_file_ids):
            file_info = self.client.file( file_id = file_id ).get()
            uploaded_sha1 = file_info.response_object['sha1']
            uploaded_size = file_info.response_object['size']

            part_sha1 = read_sha1( source_path, start_byte = file_position, read_size = uploaded_size, extra_hashers = [total_sha1] )
            if part_sha1.hexdigest() != uploaded_sha1:
                print( '\n' )
                print( 'Part sha1: ' + part_sha1.hexdigest() )
                print( 'Uploaded sha1: ' + uploaded_sha1 )
                print('Sha1 hash of uploaded file {0} ({1}) does not match'.format(file_info.response_object['name'], file_id) )
                return False

            file_position += uploaded_size
            total_part_size += uploaded_size
            if len(uploaded_box_file_ids) > 1:
                print( 'Finished verifying part {0} of {1} of {2}'.format( i+1, len(uploaded_box_file_ids), file_id ) )

        assert( source_file_size == total_part_size )

        if verbose:
            print( 'Verified uploaded file {0} ({1}) with sha1: {2}'.format(source_path, file_id, total_sha1.hexdigest()) )

        return True
585715
def make_response(self, image, size, mode, filename=None, *args, **kwargs):
        
        try:
            fmt = get_format(image)
        except IOError:
            # not a known image file
            raise NotFound()

        self.content_type = "image/png" if fmt == "PNG" else "image/jpeg"
        ext = "." + str(fmt.lower())

        if not filename:
            filename = "image"
        if not filename.lower().endswith(ext):
            filename += ext
        self.filename = filename

        if size:
            image = resize(image, size, size, mode=mode)
            if mode == CROP:
                assert get_size(image) == (size, size)
        else:
            image = image.read()

        return make_response(image)
1155759
def api_submit():
    
    data = request.files.file
    response.content_type = 'application/json'
    if not data or not hasattr(data, 'file'):
        return json.dumps({"status": "Failed", "stderr": "Missing form params"})
    return json.dumps(analyse_pcap(data.file, data.filename), default=jsondate, indent=4)
1058824
def scan_binary(patch_file, project, sha256hash, apikey):
    
    v_api = virus_total.VirusTotal()

    while True:
        binary_report = v_api.binary_report(sha256hash, apikey)
        response_code = binary_report['response_code']

        # report does not exist, need to scan
        if response_code == 0:
            logger.info('Performing new scan of %s.', patch_file)
            scan_file = v_api.scan_file(patch_file, apikey)
            logger.info('VirusTotal Response: %s', scan_file['verbose_msg'])
            logger.info('Report will be rendered at: %s', scan_file['permalink'])
            binary_report = v_api.binary_report(sha256hash, apikey)

        # Item is still queued
        if response_code == -2:
            logger.info('Report job still queued..')

        if response_code == 1:
            logger.info('Report found, job complete.')
            break

    positives = binary_report['positives']

    if positives == 0:
        negative_report(binary_report, sha256hash, project, patch_file)
    else:
        positive_report(binary_report, sha256hash, project, patch_file)
1076793
def rescan_file(self, filename, sha256hash, apikey):
        
        url = self.base_url + "file/rescan"
        params = {
            'apikey': apikey,
            'resource': sha256hash
        }

        rate_limit_clear = self.rate_limit()
        if rate_limit_clear:
            response = requests.post(url, params=params)
            if response.status_code == self.HTTP_OK:
                self.logger.info("sent: %s, HTTP: %d, content: %s", os.path.basename(filename), response.status_code, response.text)
            elif response.status_code == self.HTTP_RATE_EXCEEDED:
                    time.sleep(20)
            else:
                self.logger.error("sent: %s, HTTP: %d", os.path.basename(filename), response.status_code)
            return response
185434
def get_file_contents(self, path):
        
        path = self._normalize_path(path)
        res = self._session.get(
            self._webdav_url + parse.quote(self._encode_string(path))
        )
        if res.status_code == 200:
            return res.content
        elif res.status_code >= 400:
            raise HTTPResponseError(res)
        return False