Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

GoでTCPパケットを読む / Fukuoka.go #12

GoでTCPパケットを読む / Fukuoka.go #12

Fukuoka.go #12

Ken’ichiro Oyama

October 07, 2018
Tweet

More Decks by Ken’ichiro Oyama

Other Decks in Technology

Transcript

  1. ࠓճ࣮૷͢ΔUDQEVNQͷίϚϯυ $ tcpdump -X -i eth0 tcp and port 80

    ग़ྗϑΥʔϚοτ ωοτϫʔΫΠϯλʔϑΣʔεͷࢦఆ ϑΟϧλϦϯά
  2. ·ͣϓϩδΣΫτσΟϨΫτϦΛ࡞੒ $ go version go version go1.11 darwin/amd64 $ mkdir

    mydump $ cd mydump/ $ echo 'module "github.com/k1LoW/mydump"' > go.mod
  3. NBJOHP 1 package main 2 3 import ( 4 "encoding/hex"

    5 "fmt" 6 "log" 7 8 "github.com/google/gopacket" 9 "github.com/google/gopacket/pcap" 10 ) 11 12 func main() { 13 device := "eth0" 14 filter := "tcp and port 80" 15 16 handle, err := pcap.OpenLive( 17 device, int32(0xFFFF), true, pcap.BlockForever, 18 ) 19 if err != nil { 20 log.Fatal(err) 21 } 22 defer handle.Close() 23 if err := handle.SetBPFFilter(filter); err != nil { 24 log.Fatal(err) 25 } 26 27 packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) 28 for packet := range packetSource.Packets() { 29 fmt.Printf(“%s\n", packet) 30 fmt.Printf("%s", hex.Dump(packet.Data())) 31 } 32 }
  4. NBJOHP- 1 package main 2 3 import ( 4 "encoding/hex"

    5 "fmt" 6 "log" 7 8 "github.com/google/gopacket" 9 "github.com/google/gopacket/pcap" 10 )
  5. NBJOHP- 12 func main() { 13 device := "eth0" 14

    filter := "tcp and port 80" 15 16 handle, err := pcap.OpenLive( 17 device, int32(0xFFFF), true, pcap.BlockForever, 18 ) 19 if err != nil { 20 log.Fatal(err) 21 } 22 defer handle.Close() ࠓճ͸ݻఆɻҾ਺Ͱ༩͑ΒΕΔΑ͏ʹͳͬͨΒ ΑΓUDQEVNQͬΆ͍ ΠϯλʔϑΣʔεʹΞλον
  6. NBJOHP- 23 if err := handle.SetBPFFilter(filter); err != nil {

    24 log.Fatal(err) 25 } #1' #FSLFMFZ1BDLFU'JMUFS Λઃఆ
  7. #1'

  8. NBJOHP- 27 packetSource := gopacket.NewPacketSource( handle, handle.LinkType()) 28 for packet

    := range packetSource.Packets() { 29 fmt.Printf(“%s\n", packet) 30 fmt.Printf("%s", hex.Dump(packet.Data())) 31 } 32 } ύέοτ͕νϟϯωϧΛ௨ͯ͡΍ͬͯ͘ΔͷͰGPSͰड͚औΔ ग़ྗ
  9. PACKET: 450 bytes, wire length 450 cap length 450 @

    2018-10-04 19:02:36.200155 +0900 JST - Layer 1 (14 bytes) = Ethernet {Contents=[..14..] Payload=[..436..] SrcMAC=8c:85:90:ae:ae:c2 DstMAC=2c:33:11:ca:c8:be EthernetType=IPv4 Length=0} - Layer 2 (20 bytes) = IPv4 {Contents=[..20..] Payload=[..416..] Version=4 IHL=5 TOS=0 Length=436 Id=0 Flags=DF FragOffset=0 TTL=64 Protocol=TCP Checksum=24376 SrcIP=192.168.75.96 DstIP=108.177.97.82 Options=[] Padding=[]} - Layer 3 (32 bytes) = TCP {Contents=[..32..] Payload=[..384..] SrcPort=51190 DstPort=80(http) Seq=1867296718 Ack=2345355645 DataOffset=8 FIN=false SYN=false RST=false PSH=true ACK=true URG=false ECE=false CWR=false NS=false Window= 4096 Checksum=19074 Urgent=0 Options=[TCPOption(NOP:), TCPOption(NOP:), TCPOption(Timestamps:958590780/3024242800 0x3922ef3cb4424870)] Padding=[]} - Layer 4 (384 bytes) ^C1 63 69 6e 74 6f 73 |la/5.0 (Macintos|
  10. PACKET: 450 bytes, wire length 450 cap length 450 @

    2018-10-04 19:02:36.200155 +0900 JST - Layer 1 (14 bytes) = Ethernet {Contents=[..14..] Payload=[..436..] SrcMAC=8c:85:90:ae:ae:c2 DstMAC=2c:33:11:ca:c8:be EthernetType=IPv4 Length=0} - Layer 2 (20 bytes) = IPv4 {Contents=[..20..] Payload=[..416..] Version=4 IHL=5 TOS=0 Length=436 Id=0 Flags=DF FragOffset=0 TTL=64 Protocol=TCP Checksum=24376 SrcIP=192.168.75.96 DstIP=108.177.97.82 Options=[] Padding=[]} - Layer 3 (32 bytes) = TCP {Contents=[..32..] Payload=[..384..] SrcPort=51190 DstPort=80(http) Seq=1867296718 Ack=2345355645 DataOffset=8 FIN=false SYN=false RST=false PSH=true ACK=true URG=false ECE=false CWR=false NS=false Window= 4096 Checksum=19074 Urgent=0 Options=[TCPOption(NOP:), TCPOption(NOP:), TCPOption(Timestamps:958590780/3024242800 0x3922ef3cb4424870)] Padding=[]} - Layer 4 (384 bytes) ^C1 63 69 6e 74 6f 73 |la/5.0 (Macintos|
  11. 5$1ύέοτͬ͘͟Γ &UIFSOFU CZUF *1ϔομ CZUF 5$1ϔομ CZUF ࢒Γ 5$1Φϓγϣϯ /CZUF

    σʔλ ྫ͑͹)551΍.Z42-ͷϓϩτίϧ͸͔͜͜Β ελʔτ͍ͯ͠Δ