How to Programmatically Gain Root Privileges

How to programmatically gain root privileges?

Original answer

You might consider the setuid switch on the executable itself. Wikipedia has an article on it which even shows you the difference between geteuid() and getuid() quite effectively, the former being for finding out who you're "emulating" and the latter for who you "are". The sudo process, for example, geteuid should return 0 (root) and getuid your user's id, however, its sub-processes do truly run as root (you can verify this with sudo id -u -r).

I don't think there's a way to easily programmatically gain root access - after all, applying the principle of least privilege, why would you need to? Common practise is to run only limited parts of code with elevated privileges. A lot of daemons etc are also set up under modern systems to run as their own user with most of the privileges they need. It's only for very specific operations (mounting etc) that root privileges are truly needed.

2013 update

My original answer stands (although my 2013 self might make a better job of it than my 2010 one), but if you are designing an application that requires root access, you may want to consider exactly what sort of root access is needed and consider the use of POSIX Capabilities (man page). These are different to capability-based security as implemented in L4 et al. POSIX capabilities allow your application to be granted a subset of root's powers. For example CAP_SYS_MODULE will allow you to insert kernel modules, but give you no other root powers. This is in use in distributions e.g. Fedora has a feature to completely remove setuid binaries with indiscriminate root access.

This matters because as a programmer, your code is obviously perfect! But, the libraries on which you depend (sigh, if only you'd written them!) might have vulnerabilities in them. Using capabilities, you can limit the use of this exploit, and save yourself and your company from security-related scrutiny. This makes everyone happier.

Request root access programmatically

The idiomatic way is to make your program suid-root, and have the first two lines of main open the raw socket and drop root.

This is still less than ideal from a security standpoint, since:

  1. A compromise later in the program would give an attacker access to a raw socket, which could be used for many malicious purposes, possibly obtaining sufficient information to elevate privilege.

  2. Any suid-root binary could be subject to vulnerabilities from flaws in the dynamic linker or startup code that runs before main. While these have become increasingly rare, even last year one was found again in glibc's linker using LD_AUDIT stuff. Many security-conscious systems (e.g. Openwall Linux) ban suid-root binaries completely for this reason.

A safer but more complex approach would be to have your program run as a daemon with elevated (but still minimal) privileges, and have the CLI interface be just a trivial wrapper that communicates with the daemon over a unix socket via a trivial protocol that can be mechanically checked for vulnerabilities.

Is there a way for a Java app to gain root permissions?

There is no easy way to change permissions. Java is not good at these tasks. There are only some tricks like check permissions on start and try to change permissions via su/sudo then restart application or using Java-gnome. Please read a bit more here: Java: Ask root privileges on Ubuntu

How to gain root privileges for iOS app?

What step 4 is telling you:

Open the original executable file and delete its contents (the contents are now stored in the previously copied and renamed binary).

is simply that you have moved the executable file for your app to a new filename, and you should replace it with a script with the name of the original executable.

Example

  • If you build an app named HelloWorld, Xcode will create a HelloWorld.app directory, with a file named HelloWorld inside it, which is executable.

  • The answer you link to suggests basically renaming the executable to something like MobileHelloWorld.

  • Once you've done that, create a new file in the HelloWorld.app directory called HelloWorld, and edit it with a text editor to give it this content:

#!/bin/bash
dir=$(dirname "$0")
exec "${dir}"/MobileHelloWorld "$@"

That script will then be run when you tap the app's icon, because in the app's Info.plist file, the name of the executable is

    <key>CFBundleExecutable</key>
<string>HelloWorld</string>

and HelloWorld is now a shell script, which invokes MobileHelloWorld, the renamed binary executable file.

How do I programmatically implement super user privileges in a script?

You can't do that. If you could then malicious code would have free access to any system as root at any time!

If you want super user privileges you need to run the script from the root account or use sudo and type in the password - this is the whole point of having user accounts.

EDIT

It is worth noting that you can run bash commands from within a python script - for example using the subprocess module.

import subprocess
subprocess.run(['sudo', 'blah'])

This essentially creates a new bash process to run the given command.

If you do this your user will be prompted to enter their password in the same way as you would expect, and the privileges will only apply to the subprocess that is being created - not to the script that you are calling it from (which may have been the original question).

How to add Root privileges to my OSX application?

I use this code to get the root privilege for my application. I made a new project to use this code.

// Create authorization reference
OSStatus status;
AuthorizationRef authorizationRef;

// AuthorizationCreate and pass NULL as the initial
// AuthorizationRights set so that the AuthorizationRef gets created
// successfully, and then later call AuthorizationCopyRights to
// determine or extend the allowable rights.
// http://developer.apple.com/qa/qa2001/qa1172.html
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults, &authorizationRef);
if (status != errAuthorizationSuccess)
NSLog(@"Error Creating Initial Authorization: %d", status);

// kAuthorizationRightExecute == "system.privilege.admin"
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;

// Call AuthorizationCopyRights to determine or extend the allowable rights.
status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
if (status != errAuthorizationSuccess)
NSLog(@"Copy Rights Unsuccessful: %d", status);

NSLog(@"\n\n** %@ **\n\n", @"This command should work.");
char *tool = "/sbin/dmesg";
char *args[] = {NULL};
FILE *pipe = NULL;

status = AuthorizationExecuteWithPrivileges(authorizationRef, tool,
flags, args, &pipe);
if (status != errAuthorizationSuccess)
NSLog(@"Error: %d", status);

// The only way to guarantee that a credential acquired when you
// request a right is not shared with other authorization instances is
// to destroy the credential. To do so, call the AuthorizationFree
// function with the flag kAuthorizationFlagDestroyRights.
// http://developer.apple.com/documentation/Security/Conceptual/authorization_concepts/02authconcepts/chapter_2_section_7.html
status = AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);


Related Topics



Leave a reply



Submit