473,508 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Securing the Pyton Interpreter?

I'm looking for a way to install Python on a UNIX machine in a way such
that any user on the system can use it, but only to execute scripts that
are located in a certain directory. I do not have root access on the
machine that will be running Python, so my options are limited. I thought
about hacking the Python interpreter itself so that it will examine the
argument array and exit with an error if the script to be executed isn't
in the appropriate directory, but this seems pretty risky. The module
'site.py' is imported automatically upon initialization -- I've thought of
adding the check there instead. I don't think either of these solutions
are very elegant. Is there a better way?

Thanks for your time,

Steve VanDahm
va*****@norge.freeshell.org
Jul 18 '05 #1
6 1952
Stephen> I'm looking for a way to install Python on a UNIX machine in
Stephen> a way such that any user on the system can use it, but only
Stephen> to execute scripts that are located in a certain directory.

Why? If I were a user on that machine and wanted to execute Python
scripts in a different directory, how would you stop me from installing
Python on my own and using it for those scripts?

--
Andrew Koenig, ar*@acm.org
Jul 18 '05 #2
In article <yu**************@tinker.research.att.com>, Andrew Koenig wrote:
Stephen> I'm looking for a way to install Python on a UNIX machine in
Stephen> a way such that any user on the system can use it, but only
Stephen> to execute scripts that are located in a certain directory.

Why? If I were a user on that machine and wanted to execute Python
scripts in a different directory, how would you stop me from installing
Python on my own and using it for those scripts?

Andrew,

I'm a member of a Public Access UNIX system. Some users on the system are
allowed to use development tools (like Python) and other aren't. Also,
some users are allowed to install software that they've written into a
publically accessible area so that everyone on the system can use it. The
problem is that if the software is written in a language like Python,
regular users won't be able to use the Python interpreter to run it, and
the Python programs that we write won't be very useful. Some of us want
to install a second interpreter that's been secured somewhat so that
people can run our programs without being able to execute arbitrary
Python programs.

You are correct that nothing (in principle) prevents someone from
installing another Python interpreter in $HOME/bin and running whatever
they want. In fact, that's kind of what *we're* doing. But since I
neither make nor enforce the rules, it isn't my problem if other people
try to break them.

Basically, I need to do this for bureaucratic reasons. I know it's a
hack, and that it sounds like a stupid thing to do, but it's the best
available option for us....

Thanks for the reply,

Steve
Jul 18 '05 #3
Stephen VanDahm wrote:

I'm looking for a way to install Python on a UNIX machine in a way such
that any user on the system can use it, but only to execute scripts that
are located in a certain directory. I do not have root access on the
machine that will be running Python, so my options are limited. I thought
about hacking the Python interpreter itself so that it will examine the
argument array and exit with an error if the script to be executed isn't
in the appropriate directory, but this seems pretty risky. The module
'site.py' is imported automatically upon initialization -- I've thought of
adding the check there instead. I don't think either of these solutions
are very elegant. Is there a better way?


You want something this freaky, *and* you want it elegant?! :-)

Anyway, just go with site.py. Judging by the name, it's perfectly suited
for the task.

Of course, you know about the -S option, don't you? And PYTHONPATH?
And PYTHONHOME? And someone installing their own interpreter? And....

-Peter
Jul 18 '05 #4
I've never tried to set up a "secure" unix system, in the sense that
users will only be allowed to execute certain commands. However there
are any number of secure/restricted shells.

I suspect that if you use one of these, you can get what you want. For
instance, you would have /usr/bin forbidden, and /usr/safebin permitted.
In /usr/safebin/pyscript you'd lead off with "#!/usr/bin/python -E".
"-E" prevents Python from obeying environment variables like
PYTHONPATH, PYTHONHOME, and PYTHONINSPECT, all of which can let the user
"sneak" code in to be executed.

Of course, you have to be sure that the individual python scripts are
"secure" also. For instance, the following one *isn't*:
#!/usr/bin/python -E
# Print prime factors of a number (like /usr/bin/factor)
import sys, math
for arg in sys.argv[1:]:
num = eval(arg)

print "%d:" % num,
i=2
while num != 1:
while num % i == 0:
print i,
num = num / i
i=i+1
print
using eval() is the reason, in case you didn't catch it, but there are
more subtle ways to write Python programs that let the user do arbitrary
things. For instance, if a program uses pickle and lets the user alter
the pickle's contents, the user can execute arbitrary code. If there's
a bug in the C program that implements the Python interpreter or any
extension module, the user might be able to arrange to "smash the stack"
and do the same thing. Whether these things really matter depend on
how secure your multi-user system needs to be. (this last type of attack
could be true of any program, though, not just Python)

Jeff

Jul 18 '05 #5
Stephen VanDahm <us****@fomps.net> wrote in message news:<sl*******************@norge.freeshell.org>.. .
I'm looking for a way to install Python on a UNIX machine in a way such
that any user on the system can use it, but only to execute scripts that
are located in a certain directory. I do not have root access on the
machine that will be running Python, so my options are limited. I thought
about hacking the Python interpreter itself so that it will examine the
argument array and exit with an error if the script to be executed isn't
in the appropriate directory, but this seems pretty risky. The module
'site.py' is imported automatically upon initialization -- I've thought of
adding the check there instead. I don't think either of these solutions
are very elegant. Is there a better way?

Thanks for your time,

Steve VanDahm
va*****@norge.freeshell.org


Hi,
Hacking the interpreter seems like overkill. Why not just set up a
shell script containing the names of the allowed python scripts, and
execute it from there.
for example:

#!/usr/bin/sh

if "$1" in myscript1.py myscript2.py ....;
then
. /usr/bin/env python "$1"
else
echo "You can't execute that script."
fi

My shell scripting is a little rusty so there may be some errors, but
I hope you get the general idea.

Hope it's useful,

Vinoo
Jul 18 '05 #6
Steven Taschuk <st******@telusplanet.net> wrote in message news:<ma**********************************@python. org>...
Quoth Mel Wilson:
seem to recall there are complications with suid on scripts
.. though I don't recall what they are.


A simple example: Let the file insecure_script contain
#!/bin/sh
grep 'f.*bar' $*
This script must not be made setuid-root. Consider:
$ cat >grep
#!/bin/sh
cp /etc/shadow . && chmod 0666 ./shadow
^D
$ chmod +x ./grep
$ export PATH=.:$PATH
$ insecure_script

You could deal with this particular problem by using absolute path
names for everything in the script, and/or by setting $PATH in the
script itself. [clip]


I didn't see this post before I posted the one with the naive shell
script. As clear as I can make it, the problem seems to be two-fold :-
Not allowing normal users to access the python interpreter directly,
and making sure they run only a certain set of scripts.
One solution that may work is to set up the interpreter so that _only_
you have read and execute permissions(maybe installing it in your home
directory), and then putting a shell script which has your UID but has
read and execute permissions for all users in a commonly accessible
place. The path to the interpreter and the python scripts must be
absolute in this script to avoid security problems as mentioned
above.

Hope this works,
Vinoo
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
4735
by: David McNab | last post by:
Hi, I'm writing a web app framework which stores pickles in client cookies. The obvious security risk is that some 5cr1p7 X1ddi35 will inevitably try tampering with the cookie and malforming...
3
1890
by: mike | last post by:
Hi, I am new with python. Is it possible to have an MFC application and develop some module using python? what are the steps in doing this? can anybody give me a url or some documentation for...
11
1991
by: Mark de+la+Fuente | last post by:
I need to write simple scripts for executing command line functions. Up till now I've used C-Shell scripts for this, but I'm looking for a better alternative. And I keep reading about how “easy” it...
2
1598
by: James | last post by:
What's the best way of securing online databases and web services? At present I am using a database password, which of course is not hard-coded into the web service, but this means re-submitting it...
11
3405
by: Wm. Scott Miller | last post by:
Hello all! We are building applications here and have hashing algorithms to secure secrets (e.g passwords) by producing one way hashes. Now, I've read alot and I've followed most of the advice...
1
3380
by: Mark Goosen | last post by:
Hi ive installed wse 2.0 SP3 and was running throught the demo downlaoded on the Securing the Username Token with WSE 2.0 page the Securing the Username Token with WSE 2.0. Im spose to change...
4
1373
by: KJ | last post by:
Hello All, I have to secure my first real B2B web service. Could you please provide some guidance as to which method of security I should use. One caveat is that we will not be using SSL on the...
27
5305
by: psbasha | last post by:
Hi, I want to create a Exe of an Pyton Aplication in Windows and UNIX.For example,Ihave the following ".py"files Sample1.py Sample2.py Sample3.py ................ Samplen.py Main.py
10
3323
by: Les Desser | last post by:
In article <fcebdacd-2bd8-4d07-93a8-8b69d3452f3e@s50g2000hsb.googlegroups.com>, The Frog <Mr.Frog.to.you@googlemail.comMon, 14 Apr 2008 00:45:10 writes Not sure if I quite follow that. 1....
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.