SHARE
TWEET

pasoib8_main

saleks28 Dec 15th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. // TODO:
  4. // 1)Сделать сохранение всех данных из формы с преобразованиями - done
  5. // 2)Сделать окошко с количеством отправляемых пакетов
  6. // 3)Подключить девайс и затестить, правильно ли отправляется пакет
  7.  
  8. import (
  9.     "fmt"
  10.     "html/template"
  11.     "os"
  12.     "log"
  13.     "net"
  14.     "net/http"
  15.     "time"
  16.  
  17.     "github.com/google/gopacket"
  18.  
  19.     "github.com/google/gopacket/pcap"
  20.  
  21.     "github.com/google/gopacket/layers"
  22. )
  23.  
  24. var (
  25.     //All templates ptr
  26.     tpl *template.Template
  27.     //All interfaces array
  28.     interfArray []net.Interface
  29.     //Chosen interface
  30.     chosenInterf net.Interface
  31.     //Packet laeyrs
  32.     ipLayer   layers.IPv4
  33.     ethLayer  layers.Ethernet
  34.     tcpLayer  layers.TCP
  35.     icmpLayer layers.ICMPv4
  36.     udpLayer  layers.UDP
  37.     //Error variable
  38.     err error
  39. )
  40.  
  41. func init() {
  42.     //TODO: Parse templates only once - in init function
  43.     //tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  44.     interfArray, err = net.Interfaces()
  45.     if err != nil {
  46.         log.Fatal(err)
  47.     }
  48. }
  49.  
  50. func networkLayer(w http.ResponseWriter, req *http.Request) {
  51.     tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  52.     sendStruct := struct {
  53.         Interfaces []net.Interface
  54.         Protocol   []layers.IPProtocol
  55.         EtherType  []layers.EthernetType
  56.     }{
  57.         Interfaces: interfArray,
  58.         Protocol:   Protocols,
  59.         EtherType:  EtherTypes,
  60.     }
  61.     err = tpl.ExecuteTemplate(w, "netlayer.gohtml", sendStruct)
  62.     if err != nil {
  63.         log.Fatal(err)
  64.     }
  65. }
  66.  
  67. func changeChosenInterf(name string) {
  68.     for _, i := range interfArray {
  69.         if i.Name == name {
  70.             chosenInterf = i
  71.         }
  72.     }
  73. }
  74.  
  75. func saveIPLayer(w http.ResponseWriter, req *http.Request) {
  76.     ipLayer.Version = StringToUint8(req.FormValue("versionInput"))
  77.     ipLayer.IHL = StringToUint8(req.FormValue("ihlInput"))
  78.     // fmt.Printf("%+v", ipLayer)
  79.     // os.Exit(1)
  80.     ipLayer.TOS = StringToUint8(req.FormValue("tosInput"))
  81.     ipLayer.Length = StringToUint16(req.FormValue("lengthInput"))
  82.     ipLayer.Id = StringToUint16(req.FormValue("idInput"))
  83.     ipLayer.Flags = SetIPFlag(req.FormValue("flagInput"))
  84.     ipLayer.FragOffset = StringToUint16(req.FormValue("fragOffInput"))
  85.     ipLayer.TTL = StringToUint8(req.FormValue("ttlInput"))
  86.     ipLayer.Protocol = SetIPProtocol(req.FormValue("protocolInput"))
  87.     ipLayer.Checksum = StringToUint16(req.FormValue("checksumInput"))
  88.     ipLayer.DstIP = net.ParseIP(req.FormValue("dstInput"))
  89.     ipLayer.SrcIP = net.ParseIP(req.FormValue("srcInput"))
  90.    
  91. }
  92.  
  93. func saveEthernetLayer(w http.ResponseWriter, req *http.Request) {
  94.     if req.FormValue("srcMac") == "" {
  95.         ethLayer.SrcMAC = AutoMAC(ipLayer.SrcIP, interfArray)
  96.     } else {
  97.         ethLayer.SrcMAC = StringToHardAddr(req.FormValue("srcMac"))
  98.     }
  99.     if req.FormValue("dstMac") == "" {
  100.         ethLayer.DstMAC = AutoMAC(ipLayer.DstIP, interfArray)
  101.     } else {
  102.         ethLayer.DstMAC = StringToHardAddr(req.FormValue("dstMac"))
  103.     }
  104.  
  105.     ethLayer.EthernetType = SetEthType(req.FormValue("etherType"))
  106.     ethLayer.Length = StringToUint16(req.FormValue("etherLength"))
  107.    
  108.     changeChosenInterf(req.FormValue("networkDeviceInput"))
  109. }
  110.  
  111. func saveData(w http.ResponseWriter, req *http.Request) {
  112.     saveIPLayer(w, req)
  113.     saveEthernetLayer(w, req)
  114.     if ipLayer.Protocol.String() == "TCP" {
  115.         tcpLayer = SaveTCPLayer(w, req)
  116.     } else if ipLayer.Protocol.String() == "UDP" {
  117.         udpLayer = SaveUDPLayer(w, req)
  118.     } else if ipLayer.Protocol.String() == "ICMPv4" {
  119.         icmpLayer = SaveICMPLayer(w, req)
  120.     }
  121.     generatePacket()
  122. }
  123.  
  124. func generatePacket() {
  125.     handle, err := pcap.OpenLive(chosenInterf.Name,
  126.         1024, false, 30*time.Second)
  127.     if err != nil {
  128.         log.Fatal(err)
  129.     }
  130.  
  131.     var options gopacket.SerializeOptions
  132.     buffer := gopacket.NewSerializeBuffer()
  133.  
  134.     if ipLayer.Protocol.String() == "TCP" {
  135.         gopacket.SerializeLayers(buffer, options, &ethLayer, &ipLayer,
  136.             &tcpLayer, gopacket.Payload([]byte("hello there world")))
  137.     } else if ipLayer.Protocol.String() == "UDP" {
  138.         gopacket.SerializeLayers(buffer, options, &ethLayer, &ipLayer,
  139.             &udpLayer, gopacket.Payload([]byte("hello there world")))
  140.     } else if ipLayer.Protocol.String() == "ICMPv4" {
  141.         //fmt.Printf("%+v", icmpLayer)
  142.         fmt.Printf("%+v", ipLayer)
  143.         gopacket.SerializeLayers(buffer, options, &ethLayer,
  144.         &ipLayer,
  145.         &icmpLayer,
  146.         gopacket.Payload([]byte("HELLO-R-U-THERE")))
  147.     }
  148.    
  149.  
  150.     outgoingpacket := buffer.Bytes()
  151.     err = handle.WritePacketData(outgoingpacket)
  152.     if err != nil {
  153.         log.Fatal(err)
  154.     }
  155.  
  156. }
  157. func main() {
  158.     http.HandleFunc("/", networkLayer)
  159.     http.HandleFunc("/savedata", saveData)
  160.     http.ListenAndServe(":"+os.Args[1], nil)
  161. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top