6

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.

lucidbrot
  • 5,378
  • 3
  • 39
  • 68
  • Different versions of python **do** behave differently, that's why they're different versions :-). Im curious about your puzzle... – Kelly Bundy Jul 04 '22 at 15:06
  • 1
    @KellyBundy the rough idea is that submitted code will have to produce output that fulfills different criteria in different checks - each check being run in a different python version. Some checks verifying the opposites of each other. – lucidbrot Jul 04 '22 at 15:07
  • 1
    Intriguing. It might also be interesting to limit a submitted script to a specified subset of Python, and ask, "With what's available to you, can you correctly determine what version of Python is running your code?" – CrazyChucky Jul 04 '22 at 15:12
  • @CrazyChucky how would you go about enforcing such a constraint? Perhaps that is what my question boils down to – lucidbrot Jul 04 '22 at 15:15
  • Honestly, it miiiiight be easier to analyze the script itself before running it for disallowed keywords, rather than limit the actual environment? Python is notoriously hard to actually lock down; that's exactly the reason `eval` is so dangerous. I'm hoping someone more knowledgeable than me will answer with some "good enough for a puzzle" techniques, though. – CrazyChucky Jul 04 '22 at 15:40

1 Answers1

4

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!

MillerTime
  • 317
  • 2
  • 11
  • Thanks! It's a start, but I think approaching it like this will take a lot of work because it would be easy to miss a function, and it is easy to circumvent (e.g. `globals()['__builtins__'].help()`) so getting rid of each individual way to get the version seems like a neverending task – lucidbrot Jul 04 '22 at 15:13