목차

, , , , , , ,

tshark

TShark는 Wireshark의 command-line network protocol analyzer다. 실시간 packet을 캡처하거나 capture file을 읽고, Wireshark dissector와 display filter를 이용해 protocol과 field를 분석한다.

Installation

Debian / Ubuntu

Wireshark package에 TShark가 포함된다.

sudo apt update
sudo apt install wireshark

설치 과정에서 일반 사용자의 packet capture 허용 여부를 선택할 수 있다. 권한 정책을 확인한 뒤 최소 사용자만 capture group에 추가한다.

RHEL / Fedora

CLI 도구만 필요하면 Fedora의 wireshark-cli package를 설치한다.

sudo dnf install wireshark-cli

Wireshark GUI가 함께 필요하면 배포판의 wireshark package를 추가한다.

macOS

Homebrew의 wireshark formula는 GUI 없이 command-line utilities를 설치한다.

brew install wireshark

일반 사용자가 capture interface를 사용할 수 없으면 필요한 경우에만 별도 ChmodBPF package를 검토한다.

brew install --cask wireshark-chmodbpf

Windows

공식 Wireshark installer package를 winget으로 설치한다. TShark component가 설치 대상에 포함되어 있는지 installer selection을 확인한다.

winget install --exact --id WiresharkFoundation.Wireshark

새 terminal을 열고 설치 경로가 PATH에 포함되어 있는지 확인한다.

Verification

tshark --version

Usage

tshark -D
tshark -i eth0 -f 'tcp port 443'
tshark -r capture.pcapng -Y 'http.request'
tshark -i any -a duration:60 -w capture.pcapng
실시간 캡처에는 별도 권한이 필요할 수 있다. root로 장시간 실행하는 대신 OS별 capture group 또는 Wireshark가 제공하는 권한 분리 방식을 사용한다. capture file에는 암호, token, cookie, 개인 정보가 포함될 수 있으므로 접근 권한과 보관 기간을 제한한다.

Options

Filters

Capture filter

-f는 packet을 수집하기 전에 pcap/BPF filter를 적용한다. busy interface에서 불필요한 packet을 일찍 버리므로 live capture 부하와 file 크기를 줄이는 데 적합하다.

tshark -i eth0 -f 'host 192.0.2.10 and tcp port 443'

Display filter

-Y는 Wireshark dissector가 해석한 protocol과 field를 기준으로 표시할 packet을 고른다. capture file 분석이나 세밀한 protocol 조건에 적합하다.

tshark -r capture.pcapng -Y 'http.request.method == "POST"'
tshark -r capture.pcapng -Y 'dns.flags.response == 0'
capture filter와 display filter는 서로 다른 언어다. live capture에서 -w로 raw packet을 저장할 때 display filter로 저장 대상을 거르는 방식은 지원되지 않는다. 저장 범위를 줄이려면 -f를 사용하거나 먼저 캡처한 뒤 -r, -Y, -w로 별도 file을 만든다.

Examples

인터페이스 확인

tshark -D

시간과 packet 수를 제한한 capture

tshark -i eth0 -f 'udp port 53' -a duration:60 -w dns.pcapng
tshark -i eth0 -c 100 -w sample.pcapng

capture file 분석

tshark -r capture.pcapng
tshark -r capture.pcapng -Y 'tcp.analysis.retransmission'
tshark -r capture.pcapng -Y 'tls.handshake.type == 1'

field 추출

tshark -r capture.pcapng -Y 'dns.qry.name' \
  -T fields \
  -E header=y \
  -E separator=, \
  -E quote=d \
  -e frame.time_epoch \
  -e ip.src \
  -e dns.qry.name

field 이름은 다음 명령과 공식 Display Filter Reference에서 확인한다.

tshark -G fields

JSON 출력

tshark -r capture.pcapng -Y 'http.request' -T json > http-requests.json

-w는 raw capture file을 만들고 > redirect는 사람이 읽는 decoded output을 저장한다는 차이에 주의한다.

통계

tshark -r capture.pcapng -q -z io,phs
tshark -r capture.pcapng -q -z conv,tcp
tshark -r capture.pcapng -q -z endpoints,ip
tshark -r capture.pcapng -q -z io,stat,1

ring buffer

tshark -i eth0 -f 'tcp port 443' \
  -b duration:300 \
  -b files:12 \
  -w tls-ring.pcapng

Troubleshooting

capture interface가 비어 있음

filter 오류

packet loss

busy interface에서는 복잡한 live display filter보다 capture filter로 입력량을 먼저 줄인다. capture duration, file rotation, buffer 크기와 storage 처리량을 함께 점검한다.

Compatibility

Help

tshark --help

See Also

History