Preventing a non-root user from running a shell script - bash

Hi I made a script as root called whatever and saved it in /
I then made a new user adduser guest
when i log in as guest via su - guest and try to run the script with cd / && ./whatever
I get permission denied which is good, but all i have to do is bash /whatever and it works.
Is the correct sollution to chmod /bin/bash? No

If you want to allow a user to run a shell script, you have to give them both "execute" and "read" permissions on the file.
If you want to prevent a user from running a shell script, you have to remove the "read" permission. Even if the file is marked as executable, the user can not execute a non-readable script.
There is no way to allow a user to execute but not read a shell script.
Similarly, there is no way to allow a user to read but not to execute a shell script.

No!
Never touch the permissions on anything in /bin. You need to change the permissions on ./whatever. You'll need to add execute permissions to it - chmod u+x ./whatever. If you want it to run.
If however, you want to stop someone running it... well, you have to set it unreadable. Because there's no difference between a script you can read and a script you can 'run'. They're just sequences of commands.

Related

How do I run a shell script with administrator privileges through AppleScript without prompting for a password?

I want to have my AppleScript application run a Python script with sudo, but I don't want the application to prompt the user for a password (our users do not have sudo privileges).
The Python script has been added to the /etc/sudoers file appropriately (ALL ALL=NOPASSWD: /path/to/script.py). In the terminal, I can do (as a regular, non-privileged user):
$ sudo ./script.py
and it runs perfectly well. But in AppleScript when you try to do:
do shell script "sudo ./script.py"
You of course get the "sudo: no tty present and no askpass program specified" error. But if you change it to:
do shell script "./script.py" with administrator privileges
AppleScript insists on presenting a popup window to ask for the password. I have also tried passing a null password to sudo with a pipe:
do shell script "echo '' | sudo -S ./script.py"
but that also does not work. (I think it tries to run sudo individually first and then pass the command through, which won't work because the user doesn't have sudo privileges!)
I need a solution where AppleScript will run the Python script with sudo. I would prefer the script stays unreadable and un-executable by average users for security reasons, and is only executed through the AppleScript. (I know that, hypothetically, the users could call sudo script.py and it would run, but that's assuming they even know about sudoers; I'm trying to keep it as secure as possible while still usable).
I'm still pretty new to AppleScript, so any help would be greatly appreciated! Thanks!
When I added ALL ALL=NOPASSWD: /Users/myusername/a to sudoers and ran echo $'#!/bin/bash\nsay $(ls ~root|head -n1)'>~/a;chmod +x ~/a, do shell script "sudo ~/a" ran the script as root without requiring a password.
I'm guessing the problem is that you specified the path like do shell script "sudo ./script.py". Try to use do shell script "sudo ~/script.py" instead. The default working directory is for do shell script is / and not ~/.

What is the best way to execute CGI scripts with root privileges?

As the title says, I'm looking for a method to run a CGI script (running on APACHE on Ubuntu) with root privileges.
In particular, some commands of the script can be executed by the current user (apache), but other commands need root privileges (as iptables).
I can also write "sub-scripts" containing these privileged commands, but the problem of executing them with root privileges still remains.
I'm also confused about wether to write the CGI in C or PERL or bash script language. Any suggestion?
The best method is to use sudo for such scripts. You must specify which commands can run the script in this case.
In /etc/sudoers:
wwwdata ALL=(ALL) /usr/local/bin/allowed-operation
And then in the script:
sudo /usr/local/bin/allowed-operation

Running a bash script from another bash script with different permissions

I have a bash script archive.sh which belongs to User1 and has the permissions as 755. Also there are two other scripts archive1.sh and archive2.sh in the same directory, belonging to the same user (User1) but with permissions 744. The scripts archive1.sh and archive2.sh are called from inside the script archive.sh.
Now this script archive.sh is executed from another user User2 in the same group as User1. Since archive.sh has 755 permissions, it can be executed without any problem. But inside that script there are calls to archive1.sh and archive2.sh which have 744 permissions. So if I call archive.sh from User2, then will it execute the two scripts inside it?
When I tried it, the child scripts are running correctly but I am not sure how
Depends how the two "children" are called.
./child.sh will fail because you don't have the right permissions.
/bin/sh child.sh would work because you only need read access.

OSX Shell Script - deleting all desktop files

I am trying to delete all files on the Desktop of seperate users from my admin account using a shell script.
This is my code to delete (for student)
sudo rm /Users/student/Desktop/*
but i get an error:
rm: /Users/student/Desktop/*: No such file or directory
When i run the script it prompts me for the Admin pass, then errors out.
Is using the Desktop/* appropriate? any tips? Thanks
The path expansion is done before sudo is executed. This means, it is done with the access rights of the current user and if you may not access /Users/student/Desktop then the expansion fails. Instead, do this:
sudo bash -c "rm /Users/student/Desktop/*"
This way the path expansion is done with the elevated rights granted by sudo. You might need to do rm -r if there are any directories/bundles on the desktop.

How to use sudo inside of a Run Script build phase in Xcode 4?

I need use execute a command inside of a script in a Run Script build phase in Xcode 4 using sudo. However, the compiler complains:
sudo: no tty present and no askpass program specified
Anyone have a clever solution for this problem?
One solution is to place the sudo password in an executable shell script like the following:
#!/bin/bash
echo thesudopassword
This shell script might be called password.sh
Then, setup the environment variable SUDO_ASKPASS=password.sh
Once this is setup, the -A option can be passed to sudo. This option uses the ASKPASS program to obtain the sudo password. The ASKPASS program need only write the password to stdout.
So, for example,
sudo -A ditto -V /tmp/testserver.dst /
This is obviously a rather insecure solution, but it does work.
Two ideas that haven't been suggested yet, both of which are probably better/safer than the currently accepted answer:
First option would be to put the part of the script that needs to be run as root in a script file (.sh or something), and then make it setuid as root: chmod go-w,+sx scriptfile, sudo chown root scriptfile. This means that script will automatically run as root, which avoids you needing to authenticate to run it (just to change it). As long as its operation isn't subject to user input, this should be quite safe. (Of course, if you make a script that takes an input argument and deletes it or runs it, or does most anything else with it, that would not be safe.)
Second option would be to use applescript (possibly via osascript). Applescript allows you to do shell script "sudo command goes here" with administrator privileges, which will pop up a graphical dialog asking for a password.
The first of these options would be good for an automated environment, though it might not deal well with (for example) being checked into an SCM, or being sent to another user. The second option would work better with that, but requires a password input every time, so doesn't work as well for an automated build script.
Another solution to this problem is to modify sudoers file and add your account to it and state that you should never be asked for the sudo password. To accomplish this is fairly straightforward:
run:
sudo visudo
In the User privilege specification section add a line that looks like
youraccountname ALL=(ALL) NOPASSWD: ALL
Of course, this can be a dangerous thing to do, so be careful. I would suggest reading the man page for sudoers and visudo before going this route.
After much searching I found the following solution.
https://forum.juce.com/t/build-script-for-automatically-moving-built-aus-into-components-folder-xcode/13112
Summary
Create a keychain and store your admin password in the keychain
Create a script which uses /usr/bin/security to access the password In your run script,
Set the ASK_PASS env variable and use the -A option with sudo
You can either run commands directly as a administrator with the following (changing echo YourCommandHere > /tmp/hello to your command):
osascript -e 'do shell script "sudo echo YourCommandHere > /tmp/hello " with administrator privileges'
Or run a script in your source directory using:
osascript -e 'do shell script "bash -x $SOURCE_ROOT/MyAdminScript.sh 1>/tmp/build-log 2>/tmp/build-log.err" with administrator privileges'
This runs the script and logs it output to /tmp/build-log and /tmp/build-log.err
For useful variables in the script see https://help.apple.com/xcode/mac/8.0/#/itcaec37c2a6
You can also execute XCode giving it the project as parameter from the Terminal using sudo like this:
sudo /Developer/Applications/Xcode.app/Contents/MacOS/Xcode /path/to/your/project.xcodeproj
This is the easiest solution I could think of, but there may be some drawbacks, since you would be executing XCode as root.
No need to write your sudo password anywhere. Just open a terminal window and type
$ sudo echo "hello"
Once you've typed your password, it will be good for a while - not sure how long - and the shell spawned by Xcode will inherit this permission.
If you get the "no tty present" message again later, just repeat the procedure

Resources