天天看點

Linux之特殊權限(SUID/SGID/SBIT)一、特殊權限的介紹二、SUID/SGID/SBIT權限設定

一、特殊權限的介紹

**

1、 Set UID

**

當s這個标志出現在檔案所有者的x權限上時,如/usr/bin/passwd這個檔案的權限狀态:“-rwsr-xr-x.”,此時就被稱為Set UID,簡稱為SUID。那麼這個特殊權限的特殊性的作用是什麼呢?

1、SUID權限僅對二進制程式(binary program)有效;

2、執行者對于該程式需要具有x的可執行權限;

3、本權限僅在執行該程式的過程中有效(run-time);

4、執行者将具有該程式擁有者(owner)的權限。

SUID的目的就是:讓本來沒有相應權限的使用者運作這個程式時,可以通路他沒有權限通路的資源。 passwd就是一個很鮮明的例子,下面我們就來了解一下這相passwd執行的過程。

我們知道,系統中的使用者密碼是儲存在/etc/shadow中的,而這個檔案的權限是----------. (這個權限和以前版本的RHEL也有差别,以前的是-r--------)。其實有沒有r權限不重要,因為我們的root使用者是擁有最高的權限,什麼都能 幹了。關鍵是要把密碼寫入到/etc/shadow中。我們知道,除了root使用者能修改密碼外,使用者自己同樣也能修改密碼,為什麼沒有寫入權限,還能修改密碼,就是因為這個SUID功能。

下面就是passwd這個指令的執行過程

1、因為/usr/bin/passwd的權限對任何的使用者都是可以執行的,是以系統中每個使用者都可以執行此指令。
2、而/usr/bin/passwd這個檔案的權限是屬于root的。
3、當某個使用者執行/usr/bin/passwd指令的時候,就擁有了root的權限了。
4、于是某個使用者就可以借助root使用者的權力,來修改了/etc/shadow檔案了。
5、最後,把密碼修改成功。
           

注意:

這個SUID隻能運作在二進制的程式上(系統中的一些指令),不能用在腳本上(script),因為腳本還是把很多的程式集合到一起來執行,而不是腳本自身在執行。同樣,這個SUID也不能放到目錄上,放上也是無效的。

**

2、Set GID

**

我們前面講過,當s這個标志出現在檔案所有者的x權限上時,則就被稱為Set UID。

那麼把這個s放到檔案的所屬使用者組x位置上的話,就是SGID。

如開頭的/usr/bin/wall指令。

那麼SGID的功能是什麼呢?

和SUID一樣,隻是SGID是獲得該程式所屬使用者組的權限。

同樣SGID有幾點需要我們注意:
1、SGID對二進制程式有用;
2、程式執行者對于該程式來說,需具備x的權限;
3、SGID主要用在目錄上;
           

了解了SUID,我想SGID也很容易了解。如果使用者在此目錄下具有w權限的話,若使用者在此目錄下建立新檔案,則新檔案的群組與此目錄的群組相同。

3、Sticky Bit

這個就是針對others來設定的了,和上面兩個一樣,隻是功能不同而已。

SBIT(Sticky Bit)目前隻針對目錄有效,對于目錄的作用是:當使用者在該目錄下建立檔案或目錄時,僅有自己與 root才有權力删除。

最具有代表的就是/tmp目錄,任何人都可以在/tmp内增加、修改檔案(因為權限全是rwx),但僅有該檔案/目錄建立者與 root能夠删除自己的目錄或檔案。

注意:

這個SBIT對檔案不起作用。

**

二、SUID/SGID/SBIT權限設定

**

和我們前面說的rwx差不多,也有兩種方式,一種是以字元,一種是以數字。

4 為 SUID = u+s
2 為 SGID = g+s
1 為 SBIT = o+t
           

下面我們就來看看如何設定,并看看達到的效果。

1、先看SUID的作用及設定

[[email protected] ~]# cd /tmp/
[[email protected] japie tmp]# cp /usr/bin/passwd ./
[[email protected] japie tmp]# mkdir testdir 
           

上面兩步是在/tmp目錄下建立passwd檔案和testdir目錄

下面看看這兩個的權限

[[email protected] japie tmp]# ls -l passwd ; ls -ld testdir/
-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd
drwxr-xr-x. 2 root root 4096 Jan 20 19:25 testdir/ 
           

我們切換到yufei 使用者,然後修改自己的密碼

[[email protected] japie tmp]# su - yufei
[[email protected] japie i tmp]$ ./passwd
Changing password for user japie .
Changing password for yufei.
(current) UNIX password:
New password:            
Retype new password:
passwd: Authentication token manipulation error 
           

發現上面的yufei改不了自己的密碼,為什麼呢?就是因為沒有權限把密碼寫入到/etc/shadow中。想讓普通使用者能修改/etc/shadow的話,那就需要用到SUID了。

[[email protected] japie tmp]$ su - root
Password:
[[email protected] japie tmp]# chmod u+s passwd
[[email protected] japie tmp]# ls -l passwd
-rwsr-xr-x. 1 root root 26968 Jan 20 23:27 passwd 
           

看到有SUID權限了,下面再來修改yufei自己的密碼

[[email protected] japie tmp]$ ./passwd
Changing password for user yufei.
Changing password for yufei.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully. 
           

我們發現已經成功了。

我想這一下,你對SUID的作用已經了解了吧。

如果想把這個改回來(就是把SUID的權限去掉),我們用數字方式來設定

[[email protected] japie tmp]# chmod 0755 passwd
[[email protected] japie tmp]# ls -l passwd
-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd 
           

OK這樣就改過來了,這個數字的原理和我們前面講的rwx是一樣的,隻是在最前面設定相應的數字而已。

注: 在普通使用者修改自己的密碼是,密碼要設定的複雜點,否則的話,通過不了認證,普通使用者和root使用者的權限是不同的。

2、再看SGID的作用及設定

我們以前面建立的/tmp/testdir為例子

[[email protected] japie tmp]# ls -ld testdir/
[[email protected] japie tmp]# chmod 757 testdir/
[[email protected] japie tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 20 19:25 testdir/ 
           

這時候,任何使用者對此目錄都有寫入權限,那麼我們就在這個目錄裡面建立檔案與目錄,并看看他們的權限如何

[[email protected] japie tmp]# su japie 
[[email protected] japie tmp]$ touch testdir/file1
[[email protected] japie tmp]$ mkdir testdir/dir1
[[email protected] japie tmp]$ ls -l testdir
total 0
drw-rw-r--. 1 japie japie 0 Jan 21 10:33 dir1
-rw-rw-r--. 1 japie japie 0 Jan 21 10:33 file1 
           

這時候的檔案與目錄權限都是建立者的本身

下面我們就來看看,把這個目錄加上SGID權限後,再建立檔案與目錄,會是什麼樣的效果

[ japie @ japie tmp]$ su root
Password:
[[email protected] japie tmp]# chmod g+s testdir/
[[email protected] japie tmp]# ls -ld testdir/
drwxr-srwx. 2 root root 4096 Jan 21 10:33 testdir/
[[email protected] japie tmp]# su yufei
[[email protected] japie tmp]$ touch testdir/file2
[[email protected] japie tmp]$ mkdir testdir/dir2
[[email protected] japie tmp]$ ls -l testdir/
total 0
drw-rw-r--. 1 japie japie 0 Jan 21 10:33 dir1
drw-rw-r--. 1 japie root  0 Jan 21 10:36 dir2
-rw-rw-r--. 1 japie i japie 0 Jan 21 10:33 file1
-rw-rw-r--. 1 japie root  0 Jan 21 10:35 file2
[[email protected] japie tmp]$ ls -ld testdir/
drwxr-srwx. 2 root root 4096 Jan 21 10:36 testdir/ 
           

這時候我們就發現,file2和dir2的使用者組變成了root了,也就是他們上層目錄testdir這個目錄的所屬使用者組。

這個應用,應用在一個項目的共同開發上,是很友善的。

[[email protected] japie tmp]$ su root
Password:
[[email protected] japie tmp]# chmod g-s testdir/
[[email protected] japie tmp]$ ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 10:36 testdir/ 
           

這樣就還原了

最後我們來看SBIT的作用及設定

[[email protected] japie tmp]# rm -fr testdir/*
[[email protected] japie tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:42 testdir/ 
           

清空/tmp/testdir/目錄裡面的全部内容。

我們切換成普通使用者,然後再裡面建立檔案,至少需要兩個普通使用者來測試這個,如果沒有的話,就自己建立。

[[email protected] japie tmp]# su - japie 
[[email protected] japie tmp]$ touch testdir/ japie _file
[[email protected] japie i tmp]$ ls -l testdir/
total 0
-rw-rw-r-- 1 japie japie 0 Jan 21 11:45 japie _file
           

這時候我們建立了一個檔案,我們換成另外一個使用者

[[email protected] japie tmp]$ su opsers
Password:
[[email protected] japie tmp]$ ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:45 testdir/ 
           

我們看到,雖然其他使用者對yufei_file隻有隻讀權限,但由于japie _file所在的目錄,對其他人是全部的權限,是以,我們換其他使用者還是可以删除這個檔案的,看操作

[[email protected] japie tmp]$ rm -f testdir/ japie _file
[[email protected] japie tmp]$ ls testdir/ 
           

發現我們已經删除了這個不屬于我們的權限。

下面我們就給這個目錄加上SBIT權限,再來看看效果

[[email protected] japie tmp]$ su - root
Password:
[[email protected] japie tmp]# chmod o+t testdir
[[email protected] japie tmp]# ls -ld testdir/
drwxr-xrwt. 2 root root 4096 Jan 21 11:49 testdir/ 
           

再一次切換普通使用者,建立檔案

[[email protected] japie tmp]# su - japie 
[[email protected] japie tmp]$ touch testdir/ japie _file
[[email protected] japie tmp]$ ls -l testdir/ japie _file
-rw-rw-r-- 1 japie japie 0 Jan 21 11:51 testdir/ japie _file 
           

這個檔案的權限還是和第一次建立的時候是一樣的,我們再換成其他的使用者,看看能不能再次删除這個檔案

[[email protected] japie tmp]$ su opsers
Password:
[[email protected] japie tmp]$ rm -f testdir/ japie _file
rm: cannot remove `testdir/ japie _file': Operation not permitted 
           

看到提示,說權限不夠了,隻能由這個檔案的建立者或root使用者才能删除。這個我們就不示範了。

如果要還原權限的話,

[[email protected] japie tmp]$ su root
Password:
[[email protected] japie tmp]# chmod o-t testdir
[[email protected] japie tmp]# ls -ld testdir/
drwxr-xrwx. 2 root root 4096 Jan 21 11:51 testdir/
           

兩個需要注意的問題 OK,關于SUID/SGID/SBIT這些特殊權限的應用和作用我們已經講完了。但如果你仔細一點的話,會發現,我并沒有用數字方式來更改這個特殊的權限,為什麼呢?且看下面的分析。

問題1:用數字改變目錄的特殊權限,不起作用。

我們把/tmp/下面,我們自己建立的實驗檔案删除

[[email protected] japie tmp]# rm -fr testdir/
[[email protected] japie tmp]# rm -fr passwd 
           

然後再重新建立一個檔案和目錄,

[[email protected] japie tmp]# cp /usr/bin/passwd ./
[[email protected] japie tmp]# mkdir testdir
[[email protected] japie tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-xr-x 2 root root 4096 Jan 21 12:00 testdir/ 
           

下面我們就來用數字方式來更改這三個特殊的權限,看看會有什麼樣的結果

[[email protected] tmp]# chmod 4755 passwd 
[[email protected] tmp]# chmod 3755 testdir/ 
[[email protected] tmp]# ls -l passwd ;ls -ld testdir/
-rwsr-xr-x 1 root root 26968 Jan 21 12:00 passwd drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/
           

發現用這種方式增加這三個特殊權限沒有問題,那麼我們再把權限改回去看看

[[email protected] japie tmp]# chmod 0755 passwd
[[email protected] japie tmp]# chmod 0755 testdir/
[[email protected] japie tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/ 
           

我們發現,對檔案,權限是改回去了,而對于目錄,隻改回去了SBIT的權限,對SUID和SGID改不回去。這是RHEL6上的實驗結果,可能是出于安全性的考慮嗎?這個我就不清楚了,也找不到相關的資料。如果各位網友,有知道什麼原因的,歡迎與我聯系。在此先謝過了。

是以說,建議大家還是用最明了的方式,直接用±來更改,無論方法如何,最終能得到結果就OK了。哈哈……

問題2:為什麼會有大寫的S和T。

還是用上面的檔案和目錄

[[email protected] japie tmp]# ls -l passwd ;ls -ld testdir/
-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/ 
           

我們把passwd和testdir的x權限去掉

[[email protected] japie tmp]# chmod u-x passwd
[[email protected] japie tmp]# chmod o-x testdir/
[[email protected] japie tmp]# ls -l passwd ;ls -ld testdir/
-rw-r-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-- 2 root root 4096 Jan 21 12:00 testdir/ 
           

再給他們加上SUID和SBIT權限

[[email protected] japie tmp]# chmod u+s passwd
[[email protected] japie tmp]# chmod o+t testdir/
[[email protected] japie tmp]# ls -l passwd ;ls -ld testdir/
-rwSr-xr-x 1 root root 26968 Jan 21 12:00 passwd
drwxr-sr-T 2 root root 4096 Jan 21 12:00 testdir/ 
           

我們看到,這時候的小s和小t已經變成了大S和大T了,為什麼呢?因為他們這個位置沒有了x權限,如果沒有了x權限,根據我們上面講的内容,其實,這個特 殊的權限就相當于一個空的權限,沒有意義。也就是說,如果你看到特殊權限位置上變成了大寫的了,那麼,就說明,這裡有問題,需要排除。

繼續閱讀