473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sudo open() ? (python newbee question)

hello,

i am writing a python script that will be run by a non root user
the script needs to open a file in write mode that is owned by root

file = open('/etc/apt/sources.list', 'r+')

returns permission error

how can i call sudo on open()?

thanks alot
slava
Jul 19 '05 #1
5 7416
The script could be SUID Root, and you could use os.setuid immediately after
having performed the task to switch to a non-priviledged user. May be a big
security risk, if someone can alter the script, he gains root access to the
system...

sl***@crackpot. org wrote:
hello,

i am writing a python script that will be run by a non root user
the script needs to open a file in write mode that is owned by root

file = open('/etc/apt/sources.list', 'r+')

returns permission error

how can i call sudo on open()?

thanks alot
slava

Jul 19 '05 #2
On Tue, 14 Jun 2005 11:52:13 +0200,
Denis WERNERT <pe************ @yahoo.com> wrote:
The script could be SUID Root, and you could use os.setuid immediately
after having performed the task to switch to a non-priviledged
user. May be a big security risk, if someone can alter the script, he
gains root access to the system...


I am *not* advocating suid scripts, and *ESPECIALLY NOT* suid Python
programs, but if a user can modify an unwriteable suid script owned by
root in a an unwriteable directory, then they already have root access
to the system (unless there's' a kernel or filesystem bug, in which case
all bets are off anyway).

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
Jul 19 '05 #3
On 14 Jun 2005 08:12:17 -0400, rumours say that Dan Sommers
<me@privacy.net > might have written:
On Tue, 14 Jun 2005 11:52:13 +0200,
Denis WERNERT <pe************ @yahoo.com> wrote:
The script could be SUID Root, and you could use os.setuid immediately
after having performed the task to switch to a non-priviledged
user. May be a big security risk, if someone can alter the script, he
gains root access to the system...
I am *not* advocating suid scripts, and *ESPECIALLY NOT* suid Python
programs, but if a user can modify an unwriteable suid script owned by
root in a an unwriteable directory, then they already have root access
to the system (unless there's' a kernel or filesystem bug, in which case
all bets are off anyway).


I believe that the suid bit on scripts (either *sh or python) is
completely ignored on most *nix systems.

Try this in a shell (bash or ksh) as a sudo-capable user:

echo hello >/tmp/tmp
sudo chown root /tmp/tmp
sudo chmod 600 /tmp/tmp
cat >/tmp/ax.py <<@
#!/usr/bin/env python
x = open("/tmp/tmp", "w")
x.write("there" )
x.close()
@
sudo chown root /tmp/ax.py
sudo chmod a=rx,u+s /tmp/ax.py
ls -l /tmp/ax.py /tmp/tmp
/tmp/ax.py

I get:

-r-sr-xr-x 1 root users 75 2005-06-14 16:15 /tmp/ax.py
-rw------- 1 root users 6 2005-06-14 16:15 /tmp/tmp
Traceback (most recent call last):
File "/tmp/ax.py", line 2, in ?
x = open("/tmp/tmp", "w")
IOError: [Errno 13] Permission denied: '/tmp/tmp'

--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 19 '05 #4
On Tue, 14 Jun 2005 16:18:19 +0300,
Christos "TZOTZIOY" Georgiou <tz**@sil-tec.gr> wrote:
I believe that the suid bit on scripts (either *sh or python) is
completely ignored on most *nix systems.


Most *modern* systems, yes. ;-)

I must be getting old. :-(

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
Jul 19 '05 #5
On Tue, 14 Jun 2005 02:21:48 -0700, sl***@crackpot. org declaimed the
following in comp.lang.pytho n:
hello,

i am writing a python script that will be run by a non root user
the script needs to open a file in write mode that is owned by root

file = open('/etc/apt/sources.list', 'r+')

returns permission error

how can i call sudo on open()?

Don't think you can -- you'd have to invoke the /script/ using
sudo, I believe (haven't done this is quite some time; the only thing I
ran sudo on my Linux install was leafnode functions).

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 19 '05 #6

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

Similar topics

2
3738
by: Tim Daneliuk | last post by:
Given that setuid is a Bad Thing for scripts, what is the general consensus here on running a Python script via 'sudo' to give it root system access? Is this reasonably secure, or am I still asking for trouble? TIA, -- ---------------------------------------------------------------------------- Tim Daneliuk tundra@tundraware.com PGP Key: http://www.tundraware.com/PGP/
2
2068
by: Kanthi Kiran Narisetti | last post by:
Hi ALL, I am Windows Administrator, moving little ahead from batch files and scripts I started learning Python. I found that Python is very easy and is very well documented. Still I am looking more than examples. As a beginner i want to do lot of excersice from simple addition to complex ....which help me to understand the concepets more clearly. I appreciate if any one can give me such exersices or any link for the same.
2
1660
by: Newbee Adam | last post by:
some said that .NET app can run on any program where rutime exists. What is "runtime" in this sense? will I have to install runtime or .net framework or .NET support on an xp machine for a .NET app to work? keep in mind I am a newbee :-) !! thanks --
115
14149
by: TheAd | last post by:
At this moment I use MsAccess and i can build about every databound application i want. Who knows about a serious open source alternative? Because Windows will be a client platform for some time, i prefer a solution that (also) supports Windows. On the net I found a number of products that i looked at, but none of them gave me the impression of a serious candidate at this moment (KNoda, Gnome DB Manager, InterBase...). 2 additional...
158
6404
by: Giovanni Bajo | last post by:
Hello, I just read this mail by Brett Cannon: http://mail.python.org/pipermail/python-dev/2006-October/069139.html where the "PSF infrastracture committee", after weeks of evaluation, recommends using a non open source tracker (called JIRA - never heard before of course) for Python itself. Does this smell "Bitkeeper fiasco" to anyone else than me? --
0
2163
by: dwelch91 | last post by:
I'm having a problem using pexpect with 'sudo' on Ubuntu 6.06 (Dapper). Here's the program: #!/usr/bin/env python import pexpect import sys child = pexpect.spawn("sudo apt-get update") child.logfile = sys.stdout
7
4496
by: vml | last post by:
Hello, I am trying to promote python in my job, my collegue only see matlab and microsoft scripting language. I understood that there willl be no backward compatibility between python 2.x and 3.0, does it means that: - my script using pywin32 for the COM layer and scipy for the maths won't work under 3.0
4
10188
hsn
by: hsn | last post by:
hello everyone. i am trying to run the sudo -i command in my mac. here is the code String s = null; try {
1
3619
by: prasadshete | last post by:
Dear all, my system is $ uname -a Linux DLidps01 2.4.21-4.ELsmp #1 SMP Fri Oct 3 17:52:56 EDT 2003 i686 i686 i386 GNU/Linux 1.Now, i my a/c has sudo to root access. i do $sudo bash and enter into the root. But nowadays i am not able to do so. here is the error message.. $ sudo bash Password: sudo: pam_acct_mgmt: 7
0
8773
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9234
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4548
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
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 we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.