PDA

View Full Version : importing module dynamically in python



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

c0dist
01-08-2016, 12:59 PM
Similar can also be achieved using the "imp" (https://pymotw.com/2/imp/) module. I have seen this being used in CapTipper (https://github.com/omriher/CapTipper). Here's the simple code to load the modules from a directory "modules".



#!/usr/bin/env python

"""
This trick is inspired/taken from CapTipper - https://github.com/omriher/CapTipper/blob/master/CTPlugin.py
"""

import glob
import os
import imp

module_files = glob.glob("modules/*.py")
for module in module_files:
full_path = os.path.join(os.path.dirname(os.path.realpath(__fi le__)), module)
print "[+] Importing the module - %s" % full_path
(path, name) = os.path.split(full_path)
(name, ext) = os.path.splitext(name)

(p_file, filename, data) = imp.find_module(name, [path])
try:
mod = imp.load_module(name, p_file, filename, data)
except ImportError:
print "[-] Couldn't import module. Exiting!"
sys.exit(1)

# After the module is loaded, you can run the desired function. For ex.
mod.run()



Using the above trick, you can load the whole module in a variable, lets say "module1" and then it can be used like you would use a module, for ex. module1.function_name( arguments ), module1.variable_name, etc.

Hope this helps. Cheers.

Regards,
c0dist

b0nd
01-11-2016, 06:45 AM
Thanks for that c0dist. I'll try this out in my next code (who wants to mess with current working code :cool:)

Cheers!