Python Learning Note 15: Obtaining process information for the standard library (OS package)

Source: Internet
Author: User

Python's OS package has functions for querying and modifying process information.

Process information
The relevant functions in the OS package are as follows:
Uname () returns information about the operating system, similar to the uname command on Linux.
Umask () Sets the permission mask for the process to create a file, similar to the Umask command on Linux.

get* () query (* replaced by the following)
UID, Euid, Resuid, GID, Egid, Resgid: Permission-related, where resuid is primarily used to return the saved UID.
PID, Pgid, Ppid, sid: Process dependent.

put* () setting (* replaced by the following)
Euid, Egid: Used to change the euid,egid.
UID, GID: Change the UID of the process, GID. Only super user has the right to change the process UID and GID (meaning to run Python in $sudo python).
Pgid, sid: Changes the process group in which the process resides and session (session).

Getenviron (): Gets the environment variable for the process
Setenviron (): Changing environment variables for a process

Example 1, the real UID of the process and the real GID
Import Osprint (Os.getuid ()) print (Os.getgid ()) <span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > </span>
Save the above program as a py_id.py file, using $python py_id.py and $sudo python py_id.py to see the results of the operation

Saved UID and saved GID
When we write a Python script, we actually run the Python interpreter instead of the Python script file.
Run the execution file compiled by the C language directly in contrast to the C,c language. We have to change the permissions of the Python interpreter itself to apply the saved UID mechanism, but it is extremely dangerous.
For example, our Python execution file is/usr/bin/python (you can learn by $which Python)

First look at the permissions of the Python interpreter:
$ls-L/usr/bin/python
Output:
-rwxr-xr-x root root

We modify the permissions to set the set UID and set GID bit
$sudo chmod 6755/usr/bin/python
/usr/bin/python's permissions become:
-rwsr-sr-x root root

Subsequently, we run the file under the test.py file, this file can be owned by the ordinary user:
Import Osprint (Os.getresuid ())
Output:
(0, 0)

Above are the uid,euid,saved UID respectively.
We can get super user privileges only by executing a Python script that is owned by a normal user!
So it is extremely dangerous to do so, and we are tantamount to handing over the system's protection system.
Imagine Python's powerful features, and others can now use these powerful features as a weapon to attack you! Use the following command to revert to the previous:
$sudo chmod 0755/usr/bin/python

Python Learning Note 15: Obtaining process information for the standard library (OS package)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.