def add_module(mod): """Add new module to the framework""" try: mod_name = mod['name'] mod_ns = mod['ns'] logger.info("Adding new module: '" + mod_ns + "/" + mod_name + "'") # Check namespace if not mod_ns: ns_error_msg = "Invalid namespace." logger.error(ns_error_msg) emit('log_message', {'tag': 'danger', 'content': ns_error_msg}) return # Check module name if not re.match(r'^\w+$', mod_name): mod_error_msg = "Invalid module name." logger.error(mod_error_msg) emit('log_message', {'tag': 'danger', 'content': mod_error_msg}) return # Get modules.json with open(mod_json_file, 'r') as f: data = json.load(f) # Prepare module dict mod_dict = { "name": mod_name, "desc": mod['desc'], "command": mod['cmd'], "require_superuser": str(mod['su']).capitalize(), "target_os": "win" if check_os() else "linux" } # Append to json data try: data[mod_ns].append(mod_dict) except: data.update({mod_ns: [mod_dict]}) # Update the modules.json with open(mod_json_file, 'w') as f: json.dump(data, f, indent=4) success_msg = "Module added successfully." emit('log_message', {'tag': 'success', 'content': success_msg}) logger.info(success_msg) global module_dict module_dict = get_modules() # Success event with module count emit('new_module_added', get_mod_count(module_dict)) except Exception as e: exception_msg = "Error occurred while adding new module. " + str(e) emit('log_message', { 'tag': 'warning', 'content': exception_msg, 'duration': 2000 }) logger.warn(exception_msg)
class Shell(ShellCommands, Cmd): """Interactive Shell constructor class""" namespace = "" module = "" modules = get_modules() intro = "Welcome to the grapheneX interactive shell. " + \ "Type \"help\" or \"?\" to list commands." @property def prompt(self): """Set prompt according to the current module and namespace""" prompt_str = "" if self.namespace: prompt_str = prompt_str + ":" + self.namespace if self.module: prompt_str = prompt_str + ":" + self.module return f"[gX{prompt_str}]> "
def do_manage(self, arg): """Add, edit or delete module""" def save_mod_json(data): with open(mod_json_file, 'w') as f: json.dump(data, f, indent=4) # Read modules.json data = get_mod_json() try: edit_prompt = [{ 'type': 'list', 'name': 'option', 'message': 'What do you want to do?', 'choices': ["Add module", "Edit module", "Remove module"], }] choice = prompt(edit_prompt) # ADD if choice['option'] == "Add module": # Namespace selection ns_prompt = [{ 'type': 'list', 'name': 'namespace', 'message': 'Select a namespace for your module', 'choices': list(self.modules.keys()) + ["new"], }] # Module details mod_ns = prompt(ns_prompt)['namespace'] mod_questions = [{ 'type': 'input', 'name': 'mod_name', 'message': 'Name of your module', 'validate': ModuleNameValidation, }, { 'type': 'input', 'name': 'mod_desc', 'message': 'Module description', }, { 'type': 'input', 'name': 'mod_cmd', 'message': 'Command', }, { 'type': 'confirm', 'name': 'mod_su', 'message': 'Does this command requires superuser?', }] if mod_ns == "new": mod_question = [{ 'type': 'input', 'name': 'mod_ns', 'message': 'Name of your namespace', 'validate': NamespaceValidation }] mod_namespace = prompt(mod_question) try: mod_ns = mod_namespace['mod_ns'] except: pass # Assigning property to the ModuleNameValidation class to # access modules within the selected namespace. ModuleNameValidation.modules = self.modules[mod_ns].keys() \ if mod_ns in self.modules.keys() else [] mod_details = prompt(mod_questions) mod_dict = { "name": mod_details['mod_name'].title(), "desc": mod_details['mod_desc'], "command": mod_details['mod_cmd'], "require_superuser": mod_details['mod_su'], "target_os": "win" if check_os() else "linux" } try: data[mod_ns.lower()].append(mod_dict) except: data.update({mod_ns.lower(): [mod_dict]}) # Write the updated modules.json save_mod_json(data) logger.info("Module added successfully. Use 'list' " + \ "command to see available modules.") # EDIT & REMOVE elif choice['option'] == "Edit module" or choice[ 'option'] == "Remove module": mod_option = choice['option'].split(" ")[0].lower() # Namespace selection ns_prompt = [{ 'type': 'list', 'name': 'namespace', 'message': "Select the namespace of module to " + mod_option, 'choices': list(self.modules.keys()) }] selected_ns = prompt(ns_prompt)['namespace'] # Module selection mod_prompt = [{ 'type': 'list', 'name': 'module', 'message': "Select a module to " + mod_option, 'choices': self.modules[selected_ns] }] selected_mod = prompt(mod_prompt)['module'] mod_list = [mod['name'] for mod in list(data[selected_ns])] mod_index = mod_list.index(selected_mod) # EDIT if mod_option == "edit": # Create a list for properties prop_list = list( self.modules[selected_ns][selected_mod].kwargs.keys()) prop_list.remove('namespace') prop_list.remove('target_os') # Module property selection prop_prompt = [{ 'type': 'list', 'name': 'property', 'message': "Select a property for editing " + selected_mod, 'choices': prop_list }] selected_prop = prompt(prop_prompt)['property'] # New value for property new_val = prompt([{ 'type': 'input', 'name': 'val', 'message': "New value for " + selected_prop }])['val'] new_val = new_val.title( ) if selected_prop == "name" else new_val # Update the selected property of module data[selected_ns][mod_index][selected_prop] = new_val # Write the updated modules.json save_mod_json(data) logger.info("Module updated successfully. (" + selected_ns + "/" + selected_mod + ":" + selected_prop + ")") # REMOVE else: data[selected_ns].pop(mod_index) save_mod_json(data) logger.info("Module removed successfully. (" + selected_mod + ")") else: pass except Exception as e: logger.error(str(e)) self.namespace = "" self.module = "" self.modules = get_modules()
#!/usr/bin/python3.7 # -*- coding: utf-8 -*- from graphenex.core.web import app, logger, socketio from graphenex.core.utils.helpers import check_os, get_modules, mod_json_file from graphenex.core.utils.sysinfo import SysInformation from flask import render_template, session, request, redirect, flash from flask_socketio import emit, disconnect from functools import wraps import json import re module_dict = get_modules() current_namespace = list(module_dict.keys())[0] def login_required(f): @wraps(f) def decorated_function(*args, **kwargs): token = session.get('token', None) if token != app.config['ACCESS_TOKEN']: return redirect('/auth') else: return f(*args, **kwargs) return decorated_function def auth_socketio(f): @wraps(f)