Skip to content

Instantly share code, notes, and snippets.

@sandeshbhusal
Last active June 29, 2021 00:52
Show Gist options
  • Save sandeshbhusal/c8fa09546ffc076e5103456dd4e3742d to your computer and use it in GitHub Desktop.
Save sandeshbhusal/c8fa09546ffc076e5103456dd4e3742d to your computer and use it in GitHub Desktop.
// Author : Sandesh Bhusal
// Date Feb 23, 2021
// You may copy, download and modify the source code according to your requirements :)
// Happy Spoofing!
// Original Code Lives at https://gist.github.com/sandeshbhusal/c8fa09546ffc076e5103456dd4e3742d
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
func main() {
// Configurable options related to source IP, Destination IP and other UDP options.
var interfaceName *string
var logString *string
var sourceIP *string
var destinationIP *string
var sourcePort *int
var destinationPort *int
// Create command-line arguments.
interfaceName = flag.String("interface_name", "", "Name of interface where logs should be faked from")
logString = flag.String("log_data", "", "Log Data")
sourceIP = flag.String("source_ip", "", "Source IP Address where logs come from")
destinationIP = flag.String("destination_ip", "127.0.0.1", "Destination IP Address where Logs are to be sent to.")
sourcePort = flag.Int("source_port", 30960, "Port where logs come from (default 30960)")
destinationPort = flag.Int("destination_port", 514, "Destination port on destination machine (default 514)")
flag.Parse()
// Print usage.
if *interfaceName == "" || *logString == "" || *sourceIP == "" {
fmt.Println("----- GoSpoof -----")
fmt.Println("Tool to spoof UDP Packets. Here's How to use it:")
fmt.Println("")
fmt.Println("gospoof -interface_name <interface name> -source_port <source port> -source_ip <source ip> -log_data <log data>")
fmt.Println("Optional Flags (use for high customization) :")
fmt.Println("-destination_ip : Destination IP address to send packets to. Defaults to 127.0.0.1")
fmt.Println("-destination_port : Destination port to send packets to. Defaults to 514")
os.Exit(0)
}
// Open capture (defaults to ethernet. Can't be used on loopback. If you want loopback feature,
// remove the eth layer and create a loopback layer below in gopacket.SerializeLayers)
handle, err := pcap.OpenLive(*interfaceName, 1500, false, pcap.BlockForever)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
// Lowest layer in TCP Stack
eth := layers.Ethernet{
EthernetType: layers.EthernetTypeIPv4,
SrcMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
DstMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
}
// IP Packet configuration.
ip := layers.IPv4{
Version: 4,
TTL: 64,
SrcIP: net.ParseIP(*sourceIP),
DstIP: net.ParseIP(*destinationIP),
Protocol: layers.IPProtocolUDP,
}
// Source Port for data. Defaults to 30960 (random choice, no comments here.)
var sourcePortInt int16
sourcePortInt = int16(*sourcePort)
// Destination Port for data. Defaults to 514 (syslog).
var destPortInt int16
destPortInt = int16(*destinationPort)
// UDP Layer configuration. See the missing Length and Checksum? They come below.
udp := layers.UDP{
SrcPort: layers.UDPPort(sourcePortInt),
DstPort: layers.UDPPort(destPortInt),
}
// Payload for the UDP Packet.
payload := []byte(*logString)
// Select a layer (UDP packet will be encapsulated inside the IP one) for checksum.
udp.SetNetworkLayerForChecksum(&ip)
// Serialization options for a fixed-size packet.
options := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
buffer := gopacket.NewSerializeBuffer()
// Finallly... Generate our Payload.
if err = gopacket.SerializeLayers(buffer, options,
&eth,
&ip,
&udp,
gopacket.Payload(payload),
); err != nil {
log.Fatal("Could not serialize data into a packet.", err)
}
// Convert to bytes.
outgoingPacket := buffer.Bytes()
// Aaaaand It's gone.
if err = handle.WritePacketData(outgoingPacket); err != nil {
log.Fatal("Could not send data packet. Maybe Check permissions?", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment