Is there a way to hide the python language version from the script that is being run?

Viewed 79

I am considering creating a coding puzzle where the point would be that different versions of python behave differently. For this, I would like to prevent the player-provided python script from trivially figuring out the python version.

I consider approaches like these trivial :

  • Calling a function or reading a variable to get the version. E.g. from sys
  • Reading the version from an environment variable
  • Figuring out the path of the python installation and finding the version there

I would be surprised if it is possible to really completely hide it, because the python interpreter necessarily needs to be able to access its own files. But for my purposes it would be enough to make reading the version harder than determining different version behaviours. How could I do this with reasonable effort?

I am willing to monkey-patch the distributions, completely messing up some files in the machine, maybe even building the python source myself with a different version number, or perhaps audit hooks could be helpful (in newer versions). But I am not clear on what exactly I would need to be aware of.

1 Answers

I think at least a few of your criteria may be made possible by redefining inbuilt functions that could help the user find the version. For example:

def wall():
    print('Did you really think it would be that easy?')

help = wall

Now, whenever the user tries to use the help function...

>>> help
<function wall at 0xb5b31bf8>
>>> help()
Did you really think it would be that easy?
>>> 

This will also work for sys.version.

import sys

def wall():
    print('Did you really think it would be that easy?')

sys.version = wall

When you call it...

>>> sys.version
<function wall at 0xb5b2abf8>

I've tested this a bit, and I know approaches like this won't work:

>>> help
<function wall at 0xb5bb1bf8>
>>> help = help
>>> help
<function wall at 0xb5bb1bf8>

This would probably be easy to break for someone with more coding experience than me, and it doesn't cover all your criteria, but nonetheless I hope it's helpful!

Related