b0nd
01-07-2016, 05:13 AM
"python - load modules dynamically"
Situation is:
1. You have got a directory where you've python modules. When your fuzzer runs, it shall on-the-fly import modules randomly from that directory and use the fuction(s) existing within them.
Here you've got two problems:
1. You do not want to be statically bound to edit your main code everytime you add a new module in directory.
2. You don't know the functions existing inside your python modules because you don't know the name of randomly picked modules at the first place.
Requirement:
Pick any random number of modules from directory and utilize there function to code something as your test cases.
Solution:
import os
import glob
import sys
import inspect
import copy
sys.path.append('modules_fuzz')
jsCode = ""
__file__ = os.path.join(executablePath, fuzzModulesDir + '/')
allModulesList = []
for f in glob.glob(os.path.dirname(__file__)+"/*.py"): ## it finds all .py fuzz modules present in fuzzModulesDir directory
if os.path.isfile(f) and not os.path.basename(f).startswith('_'):
allModulesList.append(os.path.basename(f)[:-3]) ## allModulesList: ['module1', 'module2', 'module3', ......, 'upto-all-modules']
for module in allModulesList:
new_module = __import__(module) ## here module is getting imported without specifying actual name
functionFromModule = inspect.getmembers(new_module, inspect.isfunction) ## list of all functions from current module
jsCode += getattr(new_module, functionFromModule[0][0])(argument_to_function) ## doing operation on function passing it some argument
i += 1
Situation is:
1. You have got a directory where you've python modules. When your fuzzer runs, it shall on-the-fly import modules randomly from that directory and use the fuction(s) existing within them.
Here you've got two problems:
1. You do not want to be statically bound to edit your main code everytime you add a new module in directory.
2. You don't know the functions existing inside your python modules because you don't know the name of randomly picked modules at the first place.
Requirement:
Pick any random number of modules from directory and utilize there function to code something as your test cases.
Solution:
import os
import glob
import sys
import inspect
import copy
sys.path.append('modules_fuzz')
jsCode = ""
__file__ = os.path.join(executablePath, fuzzModulesDir + '/')
allModulesList = []
for f in glob.glob(os.path.dirname(__file__)+"/*.py"): ## it finds all .py fuzz modules present in fuzzModulesDir directory
if os.path.isfile(f) and not os.path.basename(f).startswith('_'):
allModulesList.append(os.path.basename(f)[:-3]) ## allModulesList: ['module1', 'module2', 'module3', ......, 'upto-all-modules']
for module in allModulesList:
new_module = __import__(module) ## here module is getting imported without specifying actual name
functionFromModule = inspect.getmembers(new_module, inspect.isfunction) ## list of all functions from current module
jsCode += getattr(new_module, functionFromModule[0][0])(argument_to_function) ## doing operation on function passing it some argument
i += 1