mosers
/
eaas-vde-proxy
Archiviert
1
0
Fork 0

Fixed type and checksum issues [Close #7, Close #6]

main
Simon Moser vor 3 Jahren
Ursprung 040814e0c3
Commit 2ec1d498c8
Signiert von: mosers
GPG-Schlüssel-ID: 96B3365A234B500C

@ -3,13 +3,16 @@ package main
import (
"encoding/binary"
"flag"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
log "github.com/sirupsen/logrus"
"io"
"net"
"proxy/cmd"
"proxy/util"
"strconv"
"time"
)
var OldMac net.HardwareAddr
@ -17,7 +20,6 @@ var NewMac net.HardwareAddr
var OldIP net.IP
var NewIP net.IP
// Start the two plugs and run two concurrent forward methods
func main() {
// Get command line arguments
@ -32,8 +34,8 @@ func main() {
log.SetLevel(log.Level(*logLvl))
OldMac, _ = net.ParseMAC(*oldmac)
NewMac, _ = net.ParseMAC(*newmac)
OldIP = net.ParseIP(*oldip)
NewIP = net.ParseIP(*newip)
OldIP = net.ParseIP(*oldip).To4()
NewIP = net.ParseIP(*newip).To4()
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
})
@ -49,7 +51,6 @@ func main() {
// Reads from an input and writes to and output,
// do things to the content in between.
// For now only output the packet's information
// Is meant to be run concurrently with "go pipeForward(...)"
func pipeForward(reader io.Reader, writer io.Writer, prefix string, passthrough bool) {
for {
@ -67,6 +68,7 @@ func pipeForward(reader io.Reader, writer io.Writer, prefix string, passthrough
// Convert frame to full stack packet
packet := gopacket.NewPacket(frameBytes, layers.LayerTypeEthernet, gopacket.Default)
isInteresting := false // Debug Help
// Handle Ethernet frame
frame := packet.Layer(layers.LayerTypeEthernet).(*layers.Ethernet)
@ -88,6 +90,19 @@ func pipeForward(reader io.Reader, writer io.Writer, prefix string, passthrough
log.Debug(prefix, "Outgoing IP rewritten ", ipv4Packet.SrcIP, NewMac)
ipv4Packet.SrcIP = NewIP
}
// Handle ICMP packet (based on IPv4)
if icmpLayer := packet.Layer(layers.LayerTypeICMPv4); icmpLayer != nil {
icmpPacket, _ := icmpLayer.(*layers.ICMPv4)
log.Infof("%s ICMP packet\nType/Code:\t%s\n", prefix, icmpPacket.TypeCode.String())
}
// Handle DHCP packet (based on IPv4)
if dhcpLayer := packet.Layer(layers.LayerTypeDHCPv4); dhcpLayer != nil && !passthrough {
//dhcpPacket, _ := dhcpLayer.(*layers.DHCPv4)
log.Info(prefix, "DHCP packet dropped")
continue
}
}
// Handle ARP packet
@ -95,7 +110,7 @@ func pipeForward(reader io.Reader, writer io.Writer, prefix string, passthrough
arpPacket := packet.Layer(layers.LayerTypeARP).(*layers.ARP)
log.Infof(
"%sARP before modification\nAddrType:\t%s\nProtocol:\t%s\nOperation:\t%s\n"+
"SrcHwAddress:\t%s\nSrcProtAddress:\t%s\nDstHwAddress:\t%s\nDstProAddress:\t%s\n",
"SrcHwAddress:\t%s\nSrcProtAddress:\t%s\nDstHwAddress:\t%s\nDstProtAddress:\t%s\n",
prefix,
arpPacket.AddrType,
arpPacket.Protocol,
@ -108,14 +123,14 @@ func pipeForward(reader io.Reader, writer io.Writer, prefix string, passthrough
if !passthrough && prefix == cmd.In {
log.Debug(prefix, "Incoming MAC rewritten (ARP) ", net.HardwareAddr(arpPacket.DstHwAddress), OldMac)
if arpPacket.Operation == layers.ARPReply {
arpPacket.DstHwAddress = OldMac
arpPacket.DstProtAddress = OldIP
log.Infof(
"%sARP after modification\nDstProtAddress:\t%s\n",
prefix,
net.IP(arpPacket.DstProtAddress),
)
if arpPacket.Operation == layers.ARPReply {
arpPacket.DstHwAddress = OldMac
log.Infof(
"%sDstHwAddress:\t%s\n",
prefix,
@ -135,22 +150,24 @@ func pipeForward(reader io.Reader, writer io.Writer, prefix string, passthrough
}
}
// Handle DHCP packet
if dhcpLayer := packet.Layer(layers.LayerTypeDHCPv4); dhcpLayer != nil {
//dhcpPacket, _ := dhcpLayer.(*layers.DHCPv4)
log.Info(prefix, "DHCP packet dropped")
continue
}
// Serialize packet back to binary
buf := gopacket.NewSerializeBuffer()
if err := gopacket.SerializePacket(buf, gopacket.SerializeOptions{}, packet); err != nil {
log.Error(prefix, "Error serializing packet to send")
opts := gopacket.SerializeOptions{ComputeChecksums: true, FixLengths: true}
if err := gopacket.SerializePacket(buf, opts, packet); err != nil {
log.Errorf("%s Error serializing packet to send\n%s\nSrc:\t%s\nDst:\t%s\n", prefix, err, frame.SrcMAC, frame.DstMAC)
continue
}
newFrameBytes := buf.Bytes()
newFrameLength := make([]byte, 2)
binary.BigEndian.PutUint16(newFrameLength, uint16(len(newFrameBytes)))
// Write interesting things to debug file
if isInteresting && !passthrough {
util.WriteBinary(fmt.Sprintf("/tmp/arp_%di.dat", time.Now().Unix()), frameBytes)
util.WriteBinary(fmt.Sprintf("/tmp/arp_%do.dat", time.Now().Unix()), newFrameBytes)
//util.WritePcap("xyz.pcap", packet.Data(), packet.Metadata().CaptureInfo)
}
// Forward original frame to other plug
writer.Write(newFrameLength)
writer.Write(newFrameBytes)

Binäre Datei nicht angezeigt.

@ -0,0 +1,32 @@
package util
import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
)
func WritePcap(file string, data []byte, ci gopacket.CaptureInfo) {
f, err := os.Create(file)
if err != nil {
log.Errorf("Error writing pcap file %s", file)
}
defer f.Close()
r, err := pcapgo.NewNgWriter(f, layers.LinkTypeEthernet)
if err != nil {
log.Errorf("Error writing pcap file %s", file)
}
defer r.Flush()
err = r.WritePacket(ci, data)
}
func WriteBinary(file string, data []byte) {
if err := ioutil.WriteFile(file, data, 0644); err != nil {
log.Errorf("Error writing binary file %s", file)
}
}