기타/IT

hping3 - 패킷을 손쉽게 만들어보자, 리눅스 패킷생성기 (1 - 기본사용편)

한그마 2020. 8. 18. 19:43

리눅스 환경에서 패킷을 만드는 방법은 여러가지가 있습니다.

여기서는 제일 간단한 방법으로 패킷을 만드는 방법을 소개합니다.

Ubuntu 기준으로 터미널에서 "apt-get install hping3"으로 hping3을 설치합니다.

 

hping3 옵션

root@ubuntu:~# hping3 --help
usage: hping3 host [options]
  -h  --help      show this help
  -v  --version   show version
  -c  --count     packet count
  -i  --interval  wait (uX for X microseconds, for example -i u1000)
      --fast      alias for -i u10000 (10 packets for second)
      --faster    alias for -i u1000 (100 packets for second)
      --flood      sent packets as fast as possible. Don't show replies.
  -n  --numeric   numeric output
  -q  --quiet     quiet
  -I  --interface interface name (otherwise default routing interface)
  -V  --verbose   verbose mode
  -D  --debug     debugging info
  -z  --bind      bind ctrl+z to ttl           (default to dst port)
  -Z  --unbind    unbind ctrl+z
      --beep      beep for every matching packet received
Mode
  default mode     TCP
  -0  --rawip      RAW IP mode
  -1  --icmp       ICMP mode
  -2  --udp        UDP mode
  -8  --scan       SCAN mode.
                   Example: hping --scan 1-30,70-90 -S www.target.host
  -9  --listen     listen mode
IP
  -a  --spoof      spoof source address
  --rand-dest      random destionation address mode. see the man.
  --rand-source    random source address mode. see the man.
  -t  --ttl        ttl (default 64)
  -N  --id         id (default random)
  -W  --winid      use win* id byte ordering
  -r  --rel        relativize id field          (to estimate host traffic)
  -f  --frag       split packets in more frag.  (may pass weak acl)
  -x  --morefrag   set more fragments flag
  -y  --dontfrag   set don't fragment flag
  -g  --fragoff    set the fragment offset
  -m  --mtu        set virtual mtu, implies --frag if packet size > mtu
  -o  --tos        type of service (default 0x00), try --tos help
  -G  --rroute     includes RECORD_ROUTE option and display the route buffer
  --lsrr           loose source routing and record route
  --ssrr           strict source routing and record route
  -H  --ipproto    set the IP protocol field, only in RAW IP mode
ICMP
  -C  --icmptype   icmp type (default echo request)
  -K  --icmpcode   icmp code (default 0)
      --force-icmp send all icmp types (default send only supported types)
      --icmp-gw    set gateway address for ICMP redirect (default 0.0.0.0)
      --icmp-ts    Alias for --icmp --icmptype 13 (ICMP timestamp)
      --icmp-addr  Alias for --icmp --icmptype 17 (ICMP address subnet mask)
      --icmp-help  display help for others icmp options
UDP/TCP
  -s  --baseport   base source port             (default random)
  -p  --destport   [+][+]<port> destination port(default 0) ctrl+z inc/dec
  -k  --keep       keep still source port
  -w  --win        winsize (default 64)
  -O  --tcpoff     set fake tcp data offset     (instead of tcphdrlen / 4)
  -Q  --seqnum     shows only tcp sequence number
  -b  --badcksum   (try to) send packets with a bad IP checksum
                   many systems will fix the IP checksum sending the packet
                   so you'll get bad UDP/TCP checksum instead.
  -M  --setseq     set TCP sequence number
  -L  --setack     set TCP ack
  -F  --fin        set FIN flag
  -S  --syn        set SYN flag
  -R  --rst        set RST flag
  -P  --push       set PUSH flag
  -A  --ack        set ACK flag
  -U  --urg        set URG flag
  -X  --xmas       set X unused flag (0x40)
  -Y  --ymas       set Y unused flag (0x80)
  --tcpexitcode    use last tcp->th_flags as exit code
  --tcp-mss        enable the TCP MSS option with the given value
  --tcp-timestamp  enable the TCP timestamp option to guess the HZ/uptime
Common
  -d  --data       data size                    (default is 0)
  -E  --file       data from file
  -e  --sign       add 'signature'
  -j  --dump       dump packets in hex
  -J  --print      dump printable characters
  -B  --safe       enable 'safe' protocol
  -u  --end        tell you when --file reached EOF and prevent rewind
  -T  --traceroute traceroute mode              (implies --bind and --ttl 1)
  --tr-stop        Exit when receive the first not ICMP in traceroute mode
  --tr-keep-ttl    Keep the source TTL fixed, useful to monitor just one hop
  --tr-no-rtt       Don't calculate/show RTT information in traceroute mode
ARS packet description (new, unstable)
  --apd-send       Send the packet described with APD (see docs/APD.txt)

위와 같이 "hping3 --help"  명령을 내리면 사용할 수 있는 여러가지 옵션이 출력됩니다.

고급 엔지니어일수록 다양한 옵션을 활용하실 수 있지만, 제일 기본적인 옵션을 확인해보겠습니다.

구성 환경은 PC1(12.12.12.1)에서 PC2(13.13.13.1)로 패킷을 전송하는 상황을 가정해보겠습니다.


프로토콜

먼저 프로토콜 옵션입니다.

  • -S : TCP SYN
  • -2 : UDP
  • -1 : ICMP
  • -0 : IP

1. TCP/UDP/ICMP/IP 옵션을 사용하여 상대방으로 패킷을 1회만 전송합니다. (옵션 -c : 전송 횟수)


PC1

root@PC1:~# hping3 -S 13.13.13.1 -c 1
root@PC1:~# hping3 -2 13.13.13.1 -c 1
root@PC1:~# hping3 -1 13.13.13.1 -c 1
root@PC1:~# hping3 -0 13.13.13.1 --ipproto 51 -c 1

PC2

root@PC2:~# tcpdump -ni eth1 host 12.12.12.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 262144 bytes
19:28:40.916864 IP 12.12.12.1.1188 > 13.13.13.1.0: Flags [S], seq 1993464310, win 512, length 0
19:28:40.916913 IP 13.13.13.1.0 > 12.12.12.1.1188: Flags [R.], seq 0, ack 1993464311, win 0, length 0
19:28:44.328948 IP 12.12.12.1.2484 > 13.13.13.1.0: UDP, length 0
19:28:44.328996 IP 13.13.13.1 > 12.12.12.1: ICMP 13.13.13.1 udp port 0 unreachable, length 36
19:28:46.581001 IP 12.12.12.1 > 13.13.13.1: ICMP echo request, id 18544, seq 0, length 8
19:28:46.581042 IP 13.13.13.1 > 12.12.12.1: ICMP echo reply, id 18544, seq 0, length 8
19:29:00.985336 IP 12.12.12.1 > 13.13.13.1: [|AH]
19:29:00.985378 IP 13.13.13.1 > 12.12.12.1: ICMP 13.13.13.1 protocol 51 unreachable, length 28

위와 같이 TCP SYN, UDP, ICMP, IP 패킷이 전송되었습니다.

 

포트

포트 옵션입니다.

  • -s : 출발지 포트
  • -p : 목적지 포트
  • -k : 출발지 포트 고정

1. UDP로 상대방으로 출발지 포트(5050), 목적지 포트(80) 옵션만 사용하여 전송


PC1

root@PC1:~# hping3 -2 -s 5050 -p 80 13.13.13.1
HPING 13.13.13.1 (eth1 13.13.13.1): udp mode set, 28 headers + 0 data bytes
ICMP Port Unreachable from ip=13.13.13.1 name=UNKNOWN
status=0 port=5050 seq=0
ICMP Port Unreachable from ip=13.13.13.1 name=UNKNOWN
status=0 port=5051 seq=1
ICMP Port Unreachable from ip=13.13.13.1 name=UNKNOWN
status=0 port=5052 seq=2
^C
--- 13.13.13.1 hping statistic ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 7.8/8.3/9.3 ms

PC2

root@PC2:~# tcpdump -ni eth1 host 12.12.12.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 262144 bytes
19:35:25.950754 IP 12.12.12.1.5050 > 13.13.13.1.80: UDP, length 0
19:35:25.950802 IP 13.13.13.1 > 12.12.12.1: ICMP 13.13.13.1 udp port 80 unreachable, length 36
19:35:26.950883 IP 12.12.12.1.5051 > 13.13.13.1.80: UDP, length 0
19:35:26.950929 IP 13.13.13.1 > 12.12.12.1: ICMP 13.13.13.1 udp port 80 unreachable, length 36
19:35:27.950969 IP 12.12.12.1.5052 > 13.13.13.1.80: UDP, length 0
19:35:27.951013 IP 13.13.13.1 > 12.12.12.1: ICMP 13.13.13.1 udp port 80 unreachable, length 36

PC2에서 수신된 패킷을 확인해보면 출발지 포트가 5050부터 계속 증가됨을 확인할 수 있습니다.

 

 

2. UDP로 상대방으로 출발지 포트(5050), 목적지 포트(80), 출발지 포트 고정하여 전송

PC1

root@PC1:~# hping3 -2 -s 5050 -p 80 13.13.13.1 -k
HPING 13.13.13.1 (eth1 13.13.13.1): udp mode set, 28 headers + 0 data bytes
ICMP Port Unreachable from ip=13.13.13.1 name=UNKNOWN
status=0 port=5050 seq=0
ICMP Port Unreachable from ip=13.13.13.1 name=UNKNOWN
status=1 port=5050 seq=0
ICMP Port Unreachable from ip=13.13.13.1 name=UNKNOWN
status=1 port=5050 seq=0

PC2

root@PC2:~# tcpdump -ni eth1 host 12.12.12.1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 262144 bytes
19:38:15.518973 IP 12.12.12.1.5050 > 13.13.13.1.80: UDP, length 0
19:38:15.519030 IP 13.13.13.1 > 12.12.12.1: ICMP 13.13.13.1 udp port 80 unreachable, length 36
19:38:16.519053 IP 12.12.12.1.5050 > 13.13.13.1.80: UDP, length 0
19:38:16.519092 IP 13.13.13.1 > 12.12.12.1: ICMP 13.13.13.1 udp port 80 unreachable, length 36
19:38:17.519107 IP 12.12.12.1.5050 > 13.13.13.1.80: UDP, length 0
19:38:17.519145 IP 13.13.13.1 > 12.12.12.1: ICMP 13.13.13.1 udp port 80 unreachable, length 36

PC2에서 수신된 패킷을 확인해보면 출발지 포트가 5050으로 고정됨을 확인할 수 있습니다.

 

위와같이 익숙해지면 간단하게 사용할 수 있습니다.

더 상세한 내용은 2편에 이어서 작성하겠습니다.