SHARE
TWEET

Untitled

a guest Jul 17th, 2017 38 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "log"
  5.     "os"
  6.  
  7.     "github.com/awalterschulze/gographviz"
  8.     "github.com/google/gopacket"
  9.     "github.com/google/gopacket/pcap"
  10. )
  11.  
  12. func main() {
  13.     // The pcap[ng] file you want to visualize
  14.     pf := "./file.pcap"
  15.  
  16.     // open file
  17.     h, err := pcap.OpenOffline(pf)
  18.     if err != nil {
  19.         log.Fatal(err)
  20.     }
  21.     defer h.Close()
  22.  
  23.     // extract the packet and remove duplication.
  24.     flows := make(map[gopacket.Flow]int, 0)
  25.     pSrc := gopacket.NewPacketSource(h, h.LinkType())
  26.     for p := range pSrc.Packets() {
  27.         if p.NetworkLayer() != nil {
  28.             flows[p.NetworkLayer().NetworkFlow()]++
  29.         }
  30.     }
  31.  
  32.     // draw the directed graph.
  33.     g := gographviz.NewGraph()
  34.     if err := g.SetName("G"); err != nil {
  35.         panic(err)
  36.     }
  37.     if err := g.SetDir(true); err != nil {
  38.         panic(err)
  39.     }
  40.     if err := g.AddAttr("G", "bgcolor", "\"#343434\""); err != nil {
  41.         panic(err)
  42.     }
  43.     if err := g.AddAttr("G", "layout", "circo"); err != nil {
  44.         panic(err)
  45.     }
  46.  
  47.     // configuration for nodes
  48.     nodeAttrs := make(map[string]string)
  49.     nodeAttrs["colorscheme"] = "rdylgn11"
  50.     nodeAttrs["style"] = "\"solid,filled\""
  51.     nodeAttrs["fontcolor"] = "6"
  52.     nodeAttrs["fontname"] = "\"Migu 1M\""
  53.     nodeAttrs["color"] = "7"
  54.     nodeAttrs["fillcolor"] = "11"
  55.     nodeAttrs["shape"] = "doublecircle"
  56.  
  57.     // make the nodes.
  58.     nodes := make(map[string]bool)
  59.     for f := range flows {
  60.         if !nodes[f.Src().String()] {
  61.             nodes[f.Src().String()] = true
  62.         }
  63.         if !nodes[f.Dst().String()] {
  64.             nodes[f.Dst().String()] = true
  65.         }
  66.     }
  67.  
  68.     // add nodes to the graph "G"
  69.     for n := range nodes {
  70.         if err := g.AddNode("G", "\""+n+"\"", nodeAttrs); err != nil {
  71.             panic(err)
  72.         }
  73.     }
  74.  
  75.     // configuration for edges
  76.     edgeAttrs := make(map[string]string)
  77.     edgeAttrs["color"] = "white"
  78.  
  79.     // connect the nodes
  80.     for f := range flows {
  81.         err := g.AddEdge("\""+f.Src().String()+"\"",
  82.             "\""+f.Dst().String()+"\"", true, edgeAttrs)
  83.         if err != nil {
  84.             panic(err)
  85.         }
  86.     }
  87.  
  88.     // save the graph as dotfile.
  89.     s := g.String()
  90.     file, err := os.Create(`./digraph.dot`)
  91.     if err != nil {
  92.         panic(err)
  93.     }
  94.     defer file.Close()
  95.     file.Write([]byte(s))
  96.  
  97. }
RAW Paste Data
Top