Example #1
0
    def save(self, obj, filename, tag, dist_q):
        data = dict()
        try:
            #Create a temporary file object and write to it
            temp = tempfile.TemporaryFile()
            temp.write(obj)
            temp.seek(0)
            
            #Hash the file in the file object
            md5_hash = md5sum(temp.read()).hexdigest()
            temp.seek(0)
            sha1_hash = sha1sum(temp.read()).hexdigest()
            temp.close()
            
            #Write the attached file to objs folder
            open('attachments/'+md5_hash,'wb').write(obj)
            
            #Send saved file to MalZoo,Cuckoo and Viper
            sample = dict()
            sample['md5']       = md5_hash
            sample['tag']    = tag
            sample['filename']  = 'attachments/'+md5_hash
            dist_q.put(sample)

            #Setup data dict for return
            data['id_tag']      = tag
            data['md5']         = md5_hash
            data['sha1']        = sha1_hash
            data['filename']    = filename
            data['submit_date'] = int(time())
        except Exception as e:
            print "SaveObject: Error on obj saving",e
            pass
        finally:
            return data
Example #2
0
    def save(self, obj, filename, tag, dist_q):
        data = dict()
        try:
            #Create a temporary file object and write to it
            temp = tempfile.TemporaryFile()
            temp.write(obj)
            temp.seek(0)

            #Hash the file in the file object
            md5_hash = md5sum(temp.read()).hexdigest()
            temp.seek(0)
            sha1_hash = sha1sum(temp.read()).hexdigest()
            temp.close()

            #Write the attached file to objs folder
            open('attachments/' + md5_hash, 'wb').write(obj)

            #Send saved file to MalZoo,Cuckoo and Viper
            sample = dict()
            sample['md5'] = md5_hash
            sample['tag'] = tag
            sample['filename'] = 'attachments/' + md5_hash
            dist_q.put(sample)

            #Setup data dict for return
            data['id_tag'] = tag
            data['md5'] = md5_hash
            data['sha1'] = sha1_hash
            data['filename'] = filename
            data['submit_date'] = int(time())
        except Exception as e:
            print "SaveObject: Error on obj saving", e
            pass
        finally:
            return data
Example #3
0
    def Download_And_Install(self, s_md5):
        """主进程。下载安装包,并安装。"""
        try:
            if not os.path.isdir(self.Download_Path):
                os.mkdir(self.Download_Path)
            install_log.debug(u'下载更新包!')
            with urlopener(self.__Agent) as ag:
                agent = ag.read()
            with open(self.__Download_File, 'wb') as f:
                f.write(agent)
            install_log.debug(u"比对下载包的Md5值。")
            with open(self.__Download_File, 'rb') as ff:
                down_md5 = md5sum(ff.read())
            if s_md5 == down_md5.hexdigest():
                install_log.debug(u'开始安装更新!')
                self.__Install(self.__Download_File, self.Download_Path)
                install_log.debug(u'清理临时目录!')
                rmtree(self.Download_Path)
                return install_log.debug(u'安装 更新包完成!')
            else:
                return install_log.warning(u'下载出错!请手动下载更新包,地址:%s' %
                                           self.__Agent)

        except urllib2.HTTPError:
            return install_log.error(u"无法下载,请检查网络!地址:%s" % self.__Agent)
Example #4
0
 def download_complete(self):
     if os.path.isfile(self.incomplete_filepath):
         self.LOG.debug("is a file")
         with open(self.incomplete_filepath) as f:
             file_md5 = md5sum(f.read()).hexdigest()
         self.LOG.debug("md5: %s", file_md5)
         self.LOG.debug("comparing to md5: %s", self.expected_md5)
         if not self.expected_md5:
             os.rename(self.incomplete_filepath, self.complete_filepath)
             return
         elif file_md5 == self.expected_md5:
             os.rename(self.incomplete_filepath, self.complete_filepath)
             return
     raise Exception("Error completing download.")
Example #5
0
 def download_complete(self):
     if os.path.isfile(self.incomplete_filepath):
         self.LOG.debug("is a file")
         with open(self.incomplete_filepath) as f:
             file_md5 = md5sum(f.read()).hexdigest()
         self.LOG.debug("md5: %s", file_md5)
         self.LOG.debug("comparing to md5: %s", self.expected_md5)
         if not self.expected_md5:
             os.rename(self.incomplete_filepath, self.complete_filepath)
             return
         elif file_md5 == self.expected_md5:
             os.rename(self.incomplete_filepath, self.complete_filepath)
             return
     raise Exception("Error completing download.")
Example #6
0
def get_site_hash():
    """Hash the current Python environment's site-packages contents, including
    the name and version of the libraries. The list we're hashing is what
    `pip freeze` would output.
    """
    site_dirs = site.getsitepackages()
    if site.ENABLE_USER_SITE:
        site_dirs.extend(site.getusersitepackages())
    packages = set()
    for site_dir in site_dirs:
        site_dir = Path(site_dir)
        for subpath in site_dir.iterdir():
            if subpath.parts[-1].endswith("dist-info"):
                packages.add(subpath.parts[-1].replace(".dist-info", ""))
    package_bytes = "".join(sorted(packages)).encode("utf8")
    return hashlib.md5sum(package_bytes).hexdigest()
Example #7
0
    def _prepare(self, request, content=True):
        node_path, format = NodeService._buildQuery(request)

        if content:
            request.content.seek(0, 0)
            content = request.content.read().strip() or "{}"
            try:
                data = json_decode(content)
            except:
                raise InvalidInputData("Invalid JSON")
        else:
            data = dict()
        return INode(node_path=node_path,
                     format=format,
                     data=data,
                     user=request.getUser(),
                     passwd=md5sum(request.getPassword()).hexdigest())
Example #8
0
def get_opkg_status_md5sum(filesystem_root='/'):
    file = pathlib.Path(filesystem_root, 'var/lib/opkg/status')
    if not file.exists():
        return 'FILE NOT FOUND'
    return hashlib.md5sum(file.read_text()).hexdigest()
Example #9
0
 def get_md5(self):
     """ Generate MD5 of the sample """
     with open(self.filename, 'rb') as f:
         md5_hash = md5sum(f.read()).hexdigest()
         return md5_hash
Example #10
0
 def get_md5(self):
     """ Generate MD5 of the sample """
     with open(self.filename, 'rb') as f:
         md5_hash = md5sum(f.read()).hexdigest()
         return md5_hash
import hashlib

file1 = "truc.txt"
file2 = "trux.txt"

print(hashlib.md5sum(open(file1).read().encode()).digest()) # format octal (non convertible en str)
print(hashlib.md5sum(open(file1).read().encode()).hexdigest()) # format hexa

print(hashlib.md5sum(open(file2).read().encode()).digest()) # format octal (non convertible en str)
print(hashlib.md5sum(open(file2).read().encode()).hexdigest()) # format hexa
Example #12
0
import hashlib

file1 = "truc.txt"
file2 = "trux.txt"

print(hashlib.md5sum(open(
    file1).read().encode()).digest())  # format octal (non convertible en str)
print(hashlib.md5sum(open(file1).read().encode()).hexdigest())  # format hexa

print(hashlib.md5sum(open(
    file2).read().encode()).digest())  # format octal (non convertible en str)
print(hashlib.md5sum(open(file2).read().encode()).hexdigest())  # format hexa