'chmod'에 해당되는 글 1건

  1. 2008.07.21 Chapter 5. Unix Basic Commands _ 파일 속성 정보 변경 명령어
2008. 7. 21. 01:22

Chapter 5. Unix Basic Commands _ 파일 속성 정보 변경 명령어

■ 파일의 속성 정보 변경(File Attribution Chaging)

chown CMD

chgrp CMD

chmod CMD



# ls -l filename

-rw-r--r-- 1 user01 staff size mtime filename

Filetype :

Mode/Perm : chmod

Link Count : ln

Ownership : chown

Groupship : chgrp

Filesize :

mtime : touch

filename : mv



chown CMD

__________________

change file ownership // 소유권 변경

The chown utility will set the user ID of the file named by

each file to the user ID specified by owner, and, option-

ally, will set the group ID to that specified by group.

If chown is invoked by other than the super-user, the set-

user-ID bit is cleared.

Only the owner of a file (or the super-user) may change the

owner of that file.


# ls -l filename

-rw-r--r-- 1 user01 staff ..... filename


(
명령어 형식)

# chown user02 filename (# chown UID filename)

# chown user02:other filename (# chown user02.other filename: X)

# chown -R user02 dir01

// -R recursive : 하위로~부터


[EX] chown
명령어 실습


(
실습 준비)

# cd /test

# rm -r *

# touch file1

# ls -l file1

-rw-r--r-- 1 root other ..... file1

(chown user01 filename 형식 실습)

# chown user01 file1

# ls -l

(chown user01:staff filename 형식 실습)

# chown user02:staff file1

# ls -l


[EX] chown -R
명령어 실습


(
실습 준비)

# cd /test

# mkdir dir1

# mkdir -p dir1/dir2/dir3

# touch dir1/file1

# touch dir1/file2

(chown -R user01:other dir 형식 실습)

# chown -R user01:other dir1

# ls -lR


chgrp CMD

__________________

change file group ownership // 파일의 소유 그룹권을 변경

The chgrp utility will set the group ID of the file named by

each file operand to the group ID specified by the group

operand.


# ls -l filename

-rw-r--r-- 1 user01 staff ..... filename


(
명령어 형식)

# chgrp other filename (# chgrp GID filename)


[EX] chgrp
명령어 실습

# cd /test

# ls -l file1

-rw-r--r-- 1 user02 staff ..... file1

# chgrp other file1

# ls -l


chmod CMD

__________________

change the permissions mode of a file // 파일의 권한을 변경

The chmod utility changes or assigns the mode of a file. The

mode of a file specifies its permissions and other attri-

butes. The mode may be absolute or symbolic.


# ls -l filename

-rw-r--r-- 1 user01 staff ..... filename

- : file type (-, d, l)

rw- : owner perm.

r-- : group perm.

r-- : other perm.

1 : link count(hard link)

user01 : ownership

staff : groupship

size : bytes

mtime : modify time



[
참고] rw-r--r-- : 퍼미션 모드(Permission Mode)


퍼미션 변경 방법

■ 심볼릭 모드 방법(Symbolic Mode)

■ 옥탈 모드 방법(Octal Mode, Absolute Mode)


(1)
심볼 모드 방법(Symbolic Mode)

심볼들의 정의

-> u(user), g(group), o(other), a(all)

-> +(plus), -(minors), =(equal)

-> r(read), w(write), x(eXcution)

chmod u + r filename

g - w

o = x // other 권한을 기존의 값과 상관없이 x만으로 셋팅이 된다.

a


[EX]
심볼 모드를 사용한 퍼미션 변경 실습

# touch file1

# ls -l file1

-rw-r--r-- 1 root other 0 mtime file1

# chmod u+x file1

# chmod g-r file1

# chmod a=rwx file1

# chmod u-r,g+x file1


(2)
옥탈 모드 방법(Octal Mode)

rwx : 7

rw- : 6

r-x : 5

r-- : 4

-wx : 3

-w- : 2

--x : 1

--- : 0

chmod 755 filename (rwxr-xr-x)


[EX]
옥탈모드(Octal Mode)를 사용한 퍼미션 변경 방법 실습

# touch file1

# ls -l file1

-rw-r--r-- 1 root other 0 mtime file1

# chmod 744 file1

# chmod 754 file1


[
참고] chmod -R (Recusive)

chmod 명령어에 -R 옵션을 사용하는 경우는 잘 사용되지 않는 경우이며 반드시 사용해야 하

는 경우 ls 명령어의 -lR 옵션을 통해서 백업을 받아 두어야 한다.


(
특정 디렉토리 하위의 모든 퍼미션 변경의 경우)

# ls -lR > perm.txt

# chmod -R 755 dir01


(
특정 디렉토리 하위의 특정 파일 퍼미션 변경의 경우)

/test/dir01

- file1

- file2

- dir02 - file3

- dir03

file* => 640(rw-r-----)

# cd /test

# find /test/dir01 -type f -exec chmod 640 {} \;

# find /test/dir01 -type f name “*.log” -exec chmod 640 {} \;


[
참고] File & Directory permission 의미

___________________________________________________________________________________

file r read 파일의 read권한은 파일안을 볼 수 있거나 복사할 수 있음

w write write 권한은 파일안의 내용을 추가 하거나 삭제, 변경 가능

x execution execution 권한은 파일을 실행할 수 있다는 의미

Dir. r ls read 권한은 디렉토리 하위 내용을 ls 할 수 있음

w add, delete write 권한은 디렉토리 하위의 내용을 삭제, 추가, 변경 가능

x cd execution 권한은 디렉토리 하위로 cd 할 수 있음

___________________________________________________________________________________


[EX]
파일 퍼미션 실습

반드시 user01, user02 사용자가 존재해야 한다.


[TERM1] user01
사용자 윈도우

# telnet localhost

user01 사용자로 로그인

$ touch file1

$ ls -l

-rw-r--r-- 1 user01 staff 0 mtime file1


[TERM2] user02
사용자 윈도우

# telnet lcoalhost

user02 사용자로 로그인

$ cd ~user01

$ vi file1

Test for user02

:wq! (파일의 내용을 넣을 수 없다.)

-rw-r--r-- 1 user01 staff 0 mtime file1

                           A       A

                           |        |

         V        |

                          user02 V

                                   user02's group


[EX]
디렉토리 퍼미션 실습


[TERM1] user01
사용자 윈도우

$ mkdir dir1

$ ls -ld dir1 (rwxr-xr-x user01 staff)

$ chmod 775 dir1

$ ls -ld dir1 (rwxrwxr-x user01 staff)

$ touch dir1/file2


[TERM2] user02
사용자 윈도우

$ cd ~user01

$ cd dir1

$ rm file2 (파일을 지울수 있다.)

$ mkdir dir2 (디렉토리를 지울수 있다.)



umask CMD

__________________

get or set the file mode creation mask

# touch file1

# mkdir dir1

# ls -l file1 (644,rw-r--r--)

# ls -ld dir1 (755,rwxr-xr-x)


* 002, 022, 027

* default permission

file : 666 (Max Perm)

dir : 777 (Max Perm)

# umask

# umask 002

# umask

# touch file2

# ls -l file2 (664, rw-rw-r--)

# mkdir dir2

# ls -ld dir2 (775, rwxrwxr-x)

# umask 027

# umask

# touch file3

# mkdir dir3

# ls -l

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

File Dir.

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

666 777

umask 022 022

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

644 755

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

비트의 and 연산 è - 연산과 같음

비트의 or 연산 è 특정비트 mask 할시


[
참고] /etc/profile umask 정의

umask 022



(3).
특수 퍼미션(SetUID, SetGID, Sticky Bits)

SetUID, SetGID 의미(SetUID, SetGID Means)

Sticky Bits 의미(Sticky Bits Means)

■ 특수 퍼미션 설정(Special Permission configuration)



(3-1) SetUID, SetGID
의미(SetUID, SetGID Means)

일반 사용자가 자신의 패스워드를 변경하기 위해 passwd 명령어를 사용할 수 있다. 하지만 passwd 명령어의 원 권한자는 root인데 파일을 실행 하는 동안 setuid를 가짐으로서 root의 역할을 가질 수 있는 것이다.

$ passwd

$ ls -l /etc/passwd (r--r--r-- root sys, 444)

r--r--r--

$ ls -l /etc/shadow (r-------- root sys, 400)

r--------

$ ls -l /usr/bin/passwd

r-sr-sr-x root sys


[user01]

$ passwd

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

[root] /etc/passwd (root, sys)

/etc/shadow

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

$ id


[user01]

$ ls -l /usr/bin/su

r-sr-xr-x root sys


[user01]

$ su -

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

....

ksh(fork) (root)

....

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

$ id

[user01]

ksh (user01)

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

                                      |                                             |

                                      |               ksh(root)               |

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


[
참고] 해킹 기법(Hacking Tech.)

// 임시파일 추적할 때 pfiles 를 이용 프로세스가 생성 시킨 file을 추적할 수 있다.


- Stack Buffer Overflow

- Heap Buffer Overflow

- Race Condition

- IP Spoofing

- etc


[Race Condition]

- SetUID filename

- tmp file make

- Hacker known a tmp filename

$ ./su.sh (user01, SetUID root)

---- su.sh ---------                                       ------ crack.sh ------

         (root)

                                                /tmp/.tmp1

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

                                                                            ------ test.sh -------


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


[Backdoor]

# cp /usr/bin/ksh /tmp

# chmod 4755 /tmp/ksh

(-rwxr-xr-x 1 root other 209268 3 15 10:37 /tmp/ksh*)

(-rwsr-xr-x 1 root other 209268 3 15 10:37 /tmp/ksh*)

# telnet localhost

user01 사용자로 로그인

$ cd /tmp

$ ./ksh

# id -a

- File Name for Backdoor

# touch " " (blank :4)

# touch ". "

# touch /dev/rdsk/c0t0d0p7

# touch /tmp/.Rx2Pailb




(3-2). Sticky Bits
의미(Sticky Bits Means)

Sticky bit는 일반적으로 디렉토리에 주어지는 의미이다. 디렉토리에 sticky bits가 주어지면 그 디렉토리 하위의 모든 파일과 디렉토리들은 다른 사용자가 볼 수 있지만 지우거나 수정을 할 수 없다.

예로 게시판을 들 수 있다.

# ls -ld /tmp

drwxrwxrwt 7 root sys 336 1 14 19:35 /tmp

/export/home/user01 <=======> /tmp

user02

......



(3-3).
특수 퍼미션 설정(Sfecial Permission Configuration)

SetUID : 4 r

SetGID : 2 w

Sticky Bits : 1 x


(a). SetUID
설정 방법

# chmod 4755 filename

rwxr-xr-x => rwsr-xr-x

# chmod 4100 filename(Min Permission) --x------ -> --s------

# chmod 4655 filename(-rwSr-xr-x) rw-r-xr-x -> rwSr-xr-x


(b). SetGID
설정 방법

# chmod 2755 filename

rwxr-xr-x => rwxr-sr-x

# chmod 2010 filename(Min Permission) -----x--- -> -----s---

# chmod 2765 filename(-rwxrwlr-x) rwxrw-r-x -> rwxrwlr-x



[
참고] Solaris(Public Writable Directory)

디렉토리에 관련해서 SetGID를 설정하는 것은 Symbolic Mode 방식으로만 가능하다.

# chmod g+s dirname (# chmod u+s filename)

                                                      g+s

                                                      o+t

======= dirname ========== (rwxrwsr-x) root class1

user01 make -> file1 (user01:class1)

user02 make -> file2 (user02:class1)

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


======== dirname ========== (root class2) rwxrwxr-x

user01 make -> file1 (user01:staff1)

user02 make -> file2 (user02:staff2)

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


# groupadd class2

# usermod -G class2 user01

# usermod -G class2 user02



(d). Sticky Bits
설정 방법

# chmod 1777 dirname

rwxrwxrwx -> rwxrwxrwt

# chmod 1001 dirname(Min Permission) --------x -> --------t

# chmod 1776 dirname(drwxrwxrwT) rwxrwxrw- -> rwxrwxrwT


(e). SetUID / SetGID
파일 검색(SetUID/SetGID Search)

# find / -perm -4000 -type f

# find / -perm -2000 -type f // -2000 : 최소 2000을 만족하는~

# find / ( -perm -4000 -o -perm -2000 ) -type f (올바른 형식 X)

# find / \( -perm -4000 -o -perm -2000 \) -type f (올바른 형식 0)



[
참고] -o : OR, -a : AND


[EX] SetUID / SetGID
관리자의 정책

- 홈디렉토리(/export/home) 하위에는 SetUID, SetGID는 존재하면 안된다.

- 홈디렉토리(/export/home) 하위에 SetUID, SetGID 파일 존재 유무를 정기적으로 찾아서

점검한다.

사용자 홈디렉토리 : /export/home/user01......


(
정책1) SetUID, SetGID 검색하는 경우

# find /export/home \( -perm -4000 -o -perm -2000 \) -type f


(
정책2) SetUID, SetGID 검색하고 삭제하는 경우

# find /export/home -perm -4000 -exec rm {} \;


(
정책3) SetUID, SetGID 생성시키는 사용자 경고 메세지 출력

- 사용자(user01)가 현재 로그인 되어 있는 상태 (who, w, talk 명령)

- 사용자(user01)가 현재 로그인 되어 있지 않은 상태 (/etc/profile)

# cat /etc/warnning.txt

Warnning

# vi /etc/profile

.....

if [ $LOGNAME = user01 ] ; then # # echo $LOGNAME

/bin/cat -s /etc/warnning.txt

fi

.....


[EX] WAS(Web Application Server)

- 상용 : Tmaxsoft JEUS, BEA Web Logic, IBM WebSphere, Oracle WAS, Sun WAS, .....

- 무료 : Jserv, Tomcat, Resin


Webserver (80, 8080) -> root
권한 필요

-> UNIX Port 번호 : Well-known Port(~1024)


Web Daemon(Example : httpd)

PORT 1 ~ 1023 : 반드시 root 권한 필요

PORT 1024 ~ : 일반사용자 권한으로도 가능

httpd (80) : 반드시 root 권한 필요

httpd (8080) : 일반사용자 권한으로도 가능


위의 경우 문제가 생길수 있다. 실제 실무에서는 서버 시스템에 root 사용자와 WAS 서비스

사용자가 구분되어 있는 경우가 많다. 일반사용자인 wasuser로서 WAS 프로그램을 설치하고

운용하기 때문에 데몬을 시작하는 스크립트가 wasuser 사용자의 권한으로 되어 있어서 이

사용자가 웹 데몬을 띄우게 되면 "Permission Denied"가 발생한다.


WAS -> wasuser
사용자

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

OS -> root 사용자


데몬 : httpd

데몬 띄우는 스크립트 : startup (: # service httpd restart)


* wasuser
httpd(80) 데몬을 띄울수 있도록 설정하는 관리자의 설정이 필요

# chown root:sys /was/bin/startup (root sys rwxr-xr-x -> rws-----x)

# chmod 4701 /was/bin/startup (root sys rwx-----x -> rws-----x)

or

# vi startup.c

main()

{

system(/was/bin/startup);

}

# gcc -o startup_C startup.c

# chmod 4701 startup_C

출처 : http://cafe.daum.net/bscsolaris