#############################################

## 주제 : 퍼미션                           ##

#############################################


여러사람이 사용하는 멀티유저 운영체제인 리눅스는 보안상의 이유로 각 파일마다 

접근권한이 설정되어 있다.  이를 퍼미션이라고 한다.

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

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

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

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

         

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

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

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


퍼미션 변경 명령어 

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

- 디렉토리 위치 : /bin

- 패키지 : coreutils 


퍼미션 변경 방법 

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

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



퍼미션의 3가지 구분

1. 소유자 (user)

2. 그룹 (group)

3. 다른사용자 (other)


리눅스의 퍼미션

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

- 퍼미션은 아이노드에 정보가 들어있다.


    s  s  t <-- 특수 권한

  rwxrwxrwx <-- 일반 권한

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

  ~~~   ~~~

  |  ~~~ |

  |  |  other (user + group 에 속하지 않은 모든 사용자들의 권한)

  |  |  

  |  group (파일의 그룹의 권한)

  |

  user (owner 파일의 소유자의 권한)



r(Read)    : 읽기 권한

w(Write)   : 쓰기 권한 

x(eXecute) : 실행 권한 

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

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

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


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

|   |  Set-UID  |  Set-GID  |Sticky-Bit |

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

|   |     s     |     s     |     t     |  <-- 특수 권한

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

|   | r | w | s | r | w | s | r | w | t |

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

|   |    user   |   group   |   other   |

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

| - | r | w | x | r | w | x | r | w | x |  <-- 일반 권한

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



octal mode를 이용한 일반 권한의 퍼미션 값

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

|   | 4 | 2 | 1 |

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

| - | r | w | x |

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

| 0 | 0 | 0 | 0 | ---

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

| 1 | 0 | 0 | 1 | --x

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

| 2 | 0 | 1 | 0 | -w-

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

| 3 | 0 | 1 | 1 | -wx

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

| 4 | 1 | 0 | 0 | r--

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

| 5 | 1 | 0 | 1 | r-x

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

| 6 | 1 | 1 | 0 | rw-

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

| 7 | 1 | 1 | 1 | rwx

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


octal mode를 이용한 특수 권한의 퍼미션 값

      0000 ~ 7777

      0 ~ 7        0 ~ 7       0 ~ 7       0 ~ 7

    |- 특수권한-|- user    -|- group   -|- other   -|

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

|   | 4 | 2 | 1 | 4 | 2 | 1 | 4 | 2 | 1 | 4 | 2 | 1 |  

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

|   | s | s | t | r | w | x | r | w | x | r | w | x |  

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

| 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | <-- rwxrwxrwx

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

|   |   |   |   | r | w | x | r | w | x | r | w | x |  

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

| 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | <-- t rwxrwxrwt

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

|   |   |   |   | r | w | x | r | w | x | r | w | t |  

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

| 2 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | <-- s   set-gid 

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

|   |   |   |   | r | w | x | r | w | s | r | w | x |  

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

| 3 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | <-- st  set-gid + sticky-bit  

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

|   |   |   |   | r | w | x | r | w | s | r | w | t | 

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

| 4 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | <-- s   set-uid   

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

|   |   |   |   | r | w | s | r | w | x | r | w | x | 

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

| 5 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | <-- st  set-uid + sticky-bit

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

|   |   |   |   | r | w | s | r | w | x | r | w | t | 

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

| 6 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | <-- ss  set-uid + set-gid

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

|   |   |   |   | r | w | s | r | w | s | r | w | x | 

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

| 7 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | <-- sst set-uid + set-gid + sticky-bit

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

|   |   |   |   | r | w | s | r | w | s | r | w | t | 

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

 


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

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

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

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

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

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

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

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

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

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

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

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

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

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

sticky-bit  현재는 사용하지 않는다.   |   모두 생성하지만 지울땐 소유자가 만든 파일만 삭제 

                                      |   (공용디렉토리에 사용, /tmp)

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


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

실행파일의 두 가지 종류

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

- 쉘스크립트 파일 (일반 텍스트 문서지만 실행 형식을 갖춘 파일)     

  (root : x 권한만 있어도 된다. 일반유저 : rx 모두 있어야 한다.)

 


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

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

- 허용문자 : augorwxst,=-+


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

ex)

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


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


chmod u+rwx,go+rx a.txt or chmod u+w,a+rx a.txt or

chmod a+rwx,go-w a.txt or chmod a=rwx,og-w a.txt

-rwxr-xr-x (0755)  


chmod go= a.txt or chmod go-rx a.txt

-rwx------   


-rw-------   chmod u-x a.txt


-rw-r--r--   chmod go+r a.txt

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


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

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

- 허용숫자 : 0 ~ 7

  rwx rwx

  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


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

ex) a.txt 를 초기화 시키고 권한 변경하기

chmod 000 a.txt

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


-rwxr-xr-x  : chmod -c 755 a.txt

-rwx------  : chmod -c 700 a.txt

-rw-------  : chmod -c 600 a.txt 

-rw-r--r--  : chmod -c 644 a.txt

---x--x--x  : chmod -c 111 a.txt

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



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

-rwxr-xr-x   

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

-rwxr-x---

-rwx------

-rwx--x---


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

drwxr-xr-x   

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

drwxr-x---

drwx------

drwx--x---


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

-rw-r--r--   

-rw-r-----

-rw-------


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

디렉토리 : -rwx??????

일반파일(실행X) : -rw-??????

일반파일(실행X) : -r--??????

일반파일(실행O) : -rwx??????


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

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


-- LAB 순서 --

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

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

-- LAB 순서 --


1. cat /etc/shadow 

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


- 사용자를 확인한다.

~ # 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 -dl /bin

-rwxr-xr-x ...

~ # 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


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

~ # 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

$ pwd

$ cd /root

-bash: cd: /root: Permission denied



$ cat /etc/shadow

cat: /etc/shadow: Permission denied


$ 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

  :

  :


- /usr/local/bin 디렉토리는 일반유저가 들어갈 수 있는 권한이 있다.

$ cd /usr/local/bin

$ pwd

/usr/local/bin


$ ls -ld /usr

drwxr-xr-x 15 root root 4096 Oct 17 10:51 /usr

         ~

$ ls -ld /usr/local

drwxr-xr-x 11 root root 4096 Oct 17 10:50 /usr/local

         ~

$ ls -ld /usr/local/bin

drwxr-xr-x 2 root root 4096 May 11  2011 /usr/local/bin

         ~



- /var/spool/cron 디렉토리는 일반유저가 들어갈 수 있는 권한이 없다.

$ cd /var/spool/cron

-bash: cd: /var/spool/cron/: Permission denied


$ ls -ld /var

drwxr-xr-x 22 root root 4096 Oct 17 11:37 /var

         ~

$ ls -ld /var/spool/

drwxr-xr-x 10 root root 4096 Oct 17 10:52 /var/spool

         ~

$ ls -ld /var/spool/cron

drwx------ 2 root root 4096 Feb 23  2012 /var/spool/cron

         ~


- /tmp 디렉토리의 파일 목록을 확인할 수 있다.

$ ls /tmp

lost+found  mc-root  TEST


$ ls -l /bin/ls

-rwxr-xr-x 1 root root 95116 Jul 22  2011 /bin/ls

         ~

$ ls -ld /tmp

drwxrwxrwt 7 root root 1024 Oct 23 19:11 /tmp

         ~


$ df

Filesystem           1K-blocks      Used Available Use% Mounted on

/dev/sda1              2847720    258048   2442680  10% /

/dev/sda3               388693     10307    358305   3% /home

/dev/sda6               956980    136728    770856  16% /var

/dev/sda7               482214     10552    446763   3% /tmp

/dev/sda5              1898468   1274028    526448  71% /usr

/dev/sda2               101105     11596     84288  13% /boot

tmpfs                   127492         0    127492   0% /dev/shm


$ ls -l /bin/df

-rwxr-xr-x 1 root root 44744 Jul 22  2011 /bin/df



- 디렉토리의 r 권한 테스트

# ls -ld /tmp

drwxrwxrwt 7 root root 1024 Oct 23 19:11 /tmp

       ~

$ ls /tmp

lost+found  mc-root  TEST


# chmod o-r /tmp 

# ls -ld /tmp

drwxrwx-wt 7 root root 1024 Oct 23 19:11 /tmp

       ~

$ ls /tmp

ls: /tmp: Permission denied


- 쓰기 권한 연습


$ cd

$ pwd

/home/linux

$ ls -ld 

drwx------ 3 linux linux 1024 Oct 23 19:31 .

  ~

$ touch a.txt

$ ls -l a.txt 

-rw-rw-r-- 1 linux linux 0 Oct 23 19:31 a.txt

$ echo 1234 > a.txt

$ ls -l a.txt 

-rw-rw-r-- 1 linux linux 5 Oct 23 19:31 a.txt

$ cat a.txt 

1234


- /etc/passwd 파일을 수정할 수 없다.

$ echo 1234 >> /etc/passwd

-bash: /etc/passwd: Permission denied

$ ls -ld /etc

drwxr-xr-x 82 root root 4096 Oct 23 19:11 /etc

        ~

$ ls -l /etc/passwd

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

        ~


파일이 쓰기 권한이 없어도 파일이 속한 디렉토리에 쓰기 권한이 있다면 

일반유저는 파일을 수정할 수 있다. 

파일보다 파일이 속한 디렉토리가 우선하기 때문이다.

# mkdir -m 707 /tmp/test

# umask 022

# echo 1234 > /tmp/test/a.txt


$ ls -ld /tmp/test

drwxr-xrwx 82 root root 4096 Oct 23 19:11 /tmp/test

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

-rw-r--r-- 1 root root 1595 Oct 20 11:39 /tmp/test/a.txt

$ cd /tmp/test

$ cp a.txt b.txt

$ rm -f a.txt

$ mv b.txt a.txt

$ echo hi >> a.txt


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



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

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


# apachectl stop

# yum -y install httpd

# /etc/init.d/httpd start

# 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


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


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

# su - apache

$ cd /var/www/html

$ 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 - apache

$ cd /var/www/html

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

-- index.html --

hahaha!!!

-- 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> chmod 를 이용한 파일의 권한을 변경해보자.


-- LAB 순서 --

1. symbolic mode 로 퍼미션 변경

2. octal mode 로 퍼미션 변경

-- LAB 순서 --


1. symbolic mode 로 퍼미션 변경

- /bin/mv 명령어를 자신의 디렉토리에 복사한 후 symbolic mode 로 퍼미션을 변경해보자.

# cp /bin/mv .

# ls -l mv

-rwxr-xr-x 1 root root 78252 Nov 18 16:12 mv

# chmod -c u-r mv

mode of `mv' changed to 0355 (-wxr-xr-x)

# chmod -c o-r mv

mode of `mv' changed to 0351 (-wxr-x--x)

# chmod a-x mv -c

mode of `mv' changed to 0240 (-w-r-----)



2. octal mode 로 퍼미션 변경

- 복사된 /bin/mv 명령어의 퍼미션을 octal mode 로 변경해보자.

# cp /bin/mv .

# chmod -c 000 mv 

mode of `mv' changed to 0000 (---------)

# chmod -c 700 mv

mode of `mv' changed to 0700 (rwx------)

# chmod -c 711 mv

mode of `mv' changed to 0711 (rwx--x--x)

# chmod -v 711 mv

mode of `mv' retained as 0711 (rwx--x--x)

~ # chmod -c 711 mv

# chmod -c 710 mv

mode of `mv' changed to 0710 (rwx--x---)

# chmod 1777 mv

# chmod -v 1777 mv

mode of `mv' retained as 1777 (rwxrwxrwt)

# chmod -c 2755 mv

mode of `mv' changed to 2755 (rwxr-sr-x)

~ # chmod -c 4755 mv

mode of `mv' changed to 4755 (rwsr-xr-x)


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


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

LAB> /tmp 디렉토리 테스트 


참고 : sticky-bit(w권한포함) 권한이 있는 디렉토리는 누구나 파일을 생성할 수 

있지만 파일을 삭제할 때는 자기가 만든 파일만 삭제할 수 있다.

남이 생성한 파일에 대해서는 삭제할 수 없다.


리눅스에서 sticky-bit 가 걸려있는 디렉토리를 검색한다.

# find / -perm -1000 -ls 2> /dev/null 1> /dev/null

# find / -perm -1000 -ls 2> /dev/null 1> /dev/null

# find / -perm -1000 -ls 2> /dev/null 1>&2

# find / -perm -1000 -ls 1> /dev/null 2>&1

# find / -perm -1000 -ls 1> /dev/null 2> /dev/null

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


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

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


[root@localhost ~]# su - user1

[user1@localhost ~]$ cd /tmp


[root@localhost ~]# su - linux

[linux@localhost ~]$ cd /tmp


[user1@localhost tmp]$ touch user1.txt

[user1@localhost tmp]$ ls -l user1.txt 

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


[linux@localhost tmp]$ touch linux.txt

[linux@localhost tmp]$ ls -l linux.txt 

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


[user1@localhost tmp]$ rm -f linux.txt 

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


[linux@localhost tmp]$ rm -f user1.txt 

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


[user1@localhost tmp]$ rm -fv user1.txt 

removed `user1.txt'

[user1@localhost tmp]$ ls -l user1.txt

ls: user1.txt: No such file or directory


[linux@localhost tmp]$ rm -f linux.txt 

[linux@localhost tmp]$ ls -l linux.txt

ls: linux.txt: No such file or directory


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


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

LAB> Set-UID 테스트1

일반유저가 실행파일을 실행시 자신의 권한으로 프로그램을 실행하게 되는데

Set-uid 가 걸린 실행파일을 실행할 때는 자신의 권한이 아니라 실행파일의 소유자의

권한으로 실행하게 된다.


좋은 예가 일반유저가 자신의 비밀번호를 저장하는 경우이다. 

이 경우 일반유저는 /etc/shadow 파일에 저장할 수 있는 권한이 없지만

/usr/bin/passwd 파일이 set-uid 권한이 있기 때문에 /etc/shadow 파일에

자신의 비밀번호를 저장할 수 있는 것이다.


UID, GID, EUID, EUGID



[root@localhost html]# ls -l /usr/bin/passwd 

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

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

-r-------- 1 root root 2441  3월 17 19:09 /etc/shadow

[root@localhost html]# useradd testpasswd 

[root@localhost html]# tail -1 /etc/shadow

testpasswd:!!:16511:0:99999:7:::

[root@localhost html]# echo 1234 | passwd --stdtn testpasswd

passwd: bad argument --stdtn: unknown option

[root@localhost html]# echo 1234 | passwd --stdin testpasswd

Changing password for user testpasswd.

passwd: all authentication tokens updated successfully.

[root@localhost html]# tail -1 /etc/shadow

testpasswd:$6$WS8.iV/N$8ZFwt5......:16511:0:99999:7:::

[root@localhost html]# su - testpasswd

[testpasswd@localhost ~]$ passwd

Changing password for user testpasswd.

Changing password for testpasswd

(current) UNIX password: 

New UNIX password: 

Retype new UNIX password: 

passwd: all authentication tokens updated successfully.

[testpasswd@localhost ~]$ exit

[root@localhost html]# tail -1 /etc/shadow

testpasswd:$6$ozop05dF$tJTxLFZ9JCN...:16511:0:99999:7:::

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


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

LAB> Set-UID 테스트2


# cd /tmp

# vi setuidtest.c

-- setuidtest.c --

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>


int main()

{

  int uid, gid, euid, egid;


  // EUID : Set-UID 가 걸린 UID 

  // GUID : Set-GID 가 걸린 GID

  // set-uid, set-gid 가 설정이 되어있지 않다면 EUID, EGID 에는 자신의 

  // UID 와 GID 값이 들어간다.


  uid = getuid();    // 현재 프로세스를 실행한 사용자의 UID 를 구한다.

  gid = getgid();    // 현재 프로세스를 실행한 사용자의 GID 를 구한다.

  euid = geteuid();  // 현재 프로세스에 설정된 UID(EUID) 를 구한다.

  egid = getegid();  // 현재 프로세스에 설정된 GUID(EGID) 를 구한다.


  printf("uid  = %d \n" 

         "gid  = %d \n" 

         "euid = %d \n" 

         "egid = %d \n", uid, gid, euid, egid);


  return 0;

}

-- setuidtest.c --


-- 실행결과 --

# gcc -o setuidtest setuidtest.c 

# ./setuidtest


# su - user1

$ id

uid=9010(user1) gid=9010(user1) groups=9010(user1)

$ /tmp/setuidtest 

uid  = 9010 

gid  = 9010 

euid = 9010 

egid = 9010 

$ exit


# chmod u+s setuidtest

# ls -l setuidtest

-rwsr-xr-x 1 root root 5394  3월 17 20:51 setuidtest

# su - user1

$ /tmp/setuidtest 

uid  = 9010 

gid  = 9010 

euid = 0 

egid = 9010 

$ exit


# chmod g+s setuidtest

# ls -l setuidtest

-rwsr-sr-x 1 root root 5394  3월 17 20:51 setuidtest

# su - user1

$ /tmp/setuidtest 

uid  = 9010 

gid  = 9010 

euid = 0 

egid = 0 

-- 실행결과 --

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

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

LAB> Set-UID 테스트3


조건 : 전체 시스템에서 set-uid 가 걸린 것을 모두 찾는데 에러는 출력하지 말아라


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

su 

passwd

 :

 :


이 랩은 일반 유저가 cat 을 이용해서 /etc/shadow 파일을 보는 내용이다.

일반적으로 /bin/cat의 실행파일은 일반유저가 실행할 수는 있지만 

/etc/shadow 파일의 읽기 권한이 없기 때문에 /etc/shadow 파일을 볼 수 없다.

하지만 /bin/cat 의 권한을 set-uid 로 설정하면 /etc/shadow 파일을 볼 수 있다.


!!! /etc/shadow 파일을 보기위한 조건 : 

!!! 1. 권한이 set-uid 가 있어야 한다.

!!! 2. 실행파일(cat)의 소유자가 root 이어야 한다.



# ls -ld /

drwxr-xr-x 25 root root 4096 Mar 17 15:52 /

# 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 24 02:40 etc

# ls -l /etc/shadow

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


$ cat /etc/shadowcat: 

/etc/shadow: Permission denied


# chmod u+s /bin/cat -c

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


$ cat /etc/shadow

  :

  :

$ exit


# chown user2 /bin/cat

# ls -l /bin/cat

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

# chmod u+s /bin/cat

# ls -l /bin/cat

-rwsr-xr-x 1 user2 root 23260 Jul 22  2011 /bin/ca

# su - user1

$ cat /etc/shadow

cat: /etc/shadow: Permission denied

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

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

LAB> SetGID 테스트


- 일반 파일

[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


- 디렉토리


- 디렉토리에 SetGID 가 없을 경우 

- 소유자가 속해있는 그룹으로 파일이 생성된다.

[root@localhost ~]# mkdir -m 777 /tmp/test

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

drwxrwxrwx 2 root root 1024 Oct 24 03:42 /tmp/test


[user1@localhost ~]$ cd /tmp/test

[user1@localhost test]$ touch a.txt

[user1@localhost test]$ ls -l a.txt 

-rw-rw-r-- 1 user1 user1 0 Oct 24 03:43 a.txt

                   ~~~~~


- 디렉토리에 SetGID 가 있을 경우 

- 디렉토리에 설정된 그룹으로 파일이 생성된다.

[root@localhost ~]# chmod g+s /tmp/test

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

drwxrwsrwx 2 root root 1024 Oct 24 03:43 /tmp/test

     ~~

[user1@localhost test]$ touch b.txt

[user1@localhost test]$ ls -l b.txt 

-rw-rw-r-- 1 user1 root 0 Oct 24 03:44 b.txt

                   ~~~~

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


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

LAB> 권한 테스트하기


421421421

---------  0000 

r--rws--t  3471

--s--s--x  6111


rwxrwxrwt  1777

rwSr-x--T  5650

-wS-w----  4220

--S-----x  4001

--srw-rwx  4167

r-S-wS-w-  6422

rw---s--t  3611

rw---S--T  3600

rw------T  1600

r----x--t  1411

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


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

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


- SUID 가 들어있는 파일들을 모두 검색한다.

# 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


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


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

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> 읽기, 쓰기 권한 연습하기


- 읽기권한 연습 ( read 권한을 제거 )

# cd /tmp


/tmp # echo "Hello World" > a.txt

/tmp # ls -l a.txt 

-rw-r--r-- 1 root root 12 Feb 17 22:05 a.txt

/tmp # cat a.txt 

Hello World


/tmp # echo "Hello World" > b.txt

/tmp # ls -l b.txt 

-rw-r--r-- 1 root root 12 Feb 17 22:05 b.txt

/tmp # cat b.txt 

Hello World


/tmp # chmod -c o-r a.txt

mode of `a.txt' changed to 0640 (rw-r-----)

/tmp # chmod -c o-r a.txt


- 읽기권한 연습 ( read 권한을 제거한 a.txt 파일은 일반유저가 읽을 수 없다. )

/tmp # useradd linux

/tmp # su - linux

~ $ cd /tmp 

/tmp $ cat b.txt 

Hello World

/tmp $ cat a.txt 

cat: a.txt: Permission denied


- 쓰기권한 연습 ( 일반유저가 write 권한이 없을때 )

/tmp # echo "Have a nice day" >> a.txt   <-- a.txt 에 내용을 추가한다.

/tmp # cat a.txt 

Hello World

Have a nice day

/tmp # chmod -c o+r a.txt

mode of `a.txt' changed to 0644 (rw-r--r--)


/tmp $ cat a.txt 

Hello World

Have a nice day


/tmp $ echo "^^*" >> a.txt       <-- a.txt 에 내용을 추가했지만 권한이 없어서 추가가 안된다.

-bash: a.txt: Permission denied

/tmp $ cat a.txt                 <-- a.txt를 확인하면 추가가 안된 것을 알 수 있다.

Hello World

Have a nice day


- 쓰기권한 연습 ( 일반유저가 write 권한이 있을때 )

/tmp # ls -l b.txt 

-rw-r--r-- 1 root root 28 Feb 17 22:29 a.txt

/tmp # chmod -c o+w b.txt

mode of `b.txt' changed to 0646 (rw-r--rw-)


/tmp $ cat b.txt 

Hello World


/tmp $ echo "I have a smartphone" >> b.txt   <-- b.txt 에 문자열이 추가되었다.

/tmp $ cat b.txt 

Hello World

I have a smartphone

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


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

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

# 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


o /dev/sda 를 변경해서 보는 방법

# 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


o set-uid 를 변경해서 보는 방법

# chmod u+s /sbin/fdisk 

# ls -l /sbin/fdisk 

-rwsr-xr-x 1 root root 96276 Feb 23  2012 /sbin/fdisk

# su - user1


$ /sbin/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          13      104391   83  Linux

/dev/sda2              14        1044     8281507+  8e  Linux LVM

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


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

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


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> Set-GID 테스트 (장치파일의 쓰기권한)


# tty

/dev/pts/0

# 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

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


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

LAB> 디렉토리 권한 테스트


[root@localhost ~]# cd

[root@localhost ~]# pwd

/root

[root@localhost ~]# ls -ld

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

[root@localhost ~]# chmod o-r .

[root@localhost ~]# ls -ld

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

[root@localhost ~]# chmod o+r .

[root@localhost ~]# ls -ld

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

[root@localhost ~]# 


[user1@localhost ~]$ pwd

/home/user1

[user1@localhost ~]$ cd /root

-bash: cd: /root: Permission denied

[user1@localhost ~]$ ls -l /root

total 0

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

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

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

  :

  :




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


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

LAB> 디렉토리 권한 테스트


~ # cd /tmp

/tmp # umask

0022

/tmp # mkdir TEST

/tmp # ls -ld TEST

drwxr-xr-x 2 root root 4096 Feb 18 00:55 TEST

/tmp $ cd TEST              <-- x 권한이 있으므로 TEST 디렉토리에 들어갈 수 있다.

/tmp/TEST $ pwd

/tmp/TEST

/tmp/TEST $ cd ..



/tmp # touch TEST/a.txt TEST/b.txt

/tmp # ls -l TEST

total 8

-rw-r--r-- 1 root root 0 Feb 18 00:59 a.txt

-rw-r--r-- 1 root root 0 Feb 18 00:59 b.txt

/tmp # chmod o-x TEST

/tmp # ls -ld TEST

drwxr-xr-- 2 root root 4096 Feb 18 00:55 TEST

/tmp $ cd TEST              <-- x 권한이 있으므로 TEST 디렉토리에 들어갈 수 없다.

-bash: cd: TEST: Permission denied

tmp $ ls TEST -l

total 0

?--------- ? ? ? ?            ? a.txt

?--------- ? ? ? ?            ? b.txt


/tmp # chmod o=x TEST

/tmp # ls -ld TEST

drwxr-x--x 2 root root 4096 Feb 18 00:59 TEST

/tmp $ cd TEST               <-- 디렉토리에 들어갈 수 있으나 파일 목록은 볼 수 없다.

/tmp/TEST $ ls

ls: .: Permission denied


/tmp # chmod o=rwx TEST

/tmp # ls -ld TEST

drwxr-xrwx 2 root root 4096 Feb 18 00:59 TEST

/tmp/TEST $ vi a.txt       <-- a.txt 파일을 수정할 수 있다.

/tmp/TEST $ ll

total 12

-rw-r--r-- 1 linux linux 8 Feb 18 01:05 a.txt

-rw-r--r-- 1 root  root  0 Feb 18 00:59 b.txt

/tmp/TEST $ rm -f b.txt    <-- other 에 w 권한이 있으므로 b.txt 파일을 삭제할 수 있다.

/tmp/TEST $ ls -l

total 16

-rw-r--r-- 1 linux linux 8 Feb 18 01:05 a.txt

-rw-r--r-- 1 linux linux 8 Feb 18 01:06 c.txt

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


===================================== 여기까지 ===================================== 


실습> 시스템에서 Set-UID / Set-GID 파일을 몽땅 검색해보자.


# find / -perm -4000 -ls -perm -2000 

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



-------



o 퍼미션 변경 연습 

# rsync -av /bin/cut .

# ls -l cut

   chmod   ln chown  chgrp vi, echo  vi, echo, touch  mv

 ~~~~~~~~~  ~ ~~~~   ~~~~  ~~~~~    ~~~~~~~~~~~~     ~~~

-rwxr-xr-x  1 root   root  31880    Jul 22  2011     cut

 ~~~

  u ~~~

     g ~~~

        o


o chmod 에서 8진수 모드와 권한의 관계


8진수에서 가질 수 있는 값 : 0 ~ 7




# chmod u-wx,g-rw,o-r cut -c

rwxrwxrwx  -> r----x-wx  변경   chmod 413 

~~~           ~~~

 7  7  7      4  ~~~

                  1 ~~~

                     3


-wx-wx-wx -> --x------

1. chmod a=,u+x 

2. chmod go=,u-w 

3. chmod og=,u-w 

4. chmod go-wx,u-w

...


5. chmod 100


Symbolic mode 에서 가질 수 있는 문자들 : ugoa=+-,rwxst


---------  -> rwxr-x--x   chmod  751

chmod 000 

---------  -> --x--x---   chmod  110


LAB> 실행 디렉토리의 권한들을 살펴보세요!!! 

/bin , /usr/bin, /sbin, /usr/sbin


설치시 : 755 -> 보안상 권한을 변경하는 것이 좋다.


# ls -l /usr/sbin | more   <-- more 를 이용해서 살펴보기

# ls -l /sbin | less       <-- less 를 이용해서 살펴보기



확장된 권한들  (Set-UID (4), Set-GID (2), StickyBit (1) )

Set-UID : rwsr-xr-x

          ~~~

          root 

             ~~~

             root

                                linux 실행

Set-GID : 

---------  -> rwsr-xr-x   4755 -> rws--s--t  7711 -> rws-----x 4701

~~~

 u ~~~

    g ~~~

       o


# ls -ld /tmp

# ls -l /usr/bin/passwd

# ls -l /usr/bin/wall

# ls -l /bin/ping

# ls -ld /dev/shm


o Set-UID 이해하기

Set-UID 권한을 가지고 있는 파일을 일반유저가 실행하면 자신의(일반유저) 

권한으로 프로그램을 실행하는 것이 아니고 소유자의 권한으로 프로그램을 

실행하게 된다.

# ls -l /etc/shadow

-r-------- 1 root root 1218 May 30 10:32 /etc/shadow

# ls -l /usr/bin/passwd  <-- 일반유저가 실행하면 관리자권한이 되는 것이다.

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

# useradd linux

# echo "1234" | passwd --stdin linux

Changing password for user linux.

passwd: all authentication tokens updated successfully.

# grep linux /etc/shadow

linux:$1$3/Z1I5Jz$irsRWkoqK2pSqRY9cmHjQ1:15490:0:99999:7:::

# 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

# grep linux /etc/shadow <-- shadow 파일을 변경할 수 있는 것은 root 만이 가능

linux:$1$IT9znGxh$fgvJlTJbKONZCd6/lc1yZ0:15490:0:99999:7:::  <-- 변경되었음.



파일 / 디렉토리 

# ls -ld /tmp

#

#

drwxrwxrwx  -> a.txt(A) b.txt(B)  a.txt,b.txt 삭제(c) 삭제 O


drwxrwxrwt  -> a.txt(A) b.txt(B)  a.txt,b.txt 삭제(c) 삭제 X

         ~ -> x + t




   

# cd ; ls -ld

drwxr-x----   .... root

# ls -l /home

drwx-------   ... a

drwx-------   ... b

drwx-------   ... c

drwx-------   ... d


리눅스에서 실행할 수 있는 파일 

1. 컴파일해서 나온 바이너리 파일 (/bin/ls, mv ...)

2. 텍스트 형태의 쉘스크립트파일  (/etc/init.d/crond)


o 디렉토리의 x 권한 테스트


# mkdir -m 700 /tmp/TESTDIR

# ls -ld /tmp/TESTDIR

drwx------ 2 root root 4096 May 30 13:30 /tmp/TESTDIR


$ cd /tmp/TESTDIR    <-- x 권한이 없기 때문에 

-bash: cd: TESTDIR: Permission denied


o 실행파일 x 권한 테스트 

# ls -l /bin/cat

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

# ls -ln /bin/cat

-rwxr-xr-x 1 0 0 20776 Jul 22  2011 /bin/cat


$ cat /etc/profile

# /etc/profile

# System wide environment and startup programs, for login setup

# Functions and aliases go in /etc/bashrc

...


# chmod -c o= /bin/cat

mode of `/bin/cat' changed to 0750 (rwxr-x---)

# ls -l /etc/profile

-rw-r--r-- 1 root root 1344 Feb 23 05:31 /etc/profile


$ cat /etc/profile   <-- cat x 권한이 없으므로 파일의 내용을 볼 수 없다.

-bash: /bin/cat: Permission denied



o 읽기권한(r) 테스트  


1. 일반 파일 테스트 

# chmod o+rx /bin/cat

# ls -l /etc/shadow

-r--------


o cat 은 실행권한이 있지만 /etc/shadow 파일에 읽기권한이 

없으므로 파일의 내용을 볼 수 없다.

$ cat /etc/shadow   

  ~~~ ~~~~~~~~~~~

   1        2

1번의 권한을 살펴보고 권한이 있으면 2번의 권한을 살펴본다.


2. 디렉토리 테스트

$ cd /bin

$ ls      <-- /bin 디렉토리에 파일 목록이 출력.


# ls -ld /bin  <-- drwxr-xr-x -> drwxr-x--x (r권한 제거)

# chmod o-r /bin


$ cd /bin

$ ls      <-- r 권한을 제거했으므로 /bin 디렉토리에 파일 목록이 출력이 안됨.



o 쓰기권한(w) 테스트

1. 일반 파일을 테스트 


$ ls -l /etc/shadow  <-- Other 부분의 쓰기권한을 살펴볼 것

$ echo "1234" >> /etc/shadow  <-- 쓰기권한이 없으므로 안된다.


2. 디렉토리 테스트

$ ls -ld /home  <-- Other 부분의 쓰기권한을 살펴볼 것

drwxr-xr-x

 ~~~~~~

root.root

    

$ mkdir /home/Test <-- 쓰기권한이 없으므로 안된다.

$ cd  <-- 자신의 홈디렉토리로 이동 

$ mkdir Test  <-- 쓰기 권한이 있으므로 만들어진다.

drwx------

 ~~~

 자신의 권한

$ pwd

$ ls -ld 

$ ls -ld /etc  <-- Other 부분의 Write 권한을 확인 

drwxr-xr-x

 ~~~~~~

root.root 

$ rm -f /etc/passwd <-- 삭제가 안됨.


일반 유저가 저장(삭제)할 수 있는 디렉토리는 

리눅스가 설치되면 기본적으로 아래 디렉토리이다.

1. 자신의 디렉토리 

2. /tmp 디렉토리

3. /dev/shm  --> 보안상 Other 권한의 쓰기를 제거

4. /var/tmp  --> 보안상 Other 권한의 쓰기를 제거


o 제일 많이 사용하는 권한 

1. 파일 

-rw-r--r--

-rw-------

-rw-r----- 

-rw----r-- 

-r--------    :w <-- x    :w! <-- O


2. 디렉토리 

drwxr-xr-x    # ls -l /

drwx------    # ls -l /home   , # ls -ld /root

drwx-----x    drwxr-xr-x 차이점?

drwx--x---

drwx--x--x


# ls -ld /etc

# chmod o-r /etc

# ls -ld /etc


$ cd /etc

$ ls   <-- 읽기 권한이 없다.

$ ls -l passwd  <-- 파일의 속성을 확인할 수 있다.

$ cat passwd <-- 파일을 볼 수 있다.



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


[root@www ~]# groupadd -g 5000 project

[root@www ~]# groupadd -g 5000 project

groupadd: GID 5000 is not unique

[root@www ~]# groupadd -g 6000 project

[root@www ~]# useradd -g project user1

useradd: user user1 exists

[root@www ~]# userdel -r user1

[root@www ~]# userdel -r user2

[root@www ~]# userdel -r user3

userdel: /var/spool/mail/user3 not owned by user3, not removing

[root@www ~]# useradd -g project user1

[root@www ~]# useradd -G project user2

[root@www ~]# tail -2 /etc/passwd

user1:x:5001:6000::/home/user1:/bin/bash

user2:x:5002:5002::/home/user2:/bin/bash

[root@www ~]# tail -2 /etc/group

project:x:6000:user2

user2:x:5002:

[root@www ~]# install -m 070 -g project -d /project 

[root@www ~]# ls -ld /project

d---rwx--- 2 root project 4096 May 16 19:30 /project

[root@www ~]# su - user1

[user1@www ~]$ cd /project/

[user1@www project]$ id

uid=5001(user1) gid=6000(project) groups=6000(project)

[user1@www project]$ touch user1.txt

[user1@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:30 user1.txt

[user1@www project]$ rm -f ~/.bash_logout 

[user1@www project]$ exit

logout

[root@www ~]# su - user2

[user2@www ~]$ cd /project

[user2@www project]$ id

uid=5002(user2) gid=5002(user2) groups=5002(user2),6000(project)

[user2@www project]$ touch user2.txt

[user2@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:30 user1.txt

-rw-rw-r-- 1 user2 user2   0 May 16 19:31 user2.txt

[user2@www project]$ rm -f ~/.bash_logout 

[user2@www project]$ exit

logout

[root@www ~]# chmod g+s                   /project

[root@www ~]# ls -ld /project

d---rws--- 2 root project 4096 May 16 19:31 /project

[root@www ~]# 

[root@www ~]# 

[root@www ~]# 

[root@www ~]# 

[root@www ~]# su - user1

[user1@www ~]$ cd /project/

[user1@www project]$ touch user1-2.txt

[user1@www project]$ ls l

ls: l: No such file or directory

[user1@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:32 user1-2.txt

-rw-r--r-- 1 user1 project 0 May 16 19:30 user1.txt

-rw-rw-r-- 1 user2 user2   0 May 16 19:31 user2.txt

[user1@www project]$ exit

logout

[root@www ~]# su - user2

[user2@www ~]$ cd /project

[user2@www project]$ touch user2-2.txt

[user2@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:32 user1-2.txt

-rw-r--r-- 1 user1 project 0 May 16 19:30 user1.txt

-rw-rw-r-- 1 user2 project 0 May 16 19:33 user2-2.txt

-rw-rw-r-- 1 user2 user2   0 May 16 19:31 user2.txt

[user2@www project]$ rm -f *

[user2@www project]$ ls

[user2@www project]$ exit

logout

[root@www ~]# su - user1

[user1@www ~]$ cd /project/

[user1@www project]$ touch user1.txt

[user1@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:34 user1.txt

[user1@www project]$ exit

logout

[root@www ~]# su - user2

[user2@www ~]$ cd /project/

[user2@www project]$ touch user2.txt

[user2@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:34 user1.txt

-rw-rw-r-- 1 user2 project 0 May 16 19:34 user2.txt

[user2@www project]$ exit

logout

[root@www ~]# chmod o+t /project -c

mode of `/project' changed to 3070 (---rws--T)

[root@www ~]# su - user2

[user2@www ~]$ cd         /project

[user2@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:34 user1.txt

-rw-rw-r-- 1 user2 project 0 May 16 19:34 user2.txt

[user2@www project]$ ls -ld 

d---rws--T 2 root project 4096 May 16 19:34 .

[user2@www project]$ rm -f *

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

[user2@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:34 user1.txt

[user2@www project]$ 

[user2@www project]$ 

[user2@www project]$ 

[user2@www project]$ exit

logout

[root@www ~]# chmod 2070 /project/ -c

mode of `/project/' changed to 2070 (---rws---)

[root@www ~]# su - user1

[user1@www ~]$ cd /project/

[user1@www project]$ ls

user1.txt

[user1@www project]$ rm -f *

[user1@www project]$ touch 1.txt

[user1@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:38 1.txt

[user1@www project]$ exit

logout

[root@www ~]# su - user2

[user2@www ~]$ cd /project/

[user2@www project]$ touch 2.txt

[user2@www project]$ ls -l

total 0

-rw-r--r-- 1 user1 project 0 May 16 19:38 1.txt

-rw-rw-r-- 1 user2 project 0 May 16 19:38 2.txt

[user2@www project]$ rm -f *

[user2@www project]$ ls -l

total 0

[user2@www project]$ 


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

LAB> SetUID 테스트 (/etc/shadow, /usr/bin/passwd 를 이해하자)

# touch file.c file.txt

# chmod 600 file.txt 

# vi file.c 

-- file.c --

#include <stdio.h>   // printf , fprintf, 

#include <stdlib.h>  // exit


int main()

{

  FILE *fp;

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

  if(fp == NULL)

  {

    fprintf(stderr,"파일을 열지 못했네용.\n");

    exit(1);

  }

  

  fprintf(fp, "Hello\n");

  fclose(fp);


  return 0;

}

-- file.c --


# gcc -o file file.c 

# ./file 

# cat file.txt 

Hello

# su user1

$ id

uid=9010(user1) gid=9010(user1) groups=9010(user1)

$ cat file.txt 

cat: file.txt: 허가 거부됨

$ ls -l file.txt 

-rw------- 1 root root 6  3월 17 23:33 file.txt

$ ./file 

파일을 열지 못했네용.

$ exit

exit

# cat file.txt 

Hello



# chmod u+s file -c

`file'의 모드를 4755(rwsr-xr-x)으로 변경하였습니다

# su user1

$ id

uid=9010(user1) gid=9010(user1) groups=9010(user1)


$ ./file 

$ ./file 

$ ./file 

$ exit

exit


# cat file.txt 

Hello

Hello

Hello

Hello


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

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

LAB>  /etc/shadow 파일에 일반 유저가 저장할 수 있는 이유는 무엇인가 ????


# ls -l /usr/bin/passwd 

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

# ls -l /etc/shadow

-r-------- 1 root root 2599  3월 17 23:07 /etc/shadow

# echo 1234 | passwd --stdin user1

# grep ^user1: /etc/shadow

user1:$6$M9cg4Pii$bCvrCaJE28A6d ...(생략)


# su - user1

$ passwd

Changing password for user user1.

Changing password for user1

(current) UNIX password:   <-- 1234

New UNIX password:         <-- 0000

Retype new UNIX password:  <-- 0000

passwd: all authentication tokens updated successfully.

$ exit


# grep ^user1: /etc/shadow

user1:$6$2uOUVUuA$xsXzjrtJlAOLtBh8

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


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

LAB> vhost_alias 모듈을 사용하여 가상호스트를 사용할 때 퍼미션 문제로 발생할 수 있는 

     문제점을 알아보고 대응을 해보자.


도메인명 : sbs.com, kbs.com, mbc.com


1. 웹서버 패키지 설치

- 패키지가 없다면 설치한다.

# yum -y install httpd


2. 사용자를 생성

- 가상호스트를 서비스하는 사용자를 생성한다.

# mkdir /etc/skel/public_html

# useradd sbsuser

# useradd kbsuser

# useradd mbcuser

# passwd sbsuser

# passwd kbsuser

# passwd mbcuser


# chmod 755 ~sbsuser

# chmod 755 ~kbsuser

# chmod 755 ~mbcuser


# echo "Welcome to sbs.com" > ~sbsuser/public_html/index.html

# echo "Welcome to kbs.com" > ~kbsuser/public_html/index.html

# echo "Welcome to mbc.com" > ~mbcuser/public_html/index.html

# chown sbsuser.sbsuser ~sbsuser/public_html/index.html:q:

# chown kbsuser.kbsuser ~kbsuser/public_html/index.html

# chown mbcuser.mbcuser ~mbcuser/public_html/index.html


3. 가상호스트(이름기반) 설정

- 웹서버의 가상호스틀 설정한다.

# cd /etc/httpd/conf

# vi httpd.conf

-- httpd.conf --

  :

LoadModule vhost_alias_module modules/mod_vhost_alias.so

  :

NameVirtualHost *:80


<VirtualHost *:80>

    DocumentRoot  /home/sbsuser/public_html 

    ServerName    sbs.com

    ServerAlias   www.sbs.com

</VirtualHost>

<VirtualHost *:80>

    DocumentRoot  /home/kbsuser/public_html 

    ServerName    kbs.com

    ServerAlias   www.kbs.com

</VirtualHost>

<VirtualHost *:80>

    DocumentRoot  /home/mbcuser/public_html 

    ServerName    mbc.com

    ServerAlias   www.mbc.com

</VirtualHost>

-- httpd.conf --


4. 아파치서버 시작

# /etc/init.d/httpd configtest

Syntax OK.

# /etc/init.d/httpd start

# netstat -nltp


5. DNS 설정

# vi /etc/hosts

-- /etc/hosts --

  :

  :

127.0.0.1   sbs.com www.sbs.com

127.0.0.1   kbs.com www.kbs.com

127.0.0.1   mbc.com www.mbc.com

-- /etc/hosts --


6. 웹서버 접속 테스트

# lynx --dump sbs.com


   Welcome to sbs.com


# lynx --dump kbs.com


   Welcome to kbs.com


# lynx --dump mbc.com


   Welcome to mbc.com

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


o 사례1)

웹사용자만 접근시키고 소유자를 뺀 모든 사용자가 각 홈디렉토리에 

접근하지 못하게 막는다. 


- 웹페이지 접근 사용자

apache

sbsuser

kbsuser

mbcuser


- 각 사용자 디렉토리 제한 사용자

sbsuser : kbsuser mbcuser (x)

kbsuser : sbsuser mbcuser (x)

mbcuser : kbsuser sbsuser (x)


-

# ls -ld /home/{sbs,kbs,mbc}user

drwxr-xr-x 3 kbsuser kbsuser 4096 Mar 18 01:47 /home/kbsuser

drwxr-xr-x 3 mbcuser mbcuser 4096 Mar 18 01:47 /home/mbcuser

drwxr-xr-x 3 sbsuser sbsuser 4096 Mar 18 02:21 /home/sbsuser


# chmod o= /home/{sbs,kbs,mbc}user

# ls -ld /home/{sbs,kbs,mbc}user

drwxr-x--- 3 kbsuser kbsuser 4096 Mar 18 01:47 /home/kbsuser

drwxr-x--- 3 mbcuser mbcuser 4096 Mar 18 01:47 /home/mbcuser

drwxr-x--- 3 sbsuser sbsuser 4096 Mar 18 02:21 /home/sbsuser


# lynx --dump sbs.com


                                   Forbidden


   You don't have permission to access / on this server.

     _________________________________________________________________



    Apache/2.2.3 (CentOS) Server at sbs.com Port 80


# grep apache /etc/passwd

apache:x:48:48:Apache:/var/www:/sbin/nologin

# grep 48 /etc/group

apache:x:48:


# chgrp apache /home/{sbs,kbs,mbc}user

# ls -ld /home/{sbs,kbs,mbc}user

drwxr-x--- 3 kbsuser apache 4096 Mar 18 01:47 /home/kbsuser

drwxr-x--- 3 mbcuser apache 4096 Mar 18 01:47 /home/mbcuser

drwxr-x--- 3 sbsuser apache 4096 Mar 18 02:21 /home/sbsuser


# lynx --dump sbs.com


   Welcome to sbs.com


# lynx --dump kbs.com


   Welcome to kbs.com


# lynx --dump mbc.com


   Welcome to mbc.com


# su - sbsuser

$ cd ~kbsuser

-bash: cd: /home/kbsuser: Permission denied

$ cd ~mbcuser

-bash: cd: /home/mbcuser: Permission denied


o 사례2)

일반 사용자를 특정 그룹에 포함시키고 홈디렉토리가 운영되는 디렉토리에 그 그룹이 

못들어오게 막는다. 


- 원래 권한으로 돌리고 시작한다.

# chmod 755 /home/{sbs,kbs,mbc}user

# chgrp sbsuser /home/sbsuser 

# chgrp kbsuser /home/kbsuser

# chgrp mbcuser /home/mbcuser

# ls -ld /home/{sbs,kbs,mbc}user

drwxr-xr-x 3 kbsuser kbsuser 4096 Mar 18 01:47 /home/kbsuser

drwxr-xr-x 3 mbcuser mbcuser 4096 Mar 18 01:47 /home/mbcuser

drwxr-xr-x 3 sbsuser sbsuser 4096 Mar 18 02:21 /home/sbsuser



- 사용자를 포함할 그룹을 생성한다.

# groupadd homepageuser

# tail -1 /etc/group

homepageuser:x:9018:


- 사용자의 그룹을 확인한다.

# su - sbsuser

$ id

uid=9015(sbsuser) gid=9015(sbsuser) groups=9015(sbsuser)

$ exit


# su - kbsuser

$ id

uid=9016(kbsuser) gid=9016(kbsuser) groups=9016(kbsuser)

$ exit


# su - mbcuser

$ id

uid=9017(mbcuser) gid=9017(mbcuser) groups=9017(mbcuser)

$ exit


# usermod -G homepageuser sbsuser

# usermod -G homepageuser kbsuser

# usermod -G homepageuser mbcuser


# su - sbsuser

$ id

uid=9015(sbsuser) gid=9015(sbsuser) groups=9015(sbsuser),9018(homepageuser)

$ exit


# su - kbsuser

$ id

uid=9016(kbsuser) gid=9016(kbsuser) groups=9016(kbsuser),9018(homepageuser)

$ exit


# su - mbcuser

$ id

uid=9017(mbcuser) gid=9017(mbcuser) groups=9017(mbcuser),9018(homepageuser)

$ exit


- 각 디렉토리의 권한을 변경한다.

# ls -ld /home/{sbs,kbs,mbc}user

drwxr-xr-x 3 kbsuser kbsuser 4096 Mar 18 03:10 /home/kbsuser

drwxr-xr-x 3 mbcuser mbcuser 4096 Mar 18 03:11 /home/mbcuser

drwxr-xr-x 3 sbsuser sbsuser 4096 Mar 18 02:21 /home/sbsuser


# chgrp homepageuser /home/{sbs,kbs,mbc}user

# ls -ld /home/{sbs,kbs,mbc}user

drwxr-xr-x 3 kbsuser homepageuser 4096 Mar 18 03:10 /home/kbsuser

drwxr-xr-x 3 mbcuser homepageuser 4096 Mar 18 03:11 /home/mbcuser

drwxr-xr-x 3 sbsuser homepageuser 4096 Mar 18 02:21 /home/sbsuser


# chmod g= /home/{sbs,kbs,mbc}user

# ls -ld /home/{sbs,kbs,mbc}user

drwx---r-x 3 kbsuser homepageuser 4096 Mar 18 03:10 /home/kbsuser

drwx---r-x 3 mbcuser homepageuser 4096 Mar 18 03:11 /home/mbcuser

drwx---r-x 3 sbsuser homepageuser 4096 Mar 18 02:21 /home/sbsuser


- 홈페이지 접근 테스트

# lynx --dump sbs.com


   Welcome to sbs.com


# lynx --dump kbs.com


   Welcome to kbs.com


# lynx --dump mbc.com


   Welcome to mbc.com


- 각 사용자 디렉토리 접근 테스트

# su - sbsuser

$ id

uid=9015(sbsuser) gid=9015(sbsuser) groups=9015(sbsuser),9018(homepageuser)

$ cd /home/kbsuser

-bash: cd: /home/kbsuser: Permission denied

$ cd /home/mbcuser

-bash: cd: /home/mbcuser: Permission denied

$ exit


# su - kbsuser

$ cd ~sbsuser

-bash: cd: /home/sbsuser: Permission denied

$ cd ~mbcuser

-bash: cd: /home/mbcuser: Permission denied

$ exit


# su - mbcuser

$ LANG=C

$ cd ~sbsuser

-bash: cd: /home/sbsuser: Permission denied

$ cd ~kbsuser

-bash: cd: /home/kbsuser: Permission denied

$ exit


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

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

LAB> Set-UID 권한 테스트


# ls -l /usr/bin/passwd 

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

# ls -l /etc/shadow

-r-------- 1 root root 2599  3월 17 23:07 /etc/shadow

# echo 1234 | passwd --stdin user1

# grep ^user1: /etc/shadow

user1:$6$M9cg4Pii$bCvrCaJE28A6d ...(생략)


# su - user1

# su - user1

$ passwd

Changing password for user user1.

Changing password for user1

(current) UNIX password:   <-- 1234

New UNIX password:         <-- 0000

Retype new UNIX password:  <-- 0000

passwd: all authentication tokens updated successfully.

$ exit


# grep ^user1: /etc/shadow

user1:$6$2uOUVUuA$xsXzjrtJlAOLtBh8

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



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

LAB> Linux Box 의 Set-UID 를 제거하자.


# mkdir bin; cd bin

# install -m 755 /dev/null change_perm.sh

# vi change_perm.sh

-- change_perm.sh --

#!/bin/sh


#filelist=`find / -perm -4000   2>/dev/null`

filelist=$(find / -perm -4000   2>/dev/null)


for i in $filelist

do

   if [ $i = "/bin/su" ]

   then

     continue

   fi  

   # 원래의 권한을 백업한다.

   echo $i >> /root/bin/change_perm.txt


   # set-uid, set-gid 권한을 제거한다.

   chmod -c ug-s $i

done


-- change_perm.sh --



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



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

LAB> 


$ cat /etc/passwd

 :

 :


1. alias 명령어, 내부명령어, 함수, 외부명령어를 찾는다.


2. 외부명령어 PATH 변수에서 찾는다. 

$ echo $PATH

/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/user1/bin


3. /bin 디렉토리에 x 권한이 있는가를 검사한다.

- /bin 디렉토리에 들어갈 수 없어서 프로그램이 종료된 것이다.

$ ls -ld /bin

drwxr-xr-x 2 root root 4096 Sep  4 23:55 /bin


# chmod o= /bin

# ls -ld /bin

drwxr-x--- 2 root root 4096 Sep  4 23:55 /bin

$ cat /etc/passwd

-bash: /bin/cat: Permission denied


# chmod o=rx /bin


4. cat 파일의 x 권한을 검사한다. 

- /bin/cat 파일을 실행할 수 없어서 프로그램이 종료된 것이다.

$ ls -l /bin/cat

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


# ls -ld /bin

drwxr-xr-x 2 root root 4096 Sep  4 23:55 /bin

# ls -l /bin/cat

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

# chmod o= /bin/cat -c

mode of `/bin/cat' changed to 0750 (rwxr-x---)


$ cat /etc/passwd

-bash: /bin/cat: Permission denied


# chmod o=rx /bin/cat


5. /etc 디렉토에 x 권한이 있는가를 검사한다.

- /etc 디렉토리에 들어갈 수 없어서 프로그램이 종료된 것이다.


cat /etc/passwd

cat: /etc/passwd: Permission denied

$ ls -ld /etc

drwxr-xr-x 81 root root 4096 Sep  5 13:00 /etc


- 권한을 변경해보자.

# chmod -c o= /etc 

mode of `/etc' changed to 0750 (rwxr-x---)


- 실행시 에러 발생

$ cat /etc/passwd

cat: /etc/passwd: Permission denied


- 원래대로 복구한다.

# chmod o=rx /etc


6. passwd 파일의 r 권한이 있는가를 검사한다.

- /etc/passwd 파일의 읽기 권한이 없어서 프로그램이 종료된 것이다.


$ ls -l /etc/passwd

-rw-r--r-- 1 root abc 1707 Sep  5 09:32 /etc/passwd


# chmod o-r /etc/passwd -c

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


$ cat /etc/passwd

cat: /etc/passwd: Permission denied


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



'OS > [Linux] CentOS' 카테고리의 다른 글

[CentOS] 9. 환경변수 테스트  (0) 2016.07.10
[CentOS] 8. 권한연습  (0) 2016.07.10
[CentOS] 중간 정리  (0) 2016.07.06
[CentOS] 7-1. 리눅스퍼미션  (0) 2016.07.02
[CentOS] 6. 파일 권한 테스트  (0) 2016.07.02

+ Recent posts