본문으로 건너뛰기

ngrep 명령어를 CentOS에서 어떻게 활용하나요?

💡 요약 정리

  • ngrep은 네트워크 트래픽 모니터링 도구로, HTTP 요청/응답 데이터까지 확인 가능합니다.
  • CentOS에서는 기본 제공되지 않으므로 EPEL 저장소를 통해 설치해야 합니다.
  • 포트, IP, 특정 문자열 등을 기준으로 세분화된 패킷 분석이 가능합니다.
  • 민감 데이터가 유출될 수 있으므로 사용 시 주의가 필요합니다.

1. 설치 환경

  • CentOS 5.x (64bit)

2. ngrep 명령어 소개

  • ngrep은 tcpdump와 함께 네트워크를 스니핑하는 도구입니다.
  • tcpdump가 패킷 경로와 특성만 추적하는 반면, ngrep은 특정 포트로 접속한 IP가 어떤 파일을 조회하며, 전달되는 데이터를 포함한 패킷 전체 내용을 확인할 수 있어 유용합니다.
  • 잘못 사용될 경우 개인정보 유출 등의 위험이 있으므로, 사용 시 주의가 필요합니다.

3. ngrep 설치 방법

  • CentOS에는 기본적으로 ngrep 패키지가 포함되어 있지 않기 때문에, EPEL(Extra Packages for Enterprise Linux) 저장소를 추가해야 합니다.
  • EPEL은 기본 저장소에 없는 다양한 RPM 패키지를 제공합니다.

설치 절차

① EPEL repository 설치를 위해 epel-release 패키지를 다운로드합니다. (예: CentOS 5.x 기준)

[root@cafe24 ~]# rpm -Uvh epel-release-5-4.noarch.rpm
Preparing...            ########################################### [100%]
   1:epel-release       ########################################### [100%]

② EPEL 등록 여부 확인

[root@cafe24 ~]# ll /etc/yum.repos.d/
total 44
-rw-r--r-- 1 root root 1926 Feb  9  2011 CentOS-Base.repo
-rw-r--r-- 1 root root  631 Feb  9  2011 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root  626 Feb  9  2011 CentOS-Media.repo
-rw-r--r-- 1 root root 4663 Feb  9  2011 CentOS-Vault.repo
-rw-r--r-- 1 root root  954 Aug 12  2010 epel.repo
-rw-r--r-- 1 root root 1054 Aug 12  2010 epel-testing.repo

③ ngrep 패키지 설치

[root@cafe24 ~]# yum install ngrep
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.tt.co.kr
 * epel: ftp.neowiz.com
 * extras: centos.tt.co.kr
 * updates: centos.tt.co.kr
...
Dependencies Resolved

===========================================================================================================
 Package         Arch        Version                 Repository        Size
===========================================================================================================
Installing:
 ngrep           x86_64      1.45-8.1.el5            epel              29 k

Transaction Summary
===========================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 29 k
Is this ok [y/N]: y

...
Complete!
[root@cafe24 ~]#

4. ngrep 활용 예시

① 포트 기준 캡처 (예: TCP 80번 포트 패킷)

[root@cafe24 ~]# ngrep -t port 80
interface: eth0 (1.234.20.0/255.255.255.0)
filter: (ip or ip6) and ( port 80 )
...
GET / HTTP/1.1..Host: x.x.x.x..User-Agent: Mozilla/5.0 ...
...
HTTP/1.1 200 OK..Date: ...Content-Type: text/html...
<html><body><h1>It works!</h1></body></html>

② 디바이스명 기준 패킷 캡처

[root@cafe24 ~]# ngrep -d eth0 -t port 80
interface: eth0 (1.234.20.0/255.255.255.0)
filter: (ip or ip6) and ( port 80 )
...

③ 특정 패턴 문자열이 있는 패킷 캡처

[root@cafe24 ~]# ngrep 'HTTP' -d eth0 -t port 80
...
HTTP/1.1 200 OK..Date: ...Content-Type: text/html...

④ 특정 IP와의 통신 패킷만 캡처 (예: 123.x.x.x)

[root@cafe24 ~]# ngrep host 123.xxx.xxx.xxx and -t port 80
interface: eth0 (1.234.20.0/255.255.255.0)
filter: (ip or ip6) and ( host 123.x.x.x and port 80 )
...

⑤ 사용 가능한 옵션 확인

[root@cafe24 ~]# ngrep --help
ngrep: invalid option -- -
usage: ngrep <-hNXViwqpevxlDtTRM> <-IO pcap_dump> <-n num> <-d dev> <-A num>
             <-s snaplen> <-S limitlen> <-W normal|byline|single|none> <-c cols>
             <-P char> <-F file> <match expression> <bpf filter>
 -h  is help/usage
 -V  is version information
 -q  is be quiet (don't print packet reception hash marks)
 -e  is show empty packets
 -i  is ignore case
 -v  is invert match
 -R  is don't do privilege revocation logic
 -x  is print in alternate hexdump format
 -X  is interpret match expression as hexadecimal
 -w  is word-regex (expression must match as a word)
 -p  is don't go into promiscuous mode
 -l  is make stdout line buffered
 -D  is replay pcap_dumps with their recorded time intervals
 -t  is print timestamp every time a packet is matched
 -T  is print delta timestamp every time a packet is matched
 -M  is don't do multi-line match (do single-line match instead)
 -I  is read packet stream from pcap format file pcap_dump
 -O  is dump matched packets in pcap format to pcap_dump
 -n  is look at only num packets
 -A  is dump num packets after a match
 -s  is set the bpf caplen
 -S  is set the limitlen on matched packets
 -W  is set the dump format (normal, byline, single, none)
 -c  is force the column width to the specified size
 -P  is set the non-printable display char to what is specified
 -F  is read the bpf filter from the specified file
 -N  is show sub protocol number
 -d  is use specified device instead of the pcap default

5. 주의사항

ngrep은 민감 정보를 포함한 실제 트래픽을 볼 수 있으므로 반드시 자체 서버 운영 목적 등 정당한 용도로만 사용해야 합니다. 타인의 사설 정보 또는 비인가된 패킷 분석 시 법적인 문제가 발생할 수 있습니다.