7

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?

Freshollie
  • 171
  • 6
  • 13
  • 1
    Probably I've got wrong what you are asking, anyway you can get the module name with the `__name__` attribute and you can use `'.'.join(["package", "subpackage", "module"])` to join a variable number of fields with the dot. – Bakuriu Oct 12 '12 at 16:34
  • I will try this, thanks. I will reply if I get anywhere :) – Freshollie Oct 12 '12 at 16:41
  • Do you mean like this? "for b in dir(".".join([modulename,a])):" – Freshollie Oct 12 '12 at 16:42
  • Yeah that doesn't work "Traceback (most recent call last): File "C:\Users\FreshOllie\Documents\Python (Computing)\Find all directories.py", line 7, in h=".".join([random,a]) TypeError: sequence item 0: expected string, module found" – Freshollie Oct 12 '12 at 16:47
  • You can get the name of the submodule(with already the dots) with the `__name__` attribute: `a.__name__ -> modulename.a`. – Bakuriu Oct 12 '12 at 17:35
  • @Bakuriu I bet you know what to do but can't convey this to me, I am a noob at programming and I still don't understand what I could do with this :(. Could you work it into a piece of my code to show me an example of where I could use it? Many thanks. – Freshollie Oct 13 '12 at 09:33

1 Answers1

5
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?

ezod
  • 7,261
  • 2
  • 24
  • 34
  • Where am I suppose to use this, if I try and input a module into this it gives ">>> list_modules(random) Traceback (most recent call last): File "", line 1, in list_modules(random) File "C:/Users/FreshOllie/Documents/Python (Computing)/test.py", line 6, in list_modules module = __import__(module_name, globals(), locals(), [module_name.split('.')[-1]]) AttributeError: 'module' object has no attribute 'split'" – Freshollie Oct 12 '12 at 17:35
  • `module_name` is a string, try `list_modules('random')` instead (although `random` has no submodules so it won't be interesting). – ezod Oct 12 '12 at 18:11
  • Thanks very much this seems to work very well - But I found it doesn't list all of the functions. In this injector if I use. `list_modules("base")` it comes up with nothing, even though if I do `print dir(base)` it comes up with hundreds of possible uses. `'autoPlayAgain', 'backfaceCullingEnabled', 'backfaceCullingOff', 'backfaceCullingOn', 'backgroundDrawable', 'bboard', 'bottomCells', 'bufferViewer', 'buttonThrowers', 'cTrav', 'cTravStack', 'cam', 'cam2d', 'cam2dp', 'camFrustumVis', 'camLens', 'camList', 'camNode', 'camera', 'camera2d', 'camera2dp', 'canScreenShot', 'cannonsEnabled'` – Freshollie Oct 13 '12 at 09:20
  • Your question seems to imply that you only want to find submodules. You could also check for objects of types `types.FunctionType` and `types.LambdaType` for example (obviously without calling `list_modules` recursively on those). You're getting into a more ambiguous situation here though -- do you want to print out classes and their methods as well? etc. – ezod Oct 14 '12 at 21:26
  • Yes, sorry I don't really know what I am talking about do I? I am not very experienced in this type of stuff so I didn't know how to describe what I wanted. If you think that is what I'm looking for then its probably right, is there anyway of doing this? – Freshollie Oct 15 '12 at 08:41
  • This code works quite fine, although dir() does not always display all modules. I made dir() work better by first loading all modules: http://stackoverflow.com/questions/1057431/loading-all-modules-in-a-folder-in-python – Robin Manoli Apr 12 '15 at 08:49