iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 30
1
Software Development

PHP新手30天實戰金流系列 第 30

[Day30]檔案權限, www-data,SUID, SGID, SBIT

前言

將專案部署在 GCE 遇到 Webserver(Apache) 存取檔案失敗之問題。

  1. Forbidden
Forbidden

You don't have permission to access this resource.

Apache/2.4.29 (Ubuntu) Server at 35.239.64.229 Port 80
  1. 在 /etc/apache2/sites-enabled/000-default.conf 的 Directory tag 內加 Require all granted
  2. 目錄開放執行的權限
  1. Permission denied
ErrorException
error_log(../PayPal.log): failed to open stream: Permission denied
http://35.239.64.229/payment/101/website/paypal

web server 不是檔案擁有者或沒有在檔案擁有群組裡面,造成沒有足夠權限拜訪

==於是來研究一下目錄和檔案給擁有者及群組的權限配置==

Step 1 httpd VS apache2

  • 其實是一樣的~ httpd is the same as apache2. It depends on the OS you use.
    • apache2 on Debian-based distros (Ubuntu)
    • httpd on RedHat-bas

Step 2 檔案資訊解讀

2-1 檔案資訊有以下七個欄位

2-2 第一欄檔案類型的代號及位置權限意義

檔案類型 有七種:
d = directory
- = regular file
l = symbolic link
s = Unix domain socket
p = named pipe
c = character device file(mouse, tty, tape streamer, virtual consoles,
printer, modem)
b = block device file(HD, floppies, CD-ROMs)

位置權限:

r = read
w = write
x = exec (決定是否可以進入該目錄)

2-3 設定檔案權限

  1. 用二進位表示: chmod 4711 test; 第一個數字先不看,用711解讀

    -rws--x--x 7的二進位是111,所以rws都是true,1的二進位是001,所以--x
    s 是?(下一節解釋)

  2. 用符號表示: chmod u=rwxs,go=x test;(與上一指令是相等的)

    u 代表 user
    g 代表 group
    o 代表 others

2-4 檔案特殊權限: SUID, SGID, SBIT

SUID, SGID

  • 咦, rwx知道了, 但 s 是..?
    小寫 s 表示執行者在執行時具有該==擁有者/擁有群組==的權限,即 SUID/SGID。

    Q:什麼時候的動作被歸類為"執行"?
    A:建立檔案或目錄, 進入目錄

  • DIY實作: 還沒設定特殊權限時,建立檔案 aa.txt。設定 SGID 之後,我們建立的檔案(bb.txt) 其擁有群組會是當前目錄(.)的擁有群組(projrct),而非建立者的有效群組(sarahcheng)
  • 所以,前提是該檔案是有給"執行"權限,若該檔案沒有執行權限,則 s 會是大寫 S。

    ex: sarahcheng 是 projrct 群組的成員之一,如果有執行(x)權限,s會是小寫,可進入目錄。

  • 雖然有寫的權限,但是沒有執行權限。也就是無法進入該資料夾,因此也無法新增檔案
    • 雖然可以進 vim, 但進去之後無法存檔

SBIT

  • 那 t 是...?

    • t 是指 SBIT = Sticky Bit, 限制刪除/更名/移動等權限只開放給擁有者和 root。僅對目錄有效。
    • chmod 1751 test;

    -rwxr-x--t

  • QA

  1. 若沒有設定 SBIT 任何 Others 都可以刪除?

  2. 為什麼 addfolder 資料夾的 other 權限是 r_x 不是 r_T

    預設如此

  3. 為什麼 test22 有設定 SBIT 了, 不是擁有者的 sarahcheng 可以刪除 test22 ?

    因為他是當前目錄的擁有者,具有全權

  • 為什麼 一直都還是有 SGID?

    用 chmod g-s 可以刪SGID

  • 使用者 nono 沒有在 projrct 群組裡, 屬於 others, 當前目錄(.)的 others 沒有寫(w)的權利,所以沒辦法刪除 jenny 建立的目錄(ll)

    • 若當前目錄(.)的 others 有寫的權利,即可刪除

    • 將 nono 加入群組 projrct 後, 也可刪除

    • 當前目錄若設定 SBIT,即使是群組裡的使用者,也無法刪。(saracheng, sasa, jenny 皆在群組裡,ray 代表 others, 不在群組裡)

      ray(others) 沒辦法刪, jenny(group) 也沒辦法刪

  • 只有上層才有全權 上上層沒有

  • group 的 user 能不能刪除目錄是看當前目錄的群組權限, 和該目錄無關

    • 只要當前目錄的群組權限可寫, 即可刪除。 不論欲刪除的目錄權限是什麼
  • 當前目錄 rwx 第一層 r-x 檔案 rwx 這樣檔案還可寫嗎?

使用方法

  • 用二進位表示特殊權限
    將數字加在權限數組的最前面,如:4750。

    • 4 為 SUID
    • 2 為 SGID
    • 1 為 SBIT
    • 所以當我們想要讓執行者有擁有者也有擁有群組的權限,即 4+2=6,則可以設成 6750。
  • 重修權限
    在數組前加0, 再設定

    • chmod 00644 $filename

牛刀小試

  1. chmod 4711 test;的 4 代表 SUID

    -rws--x--x

  2. chmod 4611 test;的 4 代表 SUID,但是因為 User 的權限是 6(只能讀、寫,不能執行),所以 S 大寫

    -rwS--x--x

  3. chmod 2611 test;的 2 代表 SGID

    -rw---s--x

  4. chmod 6711 test;的 6 代表 有 SUID 也有 SGID

    -rws--s--x

  5. chmod 7666 test;的 7 代表 有 SUID, SGID, 也有SBIT

    -rwSrwSrwT

Step 3 伺服器 Web server(Apache or Nginx) 是誰

  • 在系統裡由 www-data 這個系統使用者表示
  • 因此在將專案的擁有群組設為 www-data
    sudo chown -R sarahcheng.www-data shopping_cart/
    sudo chmod -R 755 shopping_cart/

drwx r-x r-x
詳細設定請參考[Day26]平台部署 GCP

Step 4 www-data 是什麼

www-data 是一個 system user。給 web servers 使用的特定 user/group。 設定某資料夾為 www-data 是希望 web servers 不要有太高的權限,同時讓 web application 適當進行寫入。

Step 5 系統 system user/group 有哪些

  • 下指令 cut -d: -f1 /etc/passwd 可以看到目前的 system users:

    root
    daemon
    bin
    sys
    sync
    games
    man
    lp
    mail
    news
    uucp
    proxy
    www-data
    backup
    list
    irc
    gnats
    nobody
    systemd-timesync
    systemd-network
    systemd-resolve
    syslog
    _apt
    lxd
    messagebus
    uuidd
    dnsmasq
    sshd
    pollinate
    ntp
    ubuntu
    sarahcheng
    mysql
    
  • 若是下指令 getent passwd or cat /etc/passwd 可以看到以下七欄資訊:

    1. User name
    2. Encrypted password (x means that the password is stored in the /etc/shadow file)
    3. User ID number (UID)
    4. User’s group ID number (GID)(primary group)
    5. Full name of the user (GECOS)
    6. User home directory
    7. Login shell (defaults to /bin/bash)
    root:x:0:0:root:/root:/bin/bash
    proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
    www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
    ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
    sarahcheng:x:1001:1002::/home/sarahcheng:/bin/bash
    mysql:x:103:117:MySQL Server,,,:/nonexistent:`sudo npm run dev`
    /bin/false
    .
    .
    .
    
  • 看系統群組資訊: cat /etc/group
    冒號後面即為有在群組裡的使用者

    root:x:0:
    daemon:x:1:
    bin:x:2:
    sys:x:3:
    adm:x:4:syslog,ubuntu,sarahcheng,sarahcheng1231
    tty:x:5:
    disk:x:6:
    lp:x:7:
    mail:x:8:
    news:x:9:
    uucp:x:10:
    man:x:12:
    proxy:x:13:
    kmem:x:15:
    dialout:x:20:ubuntu,sarahcheng,sarahcheng1231
    fax:x:21:
    voice:x:22:
    cdrom:x:24:ubuntu,sarahcheng,sarahcheng1231
    floppy:x:25:ubuntu,sarahcheng,sarahcheng1231
    tape:x:26:
    sudo:x:27:ubuntu
    audio:x:29:ubuntu,sarahcheng,sarahcheng1231
    dip:x:30:ubuntu,sarahcheng,sarahcheng1231
    www-data:x:33:sarahcheng
    backup:x:34:
    operator:x:37:
    list:x:38:
    irc:x:39:
    src:x:40:
    gnats:x:41:
    shadow:x:42:
    utmp:x:43:
    video:x:44:ubuntu,sarahcheng,sarahcheng1231
    sasl:x:45:
    plugdev:x:46:ubuntu,sarahcheng,sarahcheng1231
    staff:x:50:
    games:x:60:
    users:x:100:
    nogroup:x:65534:
    systemd-journal:x:101:
    systemd-timesync:x:102:
    systemd-network:x:103:
    systemd-resolve:x:104:
    input:x:106:
    crontab:x:107:
    syslog:x:108:
    netdev:x:109:ubuntu,sarahcheng,sarahcheng1231
    lxd:x:110:ubuntu,sarahcheng,sarahcheng1231
    messagebus:x:111:
    uuidd:x:112:
    ssh:x:113:
    mlocate:x:114:
    admin:x:115:
    ntp:x:116:
    ubuntu:x:1000:sarahcheng,sarahcheng1231
    google-sudoers:x:1001:sarahcheng,sarahcheng1231
    sarahcheng:x:1002:
    ssl-cert:x:105:
    mysql:x:117:
    taiwan:x:1003:sarahcheng,www-data
    sarahcheng1231:x:1004:
    
  • groups 列出當前使用者所在的群組

    sarahcheng adm dialout cdrom floppy audio dip www-data video plugdev netdev lxd ubuntu google-sudoers taiwan
    
  • 將使用者加進群組 sudo gpasswd -a sarahcheng taiwan

  • 將使用者移出群組 sudo gpasswd -d user group

  • groups 列出的第一個群組為有效群組 effective group,使用者建立的檔案會直接被有效群組擁有。

    • 切換有效群組 newgrp www-data,結果使用者 sarahcheng 的 groups 列出來:www-data adm dialout cdrom (第一個變成 www-data)
      • 則他所建立出來的 tete 檔案其擁有者/擁有群組為 sarahcheng/www-data
  • 設定使用者的優先群組 primary group 指令:usermod -g 群組名稱 使用者名稱 , 如下

  • 優先群組不等於有效群組

    • To assign secondary groups to a user (-a keeps already existing secondary groups intact otherwise they'll be removed):
      sudo usermod -a -G secondarygroupname username
    -g (primary group assigned to the users)
    -G (Other groups the user belongs to)
    -a (Add the user to the supplementary group(s))
    

上一篇
[Day29]平台串金流-- the redirection is out of control
下一篇
[GCP] Ubuntu 18 部署 Nginx server, Laravel 專案
系列文
PHP新手30天實戰金流34
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
Howard
iT邦新手 4 級 ‧ 2019-10-16 21:55:51

賀完賽/images/emoticon/emoticon64.gif

不過你若要更往前成長,一定要能夠全心力的拿出來學習。

1
Askie Lin
iT邦新手 5 級 ‧ 2019-10-19 00:20:47

恭喜沙拉完賽!一起繼續加油~~~~

0
阿展展展
iT邦好手 1 級 ‧ 2019-11-23 04:44:10

恭喜完賽!!/images/emoticon/emoticon64.gif

我要留言

立即登入留言