LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 10-16-2023, 09:41 PM   #1
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,167

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Lightbulb allow users to execute a Python File but not read


Hi guys,

Is it possible to allow users to run/execute a python file but don't allow them to read its content?

I tried --x permission, Python file will not run.

r-x, Python file will run but of course they can view and see the contents of the file.

I search the web, one way is to create a user with sudo account , no passwd and no shell login.

change the permission of the Python file to 700 and assign it to the sudo account as per above.

Then create a file in /etc/sudoers.d/ to allow a sudo user to run the Python file.


It works, user is not able to view the contents of the file and able to execute it.

However, the file has to be used by all users on the system.

It will be a security risk by allowing all users as sudoer to run the file.


Any other methods to do this? allow user to run/execute the Python file but don't allow them to view the contents of the file. Thanks.
 
Old 10-16-2023, 10:29 PM   #2
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,150

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Is there a way to change the sensitive contents to a compiled language then just call that from your python file? Then the stuff they don't need to see or know is hidden in the object file of whatever you call.
 
1 members found this post helpful.
Old 10-17-2023, 01:03 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,345
Blog Entries: 3

Rep: Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756
Quote:
Originally Posted by JJJCR View Post
It will be a security risk by allowing all users as sudoer to run the file.
I'd second the recommendation to look into compiling it, but will digress about the misinterpretation of /etc/sudoers above:

sudo does not have to run as root, it does not have to allow all programs to run either.

First, created an empty group and then set the permissions for your script:

Code:
addgrp piy
chown root:piy /usr/local/bin/script.py
chmod u=rwx,g=rx,o= /usr/local/bin/script.py
Next, configure /etc/sudoers:

Code:
%change ALL=(:piy) /usr/local/bin/script.py ""
That allows the empty group 'piy' to run the script but since no accounts are in it, only those in the group 'change' can run the script via sudo with the -g option. The "" means that the script must be run without any options. Then add people to the group 'change' who you want to run the script.

Code:
sudo -g piy /usr/local/bin/script.py
That can be wrapped in a script, an shell alias, or a shell function for a shortcut.

Last edited by Turbocapitalist; 10-17-2023 at 01:08 AM. Reason: link to compiling python
 
1 members found this post helpful.
Old 10-17-2023, 01:24 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,022

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
Quote:
Originally Posted by JJJCR View Post
It will be a security risk by allowing all users as sudoer to run the file.
No, why? If you allow anyone to run only that single command. Otherwise you can write a shell script, which will execute the python script (with config and whatever required) and you need to allow to execute only that shell script.
Quote:
Originally Posted by JJJCR View Post
Any other methods to do this? allow user to run/execute the Python file but don't allow them to view the contents of the file. Thanks.
The files which are used by script languages should be readable, because the interpreter is the real executable and it needs to read the script itself (using the credentials of the current user). You might try a setuid executable, but it is definitely not secure, not better than the sudoer setup.
 
1 members found this post helpful.
Old 10-17-2023, 03:07 AM   #5
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,167

Original Poster
Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Thank you guys for all the great ideas..
 
Old 10-19-2023, 06:42 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
What's the threat model here? Did you hardcode passwords into the Python script or something?

And anyway, no you can't do what you're asking. Some options (other than the ones suggested) include:

Putting the script in a Docker:

https://www.docker.com/blog/how-to-d...-applications/

Putting the script in an AppImage:

https://github.com/AppImage/AppImage...-not-for-users

Neither will prevent users from reading the script, but they do add a layer of discouragement.

Last edited by dugan; 10-21-2023 at 10:31 AM.
 
1 members found this post helpful.
Old 10-19-2023, 08:42 PM   #8
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,167

Original Poster
Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
@Dugan, yes indeed containerization will definitely help, good idea!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
.oui: cannot execute binary file .oui: cannot execute binary file .oui kazi9822 Linux - Server 18 09-28-2012 04:44 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
How to Deny Users to Delete, but Allow Write/Read and Exec ManRSA Linux - Security 3 11-21-2006 02:02 AM
Allow google.com but not allow google subdomains in dansguardian paul_mat Linux - Networking 4 07-17-2006 01:40 AM
fstab file to allow all users to read ntfs jza Linux - General 1 01-29-2004 02:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration