views:

208

answers:

5

I'm responsible for developing a large Python/Windows/Excel application used by a financial institution which has offices all round the world. Recently the regulations in one country have changed, and as a result we have been told that we need to create a "locked-down" version of our distribution.

After some frustrating conversations with my foreign counterpart, it seems that they are concerned that somebody might misuse the python interpreter on their computer to generate non-standard applications which might be used to circumvent security.

My initial suggestion was just to take away execute-rights on python.exe and pythonw.exe: Our application works as an Excel plugin which only uses the Python DLL. Those exe files are never actually used.

My counterpart remained concerned that somebody could make calls against the Python DLL - a hacker could exploit the "exec" function, for example from another programming language or virtual machine capable of calling functions in Windows DLLs, for example VBA.

Is there something we can do to prevent the DLL we want installed from being abused? At this point I ran out of ideas. I need to find a way to ensure that Python will only run our authorized programs.

Of course, there is an element of absurdity to this question: Since the computers all have Excel and Word they all have VBA which is a well-known scripting language somewhat equivalent in capability to Python.

It obviously does not make sense to worry about python when Excel's VBA is wide-open, however this is corporate politics and it's my team who are proposing to use Python, so we need to prove that our stuff can be made reasonably safe.

+9  A: 

"reasonably safe" defined arbitrarily as "safer than Excel and VBA".

You can't win that fight. Because the fight is over the wrong thing. Anyone can use any EXE or DLL.

You need to define "locked down" differently -- in a way that you can succeed.

You need to define "locked down" as "cannot change the .PY files" or "we can audit all changes to the .PY files". If you shift the playing field to something you can control, you stand a small chance of winning.

Get the regulations -- don't trust anyone else to interpret them for you.

Be absolutely sure what the regulations require -- don't listen to someone else's interpretation.

S.Lott
Good points: The computers all have JRE running, so in theory anybody could bring their own JAR files and exploit the Java VM. In this case no Java sandboxing would apply. Perhaps one way I can solve this is to quantify the risk relative to what is already on the systems: Risk is a language that bankers understand.
Salim Fadhley
Quantification won't help. You need to "lawyer up" as we say, and read the actual regulation. Often, they're fairly straightforward and pretty easy to summarize.
S.Lott
+3  A: 

Sadly, Python is not very safe in this way, and this is quite well known. Its dynamic nature combined with the very thin layer over standard OS functionality makes it hard to lock things down. Usually the best option is to ensure the user running the app has limited rights, but I expect that is not practical. Another is to run it in a virtual machine where potential damage is at least limited to that environment.

Failing that, someone with knowledge of Python's C API could build you a bespoke .dll that explicitly limits the scope of that particular Python interpreter, vetting imports and file writes etc., but that would require quite a thorough inspection of what functionality you need and don't need and some equally thorough testing after the fact.

Kylotan
From what I can tell, PyPy will eventually have the ability to provide a locked-down python interpreter, however I think it's never going to be practical with CPython.
Salim Fadhley
Any implementation of Python on a virtual machine - PyPy, Jython, IronPython, etc - will obviously provide whatever sandboxing benefits that environment provides. But any libraries using native code are always going to be a problem.
Kylotan
+3  A: 

One idea is to run IronPython inside an AppDomain on .NET. See David W.'s comment here.

Also, there is a module you can import to restrict the interpreter from writing files. It's called safelite.

You can use that, and point out that even the inventor of Python was unable to break the security of that module! (More than twice.)

I know that writing files is not the only security you're worried about. But what you are being asked to do is stupid, so hopefully the person asking you will see "safe Python" and be satisfied.

RossFabricant
A: 

I agree that you should examine the regulations to see what they actually require. If you really do need a "locked down" Python DLL, here's a way that might do it. It will probably take a while and won't be easy to get right. BTW, since this is theoretical, I'm waving my hands over work that varies from massive to trivial :-).

The idea is to modify Python.DLL so it only imports .py modules that have been signed by your private key. It verifies this by using the public key and a code signature stashed in a variable you add to each .py that you trust via a coding signing tool.

Thus you have:

  1. Build a private build from the Python source.
  2. In your build, change the import statement to check for a code signature on every module when imported.
  3. Create a code signing tool that signs all the modules you use and assert are safe (aka trusted code). Something like __code_signature__ = "ABD03402340"; but cryptographically secure in each module's __init__.py file.
  4. Create a private/public key pair for signing the code and guard the private key.
  5. You probably don't want to sign any of the modules with exec() or eval() type capabilities.

.NET's code signing model might be helpful here if you can use IronPython.

I'm not sure its a great idea to pursue, but in the end you would be able to assert that your build of the Python.DLL would only run the code that you have signed with your private key.

Jeff Youel
+1  A: 

Follow the geordi way.

Each suspect program is run in its own jailed account. Monitor system calls from the process (I know how to do this in Windows, but it is beyond the scope of this question) and kill the process if needed.

Unknown