Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to print every submodule inside a module in python

sorry if I got some facts wrong during this but I have only been programming for about a year, since I started a computing course at my school, and sorry if this question has been answered before I have looked everywhere but I don't know what to search for. Recently I found a game where you can use an injector to inject python code into it, but the only way of finding the codes is by using:

    import modulename
    print dir(modulename)

and then print dir(modulename.submodulename)

Which would print all the submodules inside that module to the log file of the game. Using this technique I have developed a script which when executed will find all of the sub modules in that directory and write them to a file.

import modulename

myfile=open("name.txt","w")
myfile.write("modulename\n")

for a in dir(modulename):
    a="modulename"+"."+a
    for b in dir(a):
        b=a+"."+b
        for c in dir(b):
            c=b+"."+c
            if ".__" in c:
                pass
            elif "._" in c:
                pass
            else:
                myfile.write(c+"\n")
        if ".__" in b:
            pass
        if "._" in b:
            pass
        else:
            myfile.write(b+"\n")
    if ".__" in a:
        pass
    if "._" in a:
        pass
    else:
        myfile.write(a+"\n")

print "Done"
myfile.close()

Unfortunately when run past "something.something" the modules are non existent. Here is a sample for the module "Random" where "random.weibullvariate" is a real module but anything past the second "." is not in the random submodules.

random.weibullvariate.title.translate
random.weibullvariate.title.upper
random.weibullvariate.title.zfill
random.weibullvariate.title
random.weibullvariate.translate.capitalize
random.weibullvariate.translate.center
random.weibullvariate.translate.count
random.weibullvariate.translate.decode
random.weibullvariate.translate.encode
random.weibullvariate.translate.endswith
random.weibullvariate.translate.expandtabs
random.weibullvariate.translate.find
random.weibullvariate.translate.format
random.weibullvariate.translate.index
random.weibullvariate.translate.isalnum
random.weibullvariate.translate.isalpha
random.weibullvariate.translate.isdigit
random.weibullvariate.translate.islower
random.weibullvariate.translate.isspace
random.weibullvariate.translate.istitle
random.weibullvariate.translate.isupper
random.weibullvariate.translate.join
random.weibullvariate.translate.ljust
random.weibullvariate.translate.lower
random.weibullvariate.translate.lstrip
random.weibullvariate.translate.partition
random.weibullvariate.translate.replace
random.weibullvariate.translate.rfind
random.weibullvariate.translate.rindex
random.weibullvariate.translate.rjust
random.weibullvariate.translate.rpartition
random.weibullvariate.translate.rsplit
random.weibullvariate.translate.rstrip
random.weibullvariate.translate.split
random.weibullvariate.translate.splitlines
random.weibullvariate.translate.startswith
random.weibullvariate.translate

As you can see there are submodules being put in that don't exist in "random". I ended up working out what the problem is but I am not experienced enough to solve the problem.

The problem is that using the first 2 lines as an example

for a in dir(modulename):
    a="module name"+"."+a

if I did a "modulename.submodule" As you can see "a" is a string, and if I then put "a" into a "dir()" then the same thing would be returned no matter what the submodules name was.

I need to find a way to add the submodule onto the previous modules name with a "." in between without turning it into a string.

Sorry for the long post, anyone have any ideas?

like image 695
Freshollie Avatar asked Oct 12 '12 16:10

Freshollie


1 Answers

import types

def list_modules(module_name):
    try:
        module = __import__(module_name, globals(), locals(), [module_name.split('.')[-1]])
    except ImportError:
        return
    print(module_name)
    for name in dir(module):
        if type(getattr(module, name)) == types.ModuleType:
            list_modules('.'.join([module_name, name]))

Can't claim this will work for all cases, but worth a try?

like image 59
ezod Avatar answered Sep 30 '22 12:09

ezod