본문 바로가기

리눅스/강의

[10. 특수 권한]

특수 권한 


설명

root만 접근할 수 있는 파일이나 명령에 대해 일반 사용자가 접근할 수 있게 해주는 권한

setuid , setgid , sticky bit 3가지로 구분한다.

 

접근 권한

유닉스/리눅스는 시스템 파일에 대한 접근권한 및 파일 종류를 나타내기 위해서 16bit를 사용한다.

빨간박스 : 4bit로 구성되어 있으며 , 파일인지 디렉터리인지 종류를 구분해준다.

'-' (일반파일) , 'd' (디렉터리)

파란박스 : user (3bit) , group (3bit) , other (3bit) 총 9bit로 구성되어 있다.

초록박스 : 특수권한이 있을 시 , user의 x부분에 설정되는 특수권한 이다. (3bit)

 

실제로 특수권한 없이 권한을 부여 할 떄 755 , 644 이런식으로 부여하는데 사실은 맨 앞에 '0' 이 생략되어 있다.

사실상 0755 , 0644 이런식으로 부여가 된다. (맨 앞의 0자리 부분은 특수권한이 아니면 생략해도 된다.)

ex) chmod 0755 test.txt = chmod 755 test.txt 


 

setuid bit


특징

① setuid 비트 : 8진수 4000

② 슈퍼유저(root) 만 접근 가능한 파일이나 명령어에 대해 일반유저가 접근이 필요할 떄 사용한다.

③ setuid 비트가 설정된 파일은 실행 순간만 그 파일의 소유자 권한으로 실행한다.

④ 일반 사용자가 사용하는 passwd 명령어도 root가 passwd 명령어 파일에 setuid를 적용해서 가능한 것. 


단점

① root 권한이 필요없는 프로그램에 소유주가 root로 되어 있고 setuid가 설정된 경우 보안상 매우 취약.

② 만약 cat 명령어에 setuid를 적용하면 , root만 읽을 수 있는 파일도 일반사용자가 cat 명령어로 읽을 수 있다.


설정 방법

① chmod 명령어를 사용해 8진수(4000)나 기호(u+s)를 이용하여 setuid 비트를 설정할 수 있다.

② setuid bit가 설정되어 있으면 user의 실행권한 자리에 s 또는 S로 표시된다.

* s : 실행권한 있음 / S : 실행권한 없음




setgid bit


특징

① setgid 비트 : 8진수 2000 

② setgid bit를 설정 시 , 이 디렉터리에 새로 생성된 파일들은 디렉터리 

그룹 소유권이 아닌 파일 생성자의 그룹 소유권을 얻게 된다.


설정 방법

① chmod 명령어를 사용해 8진수(2000)나 기호(g+s)를 이용하여 setgid 비트를 설정할 수 있다.

② setuid bit가 설정되어 있으면 group의 실행권한 자리에 s 또는 S로 표시된다.




sticky bit


특징

① sticky 비트 : 8진수 1000

② 리눅스는 파일의 sticky bit는 무시

③ 특정 디렉터리를 누구나 자유롭게 사용할 수 있게 하기 위해 사용한다. (공용 디렉터리에 사용)

④ sticky 비트가 디렉터리에 적용되면 디렉터리 소유자나 파일 소유자 또는 관리자가 아닌 사용자들은

    파일을 삭제하거나 이름을 변경하지 못하도록 막음. 파일 또는 디렉터리 생성은 누구나 할 수 있다.

⑤ /tmp 디렉터리에도 sticky bit가 적용되어 있어서 누구나 사용할 수 있는것임.


설정방법

① chmod 명령어를 사용해 8진수(1000)나 기호(o+s)를 이용하여 setgid 비트를 설정할 수 있다.

② setuid bit가 설정되어 있으면 other의 실행권한 자리에 t 또는 T로 표시된다




실습

1) setuid

[root@localhost test]# touch setuid1

[root@localhost test]# touch setuid2


실습을 위해 setuid1,2 파일을 생성

[root@localhost test]# ls -lF

합계 0

-rw-r--r--. 1 root root 0 2017-11-03 15:12 setuid1

-rw-r--r--. 1 root root 0 2017-11-03 15:12 setuid2


[root@localhost test]# chmod 4744 setuid1

[root@localhost test]# chmod 4644 setuid2


setuid1 파일의 권한은 4744 , user 자리에 권한이 7이므로 x 실행권한이 생기고 setuid bit 적용

setuid2 파일의 권한은 4644 , user 자리에 권한이 6이므로 x 실행권한이 없다. setuid bit 적용

[root@localhost test]# ls -lF

합계 0

-rwsr--r--. 1 root root 0 2017-11-03 15:12 setuid1*

-rwSr--r--. 1 root root 0 2017-11-03 15:12 setuid2


setuid1 파일은 실행권한이 있으므로 x자리에 s로 표기됨.

setuid2 파일은 실행권한이 없으므로 x자리에 S로 표기됨.


[root@localhost test]# ./setuid1

[root@localhost test]# ./setuid2

bash: ./setuid2: 허가 거부


setuid1은 실행파일이므로 에러없이 실행이 된다.

setuid2는 실행파일이 아니므로 허가 거부라는 에러메시지가 출력된다.


[root@localhost test]# cat setuid1

ls /var


vi 편집기를 이용해 setuid1 파일의 내용을 기입. (실제 실행이 되는지 확인하기 위해)


[root@localhost test]# ./setuid1

account  cvs games  local  lost+found  opt     spool  yp

cache db gdm    lock   mail   preserve  tmp

crash empty lib    log    nis   run     www


setuid1 파일에 저장된 명령어가 실행되는 것을 확인할 수 있다.


2) setuid 심화

[root@localhost test]# ls -lF

합계 4

-rwsr-----. 1 root root 8 2017-11-03 16:51 setuid1*

-rwSr--r--. 1 root root 0 2017-11-03 15:12 setuid2


현재 setuid1 파일의 권한은 4740 , other에 속하는 일반계정은 setuid1 파일을 실행 또는 읽을 수 없다.


[test1@localhost test]$ whoami

test1


일반 사용자 계정인 test1 계정으로 로그인 한 후 제대로 로그인이 되었는지 whoami로 확인. 


[test1@localhost test]$ cat setuid1
cat: setuid1: 허가 거부

현재 일반 사용자 계정은 setuid1 파일에 대한 권한이 부족해서 열람할 수 없다.

[root@localhost test]# ls -ld /bin/cat
-rwxr-xr-x. 1 root root 48568 2013-11-23 00:36 /bin/cat


cat 명령어에 관한 파일의 권한은 755


[root@localhost test]# chmod u+s /bin/cat
[root@localhost test]# ls -ld /bin/cat
-rwsr-xr-x. 1 root root 48568 2013-11-23 00:36 /bin/cat

관리자 계정으로 cat 파일에 setuid 권한을 부여한다. (4755)
해당 권한을 부여하게 되면 , 일반사용자는 cat 명령어를 관리자처럼 사용할 수 있게된다. (모든 파일이 cat 명령어로 확인됨)

[test1@localhost test]$ cat setuid1
ls /var

권한이 부족해서 읽을 수 없던 setuid1 파일도 cat 명령어로 root 권한을 빌려 읽을 수 있게 된다.

[test1@localhost test]$ cat /etc/shadow | tail -1
test2:$6$OYYMpcri$2nSI2onO9EOcYi.FGLsstwmwLxGWRNUzBvoYu.clxtvfXdK/ogv0RmqoypC5e31d1WOvsEhg1vOzN0rIkMkUF/:17473:0:99999:7:::
[test1@localhost test]$ tail -1 /etc/shadow
tail: cannot open `/etc/shadow' for reading: 허가 거부

shadow 파일도 마찬가지로 일반 사용자가 읽을 수 없는 파일이지만 cat 명령어를 사용하면 읽을 수 있다.
cat 명령어가 아닌 tail , vim 등의 명령어로는 setuid 가 걸려있지 않기 떄문에 shadow 파일을 열람할 수 없다.

3)setgid 
[test1@localhost tmp]$ cd /tmp
[test1@localhost tmp]$ mkdir -p test/setgid                   
[test1@localhost tmp]$ cd test/setgid                        
[test1@localhost test]$ chmod 2755 setgid                     
[test1@localhost test]$ ls -ld setgid
drwxr-sr-x. 2 test1 test1 4096 2017-11-03 18:39 setgid

빨강 : -p 옵션을 이용해 한번에 test/setgid 디렉터리를 생성한다.
파랑 : setgid 디렉터리의 권한을 2755로 설정.
초록 : setgid 권한이 변경된 것을 확인.

[root@localhost tmp]# whoami
root
[root@localhost tmp]# cd /tmp/test/setgid/
[root@localhost setgid]# touch test1

root 계정으로 setgid 디렉터리에 이동 후 test1 파일을 생성.

[test1@localhost setgid]$ touch test2

[test1@localhost setgid]$ ls -la
합계 8
drwxr-sr-x. 2 test1 test1 4096 2017-11-03 18:41 .
drwxrwxr-x. 3 test1 test1 4096 2017-11-03 18:40 ..
-rw-r--r--. 1 root  test1    0 2017-11-03 18:40 test1
-rw-rw-r--. 1 test1 test1    0 2017-11-03 18:41 test2

빨강 : test1 계정도 setgid 디렉터리에 test2 파일을 하나 생성한다.
파랑 : root 계정이 생성한 test1 파일의 GID 부분이 test1 계정의 소유권으로 되어 있다.

4)setgid 심화

[root@localhost setgid]# ls -ld /var/spool/mail
drwxrwxr-x. 2 root mail 4096 2017-11-03 17:12 /var/spool/mail
[root@localhost setgid]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

빨강 : 사용자 계정 생성시 메일에 관한 자료가 들어있는 mail 파일의 GID부분이 mail 계정으로 되어있다.
파랑 : yes 라고 적혀있으면 , 사용자 계정 생성시 mail spool 파일 생성

[root@localhost setgid]# useradd mailuser
[root@localhost setgid]# ls -ld /var/spool/mail/mailuser 
-rw-rw----. 1 mailuser mail 0 2017-11-03 19:14 /var/spool/mail/mailuser

초록 : mailuser 계정을 생성
분홍 : mailuser 계정 생성시 생성되는 mail spool 파일 그룹 소유자가 mailuser가 아닌 mail로 되어 있다. (setgid 떄문)


5)sticky 

[root@localhost setgid]# cd /tmp/test

[root@localhost test]# mkdir sticky
[root@localhost test]# chmod 1757 sticky/
[root@localhost test]# ls -ld sticky/
drwxr-xrwt. 2 root root 4096 2017-11-03 19:52 sticky/

root 계정은 test 디렉터리로 이동 후 sticky 디렉터리를 생성한다.
권한은 일반유저도 모든 작업을 할 수 있게 1757을 부여. (sticky bit 적용)

[test1@localhost ~]$ cd /tmp/test/sticky/
[test1@localhost sticky]$ touch test
[test1@localhost sticky]$ ls -l
합계 0
-rw-rw-r--. 1 test1 test1 0 2017-11-03 19:59 test

test1 계정은 sticky 디렉터리로 이동 후 test 파일을 생성한다.

[test2@localhost sticky]$ whoami
test2
[test2@localhost setgid]$ cd /tmp/test/sticky/

[test2@localhost sticky]$ ls -l test
-rw-rw-r--. 1 test1 test1 0 2017-11-03 19:59 test
[test2@localhost sticky]$ rm -rf test
rm: cannot remove `test': 명령을 허용하지 않음

test2 계정은 sticky 디렉터리로 이동 후 test1 계정이 생성 한 test 파일의 삭제를 시도한다.
분명 sticky 디렉터리의 권한이 1757 인데 삭제가 되지 않는다. (sticky bit 의 효과)
삭제할 수 있는 권한이 있어도 파일&디렉터리를 생성한 소유자이거나 관리자가 아니면 삭제가 불가능하다.

6)setgid + sticky bit 
[root@localhost test]# mkdir a
[root@localhost test]# chmod 3755 a
[root@localhost test]# ls -l
합계 4
drwxr-sr-t. 2 root root 4096 2017-11-08 15:40 a

root는 a 디렉터리를 생성하고 권한을 3755로 부여한다.
3755는 sticky와 setgid의 특수허가권을 동시에 부여한 상태.

[test2@localhost a]$ cd /tmp/test/a
[test2@localhost a]$ ls -ld
drwxr-srwt. 4 root root 4096 2017-11-08 15:41 .
[test2@localhost a]$ mkdir jelly
[test2@localhost a]$ ls -l
합계 4
drwxrwsr-x. 2 test2 root 4096 2017-11-08 15:41 jelly

test2 계정은 root가 생성한 a 디렉터리로 이동해 jelly 디렉터리를 하나 생성한다.
현재 setgid bit가 적용되어 룹의 소유자가 root인걸 확인.

[test1@localhost a]$ cd /tmp/test/a/
[test1@localhost a]$ mkdir chick
[test1@localhost a]$ ls -l
합계 8

drwxrwsr-x. 2 test1 root 4096 2017-11-08 15:41 chick

drwxrwsr-x. 2 test2 root 4096 2017-11-08 15:41 jelly


test1 계정도 root가 생성한 a 디렉터리로 이동해 chick 디렉터리를 하나 생성한다.
chick 디렉터리의 그룹의 소유자가 root인걸 확인.

[test1@localhost a]$ ls -ld 
drwxr-srwt. 4 root root 4096 2017-11-08 15:41 .
[test1@localhost a]$ rm -rf jelly/
rm: cannot remove `jelly': 명령을 허용하지 않음

a 디렉터리의 other 권한이 7임에도 test1 계정은 test2 계정이 생성한 jelly 디렉터리를 삭제할 수 없다.
이와같이 stckiy bit setgid bit 가 전부 적용된 것을 확인할 수 있다.





'리눅스 > 강의' 카테고리의 다른 글

[12. 마운트]  (0) 2017.11.02
[11. 파티션]  (0) 2017.11.01
[9-1. 소유권 실습]  (0) 2017.04.12
[9. 소유권]  (0) 2017.04.12
[8-1. 허가권 실습]  (0) 2017.04.11