달력

5

« 2024/5 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2008. 11. 12. 13:58

LSOF 설치 및 사용가이드 Enjoy/etc2008. 11. 12. 13:58


출처 : http://unix.co.kr/bbs/board.php?bo_table=02_06&wr_id=6


LSOF 설치 및 사용가이드

2001. 6.30

공재순/ kong@certcc.or.kr

1. 소개

LSOF는 'List Open File'의 약자로, 해당 System에서 구동되고 있는 프로세스들에 의해서 열려진 파일들을 확인 할 수 있는 툴이다. 시스템의 의심스러운 프로세스에 대한 확인이 용이하고, 설치가 비교적 쉬워 많이 이용되고 있다.

[cert:root]:/user/kong/lsof_4.56> lsof
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sched 0 root cwd VDIR 32,0 1024 2 /
sched 0 root 0u VCHR 12,2 0t0 140375 /devices/pseudo/sad@0:user(COMMON)
.....
.....
sendmail 23403 root 13u inet 0x603ca5b8 0t2290 TCP cert:52969->211.45.162.90:smtp (ESTABLISHED)
sendmail 23403 root 14u inet 0x603ca5b8 0t2290 TCP cert:52969->211.45.162.90:smtp (ESTABLISHED)
......

[그림 1] LSOF 실행 예

Top

2. LSOF 다운로드 받기

최신 버전은 ftp://vic.cc.purdue.edu/pub/tools/unix/lsof 에서 확인 할 수 있으며, 여러 개의 Mirror site 가 존재해 쉽게 다운로드 받을 수 있다.

Mirror site URL :

* ftp://ftp.cerias.purdue.edu/pub/tools/unix/sysutiles/lsof
* ftp://ftp.cert.dfu.du/pub/tools/admin/lsof
* ftp://ftp.cetis.hvu.nl/pub/lsof
* ftp://ftp.crc.doc.ca/packages/

Version 4 LSOF 는 gzip으로 압축되어 있으며, 파일명에 revision number를 포함하고 있다.

ftp://vic.cc.purdue.edu/pub/tools/unix/lsof_<rev>_W.tar.gz
또는 ftp://vic.cc.purdue.edu/pub/tools/unix/lsof_<rev>_W.tar.Z

※ 리눅스 시스템의 경우에는, 대부분 기본적으로 LSOF가 설치되어 있다. (만약 설치가 안되어 있는 상태라면 rpm 패키지로 배포하고 있으므로 linux 배포사이트를 이용하면 될 것이다.)

Top

3. 압축해제

이 문서에서는 ver.4(revision 4.56) LSOF를 Solaris OS에서 설치하는 과정을 보이도록 한다.

gzip -d lsof_4.56_W.tar.gz
tar -xvf lsof_4.56_W.tar

※ 참고로 4.56 버전의 lsof.tar 파일의 md5 checksum 값은 다음과 같으며, MD5 (lsof_4.56.tar) = 92155bb6430d14d044f8ca96858e63b8

md5 checksum tool은 아래 사이트에서 구할 수 있다.
ftp://ftp.cerias.purdue.edu/pub/tools/unix/crypto/md5

Top

4. 설치하기

LSOF 설치과정은 크게 압축해제 -> Inventory -> Configure -> Install 4단계로 볼 수 있다.

① 압축 풀기

우선 초기 Wrapper에 쌓인 파일 압축을 풀면 해당 디렉토리 내에 다음과 같은 5개 의 파일이 생성되고, 이와 함께 lsof_4.56 디렉토리가 생성된다.

[cert:root]:/user/kong> tar -xvf lsof_4.56_W.tar
x RELEASE.SUMMARY_4.56, 12803 bytes, 26 tape blocks
x README.lsof_4.56, 4129 bytes, 9 tape blocks
x 00.README.FIRST_4.56, 700 bytes, 2 tape blocks
x lsof_4.56.tar, 3317760 bytes, 6480 tape blocks
x lsof_4.56.tar.asc, 284 bytes, 1 tape blocks

생성되는 파일

설명

00.README.FIRST_4.56

Readme file for the distribution

README.lsof_4.56

Contains distribution and security information

RELEASE.SUMMARY_4.56

Summary of the lsof 4.56 distribution

lsof_4.56.tar

LSOF 4.56 tar archive

lsof_4.56.tar.asc

Detached PGP certificate for lsof_4.56.tar

[표 1] lsof_4.56_W.tar 압축 해지 후 생성되는 파일들

Top

※ 생성된 lsof_4.56 디렉토리에 생성되는 주요 파일들

[sparc5:root]:/user/kong/lsof_4.56> ls
./ 00DIST 00README arg.c lsof_fields.h proto.h
../ 00FAQ 00XCONFIG dialects/ main.c regex.h
00.README.FIRST 00LSOF-L AFSConfig* lib/ misc.c scripts/
00CREDITS 00MANIFEST Configure* lsof.8 node.c store.c
00DCACHE 00PORTING Customize* lsof.h print.c usage.c
00DIALECTS 00QUICKSTART Inventory* lsof.man proc.c version

② Inventory

LSOF를 설치 할때 절대적으로 필요한 과정은 아니다. 그러나 subdirectory들을 체크하고, 각 스크립트들과 파일 헤더들, 소스파일들이 실제로 존재하는지 등 패키지 상태를 체크 해 볼 필요가 있다. 다음과 같이 Inventory 스크립트를 실행하게 되면, inventory 작업에 대한 소개 가 나오고 실행할 것인지 여부를 물어오는데 'y' 로 답하면서 진행하게 되면 디렉토리들의 list 를 확인할 수 있다.

[cert:root]:/user/kong/lsof_4.56> ./Inventory
Conducting an inventory of the lsof distribution; this will take a while.
Examining /user/kong/lsof_4.56: OK
Examining dialects: OK
....
....
Examining lib: OK
Examining scripts: OK
This lsof distribution seems to be complete.

Top

③ Configure

환경설정에 필요한 여러 단계의 작업을 하게 되는데, 아래와 같이 해당 OS에 맞 게 선택하고 Configure 스크립트를 구동하면 된다.

※Configure 스크립트 구동방법

Configure <options> <target-dialect>
<options>: -clean : clean up previous configuration
-d|-dialects : display a list of supported dialect versions
-h|-help : display help information
-n : avoid AFS, customization, and inventory checks

aix | aixgcc : IBM AIX xlc (aix)
or gcc (aixgcc)
bsdi : BSDI BSD/OS
darwin : Apple Darwin
decosf : DEC OSF/1
digital_unix|du : Digital UNIX
freebsd : FreeBSD
hpux|hpuxgcc : HP-UX cc (hpux)
or gcc (hpuxgcc)
linux : Linux
netbsd : NetBSD

nextstep|next|ns|nxt : NEXTSTEP
openbsd : OpenBSD
openstep|os : OpenStep
osr | sco : SCO OpenServer,
SCO devloper's compiler
osrgcc|scogcc : SCO OpenServer,gcc compiler
ptx : Sequent PTX
solaris | solariscc : Solaris gcc (solaris)
or cc(solariscc)
tru64 : Tru64 UNIX
unixware | uw : SCO UnixWare

[표 2] Configure 스크립트 구동시 OS 지정

Top

[sparc5:root]:/user/kong/lsof_4.56> ./Configure solaris
Testing prdata.h for PR_GWINDOWS, using gcc
Testing prdata.h for PR_LDT, using gcc
Testing vnode.h for VSOCK, using gcc
.....

각 헤더파일 testing이 끝나면, 다시 inventory 해 볼 것인지를 물어보는데 file tree들을 변화시킬 것이 없다면 inventory과정을 re-run할 필요없다.

....
....
Do you want to take inventory (y|n) [y]? n
rm -f ddev.c dfile.c dlsof.h dmnt.c dnode*.c dproc.c dproto.h dsock.c dstore.c kernelbase.h machine.h machine.h.old new_machine.h __lseek.s Makefile
ln -s dialects/sun/ddev.c ddev.c
ln -s dialects/sun/dfile.c dfile.c
ln -s dialects/sun/dlsof.h dlsof.h
.....
ln -s dialects/sun/machine.h machine.h
kernelbase.h assembled.
Makefile and lib/Makefile created.

Top

위 과정에서 Make 파일이 생성됨을 볼 수 있으며, 이어 LSOF를 customize 할 것인지를 물어오는데 [yes/no] question을 따라 다음의 각 옵션을 사용자가 선택할 수 있다.

Name

Description

Default Value

HASSECURITY

enabled : root 만이 lsof를 이용해 열려진 파일들을 확인 할 수 있음.
disabled :누구든 모든 열려진 파일들을 lsof를 이 용해 확인 할 수 있음.

Disabled

WARNINGSTATE

enabled : lsof가 필요할 경우 언제든지 warning messages를 출력함.
disabled : warning messages를 출력하지 않음. -w (disables) +w (enables) 로 조절 가능함.

Enabled

WARNDEVACCESS

enabled : lsof가 /dev (또는 /devices)를 억세스 할 수 없을 때, warning messages를 출력함.
disabled : lsof가 /dev (또는 /devices)를 억세스 할 수 없을 때, skip 해 버림.

Enabled

HASDCACHE

enabled : lsof가 /dev (또는 /devices)의 파일들에 관한 정보를 포함하고 있는
device cache file을 생성함.

< define path options >

HASENVDC, HASPERSDC,
HASPERSDCPATH, HASSYSDC
disabled : lsof 가 device cache file 생성하지 않음.

Enabled

HASENVDC

HASPERSDC

HASPERSDCPATH

HASDCACHE를 정의하면, device cache file path를 기술하기 위해 연관된
다른 옵션들의 구성도 정의해 주어야 한다.

< 현재 설정된 path options >

#define HASENVDC "LSOFDEVCACHE"
#define HASPERSDC "%h/%p.lsof_%L"
#define HASPERSDCPATH "LSOFPERSDCPATH"
※ 자세한 사항은 00DCACHE ,00FAQ 참고

"LSOFDEVCACHE"

"%h/%p.lsof_%L"

"LSOFPERSDCPATH"

HASKERNIDCK

lsof가 현재의 커널상태와 최초 lsof가 설치되어 구동되었을 당시의 커널과 비교하여,
시스템에서 부적절하게 실행되는 것을 탐지할 수 있다.

kernel identity check로 인해 일부 UNIX
(예:AIX) 시스템에서 구동시간이 많이 걸리는 경우가 있어서 disable 시켜야 할 때가 있을 수도 있지만,
lsof를 부적절하게 구동함으로써 오는 위험요소가 증가할 수 있다는 것을 배제할 수 없다.

Enabled

[표 3] Customize 옵션

Top

위의 각 항목들 및 설정값들은 LSOF가 기본적으로 제공하는 것으로 변경을 원할 경우에는 machine.h 파일을 직접 수정하거나 Customize 스크립트를 직접 구동하여 변경할 수도 있다.

④ build & install

Configure 과정을 마치고 나면, 시스템에 인스톨 할 수 있다. 기본적인 install 과 정은 아래와 같다.

■ build LSOF system

[cert:root]:/user/kong/lsof_4.56> make
(cd lib; make DEBUG="-O" CFGF="-Dsolaris=20600 -DHASPR_GWINDOWS -DHAS_VSOCK -DLSOF_VSTR=\"5.6\"")
gcc -Dsolaris=20600 -DHASPR_GWINDOWS -DHAS_VSOCK -DLSOF_VSTR="5.6" -O -c ckkv.c
.....
.....
gcc -o lsof -Dsolaris=20600 -DHASPR_GWINDOWS -DHAS_VSOCK -DLSOF_VSTR=\"5.6\" -O ddev.o dfile.o dmnt.o dnode.o dnode1.o dnode2.o dproc.o dsock.o dstore.o arg.o main.o misc.o node.o print.o proc.o store.o usage.o -L./lib -llsof -lkvm -lelf -lsocket -lnsl

Top

■ Install LSOF system

[cert:root]:/user/kong/lsof_4.56> make install
(cd lib; make DEBUG="-O" CFGF="-Dsolaris=20600 -DHASPR_GWINDOWS -DHAS_VSOCK -DLSOF_VSTR=\"5.6\"")
Constructing version.h
gcc -Dsolaris=20600 -DHASPR_GWINDOWS -DHAS_VSOCK -DLSOF_VSTR=\"5.6\" -O -c usage.c
gcc -o lsof -Dsolaris=20600 -DHASPR_GWINDOWS -DHAS_VSOCK -DLSOF_VSTR=\"5.6\" -O ddev.o dfile.o dmnt.o dnode.o dnode1.o dnode2.o dproc.o dsock.o dstore.o arg.o main.o misc.o node.o print.o proc.o store.o usage.o -L./lib -llsof -lkvm -lelf -lsocket -lnsl

여기까지 과정을 마치면 일반적인 설치는 모두 끝나고 lsof 실행파일이 생성된다.

※ 별도로 install ruleset 지정하고자 할 경우

가상메모리의 커널 image를 담고있는 파일인 /dev/kmem(또는 /dev/mem)를 read 할 수 있도록 LSOF는 setgid 로 설치되어야 하는데, 일반적으로 다음과 같은 ruleset 형태로 지정한다.

SunOS install rule actions :
install <options> -m 2755 -g kmem lsof <bin_dest>
install <options> -m 444 lsof.8 <man_dest>
Solaris install rule actions :

install -[cf] <bin_dest> <options> -m 2755 -g sys lsof
install -[cf] <man_dest> <options> -m 444 lsof.8

Top

5. LSOF 사용하기

■ LSOF 옵션

옵션

기 능

옵션

기 능

-?
-h

list help

-a

AND selections (OR)

-d
-D

s select by FD set
D ?|i|b|r|u[path]

+|-f

-files +filesys

-l

list UID numbers

-n
-N

no host names
select NFS files

-s

list file size

-t
-T

terse listing
disable TCP/TPI info

-v
-V

display version info
verbose search

-F [f]

select fields;-F? for help

-o o

o 0t offset digits (8)

-S [t]

t second stat timeout(15)

-i i

select by IPv4 address: [proto][@host|addr][:svc_list|port_list]

+|-r [t]

repeat every t seconds (15);
+ until no files, - forever

-b

avoid kernel blocks

-c c

list command c

-P

no port names

-i

select IPv4 files

-p s

select by PID set

-C

no kernel name cache

+|-w

Warnings (+)

-R

list paRent PID

-k k

kernelsymbols (/dev/ksyms)

-U

select Unix socket

-u s

exclude(^)/select login/UID s

-m m

kernel memory (/dev/kmem)

+|-M

portMap registration (-)

--

end option scan

-g [s]

select by process group ID set and print process group IDs

names

select named files or files on named file systems

Top

■ 실행결과 보기

실행결과에 해당되는 각 column 들을 간단하게 살펴보면 다음 [표5] 와 같다.

Column

설 명

Command

프로세스와 관련된 Unix command 이름

PID
PPID
PGRP

Process IDentification number

Parent Process IDentification number
(해당 프로세스의 부모 프로세스 ID)

Process Group IDentification number
(해당 프로세스와 관련된 프로세스 그룹 ID)

USER

해당 프로세스를 소유한 사용자 ID 또는 login name

FD

File Descriptor number
(ex) cwd : current working directory
r : read access / w : write access / u : read and write access

TYPE

해당 파일과 관련한 노드 타입
(ex) inet : Internet domain socket

DEVICE

device number

SIZE
SIZE/OFF
OFFSET

file 이나 file offset의 사이즈

INODE
NODE-ID

local file 의 node number 또는 Internet protocol type
또는 서버 호스트의 NFS file의 inode number

NAME

해당 파일이 소속된 mount point나 파일 시스템의 이름

[표 6] lsof 실행결과의 각 Column

Top

■ LSOF 주요 옵션 사용 예

* 특정 파일을 억세스하고 있는 프로세스 확인 : lsof <path/file-name>

[cert:root]:/> lsof /etc/passwd
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
ns-httpd 244 root 9r VREG 32,0 3044 99217 /etc/passwd
....

* internet socket 확인 : lsof -i

Internet address 출력형식 : [protocol][@hostname|hostaddr][:service|port]

Top

- 특정 호스트(또는 ip)에 대한 접속 확인

[cert:root]:/> lsof -i@172.16.2.146
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
in.telnet 10124 root 0u inet 0x61d4b788 0t71 TCP cert:telnet->172.16.2.146:1109 (ESTABLISHED)
in.telnet 10124 root 1u inet 0x61d4b788 0t71 TCP cert:telnet->172.16.2.146:1109 (ESTABLISHED)
in.telnet 10124 root 2u inet 0x61d4b788 0t71 TCP cert:telnet->172.16.2.146:1109 (ESTABLISHED)
.....

- 특정 포트로 접속한 리스트 확인

[cert:root]:/usr/sbin> lsof -i @certcc.or.kr:23
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
in.telnet 104 root 2u inet 0x612df850 0t152 TCP cert:telnet->172.16.2.159:1176 (ESTABLISHED)
in.telnet 28462 root 0u inet 0x61aee578 0t71 TCP cert:telnet->172.16.2.146:4250 (ESTABLISHED)
.....

Top

* 특정 user가 오픈한 프로세스를 확인 : lsof -u <loginname> 혹은 lsof -u <UID>

[cert:root]:/> lsof -u kong
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
csh 21309 kong cwd VDIR 32,4 1536 223602 /user/kong/lsof_4.56
csh 21309 kong txt VREG 32,6 158608 298136 /usr/bin/csh
.......

- 특정사용자 제외시 "^" 심볼을 사용하고, 여러명을 동시에 지정하려면 ","를 이용한다.

[cert:root]:/dev/pts> lsof -u ^root,kong,yjkim
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
csh 8992 yjkim cwd VDIR 32,0 1024 2 /
csh 8992 yjkim txt VREG 32,6 158608 298136 /usr/bin/csh
csh 8992 yjkim txt VREG 32,6 70996 136979 /usr/lib/locale/ko/ko.so.1
csh 8992 yjkim txt VREG 32,6 1024888 6749 /usr/lib/libc.so.1
.....
csh 21309 kong cwd VDIR 32,4 1536 223602 /user/kong/lsof_4.56
csh 21309 kong txt VREG 32,6 158608 298136 /usr/bin/csh
....

Top

* 특정 프로세스가 오픈한 파일 리스트 확인 : lsof -p <PID>

- PID 112를 가진 프로세스가 사용하는 파일들 확인

[cert:root]:/usr/sbin> lsof -p 143
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
inetd 143 root cwd VDIR 32,0 1024 2 /
inetd 143 root txt VREG 32,6 33492 310933 /usr/sbin/inetd
inetd 143 root txt VREG 32,6 10696 6352 /usr (/dev/dsk/c0t0d0s6)
....
....
inetd 143 root 4u inet 0x603cab38 0t0 TCP *:ftp (LISTEN)
inetd 143 root 5u inet 0x610b13c0 0t0 TCP *:telnet (LISTEN)
inetd 143 root 6u inet 0x610b1240 0t0 TCP *:pop3 (LISTEN)
......

Top

* ps 명령이 변조된 시스템에서는 의심스러운 포트 확인하기 예

[root@linux /root]# netstat -a
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:auth *:* LISTEN
tcp 0 0 *:ftp *:* LISTEN
tcp 0 0 *:telnet *:* LISTEN
tcp 0 0 *:login *:* LISTEN
tcp 0 0 *:2626 *:* LISTEN
.......
[root@linux /dev]# fuser -n tcp 2626
2626/tcp: 607
[root@linux /dev]# lsof -p 607
PID TTY STAT TIME COMMAND
607 ? S 0:00 /usr/sbin/mingetty

※ 참고자료

1. lsof 4.56 README file and man page

2. Installing, configuring and using lsof 4.50 to list open files on systems running Solaris 2.x
http://www.cert.org/security-improvement/implementations/i042.05.html

:
Posted by 라면스프