Beispiel #1
0
def installModule(server, moduleName):
    req = __requests.post('http://{ip}/category/{m}'.format(ip=server,
                                                            m=moduleName))

    if req.json()['status'] == 'OK':
        category = req.json()['data']

        # Installing main files for the module

        req = __requests.get('http://{ip}/module/download/files/{m}'.format(
            ip=server, m=moduleName),
                             stream=True,
                             data={'module_name': moduleName},
                             headers={
                                 'Accept-Encoding': '*/*',
                                 'Content-Type': 'application/zip'
                             })

        category_folder = __os.path.join(__path, 'modules', category)
        if not __os.path.isdir(category_folder):
            __os.mkdir(category_folder)

        new_file = __os.path.join(category_folder,
                                  '{m}.zip'.format(m=moduleName))
        open(new_file, 'wb').write(req.content)

        unzipper = ht.getModule('ht_unzip')
        unzipper.extractFile(new_file)

        __os.remove(new_file)

        # Installing the json config
        req = __requests.get('http://{ip}/module/download/config/{m}'.format(
            ip=server, m=moduleName),
                             stream=True,
                             data={'module_name': moduleName},
                             headers={
                                 'Accept-Encoding': '*/*',
                                 'Content-Type': 'application/json'
                             })

        config_django_module_path = __os.path.abspath(
            __os.path.join(__os.path.dirname(__file__),
                           'config_modules_django', category))
        if not __os.path.isdir(config_django_module_path):
            __os.mkdir(config_django_module_path)

        open(
            __os.path.join(
                config_django_module_path,
                'ht_{m}.json'.format(m=moduleName.replace('ht_', ''))),
            'wb').write(req.content)
Beispiel #2
0
def loadModuleUrls(moduleName):
    main_function_config = ht.Config.getConfig(
        parentKey='modules',
        key=moduleName,
        subkey='django_form_main_function')
    functions_config = ht.Config.getConfig(
        parentKey='modules',
        key=moduleName,
        subkey='django_form_module_function')

    main_func = None
    mod = ht.getModule(moduleName)
    if hasattr(mod, '_main_gui_func_'):
        main_func = mod._main_gui_func_

    category = ht.getModuleCategory(moduleName)

    if not functions_config:
        functions = ht.getFunctionsNamesFromModule(moduleName)

        if functions and 'help' in functions:
            functions.remove('help')
        if functions:
            functions_config = {}
            for func in functions:
                new_conf = ht.DjangoFunctions.__createModuleFunctionView__(
                    category, moduleName, functionName=func)
                if new_conf:
                    functions_config[func] = new_conf[func]
    else:
        for function in ht.getFunctionsNamesFromModule(moduleName):
            if not function in functions_config and not 'help' == function:
                new_conf = ht.DjangoFunctions.__createModuleFunctionView__(
                    category, moduleName, functionName=function)
                if new_conf:
                    functions_config[function] = new_conf[function]

    if main_function_config:
        if '__function__' in main_function_config:
            try:
                func_call = main_function_config['__function__']

                url_path = 'modules/{cat}/{mod}/{func_call}/'.format(
                    cat=ht.getModuleCategory(moduleName),
                    mod=moduleName.replace('ht_', ''),
                    func_call=func_call)
                to_import = 'from .views_modules.{category} import views_{mod}'.format(
                    category=ht.getModuleCategory(moduleName), mod=moduleName)
                exec(to_import)
                to_execute = 'views_{mod}.{func_call}'.format(
                    mod=moduleName, func_call=func_call)
                view_object = eval(to_execute)

                urlpatterns.append(path(url_path, view_object, name=func_call))

            except ImportError:
                ht.Logger.printMessage(
                    message='loadModuleUrls',
                    description='Can\' load {to_import}. Creating it!'.format(
                        to_import=to_import),
                    is_info=True)
                UtilsDjangoViewsAuto.loadModuleFunctionsToView(
                    moduleName, ht.getModuleCategory(moduleName))
                loadModuleUrls(moduleName)
                if functions_not_loaded and func_call in functions_not_loaded:
                    functions_not_loaded.remove(func_call)
                functions_not_loaded.append(func_call)
                UtilsDjangoViewsAuto.createTemplateFunctionForModule(
                    moduleName, ht.getModuleCategory(moduleName), func_call)

            except:
                ht.Logger.printMessage(
                    message='loadModuleUrls',
                    description=
                    'There is no View for the URL \'{mod_url}\' Creating it!'.
                    format(mod_url=func_call),
                    is_warn=True)
                if functions_not_loaded and func_call in functions_not_loaded:
                    functions_not_loaded.remove(func_call)
                functions_not_loaded.append(func_call)
                UtilsDjangoViewsAuto.createTemplateFunctionForModule(
                    moduleName, ht.getModuleCategory(moduleName), func_call)
    else:
        if main_func and not main_function_config:
            #ht.Logger.printMessage(message='loadModuleUrls', description='{mod} has no config for main function. Creating main function setted: {m}'.format(mod=moduleName, m=main_func), is_warn=True)
            if main_func in ht.getFunctionsNamesFromModule(moduleName):
                ht.DjangoFunctions.__createModuleFunctionView__(category,
                                                                moduleName,
                                                                main_func,
                                                                is_main=True)
            else:
                ht.Logger.printMessage(
                    message='loadModuleUrls',
                    description=
                    'Error creating main function setted: {m}. Function does not exist in module {mod}'
                    .format(m=main_func, mod=moduleName),
                    is_error=True)
        #else:
        #ht.Logger.printMessage(message='loadModuleUrls', description='The module {mod} does not have any function defined as main for de gui'.format(mod=moduleName), is_error=True)

    if functions_config:
        for function_conf in functions_config:
            if '__function__' in functions_config[function_conf]:
                try:
                    func_call = functions_config[function_conf]["__function__"]
                    url_path = 'modules/{cat}/{mod}/{func_call}/'.format(
                        cat=ht.getModuleCategory(moduleName),
                        mod=moduleName.replace('ht_', ''),
                        func_call=func_call)
                    to_import = 'from .views_modules.{category} import views_{mod}'.format(
                        category=ht.getModuleCategory(moduleName),
                        mod=moduleName)
                    exec(to_import)
                    to_execute = 'views_{mod}.{func_call}'.format(
                        mod=moduleName, func_call=func_call)
                    view_object = eval(to_execute)

                    urlpatterns.append(
                        path(url_path, view_object, name=func_call))

                except ImportError:
                    ht.Logger.printMessage(
                        message='loadModuleUrls',
                        description='Can\' load {to_import}. Creating it!'.
                        format(to_import=to_import),
                        is_info=True)
                    UtilsDjangoViewsAuto.loadModuleFunctionsToView(
                        moduleName, ht.getModuleCategory(moduleName))
                    loadModuleUrls(moduleName)
                    if functions_not_loaded and func_call in functions_not_loaded:
                        functions_not_loaded.remove(func_call)
                    functions_not_loaded.append(func_call)
                    UtilsDjangoViewsAuto.createTemplateFunctionForModule(
                        moduleName, ht.getModuleCategory(moduleName),
                        func_call)

                except:
                    ht.Logger.printMessage(
                        message='loadModuleUrls',
                        description=
                        'There is no View for the URL \'{mod_url}\' Creating it!'
                        .format(mod_url=func_call),
                        is_warn=True)
                    if functions_not_loaded and func_call in functions_not_loaded:
                        functions_not_loaded.remove(func_call)
                    functions_not_loaded.append(func_call)
                    UtilsDjangoViewsAuto.createTemplateFunctionForModule(
                        moduleName, ht.getModuleCategory(moduleName),
                        func_call)
    else:
        pass
def __createModuleFunctionView__(category,
                                 moduleName,
                                 functionName,
                                 is_main=False):
    try:
        # Creates the JSON config for the view modal form
        functionParams = Utils.getFunctionsParams(category=category,
                                                  moduleName=moduleName,
                                                  functionName=functionName)

        # Get posible functions with args that uses values from another func
        funcsFromFunc = None if not hasattr(
            ht.getModule(moduleName), '_funcArgFromFunc_') else dict(
                ht.getModule(moduleName)._funcArgFromFunc_)

        moduleViewConfig = {}

        moduleViewConfig['__function__'] = functionName

        if not is_main:
            moduleViewConfig['__async__'] = False

            if ht.Utils.doesFunctionContainsExplicitReturn(
                    ht.Utils.getFunctionFullCall(
                        moduleName, ht.getModuleCategory(moduleName),
                        functionName)):
                moduleViewConfig['__return__'] = 'text'
            else:
                moduleViewConfig['__return__'] = ''

        if not functionParams:
            # Retry for reload
            functionParams = ht.Utils.getFunctionsParams(
                category=category,
                moduleName=moduleName,
                functionName=functionName)

        if functionParams:
            if 'params' in functionParams:
                for param in functionParams['params']:
                    moduleViewConfig[param] = {}
                    moduleViewConfig[param][
                        '__type__'] = 'file' if 'file' in str(param).lower(
                        ) else ht.Utils.getValueType(str(param))
                    moduleViewConfig[param]['label_desc'] = param
                    moduleViewConfig[param]['placeholder'] = param
                    moduleViewConfig[param]['required'] = True

            if 'defaults' in functionParams:
                for param in functionParams['defaults']:
                    moduleViewConfig[param] = {}
                    moduleViewConfig[param][
                        '__type__'] = 'file' if 'file' in str(
                            param).lower() else ht.Utils.getValueType(
                                str(functionParams['defaults'][param]))
                    moduleViewConfig[param]['label_desc'] = param
                    moduleViewConfig[param]['placeholder'] = param
                    moduleViewConfig[param]['value'] = functionParams[
                        'defaults'][param]
                    if moduleViewConfig[param]['__type__'] == 'checkbox':
                        moduleViewConfig[param]['selected'] = moduleViewConfig[
                            param]['value']

            # Create the data for auto fill the param with another funcs return
            if funcsFromFunc and functionName in funcsFromFunc and len(
                    funcsFromFunc[functionName]) > 0:
                for arg in funcsFromFunc[functionName]:
                    if arg in moduleViewConfig:
                        if not 'options_from_function' in moduleViewConfig[arg]:
                            moduleViewConfig[arg]['options_from_function'] = {}
                        for moduleToExecute in funcsFromFunc[functionName][
                                arg]:
                            moduleViewConfig[arg]['options_from_function'][
                                moduleToExecute] = funcsFromFunc[functionName][
                                    arg][moduleToExecute]
        else:
            ht.Logger.printMessage(message='No function params',
                                   description=functionName,
                                   debug_core=True)

        # Add pool it checkbox
        pool_param = '__pool_it_{p}__'.format(p=functionName)
        moduleViewConfig[pool_param] = {}
        moduleViewConfig[pool_param]['__type__'] = 'checkbox'
        moduleViewConfig[pool_param][
            'label_desc'] = 'Pool the execution to the pool list'
        moduleViewConfig[pool_param]['selected'] = False

        if is_main:
            moduleViewConfig['close'] = {}
            moduleViewConfig['close']['__type__'] = 'button'
            moduleViewConfig['close']['value'] = 'Close'

            moduleViewConfig['submit'] = {}
            moduleViewConfig['submit']['__id__'] = functionName
            moduleViewConfig['submit']['__type__'] = 'submit'
            if functionName:
                moduleViewConfig['submit']['value'] = ' '.join(
                    [x.capitalize() for x in functionName.split('_')])
            moduleViewConfig['submit']['loading_text'] = '{m}ing'.format(
                m=moduleName.replace('ht_', ''))

        Config.__save_django_module_config__(moduleViewConfig,
                                             category,
                                             moduleName,
                                             functionName,
                                             is_main=is_main)
        Config.switch_function_for_map(category, moduleName, functionName)
        ht.Config.__look_for_changes__()
        ht.Logger.printMessage(message='Creating Function Modal View',
                               description=functionName,
                               debug_core=True)
        return {functionName: moduleViewConfig}
    except Exception as e:
        Logger.printMessage(str(e), is_error=True)
        return None
Beispiel #4
0
def uploadModule(server, category, moduleName):
    module_folder = __os.path.join(__path, 'modules', category,
                                   moduleName.replace('ht_', ''))
    unzipper = ht.getModule('ht_unzip')

    # Zip the module directory
    zipped_file = unzipper.zipDirectory(module_folder)

    # Create Uploads directory
    uploadsFolder = __os.path.join(
        __os.path.join(__os.path.split(zipped_file)[0]), '__uploads__')
    if not __os.path.isdir(uploadsFolder):
        __os.mkdir(uploadsFolder)

    # Create Module directory inside Uploads
    moduleUploadsFolder = __os.path.join(uploadsFolder,
                                         moduleName.replace('ht_', ''))
    if not __os.path.isdir(moduleUploadsFolder):
        __os.mkdir(moduleUploadsFolder)

    # Move zipped module to uploads dir
    new_file_zipped = __os.path.join(moduleUploadsFolder,
                                     __os.path.split(zipped_file)[-1])
    try:
        __os.rename(zipped_file, new_file_zipped)
    except:
        try:
            if __os.path.isfile(new_file_zipped):
                __os.remove(new_file_zipped)
            __os.rename(zipped_file, new_file_zipped)
        except:
            pass

    # Copy the JSON config for the module into the uploads dir
    moduleConfigFile = __os.path.join(
        __path, 'core', 'config_modules_django', category,
        'ht_{f}.json'.format(f=moduleName.replace('ht_', '')))
    newModuleConfigFile = __os.path.join(
        moduleUploadsFolder,
        'ht_{f}.json'.format(f=moduleName.replace('ht_', '')))
    if __os.path.isfile(moduleConfigFile):
        if __os.path.isfile(newModuleConfigFile):
            __os.remove(newModuleConfigFile)
        __shutil.copyfile(moduleConfigFile, newModuleConfigFile)

    # Copy the Django view for the module into the uploads dir
    hackingtoolsDjangoCoreFolder = __os.path.split(
        __os.path.split(__path)[0])[0]
    moduleViewsFile = __os.path.join(
        hackingtoolsDjangoCoreFolder, 'views_modules', category,
        'views_ht_{f}.py'.format(f=moduleName.replace('ht_', '')))
    newModuleViewsFile = __os.path.join(
        moduleUploadsFolder,
        'views_ht_{f}.py'.format(f=moduleName.replace('ht_', '')))
    if __os.path.isfile(moduleViewsFile):
        if __os.path.isfile(newModuleViewsFile):
            __os.remove(newModuleViewsFile)
        __shutil.copyfile(moduleViewsFile, newModuleViewsFile)

    # Copy the requirements file for the module into the uploads dir
    moduleRequirementsFile = __os.path.join(
        __path, 'modules', '__requirements__', category,
        'ht_{f}.txt'.format(f=moduleName.replace('ht_', '')))
    newModuleRequirementsFile = __os.path.join(
        moduleUploadsFolder,
        'ht_{f}.txt'.format(f=moduleName.replace('ht_', '')))
    if __os.path.isfile(moduleRequirementsFile):
        if __os.path.isfile(newModuleRequirementsFile):
            __os.remove(newModuleRequirementsFile)
        __shutil.copyfile(moduleRequirementsFile, newModuleRequirementsFile)

    # Zip all together
    full_zipped_file = unzipper.zipDirectory(moduleUploadsFolder)

    files = {
        moduleName.replace('ht_', ''): open(full_zipped_file, 'rb'),
    }
    req = __requests.post('http://{ip}/new/module/upload/{c}'.format(
        ip=server, c=category),
                          files=files)
    if req.json()['status'] == 'OK':
        Logger.printMessage(req.json()['data'], debug_core=True)
    else:
        Logger.printMessage(req.json()['data'], is_error=True)