Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dangerous Python Keywords?

Tags:

I am about to get a bunch of python scripts from an untrusted source.

I'd like to be sure that no part of the code can hurt my system, meaning:

(1) the code is not allowed to import ANY MODULE

(2) the code is not allowed to read or write any data, connect to the network etc

(the purpose of each script is to loop through a list, compute some data from input given to it and return the computed value)

before I execute such code, I'd like to have a script 'examine' it and make sure that there's nothing dangerous there that could hurt my system.

I thought of using the following approach: check that the word 'import' is not used (so we are guaranteed that no modules are imported)

yet, it would still be possible for the user (if desired) to write code to read/write files etc (say, using open).

Then here comes the question:

(1) where can I get a 'global' list of python methods (like open)?

(2) Is there some code that I could add to each script that is sent to me (at the top) that would make some 'global' methods invalid for that script (for example, any use of the keyword open would lead to an exception)?

I know that there are some solutions of python sandboxing. but please try to answer this question as I feel this is the more relevant approach for my needs.

EDIT: suppose that I make sure that no import is in the file, and that no possible hurtful methods (such as open, eval, etc) are in it. can I conclude that the file is SAFE? (can you think of any other 'dangerous' ways that built-in methods can be run?)

like image 874
user3262424 Avatar asked Apr 01 '11 18:04

user3262424


People also ask

What is the most dangerous Python line of code?

The Most Dangerous Line of Code is "sudo rm -rf * --no-preserve-root /". Do not run it.

What color are keywords in Python?

Keywords of the Python language are shown in orange font. Keywords are words of the Python language which have a special meaning. When you type in program statements in Python's IDLE editor window and save the file with a name ending in .

Is file a keyword in Python?

file is neither a keyword nor a builtin in Python 3. file is also used as variable example from Python 3 doc.


2 Answers

This point hasn't been made yet, and should be:

You are not going to be able to secure arbitrary Python code.

A VM is the way to go unless you want security issues up the wazoo.

like image 99
Katriel Avatar answered Oct 15 '22 16:10

Katriel


You can still obfuscate import without using eval:

s = '__imp' s += 'ort__' f = globals()['__builtins__'].__dict__[s] ** BOOM ** 
like image 25
julx Avatar answered Oct 15 '22 17:10

julx