2012. 7. 18. 15:43

네트워크 패킷 캡쳐 방법 비교

리눅스에서 네트워크 상의 패킷을 캡쳐하기 위한 방법은 몇개가 있습니다


리눅스가 그렇듯 CLI 에서의 패킷 캡쳐가 더 쉽고 편합니다. 

물론 바로 보고 마우스로 클릭하는 것도 좋긴 하지만 GUI 접속이 되지 않는 원격지 서버의 경우에는 CLI 명령어가 

필요한 경우가 더 많습니다.


tcpdump  (CLI)

ethereal (GUI) -> tethereal (CLI)

wireshark (GUI) -> tshark (CLI ) 


ethereal 은 최신 리눅스에서는 wireshark 로 이름이 바뀌었습니다.


중요한 것은 패킷을 잡고 저장을 하는 방법을 우선 알아야 할 것 입니다. 


힘들게 man 페이지 까지를 읽지는 않더라도 --help 로 옵션 정보는 봐주셔야 하겠죠? 

[root@localhost ~]# tcpdump --help

tcpdump version 3.9.4

libpcap version 0.9.4

Usage: tcpdump [-aAdDeflLnNOpqRStuUvxX] [-c count] [ -C file_size ]

                [ -E algo:secret ] [ -F file ] [ -i interface ] [ -M secret ]

                [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]

                [ -W filecount ] [ -y datalinktype ] [ -Z user ]

                [ expression ]


(1) tcpdump 로 패킷 캡쳐하고 저장하기


tcpdump -i eth0 -w test.cap


- i 옵션은 물리적 인터페이스를 지정하기 위한 옵션입니다.

    만약 모든 인터페이스에서 패킷을 잡기를 원한다면 any 로 지정하면 됩니다.


- w 옵션은 잡은 패킷을 특정 파일로 저장을 하는 옵션입니다.

       파일명은 임의로 가능하며 pcap 이나 cap 확장자로 저장하시면 됩니다.


가끔 리눅스에서 PATH 를 잡아주지 않아서 명령어를 못 찾는 경우에는 which tcpdump 명령어로 

절대경로로 tcpdump 명령어가 어디에 있는지 확인하고 패킷을 잡아주세요

중지하는 것은 crtl +c 로 중지하면 됩니다.


[root@localhost ~]# which tcpdump

/usr/sbin/tcpdump

[root@localhost ~]# /usr/sbin/tcpdump -i eth0 -w test.cap

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

3 packets captured

3 packets received by filter

0 packets dropped by kernel

[root@localhost ~]# 


(2) tethereal 로 패킷 캡쳐하고 저장하기

[root@localhost ~]# tethereal -i eth0 -w test2.cap

Running as user "root" and group "root". This could be dangerous.

Capturing on eth0

[root@localhost ~]#


위의 tcpdump 명령어와 옵션은 유사합니다.


(3) tshark 로 패킷 캡쳐하고 저장하기

[root@localhost ~]# tshark -i eth0 -w test3.cap

Running as user "root" and group "root". This could be dangerous.

Capturing on eth0

[root@localhost ~]# 


솔라리스에서는 기본적으로 snoop 이란 명령어를 통해서 패킷 캡쳐가 가능합니다.

snoop 은 위에서 언급한 패킷캡쳐 명령어들과 다른 옵션을 가지고 있습니다. 

물론 기능적으로는 같지요 패킷 캡쳐 + 저장을 하는 거니깐요


(4) 솔라리스 에서 snoop 으로 패킷 캡쳐하고 저장하기

snoop -d ce0 -o test.cap


- d 옵션은 물리적 인터페이스를 선택 

- o  옵션은 캡쳐를 파일로 저장 


[root@localhost:/ ] snoop --help

snoop: illegal option -- help


Usage:  snoop

        [ -a ]                  # Listen to packets on audio

        [ -d device ]           # Listen on interface named device

        [ -s snaplen ]          # Truncate packets

        [ -c count ]            # Quit after count packets

        [ -P ]                  # Turn OFF promiscuous mode

        [ -D ]                  # Report dropped packets

        [ -S ]                  # Report packet size

        [ -i file ]             # Read previously captured packets

        [ -o file ]             # Capture packets in file

        [ -n file ]             # Load addr-to-name table from file

        [ -N ]                  # Create addr-to-name table

        [ -t  r|a|d ]           # Time: Relative, Absolute or Delta

        [ -v ]                  # Verbose packet display

        [ -V ]                  # Show all summary lines

        [ -p first[,last] ]     # Select packet(s) to display

        [ -x offset[,length] ]  # Hex dump from offset for length

        [ -C ]                  # Print packet filter code

        [ -q ]                  # Suppress printing packet count

        [ -r ]                  # Do not resolve address to name


        [ filter expression ]


Example:

        snoop -o saved  host fred


        snoop -i saved -tr -v -p19

[root@localhost:/ ]