- é¢é£è¨äº
- æ¦è¦
- ã¤ã³ã¹ãã¼ã«
- 試ãã¦ã¿ã
- ãªãã¸ããª
- åèæ å ±
- Goã®ããããæ¸ç±
é¢é£è¨äº
Goメモ-306 (go-packetメモ-01)(ネットワークインターフェースを表示) - いろいろ備忘録日記
Goメモ-307 (go-packetメモ-02)(流れるパケットをキャプチャする)(OpenLive) - いろいろ備忘録日記
Goメモ-308 (go-packetメモ-03)(pcapファイルを読み込み)(OpenOffline) - いろいろ備忘録日記
GitHub - devlights/blog-summary: ブログ「いろいろ備忘録日記」のまとめ
æ¦è¦
以ä¸ãèªåç¨ã®ã¡ã¢ã§ããå¿ããªããã¡ã«ã¡ã¢ã¡ã¢ããã
Goã§WireSharkãtcpdumpã®ããã«ãã±ãããç´æ¥è¦ããã¨ããªã©ã«å©ç¨ã§ããã©ã¤ãã©ãªã«
ã¨ããã®ãããã¾ãã
ä»ã¾ã§ä½¿ã£ããã¨ç¡ãã£ãã®ã§ããã使ãã¨é¢ç½ãã£ãã®ã§èªåç¨ã®ã¡ã¢ä»£ããã«ã¡ããã¡ããæ®ãã¦ãããã¨æãã¾ãã
Linux (Ubuntu) ä¸ã§éãã§ãã¾ãã®ã§ãWindowsã®å ´åã¯WinPcap (WireSharkãã¤ã³ã¹ãã¼ã«ããã¨ãã«ã¤ãã§ã«ã¤ã³ã¹ãã¼ã«ã§ããã¯ãï¼ãå¿ è¦ã«ãªãã¨æãã¾ãã
ã¤ã³ã¹ãã¼ã«
libpcap
ãå¿
è¦ã§ãã®ã§ã以ä¸ã§ã¤ã³ã¹ãã¼ã«ãã¾ãã
$ sudo apt install libpcap-dev
ãã¨ãtcpdumpãå ¥ã£ã¦ããªãå ´åã¯ä»¥ä¸ãã¤ãã§ã«å ¥ãã¦ããã¾ããï¼ããã¯ãªãã·ã§ãã«ã§ãï¼
$ sudo apt install tcpdump
試ãã¦ã¿ã
使ãæ¹ã«é¢ãã¦ã¯ãä¸ã«æãã¦ãã go-packet ã® godoc ã®æ¹ã«è©³ããæ¸ããã¦ãã¾ãã
ä»åã¯ãBPF (Berkeley Packet Filter) ãã£ã«ã¿ãè¨å®ããæ¹æ³ã«ã¤ãã¦ã§ãã
pcap.SetBPFFilter
é¢æ°ã使ãã¾ãã
main.go
// Package main is the example of go-packet with BPF (Berkeley Packet Filter) filter package main import ( "fmt" "log" "os" "os/signal" "github.com/google/gopacket" "github.com/google/gopacket/pcap" ) var ( appLog = log.New(os.Stderr, "", 0) ) func main() { if err := run(); err != nil { panic(err) } } func run() error { const ( pcapfile = "example.pcap" filter = "icmp or icmp6" ) defer func() { appLog.Println("DONE") }() // -------------------------------------- // Open capture handle // -------------------------------------- var ( handle *pcap.Handle err error ) handle, err = pcap.OpenOffline(pcapfile) if err != nil { return fmt.Errorf("error open handle: %w", err) } defer handle.Close() // -------------------------------------- // Apply capture filter // // # filter examples: // - ip src 192.168.1.1 // - ip dst 192.168.1.2 // - ip host 192.168.1.1 and ip host 192.168.1.2 // - tcp port 80 // - udp port 53 // - icmp or icmp6 // - ether src aa:bb:cc:dd:ee:ff // - ether dst aa:bb:cc:dd:ee:ff // - vlan 100 // - ip host 192.168.1.1 and tcp port 80 // -------------------------------------- if filter != "" { err = handle.SetBPFFilter(filter) if err != nil { return fmt.Errorf("error apply filter: %w", err) } } // -------------------------------------- // Set signal handler // -------------------------------------- var ( sigCh = make(chan os.Signal, 1) ) signal.Notify(sigCh, os.Interrupt) // -------------------------------------- // Make packet source and display. // -------------------------------------- var ( dataSource gopacket.PacketDataSource = handle decoder gopacket.Decoder = handle.LinkType() packetSource *gopacket.PacketSource = gopacket.NewPacketSource(dataSource, decoder) packetCh <-chan gopacket.Packet = packetSource.Packets() ) appLog.Println("START") LOOP: for { select { case <-sigCh: break LOOP case p, ok := <-packetCh: if !ok { break LOOP } appLog.Println(p) } } return nil }
tcpdump.sh
#!/usr/bin/env bash tcpdump -t -n -i lo -w example.pcap & echo $! > tcpdump.pid exit 0
ping.sh
#!/usr/bin/env bash timeout 3s ping localhost exit 0
kill.sh
#!/usr/bin/env bash kill $(cat tcpdump.pid) exit 0
以ä¸ãGitpodä¸ã§å®è¡ãã¦ã¿ãçµæã§ãã
gitpod /workspace/go-gopacket-example (main) $ task bpffilter task: [bpffilter] go build task: [bpffilter] sudo bash ./tcpdump.sh task: [bpffilter] bash ./ping.sh PING localhost(localhost (::1)) 56 data bytes 64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.014 ms tcpdump: listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes 64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.034 ms 64 bytes from localhost (::1): icmp_seq=3 ttl=64 time=0.039 ms task: [bpffilter] sudo bash ./kill.sh 33 packets captured 66 packets received by filter 0 packets dropped by kernel task: [bpffilter] sleep 1 task: [bpffilter] sudo ./bpffilter START PACKET: 118 bytes, wire length 118 cap length 118 @ 2023-04-18 02:07:43.05961 +0000 UTC - Layer 1 (14 bytes) = Ethernet {Contents=[..14..] Payload=[..104..] SrcMAC=00:00:00:00:00:00 DstMAC=00:00:00:00:00:00 EthernetType=IPv6 Length=0} - Layer 2 (40 bytes) = IPv6 {Contents=[..40..] Payload=[..64..] Version=6 TrafficClass=0 FlowLabel=53830 Length=64 NextHeader=ICMPv6 HopLimit=64 SrcIP=::1 DstIP=::1 HopByHop=nil} - Layer 3 (04 bytes) = ICMPv6 {Contents=[128, 0, 32, 129] Payload=[..60..] TypeCode=EchoRequest Checksum=8321 TypeBytes=[]} - Layer 4 (00 bytes) = ICMPv6Echo {Contents=[] Payload=[] Identifier=12517 SeqNumber=2} PACKET: 118 bytes, wire length 118 cap length 118 @ 2023-04-18 02:07:43.059619 +0000 UTC - Layer 1 (14 bytes) = Ethernet {Contents=[..14..] Payload=[..104..] SrcMAC=00:00:00:00:00:00 DstMAC=00:00:00:00:00:00 EthernetType=IPv6 Length=0} - Layer 2 (40 bytes) = IPv6 {Contents=[..40..] Payload=[..64..] Version=6 TrafficClass=0 FlowLabel=275845 Length=64 NextHeader=ICMPv6 HopLimit=64 SrcIP=::1 DstIP=::1 HopByHop=nil} - Layer 3 (04 bytes) = ICMPv6 {Contents=[129, 0, 31, 129] Payload=[..60..] TypeCode=EchoReply Checksum=8065 TypeBytes=[]} - Layer 4 (00 bytes) = ICMPv6Echo {Contents=[] Payload=[] Identifier=12517 SeqNumber=2} PACKET: 118 bytes, wire length 118 cap length 118 @ 2023-04-18 02:07:44.07939 +0000 UTC - Layer 1 (14 bytes) = Ethernet {Contents=[..14..] Payload=[..104..] SrcMAC=00:00:00:00:00:00 DstMAC=00:00:00:00:00:00 EthernetType=IPv6 Length=0} - Layer 2 (40 bytes) = IPv6 {Contents=[..40..] Payload=[..64..] Version=6 TrafficClass=0 FlowLabel=53830 Length=64 NextHeader=ICMPv6 HopLimit=64 SrcIP=::1 DstIP=::1 HopByHop=nil} - Layer 3 (04 bytes) = ICMPv6 {Contents=[128, 0, 222, 50] Payload=[..60..] TypeCode=EchoRequest Checksum=56882 TypeBytes=[]} - Layer 4 (00 bytes) = ICMPv6Echo {Contents=[] Payload=[] Identifier=12517 SeqNumber=3} PACKET: 118 bytes, wire length 118 cap length 118 @ 2023-04-18 02:07:44.079401 +0000 UTC - Layer 1 (14 bytes) = Ethernet {Contents=[..14..] Payload=[..104..] SrcMAC=00:00:00:00:00:00 DstMAC=00:00:00:00:00:00 EthernetType=IPv6 Length=0} - Layer 2 (40 bytes) = IPv6 {Contents=[..40..] Payload=[..64..] Version=6 TrafficClass=0 FlowLabel=275845 Length=64 NextHeader=ICMPv6 HopLimit=64 SrcIP=::1 DstIP=::1 HopByHop=nil} - Layer 3 (04 bytes) = ICMPv6 {Contents=[129, 0, 221, 50] Payload=[..60..] TypeCode=EchoReply Checksum=56626 TypeBytes=[]} - Layer 4 (00 bytes) = ICMPv6Echo {Contents=[] Payload=[] Identifier=12517 SeqNumber=3} DONE
å®è¡ããã¨ãæåã«ï¼ç§éã»ã© tcpdump ã³ãã³ããå®è¡ã㦠ping (ICMP ã¨ã³ã¼ã¡ãã»ã¼ã¸) ããã£ããã£ãã¦ãpcapãã¡ã¤ã«ãåºåãã¾ãã
ãã®å¾ããããgo-packetã使ã£ã¦èªã¿è¾¼ãã§ãã±ãããBPFãã£ã«ã¿è¨å®ãã¦è¡¨ç¤ºãã¦ãã¾ãã
pingã³ãã³ããå®è¡ããéã« -4
ãªãã·ã§ã³ãä»ãã¦ããªãã®ã§ãICMPv6 ã§ã¡ãã»ã¼ã¸ãéåä¿¡ããã¦ãã¾ãã
ãªã®ã§ãä¸ã®ã½ã¼ã¹ã®BPFãã£ã«ã¿ã icmp or icmp6
ãã icmp
ã«ãã¦ãå®è¡ããã¨ä½ã表示ãããªããªãã¾ãã
ã¡ããã¨ãã£ã«ã¿ãªã³ã°ããã¦ãã¾ããã
ãªãã¸ããª
ä¸ã®ãµã³ãã«ãªã©ã¯ã以ä¸ã®ãªãã¸ããªã§ã¢ãããã¦ãã¾ãããåèã¾ã§ã«ã
åèæ å ±
Goã®ããããæ¸ç±
éå»ã®è¨äºã«ã¤ãã¦ã¯ã以ä¸ã®ãã¼ã¸ãããåç §ä¸ããã
ãµã³ãã«ã³ã¼ãã¯ã以ä¸ã®å ´æã§å ¬éãã¦ãã¾ãã