뭉게뭉게 엔지니어

Special permission & ACL(확장퍼미션) 본문

☁ Server/기초 따라가기

Special permission & ACL(확장퍼미션)

어푸어푸🌊 2022. 7. 8. 23:09

Special permission이란?

더보기

- "특수 권한"이라 해석되는 것으로, 시스템 관리의 효율성을 높이기 위해 사용되는 특별한 권한이다.

- 특수하게 부여하는 권한인만큼 보안의 취약점이 될 수 있어 꼭 필요한 경우가 아니면 사용하지 않는 것이 좋다.

 

특수 권한의 종류는 아래와 같다.

1) setuid (4000 = u+s)

- 파일을 실행하는 동안 해당 파일의 소유주의 권한으로 실행하며, 실행 파일에만 설정할 수 있다.

- 일반적인 실행은 파일을 이용하려는 사용자의 소유권에 의해 실행 권한이 부여되지만,

  setuid권한이 적용되어 있는 파일은 실행되는 동안은 사용자가 아닌, 파일의 소유주의 권한을 할당받는다.

- 소유자의 허가권에서 실행 권한이 x가 아닌 s로 표시된다.( ex. rwsr--r--)

 

▶/etc/passwd 파일로 예시를 들어보자!

/etc/passwd 는 계정의 정보를 알 수 있는 파일이자, 암호가 저장되는 파일이다.

[root@localhost ~]# grep test01 /etc/passwd
test01:x:1001:1001::/home/test01:/bin/bash
       └/etc/shadow 에 암호가 저장되어있음을 의미! 

사용자 전환 > 암호 변경 
[root@localhost ~]# su - test01
[test01@localhost ~]$ passwd 
test01 사용자의 비밀 번호 변경 중
test01에 대한 암호 변경 중  
(현재) UNIX 암호:
새  암호:
새  암호 재입력:
passwd: 모든 인증 토큰이 성공적으로 업데이트 되었습니다
[test01@localhost ~]$ exit
logout.

/etc/shadow 의 정보확인 : 퍼미션이 없음 > 어떻게 암호변경이 되지?
[root@localhost ~]# ll /etc/shadow
---------- 1 root root 1369  6월 30 18:38 /etc/shadow

실행시에 root 로 실행이되서 모든 퍼미션 무시하고 업데이트 됨 
실행시 소유자의 퍼미션으로 동작할것 
[root@localhost ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856  4월  1  2020 /usr/bin/passwd
   └ x가 아닌, s!!!!! 그러므로 setuid가 설정됐기 때문에 해당 파일의 소유자인 root로 실행되어
     비밀번호 변경이 가능했던 것!

 

▶setuid test

permission 을 4755-> 755 로 회수]
[root@localhost ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856  4월  1  2020 /usr/bin/passwd

[root@localhost ~]# chmod 755 /usr/bin/passwd 
[root@localhost ~]# ll /usr/bin/passwd
-rwxr-xr-x. 1 root root 27856  4월  1  2020 /usr/bin/passwd

test01 로 비밀번호 변경 시도]
[test01@localhost ~]$ passwd
test01 사용자의 비밀 번호 변경 중
test01에 대한 암호 변경 중  
(현재) UNIX 암호:
새  암호:
새  암호 재입력:
passwd: 인증 토근 수정 오류

원상 복구]
[root@localhost ~]# chmod 4755 /usr/bin/passwd
[root@localhost ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856  4월  1  2020 /usr/bin/passwd

 

 

2) setgid (2000 = g+s)

- 파일을 실행하는 동안 해당 파일의 소속 그룹 권한으로 실행하며, 파일 및 디렉터리에서 설정 가능하다.

- 파일이 실행되는 동안은ㅇ 사용자가 아닌 파일의 소속 그룹 권한을 할당받는다.

- 소속 그룹의 허가권에서 실행 권한이 x 가 아닌 s 로 표시 된다. (ex. rwxr-sr--)

- 디렉터리 적용 

  •   setgid가 적용된 디렉터리의 하위에 생성되는 디렉터리도 동일하게 setgid가 설정된다.
  •  setgid가 적용된 디렉터리 안에서 새로운 디렉터리를 생성하면 setgid에 명시된 소속그룹으로 설정된다.

 

▶ setgid test

setgid 설정된 파일 선택]

#find / -perm -2000
[root@localhost ~]# ll /usr/bin/wall
-r-xr-sr-x. 1 root tty 15344  6월 10  2014 /usr/bin/wall

/usr/bin/wall]
[test01@localhost ~]$ wall hi
Broadcast message from test01@localhost.localdomain (pts/1) (Thu Jun 30 11:03:52 2022):
hi

다른 사용자 에게 전송]
[root@localhost ~]#
Broadcast message from test01@localhost.localdomain (pts/1) (Thu Jun 30 11:03:52 2022):
hi

setgid 제거]
[root@localhost ~]# chmod 555 /usr/bin/wall
[root@localhost ~]# ll /usr/bin/wall
-r-xr-xr-x. 1 root tty 15344  6월 10  2014 /usr/bin/wall

다시 test01로 wall 명령어 테스트]
[test01@localhost ~]$ wall hi
Broadcast message from test01@localhost.localdomain (pts/1) (Thu Jun 30 11:08:44 2022):
hi

▶ 자신을 제외한 다른 사용자에게는 전달 되지 않는다 
  명령어를 전달하려면 tty group 소속 사용자 이어야 한다 

[root@localhost ~]# grep tty /etc/group
tty:x:5:                          -- 주로 시스템이 사용하는 그룹

원상복구]
[root@localhost ~]# ll /usr/bin/wall
-r-xr-xr-x. 1 root tty 15344  6월 10  2014 /usr/bin/wall

1) numeric
[root@localhost ~]# chmod 2555 /usr/bin/wall
[root@localhost ~]# ll /usr/bin/wall
-r-xr-sr-x. 1 root tty 15344  6월 10  2014 /usr/bin/wall

2) symbolic
[root@localhost ~]# chmod g+s /usr/bin/wall
[root@localhost ~]# ll /usr/bin/wall
-r-xr-sr-x. 1 root tty 15344  6월 10  2014 /usr/bin/wall

 

▶ find 명령어를 이용한 퍼미션 확인

setuid 들어간 파일 찾기]
[root@localhost ~]# find /usr/bin -perm 4755 -type f
/usr/bin/fusermount
/usr/bin/passwd
/usr/bin/su
/usr/bin/chage
/usr/bin/gpasswd
/usr/bin/newgrp
/usr/bin/mount
/usr/bin/umount
/usr/bin/pkexec
/usr/bin/crontab
/usr/bin/Xorg
/usr/bin/at

[root@localhost ~]# ll /usr/bin/fusermount 
-rwsr-xr-x. 1 root root 32096 10월 31  2018 /usr/bin/fusermount

퍼미션이 4xxx 인거를 찾자]
find / -perm -4000

최소 -----s--- 만 만족하면 나오게!!]
find / -perm -2000

find: ‘/proc/59751/task/59751/fd/5’: 그런 파일이나 디렉터리가 없습니다
find: ‘/proc/59751/task/59751/fdinfo/5’: 그런 파일이나 디렉터리가 없습니다
find: ‘/proc/59751/fd/6’: 그런 파일이나 디렉터리가 없습니다
find: ‘/proc/59751/fdinfo/6’: 그런 파일이나 디렉터리가 없습니다
~
/usr/bin/wall

[root@localhost ~]# ll /usr/bin/wall
-r-xr-sr-x. 1 root tty 15344  6월 10  2014 /usr/bin/wall

 

3) Sticky bit

- 모든 소유권에 모든 허가권을 부여하지만 삭제는 소유주만 가능하다.

   ▶ 기본 소유권과 허가권을 먼저 확인하기 때문에 기본 허가권에 삭제 권한이 있으면 삭제가 가능하다.

- 디렉터리에 설정하는 것으로 /tmp , /var/tmp에 적용되어 있다.

- 그 외 사용자(o)의 허가권에서 실행 권한이 x가 아닌 t로 표시된다.(ex. rwxr-wr-t

 

test01 사용자는 /test 디렉터리에 파일을 생성할 수 있을까?

- test01 은  u , g , o  : other에 속한다 

[root@localhost ~]# ls -ld /test
drwxr-xr-x 2 root root 6  6월 30 19:08 /test
        ┗ other의 permission r-x : w 가 없다.     ▶디렉터리이므로 파일생성 안됨 

[test01@localhost ~]$ cd /test/
[test01@localhost test]$ pwd
/test
[test01@localhost test]$ touch ./1.txt
touch: cannot touch `./1.txt': 허가 거부

/tmp , /var/tmp 퍼미션 확인 1777]
[root@localhost ~]# ls -ld  /tmp /var/tmp
drwxrwxrwt. 17 root root 4096  6월 30 19:20 /tmp 
drwxrwxrwt.  9 root root 4096  6월 30 19:17 /var/tmp

※모든 사용자가 디렉터리 파일을 생성/삭제할 수 있음. 

/Stickytest 디렉터리 생성, 777 퍼미션 부여]
[root@localhost ~]# mkdir /stickytest
[root@localhost ~]# chmod 777 /stickytest/
[root@localhost ~]# ls -ld /stickytest/
drwxrwxrwx 2 root root 6  6월 30 19:23 /stickytest/

각 사용자별 파일 생성]
[root@localhost ~]# su - test01
[test01@localhost ~]$ touch /stickytest/t1.txt

[root@localhost ~]# su - test02
[test02@localhost ~]$ touch /stickytest/t2.txt


다른 소유자의 파일을 삭제]
test01 사용자로 test02 가 소유자인 파일 삭제
[root@localhost ~]# su - test01
[test01@localhost ~]$ cd /stickytest/
[test01@localhost stickytest]$ ll
합계 0
-rw-rw-r-- 1 test01 test01 0  6월 30 19:26 t1.txt
-rw-rw-r-- 1 test02 test02 0  6월 30 19:27 t2.txt    ▶소유자(u)가 다른 파일 

삭제 후 확인]
[test01@localhost stickytest]$ rm -r /stickytest/t2.txt 
rm: remove write-protected 일반 빈 파일 `/stickytest/t2.txt'? y
[test01@localhost stickytest]$ ll
합계 0
-rw-rw-r-- 1 test01 test01 0  6월 30 19:26 t1.txt

/stickytest 디렉터리에 sticky bit 부여]
[root@localhost ~]# chmod 1777 /stickytest/
[root@localhost ~]# ls -ld /stickytest/
drwxrwxrwt 2 root root 20  6월 30 19:28 /stickytest/

다른 소유자의 파일 삭제]
test02 >> 
[root@localhost ~]# ls -ld /stickytest/
drwxrwxrwt 2 root root 20  6월 30 19:28 /stickytest/

[root@localhost ~]# su - test02
[test02@localhost ~]$ cd /stickytest/
[test02@localhost stickytest]$ ll
합계 0
-rw-rw-r-- 1 test01 test01 0  6월 30 19:26 t1.txt
[test02@localhost stickytest]$ rm -r ./t1.txt 
rm: remove write-protected 일반 빈 파일 `./t1.txt'? y  
rm: cannot remove `./t1.txt': 명령을 허용하지 않음

test01 >>
[root@localhost ~]# su - test01
[test01@localhost stickytest]$ id
uid=1001(test01) gid=1001(test01) groups=1001(test01)
[test01@localhost ~]$ cd /stickytest/
[test01@localhost stickytest]$ ll
합계 0
-rw-rw-r-- 1 test01 test01 0  6월 30 19:26 t1.txt
[test01@localhost stickytest]$ rm -r ./t1.txt

 

 

ACL이란?

더보기

- 확장 권한이라 해석되는 것으로, 기본적인 소유권과 허가권 외에 세부 설정이 필요한 경우 사용되는 허가권이다.

  ▶ 쉽게 말해, 기본적인 ownership 과 permission 이외에 특정한 사용자에게 추가 퍼미션을 부여한다. 

- 파일 및 디렉터리에 특정 사용자/소속 그룹의 권한을 부여할 때 사용하며, 

  권한 부여에는 setfacl 명령어를, 권한 확인에는 getfacl 명령어를 사용한다 .

  • getfacl 구조 : # getfacl [옵션] [파일이름]
  • setfacl 구조 : #setfacl [옵션 -m] [정책] [파일이름]

- 파일 및 디렉터리의 세부 정보 중에서 권한 정보(rw-r--r--)의 마지막 10번째 값으로 ACL설정 여부를 확인할 수 있다.

 

 

▶ACL 설정 후 확인 실습!

환경 설정]
useradd test01
useradd test02

[root@localhost ~]# mkdir /aclfile
[root@localhost ~]# mkdir /acldir
[root@localhost ~]# mkdir /acldefault


ACL 적용전 파일 퍼미션 상태 확인] 
[root@localhost ~]# getfacl /aclfile/file1 
getfacl: Removing leading '/' from absolute path names
# file: aclfile/file1
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@localhost ~]# ls -l /aclfile/file1 
-rw-r--r-- 1 root root 0  7월  1 12:09 /aclfile/file1


test01 만 rw 를 가지도록 ACL 설정]
# setfacl -m u:test01:rw /aclfile/file1


other에 대해서 r]
# ls -l /aclfile/file1 
-rw-rw-r--+ 1 root root 0  7월  1 12:09 /aclfile/file1


test01,test02 로 파일을 수정할 수 없는지 test]
[root@localhost ~]# su - test01
[test01@localhost ~]$ cd /aclfile/
[test01@localhost aclfile]$ ls
file1
[test01@localhost aclfile]$ vi /aclfile/file1  ▶ 수정 잘 됨!

[test01@localhost aclfile]$ exit
logout

[root@localhost ~]# su - test02
마지막 로그인: 금  7월  1 12:23:27 KST 2022 일시 pts/0
[test02@localhost ~]$ vi /aclfile/file1
"/aclfile/file1" [읽기 전용] 2L, 5C             ▶ 수정 안 됨!

 

▶setfacl 설정 후 group 퍼미션은 그룹이 아닌 ACL의 mask값으로 활용된다!

[root@localhost ~]# ls -l /aclfile/file1 
-rw-rw-r--+ 1 root root 5  7월  1 12:23 /aclfile/file1
   group 퍼미션이 아님

[root@localhost ~]# getfacl /aclfile/file1 
getfacl: Removing leading '/' from absolute path names
# file: aclfile/file1
# owner: root
# group: root
user::rw-
user:test01:rw-
group::r--          ▶ 실제 소유그룹의 퍼미션 
mask::rw-
other::r--

 

▶mask는 ACL 의 적용 범위를 제한한다! 

즉 최대 퍼미션을 부여해도 ACL에는 영향을 주지 않으며, mask값의 변화는 group 퍼미션 표시 부분에 영향을 준다.

[root@localhost ~]# ll /aclfile/file1 
-rw-rw-r--+ 1 root root 5  7월  1 12:23 /aclfile/file1

[root@localhost ~]# chmod 777 /aclfile/file1 

[root@localhost ~]# ll /aclfile/file1 
-rwxrwxrwx+ 1 root root 5  7월  1 12:23 /aclfile/file1

[root@localhost ~]# getfacl /aclfile/file1 
getfacl: Removing leading '/' from absolute path names
# file: aclfile/file1
# owner: root
# group: root
user::rwx
user:test01:rw-
group::r--
mask::rwx
other::rwx

 

▶mask를 제어하여 test01의 ACL을 제한해보자!

예시1_mask 를 r 만 부여]
# ll /aclfile/file1 
-rwxrwxrwx+ 1 root root 5  7월  1 12:23 /aclfile/file1
# chmod 747 /aclfile/file1

[root@localhost ~]# getfacl /aclfile/file1 
getfacl: Removing leading '/' from absolute path names
# file: aclfile/file1
# owner: root
# group: root
user::rwx
user:test01:rw-			#effective:r--
group::r--
mask::r--
other::rwx

test01 로 확인 - 	#effective:r-- 적용됨 
[root@localhost ~]# su - test01
[test01@localhost ~]$ vi /aclfile/file1
"/aclfile/file1" [읽기 전용]

예시2_mask를 rw만 부여]
[root@localhost ~]# ll /aclfile/file1 
-rwxr--rwx+ 1 root root 5  7월  1 12:23 /aclfile/file1

[root@localhost ~]# setfacl -m u:test01:rw /aclfile/file1 

[root@localhost ~]# getfacl /aclfile/file1 
getfacl: Removing leading '/' from absolute path names
# file: aclfile/file1
# owner: root
# group: root
user::rwx
user:test01:rw-
group::r--
mask::rw-
other::rwx

[root@localhost ~]# setfacl -m m::r /aclfile/file1 
[root@localhost ~]# getfacl /aclfile/file1 
getfacl: Removing leading '/' from absolute path names
# file: aclfile/file1
# owner: root
# group: root
user::rwx
user:test01:rw-			#effective:r--
group::r--
mask::r--
other::rwx

 

나는 mask 값을 굳이 왜 설정해주는지 이해가 가지 않았는데 동기에게 물어보고 이해 완료!

mask 값은 ACL이 설정된 모든 사용자에게 적용되는 것이므로, 이를 통해 한번에 특정 상황을 연출(제어)할 수 있다!

 

 

▶ ACL d옵션 적용 확인(Default ACL)

[root@localhost ~]# setfacl -m d:u:test01:rwx /acldefault
[root@localhost ~]# getfacl /acldefault
getfacl: Removing leading '/' from absolute path names
# file: acldefault
# owner: root
# group: root
user::rwx
user:test01:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:test01:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

test01 사용자로  /acldefault 아래에 파일/디렉터리를 생성 해서 비교
[root@localhost ~]# su - test01
[test01@localhost ~]$ touch /acldefault/file5
[test01@localhost ~]$ mkdir /acldefault/dir5

설정 이후에 생성된 파일과 디렉터리는 확장 퍼미션이 설정!!! 
[root@localhost ~]# ls -l /acldefault
합계 0
drwxrwxr-x  2 test01 test01 6  7월  1 13:47 dir4
drwxrwxr-x+ 2 test01 test01 6  7월  1 13:51 dir5
-rw-r--r--  1 root   root   0  7월  1 13:42 file3
-rw-rw-r--  1 test01 test01 0  7월  1 13:45 file4
-rw-rw-r--+ 1 test01 test01 0  7월  1 13:51 file5

생성시 자동으로 확장 퍼미션이 부여됨 
[root@localhost ~]# getfacl /acldefault/file5 /acldefault/dir5
getfacl: Removing leading '/' from absolute path names
# file: acldefault/file5
# owner: test01
# group: test01
user::rw-
user:test01:rwx			#effective:rw-
group::r-x			#effective:r--
mask::rw-
other::r--

# file: acldefault/dir5
# owner: test01
# group: test01
user::rwx
user:test01:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:test01:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

 

'☁ Server > 기초 따라가기' 카테고리의 다른 글

Package 관리  (0) 2022.07.17
Compress(tar,gzip,bzip2)  (0) 2022.07.17
Permission  (0) 2022.07.08
Ownership  (0) 2022.07.05
사용자 & 그룹 관리  (0) 2022.06.30
Comments