def post(self, request, *args, **kwargs): # Set Project project = request.POST.get('project', 'default') open_db(project) url = request.POST.get('url') tags = request.POST.get('tag_list') tags = "url," + tags if request.POST.get('tor'): downloaded_file = network.download(url, tor=True) else: downloaded_file = network.download(url, tor=False) if downloaded_file is None: messages.error(request, "server can't download from URL") return redirect(reverse("main-page-project", kwargs={"project": project})) tf = NamedTemporaryFile() tf.write(downloaded_file) if not tf: messages.error(request, "server can't download from URL") return redirect(reverse("main-page-project", kwargs={"project": project})) tf.flush() sha_256 = add_file(tf.name, name=url.split('/')[-1], tags=tags) if sha_256: messages.success(request, "stored file in database: {}".format(tf.name)) return redirect(reverse('main-page-project', kwargs={'project': project})) else: messages.error(request, "Unable to Store The File, already in database") return redirect(reverse("main-page-project", kwargs={"project": project}))
def url_download(): url = request.forms.get('url') tags = request.forms.get('tag_list') tags = "url," + tags if request.forms.get("tor"): upload = network.download(url, tor=True) else: upload = network.download(url, tor=False) if upload == None: return template('error.tpl', error="server can't download from URL") # Set Project project = 'Main' db = Database() tf = tempfile.NamedTemporaryFile() tf.write(upload) if tf == None: return template('error.tpl', error="server can't download from URL") tf.flush() tf_obj = File(tf.name) tf_obj.name = tf_obj.sha256 new_path = store_sample(tf_obj) success = False if new_path: # Add file to the database. success = db.add(obj=tf_obj, tags=tags) if success: #redirect("/project/{0}".format(project)) redirect("/file/Main/" + tf_obj.sha256) else: return template('error.tpl', error="Unable to Store The File,already in database")
def url_download(request): url = request.POST['url'] tags = request.POST['tag_list'] tags = "url,"+tags project = request.POST['project'] if 'tor' in request.POST: upload = network.download(url,tor=True) else: upload = network.download(url,tor=False) if upload is None: error = "server can't download from URL" return main_page(request, project=project, error=error) # Set Project project = request.POST['project'] if not project: project = 'default' open_db(project) tf = tempfile.NamedTemporaryFile() tf.write(upload) if tf == None: error = "server can't download from URL" return main_page(request, project=project, error=error) tf.flush() sha_256 = add_file(tf.name, tags, None) if sha_256: return redirect("/project/{0}".format(project)) else: error = "Unable to Store The File, already in database" return main_page(request, project=project, error=error)
def url_download(): url = request.forms.get('url') tags = request.forms.get('tag_list') tags = "url,"+tags if request.forms.get("tor"): upload = network.download(url,tor=True) else: upload = network.download(url,tor=False) if upload == None: return template('error.tpl', error="server can't download from URL") # Set Project project = 'Main' db = Database() tf = tempfile.NamedTemporaryFile() tf.write(upload) if tf == None: return template('error.tpl', error="server can't download from URL") tf.flush() tf_obj = File(tf.name) tf_obj.name = tf_obj.sha256 new_path = store_sample(tf_obj) success = False if new_path: # Add file to the database. success = db.add(obj=tf_obj, tags=tags) if success: #redirect("/project/{0}".format(project)) redirect("/file/Main/"+tf_obj.sha256) else: return template('error.tpl', error="Unable to Store The File,already in database")
def do(self): backup() ps = Parser() li = ps.list() for i in li: project = 'default' open_db(project) url = "http://" + i["url"] website = i["url"].split('/')[0] tags = i["ip"] + "," + website + "," + website.split('.')[-1] downloaded_file = network.download(url, tor=False) try: if downloaded_file is None: continue tf = NamedTemporaryFile() tf.write(downloaded_file) if not tf: continue tf.flush() sha_256 = add_file(tf.name, name=url.split('/')[-1], url=url, tags=tags) print("File Added") except: continue
def main(): print_warning( "WARNING: If you proceed you will lose any changes you might have made to Viper." ) choice = raw_input("Are you sure you want to proceed? [y/N] ") if choice.lower() != 'y': return # Download the latest Zip archive from GitHub's master branch. master = download(url) # Instantiate a StringIO, we will store the master.zip data in here. zip_data = StringIO() zip_data.write(master) # Initialize the Zip archive. zip_file = ZipFile(zip_data, 'r') # Obtain a list of all the files contained in the master.zip archive. names = zip_file.namelist() # Loop through all file and directories in master.zip. for name in names[1:]: # Split the path in parts. name_parts = path_split_all(name) # We strip the base directory, which is generated by GitHub in the # master.zip archive as {project}-{branch}. local_file_path = os.path.join(*name_parts[1:]) # Skip if the entry is a directory. if os.path.isdir(local_file_path): continue # Read the data of the current file. name_data = zip_file.read(name) # Calculate MD5 hash of the new file. name_data_md5 = hashlib.md5(name_data).hexdigest() # If the file already exists locally, we check if its MD5 hash # matches the one of the newly downloaded copy. If it does, we # obviously skip it. exists = False if os.path.exists(local_file_path): exists = True if File(local_file_path).md5 == name_data_md5: print_info("{0} up-to-date".format(local_file_path)) continue # Open the local file, whether it exists or not, and either # rewrite or write the new content. new_local = open(local_file_path, 'w') new_local.write(name_data) new_local.close() if exists: print_success("File {0} has been updated".format(local_file_path)) else: print_success( "New file {0} has been created".format(local_file_path)) zip_file.close() zip_data.close()
def main(): print_warning("WARNING: If you proceed you will lose any changes you might have made to Viper.") choice = raw_input("Are you sure you want to proceed? [y/N] ") if choice.lower() != 'y': return # Download the latest Zip archive from GitHub's master branch. master = download(url) # Instantiate a StringIO, we will store the master.zip data in here. zip_data = StringIO() zip_data.write(master) # Initialize the Zip archive. zip_file = ZipFile(zip_data, 'r') # Obtain a list of all the files contained in the master.zip archive. names = zip_file.namelist() # Loop through all file and directories in master.zip. for name in names[1:]: # Split the path in parts. name_parts = path_split_all(name) # We strip the base directory, which is generated by GitHub in the # master.zip archive as {project}-{branch}. local_file_path = os.path.join(*name_parts[1:]) # Skip if the entry is a directory. if os.path.isdir(local_file_path): continue # Read the data of the current file. name_data = zip_file.read(name) # Calculate MD5 hash of the new file. name_data_md5 = hashlib.md5(name_data).hexdigest() # If the file already exists locally, we check if its MD5 hash # matches the one of the newly downloaded copy. If it does, we # obviously skip it. exists = False if os.path.exists(local_file_path): exists = True if File(local_file_path).md5 == name_data_md5: print_info("{0} up-to-date".format(local_file_path)) continue # Open the local file, whether it exists or not, and either # rewrite or write the new content. new_local = open(local_file_path, 'w') new_local.write(name_data) new_local.close() if exists: print_success("File {0} has been updated".format(local_file_path)) else: print_success("New file {0} has been created".format(local_file_path)) zip_file.close() zip_data.close()
def cmd_open(self, *args): parser = argparse.ArgumentParser(prog="open", description="Open a file", epilog="You can also specify a MD5 or SHA256 hash to a previously stored file in order to open a session on it.") group = parser.add_mutually_exclusive_group() group.add_argument('-f', '--file', action="store_true", help="target is a file") group.add_argument('-u', '--url', action="store_true", help="target is a URL") group.add_argument('-l', '--last', action="store_true", help="target is the entry number from the last find command's results") parser.add_argument('-t', '--tor', action="store_true", help="Download the file through Tor") parser.add_argument("value", metavar='Path, URL, hash or ID', nargs='*', help="Target to open. Hash can be md5 or sha256. ID has to be from the last search.") try: args = parser.parse_args(args) except: return target = " ".join(args.value) if not args.last and target is None: parser.print_usage() return # If it's a file path, open a session on it. if args.file: target = os.path.expanduser(target) if not os.path.exists(target) or not os.path.isfile(target): self.log('error', "File not found: {0}".format(target)) return __sessions__.new(target) # If it's a URL, download it and open a session on the temporary file. elif args.url: data = download(url=target, tor=args.tor) if data: tmp = tempfile.NamedTemporaryFile(delete=False) tmp.write(data) tmp.close() __sessions__.new(tmp.name) # Try to open the specified file from the list of results from # the last find command. elif args.last: if __sessions__.find: count = 1 for item in __sessions__.find: if count == int(target): __sessions__.new(get_sample_path(item.sha256)) break count += 1 else: self.log('warning', "You haven't performed a find yet") # Otherwise we assume it's an hash of an previously stored sample. else: target = target.strip().lower() if len(target) == 32: key = 'md5' elif len(target) == 64: key = 'sha256' else: parser.print_usage() return rows = self.db.find(key=key, value=target) if not rows: self.log('warning', "No file found with the given hash {0}".format(target)) return path = get_sample_path(rows[0].sha256) if path: __sessions__.new(path)
def run(self, *args): try: args = self.parser.parse_args(args) except SystemExit: return target = " ".join(args.value) if not args.last and target is None: self.parser.print_usage() return # If it's a file path, open a session on it. if args.file: target = os.path.expanduser(target) if not os.path.exists(target) or not os.path.isfile(target): self.log('error', "File not found: {0}".format(target)) return __sessions__.new(target) # If it's a URL, download it and open a session on the temporary file. elif args.url: data = download(url=target, tor=args.tor) if data: tmp = tempfile.NamedTemporaryFile(delete=False) tmp.write(data) tmp.close() __sessions__.new(tmp.name) # Try to open the specified file from the list of results from # the last find command. elif args.last: if __sessions__.find: try: target = int(target) except ValueError: self.log('warning', "Please pass the entry number from the last find to -l/--last (e.g. open -l 5)") return for idx, item in enumerate(__sessions__.find, start=1): if idx == target: __sessions__.new(get_sample_path(item.sha256)) break else: self.log('warning', "You haven't performed a find yet") # Otherwise we assume it's an hash of an previously stored sample. else: target = target.strip().lower() if len(target) == 32: key = 'md5' elif len(target) == 64: key = 'sha256' else: self.parser.print_usage() return rows = db.find(key=key, value=target) if not rows: self.log('warning', "No file found with the given hash {0}".format(target)) return path = get_sample_path(rows[0].sha256) if path: __sessions__.new(path)
def cmd_open(self, *args): def usage(): print("usage: open [-h] [-f] [-u] [-l] [-t] <target|md5|sha256>") def help(): usage() print("") print("Options:") print("\t--help (-h)\tShow this help message") print("\t--file (-f)\tThe target is a file") print("\t--url (-u)\tThe target is a URL") print("\t--last (-l)\tOpen file from the results of the last find command") print("\t--tor (-t)\tDownload the file through Tor") print("") print("You can also specify a MD5 or SHA256 hash to a previously stored") print("file in order to open a session on it.") print("") try: opts, argv = getopt.getopt(args, "hfult", ["help", "file", "url", "last", "tor"]) except getopt.GetoptError as e: print(e) usage() return arg_is_file = False arg_is_url = False arg_last = False arg_use_tor = False for opt, value in opts: if opt in ("-h", "--help"): help() return elif opt in ("-f", "--file"): arg_is_file = True elif opt in ("-u", "--url"): arg_is_url = True elif opt in ("-l", "--last"): arg_last = True elif opt in ("-t", "--tor"): arg_use_tor = True if len(argv) == 0: usage() return else: target = argv[0] # If it's a file path, open a session on it. if arg_is_file: target = os.path.expanduser(target) if not os.path.exists(target) or not os.path.isfile(target): print_error("File not found") return __sessions__.new(target) # If it's a URL, download it and open a session on the temporary # file. elif arg_is_url: data = download(url=target, tor=arg_use_tor) if data: tmp = tempfile.NamedTemporaryFile(delete=False) tmp.write(data) tmp.close() __sessions__.new(tmp.name) # Try to open the specified file from the list of results from # the last find command. elif arg_last: if __sessions__.find: count = 1 for item in __sessions__.find: if count == int(target): __sessions__.new(get_sample_path(item.sha256)) break count += 1 else: print_warning("You haven't performed a find yet") # Otherwise we assume it's an hash of an previously stored sample. else: target = argv[0].strip().lower() if len(target) == 32: key = "md5" elif len(target) == 64: key = "sha256" else: usage() return rows = self.db.find(key=key, value=target) if not rows: print_warning("No file found with the given hash {0}".format(target)) return path = get_sample_path(rows[0].sha256) if path: __sessions__.new(path)
def cmd_open(self, *args): def usage(): print("usage: open [-h] [-f] [-u] [-l] [-t] <target|md5|sha256>") def help(): usage() print("") print("Options:") print("\t--help (-h)\tShow this help message") print("\t--file (-f)\tThe target is a file") print("\t--url (-u)\tThe target is a URL") print( "\t--last (-l)\tThe target is the entry number from the last find command's results" ) print("\t--tor (-t)\tDownload the file through Tor") print("") print( "You can also specify a MD5 or SHA256 hash to a previously stored" ) print("file in order to open a session on it.") print("") try: opts, argv = getopt.getopt(args, 'hfult', ['help', 'file', 'url', 'last', 'tor']) except getopt.GetoptError as e: print(e) usage() return arg_is_file = False arg_is_url = False arg_last = False arg_use_tor = False for opt, value in opts: if opt in ('-h', '--help'): help() return elif opt in ('-f', '--file'): arg_is_file = True elif opt in ('-u', '--url'): arg_is_url = True elif opt in ('-l', '--last'): arg_last = True elif opt in ('-t', '--tor'): arg_use_tor = True if len(argv) == 0: usage() return else: target = argv[0] # If it's a file path, open a session on it. if arg_is_file: target = os.path.expanduser(target) # This is kind of hacky. It checks if there are additional arguments # to the open command, if there is I assume that it's the continuation # of a filename with spaces. I then concatenate them. # TODO: improve this. if len(argv) > 1: for arg in argv[1:]: target += ' ' + arg if not os.path.exists(target) or not os.path.isfile(target): print_error("File not found: {0}".format(target)) return __sessions__.new(target) # If it's a URL, download it and open a session on the temporary # file. elif arg_is_url: data = download(url=target, tor=arg_use_tor) if data: tmp = tempfile.NamedTemporaryFile(delete=False) tmp.write(data) tmp.close() __sessions__.new(tmp.name) # Try to open the specified file from the list of results from # the last find command. elif arg_last: if __sessions__.find: count = 1 for item in __sessions__.find: if count == int(target): __sessions__.new(get_sample_path(item.sha256)) break count += 1 else: print_warning("You haven't performed a find yet") # Otherwise we assume it's an hash of an previously stored sample. else: target = argv[0].strip().lower() if len(target) == 32: key = 'md5' elif len(target) == 64: key = 'sha256' else: usage() return rows = self.db.find(key=key, value=target) if not rows: print_warning( "No file found with the given hash {0}".format(target)) return path = get_sample_path(rows[0].sha256) if path: __sessions__.new(path)
def cmd_open(self, *args): parser = argparse.ArgumentParser( prog="open", description="Open a file", epilog="You can also specify a MD5 or SHA256 hash to a previously stored file in order to open a session on it.", ) group = parser.add_mutually_exclusive_group() group.add_argument("-f", "--file", action="store_true", help="Target is a file") group.add_argument("-u", "--url", action="store_true", help="Target is a URL") group.add_argument( "-l", "--last", action="store_true", help="Target is the entry number from the last find command's results" ) parser.add_argument("-t", "--tor", action="store_true", help="Download the file through Tor") parser.add_argument( "value", metavar="PATH, URL, HASH or ID", nargs="*", help="Target to open. Hash can be md5 or sha256. ID has to be from the last search.", ) try: args = parser.parse_args(args) except: return target = " ".join(args.value) if not args.last and target is None: parser.print_usage() return # If it's a file path, open a session on it. if args.file: target = os.path.expanduser(target) if not os.path.exists(target) or not os.path.isfile(target): self.log("error", "File not found: {0}".format(target)) return __sessions__.new(target) # If it's a URL, download it and open a session on the temporary file. elif args.url: data = download(url=target, tor=args.tor) if data: tmp = tempfile.NamedTemporaryFile(delete=False) tmp.write(data) tmp.close() __sessions__.new(tmp.name) # Try to open the specified file from the list of results from # the last find command. elif args.last: if __sessions__.find: count = 1 for item in __sessions__.find: if count == int(target): __sessions__.new(get_sample_path(item.sha256)) break count += 1 else: self.log("warning", "You haven't performed a find yet") # Otherwise we assume it's an hash of an previously stored sample. else: target = target.strip().lower() if len(target) == 32: key = "md5" elif len(target) == 64: key = "sha256" else: parser.print_usage() return rows = self.db.find(key=key, value=target) if not rows: self.log("warning", "No file found with the given hash {0}".format(target)) return path = get_sample_path(rows[0].sha256) if path: __sessions__.new(path)
def cmd_open(self, *args): def usage(): print("usage: open [-h] [-f] [-u] [-t] <target>") def help(): usage() print("") print("Options:") print("\t--help (-h)\tShow this help message") print("\t--file (-f)\tThe target is a file") print("\t--url (-u)\tThe target is a URL") print("\t--tor (-t)\tDownload the file through Tor") print("") print("You can also specify a SHA256 hash to a previously stored") print("file in order to open a session on it.") print("") try: opts, argv = getopt.getopt(args, 'hfut', ['help', 'file', 'url', 'tor']) except getopt.GetoptError as e: print(e) usage() return is_file = False is_url = False use_tor = False for opt, value in opts: if opt in ('-h', '--help'): help() return elif opt in ('-f', '--file'): is_file = True elif opt in ('-u', '--url'): is_url = True elif opt in ('-t', '--tor'): use_tor = True if len(argv) == 0: usage() return else: target = argv[0] # If it's a file path, open a session on it. if is_file: target = os.path.expanduser(target) if not os.path.exists(target) or not os.path.isfile(target): print_error("File not found") return __session__.set(target) # If it's a URL, download it and open a session on the temporary # file. elif is_url: data = download(url=target, tor=use_tor) if data: tmp = tempfile.NamedTemporaryFile(delete=False) tmp.write(data) tmp.close() __session__.set(tmp.name) # Otherwise we assume it's an hash of an previously stored sample. else: target = argv[0].strip().lower() path = get_sample_path(target) if path: __session__.set(path)
def cmd_open(self, *args): def usage(): print("usage: open [-h] [-f] [-u] [-l] [-t] <target|md5|sha256>") def help(): usage() print("") print("Options:") print("\t--help (-h)\tShow this help message") print("\t--file (-f)\tThe target is a file") print("\t--url (-u)\tThe target is a URL") print("\t--last (-l)\tThe target is the entry number from the last find command's results") print("\t--tor (-t)\tDownload the file through Tor") print("") print("You can also specify a MD5 or SHA256 hash to a previously stored") print("file in order to open a session on it.") print("") try: opts, argv = getopt.getopt(args, 'hfult', ['help', 'file', 'url', 'last', 'tor']) except getopt.GetoptError as e: print(e) usage() return arg_is_file = False arg_is_url = False arg_last = False arg_use_tor = False for opt, value in opts: if opt in ('-h', '--help'): help() return elif opt in ('-f', '--file'): arg_is_file = True elif opt in ('-u', '--url'): arg_is_url = True elif opt in ('-l', '--last'): arg_last = True elif opt in ('-t', '--tor'): arg_use_tor = True if len(argv) == 0: usage() return else: target = argv[0] # If it's a file path, open a session on it. if arg_is_file: target = os.path.expanduser(target) # This is kind of hacky. It checks if there are additional arguments # to the open command, if there is I assume that it's the continuation # of a filename with spaces. I then concatenate them. # TODO: improve this. if len(argv) > 1: for arg in argv[1:]: target += ' ' + arg if not os.path.exists(target) or not os.path.isfile(target): print_error("File not found: {0}".format(target)) return __sessions__.new(target) # If it's a URL, download it and open a session on the temporary # file. elif arg_is_url: data = download(url=target, tor=arg_use_tor) if data: tmp = tempfile.NamedTemporaryFile(delete=False) tmp.write(data) tmp.close() __sessions__.new(tmp.name) # Try to open the specified file from the list of results from # the last find command. elif arg_last: if __sessions__.find: count = 1 for item in __sessions__.find: if count == int(target): __sessions__.new(get_sample_path(item.sha256)) break count += 1 else: print_warning("You haven't performed a find yet") # Otherwise we assume it's an hash of an previously stored sample. else: target = argv[0].strip().lower() if len(target) == 32: key = 'md5' elif len(target) == 64: key = 'sha256' else: usage() return rows = self.db.find(key=key, value=target) if not rows: print_warning("No file found with the given hash {0}".format(target)) return path = get_sample_path(rows[0].sha256) if path: __sessions__.new(path)
def run(self, *args): try: args = self.parser.parse_args(args) except SystemExit: return target = " ".join(args.value) if not args.last and target is None: self.parser.print_usage() return # If it's a file path, open a session on it. if args.file: target = os.path.expanduser(target) if not os.path.exists(target) or not os.path.isfile(target): self.log('error', "File not found: {0}".format(target)) return __sessions__.new(target) # If it's a URL, download it and open a session on the temporary file. elif args.url: data = download(url=target, tor=args.tor) if data: tmp = tempfile.NamedTemporaryFile(delete=False) tmp.write(data) tmp.close() __sessions__.new(tmp.name) # Try to open the specified file from the list of results from # the last find command. elif args.last: if __sessions__.find: try: target = int(target) except ValueError: self.log( 'warning', "Please pass the entry number from the last find to -l/--last (e.g. open -l 5)" ) return for idx, item in enumerate(__sessions__.find, start=1): if idx == target: __sessions__.new(get_sample_path(item.sha256)) break else: self.log('warning', "You haven't performed a find yet") # Otherwise we assume it's an hash of an previously stored sample. else: target = target.strip().lower() if len(target) == 32: key = 'md5' elif len(target) == 64: key = 'sha256' else: self.parser.print_usage() return db = Database() rows = db.find(key=key, value=target) if not rows: self.log( 'warning', "No file found with the given hash {0}".format(target)) return path = get_sample_path(rows[0].sha256) if path: __sessions__.new(path)