Skip to content

Instantly share code, notes, and snippets.

@sandeshbhusal
Last active March 1, 2021 16:20
Show Gist options
  • Save sandeshbhusal/200e1cf4939a124685b3b2939d82fd88 to your computer and use it in GitHub Desktop.
Save sandeshbhusal/200e1cf4939a124685b3b2939d82fd88 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"
"math/rand"
"net"
"os"
"os/exec"
"strconv"
"syscall"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func main() {
cmd := exec.Command("id", "-u")
output, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
i, err := strconv.Atoi(string(output[:len(output)-1])) // Remove trailing \n for Atoi to work correctly
if err != nil {
log.Fatal(err)
}
if i != 0 {
fmt.Println("gospoof must be run as root.")
os.Exit(-2)
}
fmt.Println("------ > gospoof < ------")
fmt.Println("")
var source *string
var destination *string
var source_port *int
var dest_port *int
var log *string
source = flag.String("source", "", "Source ip address from where logs are being shot. ")
destination = flag.String("destination", "127.0.0.1", "Destination of Logs.")
source_port = flag.Int("sport", 30960, "Source port from where logs are to be sent (default: 30960)")
dest_port = flag.Int("dport", 514, "Destination port (default 514)")
log = flag.String("log", "", "Log Data")
flag.Parse()
if *source == "" || *log == "" {
fmt.Println("Usage: gospoof -source <source_ip> -log <data>")
fmt.Println("Optional flags:")
fmt.Println(" -sport <port number> : source port")
fmt.Println(" -dport <port number> : destination port")
fmt.Println(" -destination <destination_ip> : destination ip address (needs recompilation)")
os.Exit(-1)
}
buf := gopacket.NewSerializeBuffer()
serializeOpts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
ipLayer := &layers.IPv4{
SrcIP: net.ParseIP(*source),
DstIP: net.ParseIP(*destination),
Protocol: layers.IPProtocolUDP,
Version: 4,
TTL: 255,
Id: uint16(rand.Intn(60000)),
}
udpLayer := &layers.UDP{
SrcPort: layers.UDPPort(int16(*source_port)),
DstPort: layers.UDPPort(int16(*dest_port)),
}
udpLayer.SetNetworkLayerForChecksum(ipLayer)
err = gopacket.SerializeLayers(buf, serializeOpts, ipLayer, udpLayer, gopacket.Payload(*log+"\n"))
if err != nil {
panic(err)
}
bytes := buf.Bytes()
fd, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW)
addr := syscall.SockaddrInet4{
Port: *dest_port,
Addr: [4]byte{127, 0, 0, 1}, // TODO: Change this to destination from flag above :)
}
if err := syscall.Sendto(fd, bytes, 0, &addr); err != nil {
panic(err)
}
fmt.Println("Shot a packet.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment